﻿
    var gmap = null;
    var mapSize = "large";
    var mgr = null;
    var cluster;
    var batch = [];
    var batchRA = [];
    var batchDR = [];
    var batchHV = [];
    var batchWC = [];
    var batchWCzoom = [];
    var batchSS = [];
    var batchHVSP = [];
    var batchCE = [];
    var batchRAHV = [];
    var batchRADR = [];
    
    var initMarker;
       
    var checkBoxes = [];
    checkBoxes.push("RA");
    checkBoxes.push("DR");
    checkBoxes.push("HV");
    checkBoxes.push("WC");
    checkBoxes.push("SS");
    checkBoxes.push("HVSP");
    checkBoxes.push("CE");
    checkBoxes.push("RAHV");
    checkBoxes.push("RADR");
    
  //WebCam image refresh  
  var refreshId = setInterval(function()
 {
 if(jQuery('#wcImg').length > 0)
 {
 jQuery('#wcImg').fadeOut("normal");
 var src =jQuery('#wcImg').attr('src').replace(/\&.*$/, '') + '&' + Math.random();
 jQuery('#wcImg').attr('src',src);
 jQuery('#wcImg').fadeIn("normal");
 }
 }, 60000);
 
 
/* initialise interactive maps */
jQuery(document).ready(function()
{
	// add event handlers for map controls
	jQuery("#poi-controls input[type='checkbox']").click(updateMarkers);
	
	initializeMap();
	updateMarkers();
});
jQuery(document).unload(function(){GUnload();});
jQuery.preloadImages("/images/mapicons/wsupply.gif","/images/mapicons/toilet.gif",
        "/images/mapicons/disabled_toilet.gif","/images/mapicons/table.gif",
        "/images/mapicons/shower.gif","/images/mapicons/sshed.gif","/images/mapicons/play.gif",
        "/images/mapicons/no_dogs.gif","/images/mapicons/lkout.gif","/images/mapicons/campg.gif",
        "/images/mapicons/bushwalk.gif","/images/mapicons/boat.gif","/images/mapicons/bbq.gif",
        "/images/mapicons/ce.gif","/images/mapicons/dr.gif","/images/mapicons/hv.gif",
        "/images/mapicons/hvsp.gif","/images/mapicons/ra.gif","/images/mapicons/radr.gif",
        "/images/mapicons/rahv.gif","/images/mapicons/ss.gif","/images/mapicons/wc.gif",
        "/images/mapicons/wce.gif","/images/mapicons/wcn.gif","/images/mapicons/wcne.gif",
        "/images/mapicons/wcnw.gif","/images/mapicons/wcs.gif","/images/mapicons/wcse.gif",
        "/images/mapicons/wcsw.gif","/images/mapicons/wcw.gif","/images/mapicons/wce.gif",
        "/images/mapicons/wcn.gif","/images/mapicons/wcne.gif","/images/mapicons/wcnw.gif",
        "/images/mapicons/wcs.gif","/images/mapicons/wcse.gif","/images/mapicons/wcsw.gif",
        "/images/mapicons/wcw.gif","/images/mapicons/arrow.gif",
        "/images/mapicons/arrowshadow.gif");

function initializeMap() {
    var tabheight = jQuery(window).height() -300;
    
    //Set the minimum
    if(tabheight<400)
        tabheight=400;

    jQuery('#gmap').height(tabheight);

    if(jQuery(window).width()<1024)
    {
        mapSize = "small";
    }
    else
    {
        mapSize = "large";
    }
    gmap = new GMap2(document.getElementById('gmap'));
    var mt = gmap.getMapTypes();    
    for (var i=0; i<mt.length; i++) {
        mt[i].getMinimumResolution = function() {return 4;}
        mt[i].getMaximumResolution = function() {return 20;}
    }
    gmap.setMapType(G_NORMAL_MAP);
    if(mapSize=="large")
    {
        gmap.addControl(new GMapTypeControl());
        gmap.addControl(new GLargeMapControl());
    }
    else
    {
        gmap.addControl(new GSmallMapControl());
    }
    //gmap.setCenter(new GLatLng(-19.5719, 146.277), 5); 
    gmap.enableScrollWheelZoom();
    centerMap();
    GEvent.addListener(gmap, "zoomend", function() {updateMarkers();});
    addMarkers();
}

 function toggleMarkers(checkbox, category){
     var cb = document.getElementById(checkbox);
    
	if(cb.checked){
	
		for(var i=batch.length-1; i>=0; i--){
			if(batch[i].category===category){
				batch[i].show();
			}
		}
	} else {
		for(var i=batch.length-1; i>=0; i--){
			if(batch[i].category===category){
				batch[i].hide();
			}
		}
	}
	cluster.refresh();
}

