//<![CDATA[
var arrHotel = new Array();
var map;
var restmap;
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,blnShowHotels,zoom) {
	//alert(point)
    var locationAddress;
	if (map.getZoom() == 12)  {
		map.setZoom(14);
	}
    map.clearOverlays();
    
	if(address != "")
		locationAddress = '<span class="red">'+unescape(restaurantName)+'</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())");
   
	if(blnShowHotels == null)
	{
		blnShowHotels = false;
	}
	if(blnShowHotels)
	{
		showNearByHotels(point)
	}
}

function showNearByHotels(point)
{
	$.ajax({
		url: "nearest_hotels_lat_lon.php", //path to external content
		data: "pt=" + point,
		async: true,
		error:function(ajaxrequest){
			alert('Error fetching content.\nServer Response: '+ajaxrequest.responseText)
		},
		success:function(content)
		{
            eval ("var result = new Array(" + content + ")");
            var nearbyhotelsId = "";
            for (counter = 0;counter < result.length;counter++) {
                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><br><span>' + result[counter][4] + '</span>';
                map.addOverlay(createMarker(pt,info));
				nearbyhotelsId += '<div style="width:212px;float:left;padding-top:4px;" class="col1_inner p_blue"> <a href="../' + result[counter][1] + '">' + result[counter][0] + '</a></div><div style="width:60px;float:left;padding-top:4px;"><img src="../images/'+result[counter][5]+'_redstar.gif"></div><br/>';
			}
			$("#nearbyhotels").html(nearbyhotelsId)
		}
	})
}

var pollCnt = 0; // flag to specify how many times the google map is invoked 
function noLocationError() {
	
	$("#noLocation").html(no_location_found);
	$("#noLocation").show();
}

/*
function searchLocation(address)
{
	$.ajax({
		url: "nearest_hotels_lat_lon_by_location_restaurants.php", //path to external content
		data: "location=" + escape(address),
		async: true,
		error:function(ajaxrequest){
			alert('Error fetching content.\nServer Response: '+ajaxrequest.responseText)
		},
		success:function(content)
		{
			var result = new String(content);
			var arrResult = new Array();
			arrResult = result.split("|");
			switch (arrResult[0])
			{
				case "0":
					resetMap(map);
					noLocationError();
					break;
				case "1":
					showMapwithMarker(arrResult[1],address,map,true);
					break;
				default:
					resetMap(map);
					break;
            } 
		}
	})
}
function showRestaurantMap(address)
{
	$.ajax({
		url: "nearest_hotels_lat_lon_by_location_restaurants.php", //path to external content
		data: "location=" + escape(address),
		async: true,
		error:function(ajaxrequest){
			alert('Error fetching content.\nServer Response: '+ajaxrequest.responseText)
		},
		success:function(content)
		{
			var result = new String(content);
			var arrResult = new Array();
			arrResult = result.split("|");
			showMapwithMarker(arrResult[1],address,restmap);
		}
	})
}
*/

function searchLocation(lat,lng,address)
{
	if(lat == null && lng == null)
	{
		resetMap(map);
		noLocationError();
	}
	else
	{
		var point = lat+","+lng;
		showMapwithMarker(point,address,map,true);
	}
}

function showRestaurantMap(lat, lng,address)
{
	var point = lat+","+lng;
	showMapwithMarker(point,address,restmap);
}

function showFeaturedRestaurantsMap()
{
	var point = "48.85884228699207,2.3328781127929688"
	showMapwithMarker(point,"",map);
	$.ajax({
		url: "nearest_featured_restaurants_lat_lon.php", //path to external content
		data: "pt=" + point,
		async: true,
		error:function(ajaxrequest){
			alert('Error fetching content.\nServer Response: '+ajaxrequest.responseText)
		},
		success:function(content)
		{
			eval ("var result = new Array(" + content + ")");
            for (counter = 0;counter < result.length;counter++) {
                var pt = new GLatLng(result[counter][2],result[counter][3]);
                var info = '<span class="red"><a href="restaurant_details.php?id=' + result[counter][1] + '&lang='+lang+'">' + result[counter][0] + '</a></span><br><span class="arial12blacknormal">' + result[counter][4] + '</span>';
                map.addOverlay(createMarker(pt,info));
			}
		}
	})
}

function showSearchedRestaurantsMap()
{
	if(arrSearchResult.length <= 0)
	{
		noLocationError();
		return;
	}
	var point = arrSearchResult[0][2]+","+arrSearchResult[0][3];
	showMapwithMarker(point,"",map);
	map.setZoom(13);
	for (counter = 0;counter < arrSearchResult.length;counter++)
	{
		var pt = new GLatLng(arrSearchResult[counter][2],arrSearchResult[counter][3]);
		var info = '<span class="red"><a href="restaurant_details.php?id=' + arrSearchResult[counter][1] + '&lang='+lang+'">' + arrSearchResult[counter][0] + '</a></span><br><span class="arial12blacknormal">' + arrSearchResult[counter][4] + '</span>';
		map.addOverlay(createMarker(pt,info));
	}
}

function initMap() {
    
    if (typeof(GMap2) != "undefined") {
        // Maps API version >= 2.36
        map = new GMap2(document.getElementById("mapDiv"));
		if(document.getElementById("mapDiv2") != null)
			restmap = new GMap2(document.getElementById("mapDiv2"));
        zoomEvent = 'zoomend'
    } else {
        // Maps API version <= 2.35
        map = new GMap2(document.getElementById("mapDiv"));
		if(document.getElementById("mapDiv2") != null)
			restmap = new GMap2(document.getElementById("mapDiv2"));
        zoomEvent = 'zoom'
    }
    
    map.addControl(new GLargeMapControl());
    map.setCenter(new GLatLng(48.85884228699207, 2.3328781127929688), 12);
	if(restmap != null)
	{
		restmap.addControl(new GSmallMapControl());
		restmap.setCenter(new GLatLng(48.85884228699207, 2.3328781127929688), 12);
	}
}