/* $Id: map_search.js,v 1.2 2009/01/27 07:09:30 dipak Exp $ */

function sendMapRequest(ulLat,ulLng,brLat,brLng,zoomLevel)
{
	isCatch=true;
	var urlStr='';

	//  get request from database and show loading text
	pendingRequestCount--;
	isRunning = true;
	showLoadingText();

	urlStr=getUrl(ulLat,ulLng,brLat,brLng,zoomLevel);

	/** To check if First time map load then no need add hash */
	if(isFirstTime) {
		isFirstTime = false;
	}
	else {
		document.location.hash=urlStr;
	}
	for(var i=0;i<arrCatchUrl.length;i++)
	{
		/** Check if request is already in catch then get data from array */

		if(arrCatchUrl[i] == urlStr)
		{
			isCatch=false;
			getDataCallback(arrCatchUrlValue[i]); // pass the data from catch
			return false;
		}
	}

	/** To check if isCatch is true then send request to server */
	if(isCatch)
	{
		arrCatchUrl[arrCatchUrl.length+0]=urlStr;
		xmlHttp=GXmlHttp.create();
		xmlHttp.open("POST",location.protocol+'//'+location.hostname+'/getCoordinate.php', true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttp.send(urlStr);
		xmlHttp.onreadystatechange = function() {
				if (xmlHttp.readyState == 4)
				{
					var response = xmlHttp.responseXML.documentElement;

					/** Cache the respone into array */
					arrCatchUrlValue[arrCatchUrl.length-1]=response;

					if(xmlHttp.responseText == "not")
						return;
					else
						getDataCallback(response);

					delete response;
				}
		}
	}
}

function getDataCallback(response)
{
	if(lastOpenWindow[0] == '')
		map.clearOverlays();

	lastOpenWindow[0]='';

	rows = response.getElementsByTagName("Marker");
	for(var i=0;i<rows.length;i++)
	{
		markerOptions='';

		adId = rows[i].getElementsByTagName("adId")[0].childNodes[0].nodeValue;

		if(rows[i].getElementsByTagName("adType")[0])
		{
			adType = rows[i].getElementsByTagName("adType")[0].childNodes[0].nodeValue;
			markerOptions = { icon:icons[adType] };
		}

		currLatitude = rows[i].getElementsByTagName("lat")[0].childNodes[0].nodeValue;
		currLongitude = rows[i].getElementsByTagName("lng")[0].childNodes[0].nodeValue;
		showDetail=rows[i].getElementsByTagName("showDetail")[0].childNodes[0].nodeValue;

		// replace #euro# to euro sign
		showDetail = showDetail.gsub('#euro#',"€"); // gsub is a method of prototype.js

		point=new GLatLng(currLatitude,currLongitude);
		markerData = createMapMarker(point,adId,showDetail,markerOptions);
		map.addOverlay(markerData);
	}

	if(pendingRequestCount == 0) {
		hideLoadingText();
	}
	isRunning = false;
}

var lastOpenWindow = [];

function createMapMarker(point,adId,showDetail,option)
{
	var marker = new GMarker(point,option);
	var markerHtmlData="<div id=\"map_marker\"><ul>" + showDetail + "</ul></div>";
	marker.tooltip = "<div id=\"map_tooltip\">"+markerHtmlData+"</div>";

	GEvent.addListener(marker, "mouseover", function() {
		showTooltip(marker);
		//marker.openInfoWindowHtml(markerHtmlData);
	});
	GEvent.addListener(marker, "mouseout", function() {
		//marker.closeInfoWindow();
		if($('hideShowTooltip'))
			$('hideShowTooltip').style.visibility="hidden";
	});
	GEvent.addListener(marker, "click", function() {
		if($('hideShowTooltip'))
			$('hideShowTooltip').style.visibility="hidden";
		marker.openInfoWindowHtml(markerHtmlData,getOpenInfoWindow(adId));
	});
	return marker;
}

// ====== This function displays the tooltip ======
function showTooltip(marker)
{
	tooltip=$('hideShowTooltip');
	tooltip.innerHTML = marker.tooltip;

	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y));
	pos.apply(tooltip);

	tooltip.style.visibility="visible";
}

function getOpenInfoWindow(adId)
{
	lastOpenWindow[0] = adId;
}

/** To check request is running or not */
var isRunning=false;

function chkUpdater()
{

	/** To check if First time map loaded then no need to send request */
	if(isFirstTime) {
		return false;
	}

	/** To check if request is running */
	if(pendingRequestCount > 0){
		// already request is running
	} else {
		pendingRequestCount++;
		chkRequest();
	}
}

function chkRequest()
{
	// To check if request is running then wait and again check after specified time
	if(isRunning) {
		window.setTimeout("chkRequest()", 100);
	} else {
		sendMapRequest();
	}
}

function showLoadingText()
{
	$('loadingText').style.display = "block";
}

function hideLoadingText()
{
	$('loadingText').style.display = "none";
}

mapFull=true;
function mapResize(imgUrl)
{
	if(mapFull)
	{
		mapFull=false;
		$('right_nav').style.display='none';
		$('largeMap').style.position='absolute';
		$('content_size').style.width='97%';
		$('mapbluebox').style.height='660px';
		$('largeMap').style.width=$('content_size').getWidth() - 205 + 'px';
		$('map_canvas').style.width=$('content_size').getWidth() - 230 + 'px';
		$('map_canvas').style.height='600px';
		$('loadingText').style.left=$('content_size').getWidth() - 430 + 'px';
		$('loadingText').style.top= -570 + 'px';
		$('zoomImg').src=imgUrl+'/zoom_out.gif';
	}
	else
	{
		mapFull=true;
		$('right_nav').style.display='';
		$('largeMap').style.position='';
		$('content_size').style.width='76em';
		$('mapbluebox').style.height='560px';
		$('largeMap').style.width='auto';
		$('map_canvas').style.width='540px';
		$('map_canvas').style.height='500px';
		$('loadingText').style.left='340px';
		$('loadingText').style.top= -470 + 'px';
		$('zoomImg').src=imgUrl+'/zoom_in.gif';
	}

	var bounds = map.getBounds();
	ul=bounds.getSouthWest();
	br=bounds.getNorthEast();
	map_load(ul.lat(),ul.lng(),br.lat(),br.lng(),map.getZoom());
}