﻿// JScript File
var map;
var index = 0;
var intervalID;
var outposts;
//var mgr;

function InitMap(mapview, width, height)    //param: div container ID, map width, map height
{
    mapview.style.width = width;
    mapview.style.height = height;
			
    map = new GMap2(mapview);		
    
    map.addControl(new GLargeMapControl());
    //map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());    
    
    map.addMapType(G_PHYSICAL_MAP); 
	var myMapTypeControl = new GHierarchicalMapTypeControl();
	map.addControl(myMapTypeControl)								
					
    map.enableDoubleClickZoom();
    map.enableScrollWheelZoom();
    map.enableContinuousZoom();
    
    map.setCenter(new GLatLng(-26.086952411999144,133.0640625), 4, G_NORMAL_MAP);
    //map.setCenter(new GLatLng(-41.5214166666667, 173.8386), 12, G_NORMAL_MAP);
        
    ShowOutposts(document.getElementById(valueControlID).value);
}

function ShowOutposts(outpostName)
{   
    MeterMateV2.GMapService.ShowOutposts(outpostName, SucceededCallback_ShowOutposts);
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object, the user context, and the 
// calling method name as parameters.
function SucceededCallbackWithContext(result, userContext, methodName)
{
    var output;
    
    // Page element to display feedback.
    var RsltElem = document.getElementById("ResultId");
    
    var readResult;
    if (userContext == "XmlDocument")
	{	
	    if (document.all) 
	        readResult = 
		        result.documentElement.firstChild.text;
		else
		    // Firefox
		   readResult =
		        result.documentElement.firstChild.textContent;
		
	     RsltElem.innerHTML = "XmlDocument content: " + readResult;
	}
    
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.
function SucceededCallback(result, eventArgs)
{
    // Page element to display feedback.
    var RsltElem = document.getElementById("ResultId");
    RsltElem.innerHTML = result;
}

function SucceededCallback_ShowOutposts(result, eventArgs)
{
    //alert(1);
    clearInterval(intervalID);
    index=0;    
    map.clearOverlays();
    
    //var mgrOptions = { borderPadding: 0, maxZoom: 15, trackMarkers: false };    
    //mgr = new GMarkerManager(map, mgrOptions);
    
    if(result != '') 
    {
        outposts = result.split('^');        
        intervalID = setInterval("AddNextMarkers()", 100);
    }   
}

function AddNextMarkers() 
{
    //alert(index);
    for(i=index; i<=index + 20; i++)
    {   
        if (i > outposts.length - 2) {
            clearInterval( intervalID );
            return;
        }          
        var temp = outposts[i].split("~");
        
        var icon = new GIcon();
        icon.image = 'http://www.outpostcentral.com/MeterMate/images/gis_marker_green.gif';
        //icon.image = '../images/gis_marker_green.gif';
        icon.shadow = 'http://www.google.com/mapfiles/shadow50.png';
        icon.iconSize = new GSize(20, 34);
        icon.shadowSize = new GSize(37, 34);
        icon.iconAnchor = new GPoint(6, 20);
        icon.infoWindowAnchor = new GPoint(8, 12);
    	
        var opts = new Object(); 
        opts.icon = icon; 
        opts.title = temp[1];
    						
        var marker = new GMarker(new GLatLng(temp[2], temp[3]), opts);
        
        markerClickedEvent(marker, temp[0], temp[1]);                       
            									
        map.addOverlay(marker);       
        
        if(temp[0] == "1222")
            marker.openInfoWindow('Click on this green icon to check out our demo site!');
        //mgr.addMarker(marker,0,15); 
    }
    index = i;
}


// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.
function FailedCallback(error)
{
    // Display the error.    
    var RsltElem = 
        document.getElementById("ResultId");
    RsltElem.innerHTML = 
    "Service Error: " + error.get_message();
}


function markerClickedEvent(marker, outpostID, outpostName) {
	GEvent.addListener(marker, "click", function() {		     
	    document.getElementById('childFrame').src = 'UserLogin.aspx?id=' + outpostID;
	    showModalPopupViaClient();	   
	});
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
