﻿// JScript File

var craftMapObj;
var craftzoomlevel;
//var load;
//var OtherObj;
//Gets called when country combo box selection changes
function CreateCraftMap(mapCanvas,craftid,zoomlevel) 
{
    craftMapObj = mapCanvas;
   
   // URL to get states for a given country
	var requestUrl = "ajaxPages/craftmap.aspx?craft=" + craftid
    craftzoomlevel = zoomlevel;
    CreateXmlHttp();
		
		
		// If browser supports XMLHTTPRequest object
		if(XmlHttp)
		{
			//Setting the event handler for the response
			XmlHttp.onreadystatechange = CraftMapHandleResponse;
			
			//Initializes the request object with GET (METHOD of posting), 
			//Request URL and sets the request as asynchronous.
			
			XmlHttp.open("GET", requestUrl,  true);
			
			//Sends the request to server
			XmlHttp.send(null);		
		}
	
}


//Called when response comes back from server
function CraftMapHandleResponse()
{
	   var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
        var ie = ( typeof window.ActiveXObject != 'undefined');
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{	
		    //var result = XmlHttp.responseText;
		    var strData= XmlHttp.responseText;
            var data  = JSON.decode(strData);
			createMap(data);
		}
		else
		{
		//	alert("There was a problem retrieving data from the server." );
		}
	}
}
function createMarker(point, index) 
{
    var options = {title: index,draggable: false,bounceGravity: 0.8};
    
    var marker = new GMarker(point,options);
    GEvent.addListener(marker, "click", function() 
    {
            marker.openInfoWindowHtml( index );
    });
    return marker;
}
function createMap(data) 
{
        
        var mapLat='22.5642111111';
        var mapLng='71.5976388889';
        
        if (GBrowserIsCompatible()) 
        {
            var map = new GMap2(document.getElementById(craftMapObj));
//        
             map.setCenter(new GLatLng(mapLat, mapLng), craftzoomlevel);
//        
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());

//            // Add 10 markers to the map at random locations
            var bounds = map.getBounds();
            var southWest = bounds.getSouthWest();
            var northEast = bounds.getNorthEast();
            var lngSpan = northEast.lng() - southWest.lng();
            var latSpan = northEast.lat() - southWest.lat();
//            
//            var placeLtd = latitude;
//            var placeLng = longitude;
//            var locationNam = loc;
            //alert(placeLtd.length + " - " + placeLng.length + " - " + locationNam.length)
//            
            for (var i = 0; i <= data.point.length -1; i++)
            {
                var obj = data.point[i];
                  var point = new GLatLng(obj.long,obj.let);
     	          map.addOverlay(createMarker(point, obj.city));        
            }
          
      }
    }