//if for some reason the javascript interpreter couldn't read the previous values set them to 0
if (use_java == null) {
    var use_java = 0;
}
if (use_flash == null) {
    var use_flash = 0;
}

//---------------------------------------------------------------
//                          flow
//---------------------------------------------------------------

var current_media;
var old_media;
var current_media_id;
var last_media_id = null;
var large_window = null;
var override_unload = null;
var showing_larger = null;
var cnt = 0;

//get the main elements
var parent_div = parent.document.getElementById('parent_div');
var audio_1_div = parent.document.getElementById('audio_1_div');
var audio_content_div = parent.document.getElementById('audio_content_div');
var audio_2_div = parent.document.getElementById('audio_2_div');
var image_div = parent.document.getElementById('image_div');
var tour_div = parent.document.getElementById('tour_div');
var action_div = parent.document.getElementById('action_div');
var media_div = parent.document.getElementById('media_display_div');
var desc_div = parent.document.getElementById('desc_div');
var map_div = parent.document.getElementById('map_div');
var ad_space = parent.document.getElementById('ad_space');
var bottom_space = parent.document.getElementById('bottom_handled_space');

var id = parent.document.getElementById('id').value;
var audio1 = parent.document.getElementById('form_audio_1');
var audio2 = parent.document.getElementById('form_audio_2');
var http_server = "http://" + parent.document.getElementById('http_server').value;
var ad_url = parent.document.getElementById('remote_top_url');
var bottom_url = parent.document.getElementById('remote_bottom_url');

//description as text should exist..
if (desc_div != null) {
    desc_div.innerHTML = parent.document.getElementById('description').value;
}

//all contact information is not guaranteed to exist..
seller_name = parent.document.getElementById('person_name');
seller_email = parent.document.getElementById('person_email');
seller_phone_1 = parent.document.getElementById('person_phone_1');
seller_phone_2 = parent.document.getElementById('person_phone_2');

contact_information = '';

if (seller_name != null) {

    contact_information = "<br><br>Contact:<br>";
    
    if (seller_email != null) {
        contact_information = contact_information + "<a href='mailto:" + seller_email.value + "'>";
        
        if (seller_name.value != null) {
            contact_information = contact_information + seller_name.value;
        } else {
            contact_information = contact_information + seller_email.value;
        }
        
        contact_information = contact_information + "</a>";

    } else {
        contact_information = contact_information + seller_name.value;
    }
    
    if (seller_phone_1 != null) {
        contact_information = contact_information + "<br>" + seller_phone_1.value;
    }
    if (seller_phone_2 != null) {
        contact_information = contact_information + "<br>" + seller_phone_2.value;
    }

}

