﻿//------------------------------------------------------------------------
// This script is customised to load a map on the Our Roles and Goals
// Horizons page with markers which are specified in an XML file
//------------------------------------------------------------------------

var AirOurRolesAndGoals = new Object();

AirOurRolesAndGoals.zoom = 8;
AirOurRolesAndGoals.initialLat = -39.67627712760097;
AirOurRolesAndGoals.initialLng = 175.79566955566406;
AirOurRolesAndGoals.map = null;

AirOurRolesAndGoals.Init = function(div) {
    if(AirOurRolesAndGoals.GMap != undefined) {
	    GUnload();
    }
	AirOurRolesAndGoals.GMap = new AirOurRolesAndGoals.Map(div);
}

AirOurRolesAndGoals.Map = function(div) {
    if(div) {
        div.style.width = "450px";
        div.style.height = "630px";
        this.map = new GMap2(div);
        AirOurRolesAndGoals.map = this.map;
        
        LegendControl1 = new LegendControl();
        this.map.addControl(LegendControl1);
        
	    this.map.setCenter(new GLatLng(AirOurRolesAndGoals.initialLat, AirOurRolesAndGoals.initialLng), AirOurRolesAndGoals.zoom);
	    //YAHOO.util.Event.addListener(window, "unload", GUnload);
	    	    	    
	    this.LoadOverlays('images/AirOurRolesAndGoals/overlays.xml');
	    
	    setTimeout("AirOurRolesAndGoals.map.setMapType(G_PHYSICAL_MAP);", 1);
	}
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Description: 
//     performs the common function of checking if a file
//     is in the cache and loading it if its not. It then
//     calls the provided processing function to process
//     the loaded data.
// Params:
// - filename : file to be loaded, usually and xml file
// - processFunc : the function to process the file
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AirOurRolesAndGoals.Map.prototype.LoadFileData = function(filename,processFunc) {
    var xml = null;
    GDownloadUrl(filename, function(data, responseCode) {
	    xml = GXml.parse(data); 
	    processFunc(this.map,xml);
    });  
}

    
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Description: 
//     Load the overlays defined in the xmlfile onto the map
// Params:
// - xmlfile : filename of xml file containing markers
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AirOurRolesAndGoals.Map.prototype.LoadOverlays = function(xmlfile) {
    var iconpath = 'look/Horizons2007Brand/AirQualityMatters/';
	if(this.map) {
		var processFunc = function (map,xml) {
			var lat = 0;
			var lng = 0;
			var txt = "";
			var icon = "";
			
			// Load Markers
		    var markers = xml.documentElement.getElementsByTagName("marker");
			for (var iu = 0; iu < markers.length; iu++) {
				lat = parseFloat(markers[iu].getAttribute("lat"));
				lng = parseFloat(markers[iu].getAttribute("lng"));
				titleIcon = markers[iu].getAttribute("titleIcon");
				title = markers[iu].getAttribute("title");
				name = markers[iu].getAttribute("name");
				icon = markers[iu].getAttribute("icon");
				
				// AirOurRolesAndGoals.AddTitle(new GLatLng(lat,lng), name + " - " + title,  iconpath + titleIcon);
				AirOurRolesAndGoals.AddIcon(new GLatLng(lat,lng), name + " - " + title, iconpath + icon);
			}
			
			// Load Polygons
			var areas = xml.documentElement.getElementsByTagName("polygon");
			if(areas) {
			    for (var i = 0; i < areas.length; i++) {
			        var pointStr = areas[i].getAttribute("latlngs");
			        var fill = areas[i].getAttribute("fill");
			        var border = areas[i].getAttribute("border");
        			
			        pointStr = "[" + pointStr + "]";
			        var latLngArray = eval(pointStr);
			        var pointArray = [];
        			
			        for(var j = 0; j < latLngArray.length; j = j + 2){
				        lat = latLngArray[j+1];
				        lng = latLngArray[j];
        				
				        pointArray.push(new GLatLng(lat,lng));
			        }
        			
			        if(latLngArray[0] != latLngArray[j] || latLngArray[1] != latLngArray[j+1])
				        pointArray.push(new GLatLng("" + latLngArray[1],"" + latLngArray[0]));
        			
			        var polygon = new GPolygon(pointArray, border, 1, 0.9,fill,0.2, {clickable: false}); 
        			
			        AirOurRolesAndGoals.map.addOverlay(polygon);
		        }	
		    }
		};
	
		this.LoadFileData(xmlfile,processFunc);
	}
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Description: 
    //     Add an icon to the map
    // Params:
    // - latlng : GLatlng value for the position to place the icon
    // - text : the text to be displayed in the popup
    // - iconSrc : filename of icon image
    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AirOurRolesAndGoals.AddIcon = function(latlng,title,iconSrc) {
    if(AirOurRolesAndGoals.map) {
		var opt = new Object();
		var icon = new GIcon();
		
		icon.image = iconSrc;
		icon.iconSize = new GSize(15, 15);
		icon.iconAnchor = new GPoint(7, 7);
		
		opt.title = title;
		opt.icon = icon;
		opt.tooltip = title;
		opt.draggable = false;
		
		var marker = new GMarker(latlng, opt);
		AirOurRolesAndGoals.map.addOverlay(marker);
	}
}

AirOurRolesAndGoals.AddTitle = function(latlng,title,iconSrc) {
    if(AirOurRolesAndGoals.map) {
        var opt = new Object();
		var icon = new GIcon();
		
		icon.image = iconSrc;
		icon.title = title;
		icon.iconSize = new GSize(100, 15);
		icon.iconAnchor = new GPoint(0,7);
		
		opt.icon = icon;
		opt.title = title;
		opt.draggable = false;
		
		var marker = new GMarker(latlng, opt);
		AirOurRolesAndGoals.map.addOverlay(marker);
	}

}

//==========================================================
//
//       Water Quality Matters Legend Control
//
//==========================================================

//
// Parameter Control Constructor
//
function LegendControl() { }

//
// To "subclass" the GControl, we set the prototype 
// object to an instance of the GControl object
//
LegendControl.prototype = new GControl();

//
// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
//
// Params:
//  - map : google map instance
// 
LegendControl.prototype.initialize = function(map) {
    var container = document.createElement("div");
    
    //ParametersContainer
    var parametersContainer = document.createElement("div");
    parametersContainer.className = "ARGLegend";
 
    container.appendChild(parametersContainer);
    map.getContainer().appendChild(container);
    return container;
}

//
// Function: ParametersControl.prototype.getDefaultPosition
//
LegendControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(5, 5));//anchor control to top left with padding of 5px
}
