// JavaScript Document
function lh__handleException(ex) {
	alert(ex);	
}

function lh__preventBubble(ev) {
	try {
		ev.cancelBubble = true;
	}
	catch(e) {  lh__handleException(e); }
}

//var lh__currentLocationDiv = null;
function lh__show(evnt, target) {
	//if(lh__currentLocationDiv != null) return;
	evnt = evnt == null ? window.event : evnt;	
	evnt.cancelBubble = true;
	//if(evnt.target !== target) return;
	
	target = $(target); //Add Prototype extensions, for IE.
	var locationID = target.id.match(/location_([0-9]+)/)[1];
	
	var existingDivs = $$('#locationHover'+locationID+".locationHoverFragment");
	if(existingDivs.length > 0) {
		//alert("Already a div for this");
		return;
	}
	
	var existingFragments = $$(".locationHoverFragment");
	for(var i = existingFragments.length-1; i >= 0; i--) {
		existingFragments[i].parentNode.removeChild(existingFragments[i]);
	}
	
	var hoverDiv = new Element("div", {id:"locationHover"+locationID, "class":"locationHoverFragment"});
	target.appendChild(hoverDiv);
	
	new Ajax.Request('/locations/ajah_hover_fragment/'+locationID+'.htm', {
		method:"get",
		onSuccess:function(transport) {lh__showCB(transport, locationID); }
	});
	
}

function lh__showCB(transport, locationID) {
	try {
		//alert("moo1");
		var hoverDivs = $$('.locationHoverFragment#locationHover'+locationID);
		if(hoverDivs.length == 0) {
			//alert("No elements matching: "+'.locationHoverFragment#locationHover'+locationID);
			return;
		}
		var hoverDiv = hoverDivs[0];
		hoverDiv.parentNode.style.position = "relative";
		hoverDiv.innerHTML = transport.responseText;
		//alert("moo2");
		var height = hoverDiv.offsetTop;
		var offsetParent = hoverDiv.offsetParent;
		while(offsetParent !== null) {
			height += offsetParent.offsetTop;
			offsetParent = offsetParent.offsetParent;
		}
		//alert("moo3");
		//alert(height+hoverDiv.offsetHeight+":"+document.body.clientHeight)
		if(height+hoverDiv.offsetHeight > document.body.clientHeight) {
			hoverDiv.style.bottom="0px";
			hoverDiv.select("img.locationHoverArrowBottom")[0].style.display = "inline";
			hoverDiv.select(".locationHoverMarginBottom")[0].style.display = "block";
		}
		else {
			hoverDiv.style.top="0px";	
			hoverDiv.select("img.locationHoverArrow")[0].style.display = "inline";
			hoverDiv.select(".locationHoverMarginTop")[0].style.display= "block";
		}
		//alert("moo4");
		/** START MAP **/	
		var lat = hoverDiv.select(".lat");
		var longi = hoverDiv.select(".long");
		if(lat.length > 0 && longi.length > 0) {
			lat = lat[0].value;
			longi = longi[0].value;
			var map = new GMap2(document.getElementById("map"), 
			{size:new GSize(210,200)});
			map.setCenter(new GLatLng(lat, longi), 11);
			
			// Create our "tiny" marker icon
			//var icon = new GIcon();
			//icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
			//icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			//icon.iconSize = new GSize(12, 20);
			//icon.shadowSize = new GSize(22, 20);
			//icon.iconAnchor = new GPoint(6, 20);
			//icon.infoWindowAnchor = new GPoint(5, 1);

			marker = new PdMarker(new GLatLng(lat, longi));
				marker.setTitle("");
				//GEvent.addListener(marker, "click", function() {
				//	marker.openInfoWindowHtml('<img src="/images/badger-sm.gif" width="43" height="33" style="float:left; margin-right:10px;"><span>Get directions from: <form action="http://maps.google.com/maps" method="get" target="_blank" style="margin:0; padding:0;"><input type="text" SIZE="20" MAXLENGTH="40" name="saddr" id="saddr" value="town, postcode, uk" class="login-form-name" style="font-size:11px;" ><input value="Go" TYPE="SUBMIT" class="login-form-submit" style="margin-left:3px; font-size:11px;"><input type="hidden" name="daddr" value="B30 2YG, uk"></form></span>');
				//});
				//http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=52.231243,-2.009232&sll=52.233661,-2.005434&sspn=0.007675,0.019312&ie=UTF8&ll=52.233359,-2.005906&spn=0.007675,0.019312&t=h&z=16&iwloc=A
				map.addOverlay(marker);

		}
		/** END MAP **/
		//alert("moo5");
	}
	catch(e) {
		lh__handleException(e);
	}
}

function lh__checkMouseMoved(evnt) {
	try {
		var hoverDivs = $$('.locationHoverFragment');
		for(var i = 0; i < hoverDivs.length; i++) {
			var divParent = hoverDivs[i].parentNode;
			var targetElem = evnt.findElement();
			while(targetElem != null && targetElem != divParent) {
				targetElem = targetElem.parentNode;	
			}
			
			if(targetElem != null && targetElem == divParent) return; //Move was on hover div, don't do anyting
						
			divParent.style.position = "";
			divParent.removeChild(hoverDivs[i]);
		}
	}
	catch(e) {
		lh__handleException(e);
	}
}
