// JavaScript Document

	function newImage(arg) {
		if (document.images) {
			rslt = new Image();
			rslt.src = arg;
			return rslt;
		}
	}

	function preloadImages() {
		if (document.images) {
			close_gif = newImage(""+host+"images/close.gif");
			close_hover_gif = newImage(""+host+"images/close-hover.gif");
		}
	}
	preloadImages();
	
	function findPos(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		return [curleft,curtop];
	}

	function open_pano_window(mlsnum){
		var el = document.getElementById("pano_container");
	//	var map_obj = document.getElementById("prop_details");
		var map_obj = document.getElementById("pagecontainerfixed");
		
		var map_left = map_obj.offsetLeft;
		if (map_obj.offsetParent.offsetLeft){
			map_left = map_obj.offsetParent.offsetLeft;
		}
		
		var tmp = findPos(map_obj);
		var map_top = tmp[1];		


		var map_width = map_obj.offsetWidth;
		var map_height = map_obj.offsetHeight;
		var load_width = el.offsetWidth;
		var load_height = el.offsetHeight;
		
		
		var load_left = (map_left + parseInt((map_width - load_width)/2))+"px";
	//	var load_top = (map_top + parseInt((map_height - load_height)/2))+"px";
		var load_top = "200px";
		
		el.style.left = load_left;
		el.style.top = load_top;
		
		el.style.visibility = "visible";
		document.getElementById("pano").style.visibility = "visible";
		document.getElementById("pano_label_dp").style.visibility = 'visible';
/*		document.getElementById("top_links").style.visibility = "visible";*/
		document.getElementById("close_box").style.visibility = 'visible';
		
	}

	function set_el(el, width, height, display, visibility){
		el.style.width = width+"px";
		el.style.height = height+"px";
		el.style.display = display;
		el.style.visibility = visibility;
	}
	
	function hide_el(el_name){
		var el = document.getElementById(el_name);
		set_el(el, 0, 0, 'none', 'hidden');
/*		el.style.width = 0;
		el.style.height = 0;
		el.style.visibility = 'hidden';
		el.style.display = 'none';*/
	}
	
	function show_el(el_name, width, height){
		var el = document.getElementById(el_name);
		set_el(el, width, height, 'block', 'visible');
	}
	
	var myPano = null;
	var noPano = true;
	var myPOV = null;
	function handleNoPano(errorCode){
		if (errorCode == 600) {
			noPano = true;
		}
	}

	function nearby_pano(mlsnum){
		var tpano = new GStreetviewPanorama(document.getElementById("pano_dummy"));
		var tp_loc = new GLatLng(prop_cache[prop_cache_indices[mlsnum]][2].getPoint().lat(), prop_cache[prop_cache_indices[mlsnum]][2].getPoint().lng());
	//	tp_loc = new GLatLng(lat, lon);
		myPOV = {yaw:180,pitch:10};
		tpano.setLocationAndPOV(tp_loc, myPOV);
		GEvent.addListener(tpano, "error", handleNoPano);
		tpano.remove();
	}
	
	function hide_summary(){
		hide_el("narrative");
		hide_el("feat_pic");
		hide_el("display_pic_controls2");
		hide_el("left_display_arrow2");
		hide_el("right_display_arrow2");
		hide_el("pic_number2");
		hide_el("feat_links");
	}
	
	function init_pano(mlsnum){
	//	init_pano(prop_cache[prop_cache_indices[mlsnum]][2].getPoint().lat(), prop_cache[prop_cache_indices[mlsnum]][2].getPoint().lng());
	//	var mlsnum = this.id;
		hide_summary();
		
		myPano = new GStreetviewPanorama(document.getElementById("pano"));
		pano_loc = new GLatLng(prop_cache[prop_cache_indices[mlsnum]][2].getPoint().lat(), prop_cache[prop_cache_indices[mlsnum]][2].getPoint().lng());
	//	pano_loc = new GLatLng(lat, lon);
		myPOV = {yaw:180,pitch:10};
		myPano.setLocationAndPOV(pano_loc, myPOV);
		GEvent.addListener(myPano, "error", handleNoFlash);
	//	document.getElementById("top_links").innerHTML = "<a href=\"details.asp?ID="+mlsnum+"\">Full Details</a> &middot; <a href=\"javascript:load_summary("+mlsnum+")\">Summary</a>";
	}
	
//	
	function setup_pano(mlsnum){
		var xmlhttp = create_xmlhttp();
		document.getElementById("working").innerHTML = "<img src=\""+host+"images/waiting-16.gif\" width=\"16\" height=\"16\" />";
		serverPage = ""+host+"processors/setup-pano.asp?id="+mlsnum+"&dp=1";
		serverPage += "&ms=" + new Date().getTime();
		
		xmlhttp.open("GET", serverPage, true); // asynch param set to false for synch requests, true for asynch

		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				hide_summary();
				eval(xmlhttp.responseText);
				document.getElementById("working").innerHTML = "<img src=\""+host+"images/pix.gif\" width=\"16\" height=\"16\" />";
			}
		}
		xmlhttp.send(null);
	}
	
	function handleNoFlash(errorCode) {
	  var FLASH_UNAVAILABLE = 603;
	  if (errorCode == FLASH_UNAVAILABLE) {
		alert("Error: Flash doesn't appear to be supported by your browser.");
		return;
	  } else if (errorCode = 600) {
		var pano_el = document.getElementById("pano");
		pano_el.style.background = "url(images/no_featured_pic.jpg)";
		pano_el.style.backgroundColor = "#FFFFFF";
		pano_el.style.backgroundPosition = "center";
		pano_el.style.backgroundRepeat = "no-repeat";
		alert("Error: There are no nearby panoramic images for this listing.");
		return;
	  } else if (errorCode != 0) {
		alert("Error: "+errorCode+" (unspecified)");
		return;
	  }
	}
	
	function remove_pano(pano){
		if (pano != null) {
			pano.remove();
			pano = null;
		}
	}
	
	function close_pano(){
		remove_pano(myPano);
		
		hide_el("narrative");
		hide_el("feat_pic");
		hide_el("display_pic_controls2");
		hide_el("left_display_arrow2");
		hide_el("right_display_arrow2");
		hide_el("pic_number2");
		hide_el("feat_links");

		document.getElementById("pano").style.visibility = 'hidden';
		document.getElementById("pano_label_dp").style.visibility = 'hidden';
/*		document.getElementById("top_links").style.visibility = 'hidden';*/
		document.getElementById("close_box").style.visibility = 'hidden';
		document.getElementById("pano_container").style.visibility = 'hidden';
	//	document.getElementById("feat_pic").innerHTML = "<img id=\"ft_pic\" src=\""+host+"images/loading.gif\" height=\"240\" width=\"320\" />";
	//	document.getElementById("ft_pic").src = ""+host+"images/loading.gif";
		document.images["ft_pic"].src = ""+host+"images/loading.gif";
		
		document.getElementById("narrative").innerHTML = "";
		cur_pic = 0;
	}

	function open_pano(mlsnum){
		open_pano_window(mlsnum);
		setup_pano(mlsnum);
	}

