/* Content copyright David B. Zwiefelhofer. All rights reserved. */
	
	var tWidth = 152;
	var tHeight = 114;
	var cur_id = 0;
	var pOffset_y = 140;
	var pOffset_x = 8;
	
	document.onmousemove = mouseXandY;
	// Global variables
	xMousePos = 0; // Horizontal position of the mouse on the screen
	yMousePos = 0; // Vertical position of the mouse on the screen
	
	function mouseXandY(e) { // by Kravvitz of DynamicSiteSolutions.com
	  var x=0,y=0;
	  e=e||window.event;
	  if(e.pageX || e.pageY) {
		x=e.pageX;
		y=e.pageY;
	  } else if(typeof(e.clientX)=='number') {
		var dE=document.documentElement;
		x=e.clientX+document.body.scrollLeft+(dE?dE.scrollLeft:0);
		y=e.clientY+document.body.scrollTop+(dE?dE.scrollTop:0);
	  }
	//  return [x,y];
		xMousePos = x; // Horizontal position of the mouse on the screen
		yMousePos = y; // Vertical position of the mouse on the screen
	}

	function fix_image_size(isrc) {
		var oImg = new Image();
		oImg.src = isrc;
		if (oImg.complete) {
		//	scale image to fit popup
			
			document["popupimg_ll"].width = oImg.width;
			document["popupimg_ll"].height = oImg.height;
			
			if (document["popupimg_ll"].width > tWidth){
				document["popupimg_ll"].width = tWidth;
				document["popupimg_ll"].height = (tHeight*document["popupimg_ll"].height)/tWidth;
			}
			
			if (document["popupimg_ll"].height > tHeight){
				document["popupimg_ll"].height = tHeight;
				document["popupimg_ll"].width = (tWidth*document["popupimg_ll"].width)/tHeight;
			}
			
			if ((oImg.width <= tWidth) && (oImg.height <= tHeight)){
				document["popupimg_ll"].width = oImg.width;
				document["popupimg_ll"].height = oImg.height;
			}
			
/*			if ((oImg.width <= tWidth) &&  (oImg.height <= tHeight)){
				document["popupimg_ll"].width = oImg.width;
				document["popupimg_ll"].height = oImg.height;
			} else {
				if (oImg.width > tWidth){
					document["popupimg_ll"].width = tWidth;
					document["popupimg_ll"].height = (tHeight*oImg.height)/tWidth;
				}
				if (oImg.height > tHeight){
					document["popupimg_ll"].height = tHeight;
					document["popupimg_ll"].width = (tWidth*oImg.width)/tHeight;
				}
			}*/
		}
		else {
			window.setTimeout('fix_image_size(document["popupimg_ll"].src)', 50);
		}
	}
	
	function load_alt_image() {
		document["popupimg_ll"].src = "http://"+host+"/images/no_pic_hl.jpg";
		document["popupimg_ll"].width = tWidth;
		document["popupimg_ll"].height = tHeight;
	}

	function fetch_image_name(id){
		var xmlhttp = create_xmlhttp();
		serverPage = ""+host+"processors/fetch-img.asp?id="+id+"&sz=hl&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) {
			//	document["popupimg_ll"].src = "http://"+host+"/images/no_pic_hl.jpg";
				document["popupimg_ll"].src = "http://"+host+xmlhttp.responseText;
				document["popupimg_ll"].width = tWidth;
				document["popupimg_ll"].height = tHeight;
				window.setTimeout('fix_image_size(document["popupimg_ll"].src)', 50);
			} else if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
				load_alt_image();
			}
		}
		
		xmlhttp.send(null);
	}
	
	function do_preview(id){
		if (preview_on) {
			var el = document.getElementById("prop_tag_ll");
			if (cur_id != id){
				document["popupimg_ll"].onerror = function() {load_alt_image()};
			//	document["popupimg_ll"].onerror = function() {fetch_image_name(id)};
				document["popupimg_ll"].src = "http://"+host+"/pics/"+id+"/"+id+"_1_hl.jpg";
				cur_id = id;
			}
			if (el.style.visibility == 'visible'){
				el.style.left = xMousePos+pOffset_x+"px";
				el.style.top = yMousePos-pOffset_y+"px";
			} else {
				
				fix_image_size(document["popupimg_ll"].src);
					
				el.style.visibility = 'visible';
				el.style.left = xMousePos+pOffset_x+"px";
				el.style.top = yMousePos-pOffset_y+"px";	
			}
		}
	}
	
	function remove_preview(){
		var el = document.getElementById("prop_tag_ll");
		el.style.visibility = 'hidden';
		cur_id = 0;
	}

	function set_preview_state(cb){
		var exp = new Date();
		var oneYearFromNow = exp.getTime() + (365 * 24 * 60 * 60 * 1000);
		exp.setTime(oneYearFromNow);
		preview_on = cb.checked;
		SetCookie("preview_on", preview_on, exp);
	}

	function move_preview(id){
		if (preview_on) {
			var el = document.getElementById("prop_tag_ll");
			if (el.style.visibility == 'visible'){
				if (cur_id != id){
					document["popupimg_ll"].onerror = function() {load_alt_image()};
				//	document["popupimg_ll"].onerror = function() {fetch_image_name(id)};
					document["popupimg_ll"].src = "http://"+host+"/pics/"+id+"/"+id+"_1_hl.jpg";
					cur_id = id;
				}
				el.style.left = xMousePos+pOffset_x+"px";
				el.style.top = yMousePos-pOffset_y+"px";			
			} else {
				do_preview(id);
			}
		}
	}

	function write_debugging(msg){
		var el = document.getElementById("debugging");
		el.innerHTML = msg+"<br />\n"+el.innerHTML;
	}

	function remove_preview_thumb(){
		var el = document.getElementById("prop_tag_ll");
		el.style.visibility = 'hidden';
	}

	function move_preview_thumb(pic){
		var el = document.getElementById("prop_tag_ll");
		if (el.style.visibility == 'visible'){
			document["popupimg_ll"].src = pic;
			el.style.left = xMousePos+pOffset_x+"px";
			el.style.top = yMousePos-pOffset_y+"px";			
		} else {
			preview_thumb(pic);
		}
	}
//house-for-sale-preview.jpg open-house-preview.jpg
	function preview_thumb(pic){
		var el = document.getElementById("prop_tag_ll");
		document["popupimg_ll"].src = pic;
		if (el.style.visibility == 'visible'){
			el.style.left = xMousePos+pOffset_x+"px";
			el.style.top = yMousePos-pOffset_y+"px";
		} else {
			
		//	fix_image_size(document["popupimg_ll"].src);
				
			el.style.visibility = 'visible';
			el.style.left = xMousePos+pOffset_x+"px";
			el.style.top = yMousePos-pOffset_y+"px";	
		}
	}