function clearAllCheckboxes()
{
    jQuery('input[id=RA]').attr('checked',false);   
    jQuery('input[id=DR]').attr('checked',false);   
    jQuery('input[id=HV]').attr('checked',false);   
    jQuery('input[id=WC]').attr('checked',false);   
    jQuery('input[id=SS]').attr('checked',false);   
    jQuery('input[id=HVSP]').attr('checked',false);   
    jQuery('input[id=CE]').attr('checked',false);   
    jQuery('input[id=RAHV]').attr('checked',false);   
    jQuery('input[id=RADR]').attr('checked',false);   
}

function checkAllCheckboxes()
{
    jQuery('input[id=RA]').attr('checked',true);   
    jQuery('input[id=DR]').attr('checked',true);   
    jQuery('input[id=HV]').attr('checked',true);   
    jQuery('input[id=WC]').attr('checked',true);   
    jQuery('input[id=SS]').attr('checked',true);   
    jQuery('input[id=HVSP]').attr('checked',true);   
    jQuery('input[id=CE]').attr('checked',true);   
    jQuery('input[id=RAHV]').attr('checked',true);   
    jQuery('input[id=RADR]').attr('checked',true);   
}

function updateMarkers()
{
	mgr.clearMarkers();
	mgr.refresh();
	cluster.removeMarkers();
	var tmp = [];
	var masterBatch = [];
	var chkList = '';
	
	if(jQuery('#CheckedPOIList').length > 0)
    {
       jQuery('#CheckedPOIList').html('');
       
    }
    
	for(var i=0; i<checkBoxes.length; i++)
	{
	    var chkBox = document.getElementById(checkBoxes[i]);
	    if(chkBox.checked)
	    {
	        switch(checkBoxes[i])
	        {
	            case "RA": tmp = batchRA;
	            break;
	            case "DR": tmp = batchDR;
	            break;
	            case "HV": tmp = batchHV;
	            break;
	            case "WC": 
	                if(gmap.getZoom()>=14)
	                    tmp = batchWCzoom;
	                else
	                    tmp = batchWC;
	            break;
	            case "SS": tmp = batchSS;
	            break;
	            case "HVSP": tmp = batchHVSP;
	            break;
	            case "CE": tmp = batchCE;
	            break;
	            case "RAHV": tmp = batchRAHV;
	            break;
	            case "RADR": tmp = batchRADR;
	            break;
	        }
	        masterBatch = masterBatch.concat(tmp);
	        
	         if(jQuery('#CheckedPOIList').length > 0)
             {
                chkList += checkBoxes[i] + ',';
//                var chkPoi = checkBoxes[i];
//                var lstPoi = jQuery('#CheckedPOIList').html();
//                alert('html' + lstPoi);
//                var lstPoiVal = jQuery('#CheckedPOIList').val();
//                alert('val' + lstPoiVal);
//                var fullList = lstPoi + chkPoi + ',';
//                alert('full' + fullList);
//                jQuery('#CheckedPOIList').html(fullList);
	         }
	    }
	 }   
	    if(jQuery('#CheckedPOIList').length > 0)
             {
             //alert(chkList);
                jQuery('#CheckedPOIList').val(chkList);
             }
	

	mgr.addMarkers(masterBatch, 10);
	mgr.refresh();
	cluster = new ClusterMarker(gmap, {markers:masterBatch});
	cluster.fitMapMaxZoom=10;
    cluster.intersectPadding=-2;
    cluster.clusterMarkerTitle='Click to zoom in and see %count Points of Interest';
    cluster.refresh();
}

function getInfo(lat, lng, cat, marker)
{
        var options = {  
            url: '/layouts/mrinternet/pages/mapinfo.aspx?lat=' + lat + '&lng=' + lng + '&cat=' + cat, 
            dataType: 'html', 
            async:false,
            success: function(responseText){ 
                    marker.openInfoWindowHtml(responseText);
		            },
		     error: function(XMLHttpRequest, textStatus, errorThrown){
                    marker.openInfoWindowHtml("An error occured retrieving information<br/> for this Point of interest");
                    }
           } 
     
	jQuery.ajax(options);
}  


function getM(point,moptions,category) {
var marker = new GMarker(point, moptions);
marker.category = category;
GEvent.addListener(marker, "click", function() {
getInfo(marker.getLatLng().lat(), marker.getLatLng().lng(), category, marker);
//marker.openInfoWindow(document.getElementById("mapinfodiv"));
});
return marker;
}

function showPOIInfo(marker)
{
    if(marker != null)
    {
    gmap.setCenter(marker.getLatLng(), 14);
    getInfo(marker.getLatLng().lat(), marker.getLatLng().lng(), marker.category, marker);
    }
}