//<![CDATA[
var arrHotel = new Array();
var map;
// var xmlhttp=false;
// var xmlhttp_location = false;
// var xmlhttp_check_location = false;
var zoomEvent;
var marker_click_flag = false;
var baseIcon = new GIcon();
baseIcon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
baseIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
baseIcon.iconSize = new GSize(12, 20);
baseIcon.shadowSize = new GSize(22, 20);
baseIcon.iconAnchor = new GPoint(6, 20);
baseIcon.infoWindowAnchor = new GPoint(5, 1);

// Creates a marker at the given point with the given number label
function createMarker(point, htmlInfo)
{
    var marker = new GMarker(point);
    GEvent.addListener(marker, "mouseover", function() {
    marker.openInfoWindowHtml(htmlInfo);
    });
    
    GEvent.addListener(marker, "click", function() {
        marker_click_flag = true;
    });
    return marker;
}

function resetMap(map)
{
    map.clearOverlays();
    map.setCenter(new GLatLng(48.85884228699207, 2.3328781127929688), 12);
}

function showMapwithMarker(point,address,map) {
	//alert(point)
    var locationAddress;
    var errorDiv = document.getElementById("noLocation");
    errorDiv.style.display = "none";

    if (map.getZoom() == 12)  {
        map.setZoom(14);
    }
    
    map.clearOverlays();
    
	locationAddress = '<span class="jeventName">'+unescape(eventName)+'</span><br><span class="arial12blacknormal">'+address+'</spna>';

    eval ("var center_marker = new GMarker(new GLatLng(" + point + "), baseIcon)");
    
    GEvent.addListener(center_marker, "mouseover", function() {
        center_marker.openInfoWindowHtml(locationAddress);
    });
    
    GEvent.addListener(center_marker, "mouseout", function() {
        map.closeInfoWindow();
    });
    
    GEvent.addListener(center_marker, "click", function() {
        marker_click_flag = true;
    });

    map.addOverlay(center_marker);
    
    eval ("map.setCenter(new GLatLng(" + point + "), map.getZoom())");
    
    xmlhttp=getXMLHttpRequestObj();//new XMLHttpRequest();
    xmlhttp.onreadystatechange=function() 
    {
        if(xmlhttp.readyState == 4 && (xmlhttp.status == 200 || xmlhttp.status == 304)) {
            eval ("var result = new Array(" + xmlhttp.responseText + ")");
			var nearbyhotelsId = ""; //document.getElementById("nearbyhotels");
			var counter = 0;
            while (counter < result.length) {
                var pt = new GLatLng(result[counter][2],result[counter][3]);
                var info = '<span class="red"><a href="' + result[counter][1] + '">' + result[counter][0] + '</a></span> <img src="images/' + result[counter][5] + '_blackstar_hoteldetails.gif"><br><span class="arial12blacknormal">' + result[counter][4] + '</span>';
                map.addOverlay(createMarker(pt,info));
				if(counter % 2 == 0)
				{
					if(counter == 0)
					{
						nearbyhotelsId += '<div class="col1_inner3">';
					}
					if(counter > 0)
					{
						nearbyhotelsId += '</div><div class="col1_inner3">';
					}
				}
				nearbyhotelsId += '<span class="col1_inner p_blue"><a href="' + result[counter][1] + '">' + result[counter][0] + '</a></span>';
				counter++;
			}
			nearbyhotelsId += '</div>';
			$("#nearbyhotels").html(nearbyhotelsId)
        } 
    };
    
    xmlhttp.open("GET","nearest_hotels_lat_lon.php?pt=" + point+"&lang="+lang);
    xmlhttp.send(null);
	//tb_remove();
}
var pollCnt = 0; // flag to specify how many times the google map is invoked 
function noLocationError() {
	
	var labelNearBy = document.getElementById("labelNearBy");
	var nearByTbl = document.getElementById("nearbyhotels");
	var hotelRow = nearByTbl.insertRow(0);
	var hotelCell = hotelRow.insertCell(0);
	var errorMsg ="";
	pollCnt++;
	if(pollCnt < 2)
	{
		errorMsg = "<span class='arial11redbold'>"+system_error+"</span>";
		venueName = unescape(venueName);
		//alert(venueName)
		searchLocation(venueName,map);
	}
	if(pollCnt == 2)
	{
		labelNearBy.innerHTML = no_location_found;
		//hotelCell.innerHTML = errorMsg;
	}
}

function showMultipleLocations (arrResult,address,map) {
    var result = arrResult.join("|");
    var errorDiv = document.getElementById("noLocation");
    errorDiv.style.display = "none";
    
    xmlhttp_location=getXMLHttpRequestObj();//new XMLHttpRequest();
    xmlhttp_location.onreadystatechange=function() 
    {
        if(xmlhttp_location.readyState == 4 && (xmlhttp_location.status == 200 || xmlhttp_location.status == 304)) {
            errorDiv.style.display = "";
            errorDiv.innerHTML = xmlhttp_location.responseText;
        }
    };
    
    xmlhttp_location.open("GET","nearest_hotels_location_by_lat_lon.php?location=" + result);
    xmlhttp_location.send(null);
	//tb_remove();
    
}

function searchLocation(address,map) {
		//alert(address)
        xmlhttp_location=getXMLHttpRequestObj();//new XMLHttpRequest();
        xmlhttp_location.onreadystatechange=function() 
        {
			if(xmlhttp_location.readyState == 4 && (xmlhttp_location.status == 200 || xmlhttp_location.status == 304)) {
                var result = new String(xmlhttp_location.responseText);
                var arrResult = new Array();
				//alert(result);
				arrResult = result.split("|");
                switch (arrResult[0]) {
                    case "0":
                        resetMap(map);
                        noLocationError();
						//setTimeout("tb_remove()",5000);
                        break;
                    case "1":
                        showMapwithMarker(arrResult[1],address,map);
						//setTimeout("tb_remove()",5000);
                        break;
                    default:
                        resetMap(map);
                        arrResult.shift();
                        showMultipleLocations(arrResult,address,map);
						//setTimeout("tb_remove()",5000);
                        break;
                } 
            }
        };
        xmlhttp_location.open("GET","nearest_hotels_lat_lon_by_location_event.php?location=" + address);
        xmlhttp_location.send(null);
}

function initMap() {
    
    if (typeof(GMap2) != "undefined") {
        // Maps API version >= 2.36
        map = new GMap2(document.getElementById("mapDiv"));
        zoomEvent = 'zoomend'
    } else {
        // Maps API version <= 2.35
        map = new GMap2(document.getElementById("mapDiv"));
        zoomEvent = 'zoom'
    }
    
    map.addControl(new GLargeMapControl());
    map.setCenter(new GLatLng(48.85884228699207, 2.3328781127929688), 12);
    
    GEvent.addListener(map, zoomEvent, function(oldLevel,  newLevel) {
        if (newLevel < 12) {
            map.setZoom(12);
        } else if (newLevel > 17) {
            map.setZoom(17);
        } else {
            map.setZoom(newLevel);
        }
    });
    
	return map;
}

function getXMLHttpRequestObj()
{
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
 }
//]]>