if (desc_div != null) {
    desc_div.innerHTML = desc_div.innerHTML + contact_information;
    //process links and emails in the description
    desc_div.innerHTML = desc_div.innerHTML.replace(/(([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+)/g, "<a href=\"mailto:$1\">$1</a>");
    //desc_div.innerHTML = desc_div.innerHTML.replace(/(http:\/\/[^<\s\]]*)/g, "<a href=\"$1\" target=\"_new\">$1</a>");
}

//map link
var s1 = parent.document.getElementById('address_1').value;
var s2 = parent.document.getElementById('address_2').value;
var c = parent.document.getElementById('city').value;
var s = parent.document.getElementById('state').value;

if (s1.length > 1 && c.length > 1 && s.length > 1) {

    s1 = s1.replace(/\s/g, "+");
    s2 = s2.replace(/\s/g, "+");
    c = c.replace(/\s/g, "+");
    s = s.replace(/\s/g, "+");
    
    var map_text = s1;
    if (s2.length > 1) {
        map_text = map_text + ",+" + s2;
    }
    map_text = map_text + ",+" + c + ",+" + s;
    
    if (map_div != null) {
        map_div.innerHTML = "<a href='http://maps.google.com/?hl=en&q=" + map_text + "' target='_blank'><img src='/vtsuite/v6/images/spacer.gif' width='142' height='55' border='0' alt=''></a>";
    }
    
}

//output the media links
handle_media('image');
handle_audio();
if (ad_url.value != '') {
	handle_ads();
}

//if java is enabled, show the java and image list
//otherwise, show the flash and image list
if (use_java == 1) {
    handle_media('java');
    if (parent.document.getElementById('form_java_1') != null) {
		click_java(parent.document.getElementById('form_java_1').id, '1');
    }
} else if (use_flash == 1) {
    handle_media('flash');
	if (parent.document.getElementById('form_flash_1') != null) {
		click_flash(parent.document.getElementById('form_flash_1').id, '1');
    }
} else {
    handle_no_plugin();
  	if (parent.document.getElementById('form_image_1') != null) {  
		click_image(parent.document.getElementById('form_image_1').id, '1');
	}
}

//---------------------------------------------------------------
//                          functions
//---------------------------------------------------------------

//print the list of the images
function output_image_text(image_element, cnt) {

    var new_text = "<a class='media_link' id='img_div_" + cnt + "' onClick=\"work.click_image('" +  image_element.id + "', '" + cnt + "');\" onMouseOver=\"this.style.cursor='pointer'\" onMouseOut=\"this.style.cursor='auto'\">" + image_element.name + "</a><br />";
    image_div.innerHTML = image_div.innerHTML + new_text;
    
}

//print the list of the java ipix files
function output_java_text(java_element, cnt) {

    var new_text = "<a class='media_link' id='java_div_" + cnt + "' onClick=\"work.click_java('" +  java_element.id + "', '" + cnt + "');\" onMouseOver=\"this.style.cursor='pointer'\" onMouseOut=\"this.style.cursor='auto'\">" + java_element.name + "</a><br />";
    tour_div.innerHTML = tour_div.innerHTML + new_text;
    
}

//print the list of the flash swf files
function output_flash_text(flash_element, cnt) {

    var new_text = "<a class='media_link' id='flash_div_" + cnt + "' onClick=\"work.click_flash('" +  flash_element.id + "', '" + cnt + "');\" onMouseOver=\"this.style.cursor='pointer'\" onMouseOut=\"this.style.cursor='auto'\">" + flash_element.name + "</a><br />";
    tour_div.innerHTML = tour_div.innerHTML + new_text;
    
}

//add a link for larger tours
function add_larger_link(media_type) {
    
    var new_text = "<a id='" + media_type + "_larger_link' onClick=\"work.open_larger('" + media_type + "', '" + id + "');\" onMouseOver=\"this.style.cursor='pointer'\" onMouseOut=\"this.style.cursor='auto'\">LARGE VIRTUAL TOURS</a>";
    action_div.innerHTML = action_div.innerHTML + new_text;

}

//embed the audio files
function output_audio(audio_element, cnt) {

    var new_text = "<embed src='/vtsuite/v6/listing_files/" + id + "/" + audio_element.value + "' width='65' height='23' controls='console' volume='100' loop='true' autostart='true'>";
    audio_div.innerHTML = new_text;

}

//sets the current media link of any type to a different color
function change_media_color(div_type, which_div) {
        
    old_media = current_media;
    current_media = parent.document.getElementById(div_type + which_div);
    current_media.className = "active_media";
    if ((old_media != null) && (old_media != current_media)) {
        old_media.className = "media_link";
    }    

}

//called whenever an image is clicked.
//arg 1 is the id of the form image, arg 2 is it's incremented counter
function click_image(which_image, img_div_id) {

    change_media_color('img_div_', img_div_id);
    var this_image = parent.document.getElementById(which_image);
    media_div.innerHTML = "<img src='/vtsuite/v6/listing_files/" + id + "/" + this_image.value + "' width='400' height='300'>";
    current_media_id = 0;
    return true;

}

//called whenever an ipix file is clicked.
//arg 1 is the id of the form image, arg 2 is it's incremented counter
function click_java(which_java, img_div_id) {
    
    //can't have both the big tour window and the original sized tours going at the same time
    if (large_window) {
        override_unload = true;
        large_window.close(); //close the child window, which will trigger it's in onUnload and click on the 1st still normally.
        //but because we are set to override, it won't actually click the image it will process the tour click as expected
        large_window = null; 
    }
    
    //can't click twice on the current media file, unless the click was issues because the child closed (last_media_id isn't null)
    if ((current_media_id != null) && (current_media_id == img_div_id) && (last_media_id == null) && (override_unload != true)) {
        return true;
    }

    current_media_id = img_div_id;
    
    if ((last_media_id == null) || (override_unload != true)) { //don't toggle media color for the child closing
        change_media_color('java_div_', img_div_id);
    }
    
    var this_java = parent.document.getElementById(which_java);    
   
    media_div.innerHTML = "<applet code='IpixViewer.class' archive='IpixViewer.jar' width='400' height='300' name='Tour Viewer' codebase='" + http_server + "/vtsuite/v6/ipix3viewer/files/'>" + 
"<param name='URL' value='" + http_server + "/vtsuite/v6/listing_files/" + id + "/" + this_java.value + "'>" +
"<param name='toolbar' value='small'>" +
"<param name='BackgroundColor' value='#ffffff'>" +
"<param name='spinspeed' value='5'>" +
"<param name='spinstyle' value='flat'>" +
"<param name='splashbg' value='ffffff'>" +
"</applet>";

    return true;

}

//called whenever a swf file is clicked.
//commentary is the same as click_java
function click_flash(which_flash, img_div_id) {

    if (large_window) {
        override_unload = true;
        large_window.close();
        large_window = null; 
    }

    if ((current_media_id != null) && (current_media_id == img_div_id) && (last_media_id == null) && (override_unload != true)) {
        return true;
    }
    
    current_media_id = img_div_id;
    
    if ((last_media_id == null) || (override_unload != true)) {
        change_media_color('flash_div_', img_div_id);
    }
    
    var this_flash = parent.document.getElementById(which_flash);    

    media_div.innerHTML =
"<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' width='400' height='300'>" + 
"<param name='movie' value='/vtsuite/v6/flash_loader.swf'>" +
"<param name='quality' value='MEDIUM'>" +
"<param name='flashvars' value='q=7&f=90&au=2&p=360&s=/vtsuite/v6/listing_files/" + id + "/"  + this_flash.value + "&u=Shawn Verne&n=1&l=1&c=FFFFFF'>" +
"<param name='bgcolor' value='#FFFFFF'>" +
"<embed src='/vtsuite/v6/flash_loader.swf' quality='MEDIUM' flashvars='q=7&f=90&au=2&p=360&s=/vtsuite/v6/listing_files/" + id + "/"  + this_flash.value + "&u=Shawn Verne&n=1&l=1&c=FFFFFF' bgcolor='#FFFFFF' width='400' height='300' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'>" +
"</embed>" +
"</object>";

    return true;

}

function click_audio(which_audio) {

    var new_text = "<embed src='/vtsuite/v6/listing_files/" + id + "/" + parent.document.getElementById('form_audio_' + which_audio).value + "' width='65' height='23' controls='console' volume='100' loop='true' autostart='true'>";
    audio_content_div.innerHTML = new_text;
    
}

//generic function to handle images, ipix, flash, etc
//relies on the values in the form to process that type of file
function handle_media(media_type) {

    var media_count = parent.document.getElementById(media_type + '_count').value;
    if (media_count != 0) {

        var i = 0;
        while (i <= media_count) {
            
            var current_media = parent.document.getElementById('form_' + media_type + '_' + i);
            if (current_media != null) {
            
                switch (media_type) {
                        case 'image':
                            output_image_text(current_media, i);
                            break;
                        case 'java':
                            media_div.innerHTML = '';
                            output_java_text(current_media, i);
                            break;
                        case 'flash':
                            media_div.innerHTML = '';
                            output_flash_text(current_media, i);
                            break;
                        default:
                            break;
                }
            
            }

            i++;

        }
        
        //add the extra link for larger tours
        switch (media_type) {
            case 'java':
                add_larger_link('java');
                break;
            case 'flash':
                add_larger_link('flash');
                break;
        }        
        
    }
    
}

function handle_audio() {
    
    if ((audio1 != null) && (audio_1_div != null)) {
        audio_1_div.innerHTML = "<a href='javascript:work.click_audio(1);' onMouseOver=\"this.style.cursor='pointer'\" onMouseOut=\"this.style.cursor='auto'\"><img src='/vtsuite/v6/images/spacer.gif' width='65' height='18' border='0'></a>";
        click_audio(1);
    }
    if ((audio2 != null) && (audio_1_div != null)) {
        audio_2_div.innerHTML = "<a href='javascript:work.click_audio(2);' onMouseOver=\"this.style.cursor='pointer'\" onMouseOut=\"this.style.cursor='auto'\"><img src='/vtsuite/v6/images/spacer.gif' width='65' height='18' border='0'></a>";
    }
    
}

function handle_ads() {

	if (ad_url != null) { 
		url_for_iframe = ad_url.value + id;
		ad_space.style.visibility = "visible";
		ad_space.innerHTML = '<center><iframe src="' + url_for_iframe + '" scrolling="no" frameborder="0" align="middle" hspace="0" vspace="0" class="iframead">';
	}
	
	if (bottom_url != null) {	
		url_for_bottom = bottom_url.value + id;
		bottom_space.style.visibility = "visible";
		bottom_space.innerHTML = '<iframe src="' + url_for_bottom + '" scrolling="no" frameborder="0" align="middle" hspace="0" vspace="0" class="iframebottom">';
	}
	
}

function handle_no_plugin() {
    tour_div.innerHTML = '<center><br /><b>It appears that your browser is not set to display Java or Flash files.</b><br /><br /><a href="http://www.macromedia.com/go/getflashplayer">Click here to download the free Flash software...</a></center>';
}

function open_larger(media_type, tour) {

    if (large_window == null) {
    
        //show the first media if an image is currently displayed
        if ((current_media_id == null) || (current_media_id == 0)) {
            current_media_id = 1;
        }
    
        large_window = window.open("/large/" + tour + "&" + media_type + "=1&which=" + current_media_id, "LargeVirtualTour", "height=450,width=700");
        media_div.innerHTML = '<br /><br /><center>Now showing virtual tours in larger window....';
        current_media_id = null;
        showing_larger = true;

    } else {
    
        if (large_window.focus) {
            large_window.focus();
        }

    }

}

//some browsers won't properly unload the child window.
//whenver this window gets the focus, check to see if the child is gone but the image isn't clicked
function determine_child() {

    //parent is focused now, but child is closed. open image one.
    if ((showing_larger) && (large_window != null) && (large_window.closed)) {

        click_image(parent.document.getElementById('form_image_1').id, '1');
        showing_larger = null;
        large_window = null;
       
    //parent was clicked, but the click registered on a tour link. it closes the child, so this sets the variables back.
    } else if ((showing_larger) && (override_unload)) {

        override_unload = null;
        showing_larger = null;
        large_window = null;

    }
    
}