﻿/*

    GMapWrapper : Google Maps Wrapper
    
    Ali Granger

*/

var Global = [];
var localSearch = new GlocalSearch();
 
function GMapWrapper ( oArg ) {

    Global.push(this);
    this.scopeIdx = Global.length - 1;
    this.timeout = 200;

    if (GBrowserIsCompatible()) {

        this.browsercompatible = true;   

        // Constructor
        this.mapid = typeof oArg.mapid != 'undefined' ? oArg.mapid : "map";
        this.title = typeof oArg.title != 'undefined' ? oArg.title : "";
        this.tooltip = typeof oArg.tooltip != 'undefined' ? oArg.tooltip : "";
        this.descid = typeof oArg.descid != 'undefined' ? oArg.descid : "";
        this.zoom = typeof oArg.zoom != 'undefined' ? oArg.zoom : 15;
        this.view = typeof oArg.view != 'undefined' ? oArg.view : G_NORMAL_MAP;
        this.controls = typeof oArg.controls != 'undefined' ? oArg.controls : [[new GSmallMapControl()]];
        this.pqueue = typeof oArg.points != 'undefined' ? oArg.points.reverse() : [];
        this.centericon = typeof oArg.centericon != 'undefined' ? oArg.centericon : [];
        this.showcentermarker = typeof oArg.showcentermarker != 'undefined' ? oArg.showcentermarker : true;
        this.queuestate = 1;
        this.points = [];
        this.markers = [];
        this.icons = [];
        this.manager;
        
        // Icons
        this.icons["default"] = getGIcon(getDefaultIcon());
        if (typeof oArg.icons != 'undefined') {
            if (oArg.icons.length > 0) {
                for (var i = 0; i <oArg.icons.length; i++) {
                    this.icons[oArg.icons[i][0]] = oArg.icons[i][1];
                } 

            }
        }

        // Add a centerpoint
        this.pqueue.push(this.getPoint(oArg));
        
      	if (Global.length == 1) {
      	    addLoadEvent(loadMaps);
		    addUnLoadEvent(GUnload);
		}
    }
    else {
        this.browsercompatible = false;
    }
   
}   

GMapWrapper.prototype.focus = function () {
    // Focus on the map
    if (typeof document.getElementById(this.mapid) != 'undefined') {
        document.getElementById(this.mapid).focus();
    }
}


GMapWrapper.prototype.getPoint = function (oArg) {
    // Returns a GMapWrapper point
    var icon = '';
    if (typeof oArg.icon != 'undefined') { icon = oArg.icon;}
    else if (oArg.centericon != 'undefined') {icon = oArg.centericon;}
    else {icon = "default";};
    var gpcoords =  (typeof oArg.coords != 'undefined' ? oArg.coords : []);
    var gppostcode = (typeof oArg.postcode != 'undefined' ? oArg.postcode : "");
    var gptitle = (typeof oArg.descid != 'undefined' ? oArg.descid : "")   
    return new GMWMarker(gpcoords,gppostcode,icon,gptitle);
}  

GMapWrapper.prototype.ready=function () {
    // Adds the markers and draws the map
    var m;
    for (var i = 0; i <this.points.length; i++) {
            var p = this.points[i];
            var ic = typeof this.icons[p.category] != 'undefined' ? this.icons[p.category] : G_DEFAULT_ICON;
            var d = document.getElementById(p.descid) != null ? document.getElementById(p.descid).innerHTML : '';
            m = d == ''  ? new GMarker(new GLatLng(p.coords[0],p.coords[1]),{icon:ic,clickable:false,title:p.tooltip}) : new GMarker(new GLatLng(p.coords[0],p.coords[1]),{icon:ic,title:p.tooltip});
            m.category = p.category;
            if (d!='') {
                m.html = d;
                GEvent.addListener(m,"click",function(){this.openInfoWindowHtml(this.html);}); 
            }           
            this.markers.push(m);
    } 
    this.map.setCenter(this.markers[0].getLatLng(),this.zoom,this.view);
        
    // add the Marker manager
    this.manager = new MarkerManager(this.map,{borderPadding: 10});

    for (var i = 0; i <this.markers.length; i++) {
        m = this.markers[i];
        this.manager.addMarker(m,this.points[i].minzoom,this.points[i].maxzoom);
        if (this.showcentermarker==false && i==0) {m.hide();}
    } 
}

GMapWrapper.prototype.addControls = function () {
    // Add GControls to the map
    for (var i=0;i<this.controls.length;i++) {
        if (this.controls[i].length == 1) {
            this.map.addControl(this.controls[i][0]);
        }
        else if (this.controls[i].length == 2) {
            this.map.addControl(this.controls[i][0],this.controls[i][1]);      
        }   
    }
}

GMapWrapper.prototype.processQueue = function (obj) {
    // Process the points.  If Lat and Lng are provided, then it's good, otherwise search for them from the postcode.
    endFunction = false;
    if (this.queuestate == 1) {
        if (this.pqueue.length > 0) {
            var p = this.pqueue[this.pqueue.length-1];
            if (p.coords.length == 2) {
                // point already specifies coordinates, so pop this to the points array
                this.points.push(p);
                this.pqueue.pop();
            }
            else {
                // instigate a local search
                localSearch.setSearchCompleteCallback(this, 
	                function() {
	                    var p1 = this.pqueue[this.pqueue.length-1];
		                if (localSearch.results[0])
		                {
			                p1.coords[0] = localSearch.results[0].lat;
			                p1.coords[1] = localSearch.results[0].lng;
			                this.points.push(p1);
		                }
		                this.pqueue.pop();
		                this.queuestate =1;
	                });	
                localSearch.execute(p.postcode + ", UK");  
                this.queuestate ++;                
            }
        } else
        {
            // No more entries in the queue - exit processQueue
            this.ready();
            endFunction = true;
        }
        
    }
    else {
        this.queuestate++;
        endFunction = (this.queuestate > 20);
    }   
    if (!endFunction) {
        setTimeout(function(){obj.processQueue(obj)},  this.timeout);
    }

}

GMapWrapper.prototype.show = function (category) {
    for (var i = 0; i <this.markers.length; i++) {
        
        if (this.markers[i].category == category) {
            this.markers[i].show();
        }
    }
}

GMapWrapper.prototype.hide = function (category) {
    for (var i = 0; i <this.markers.length; i++) {
        if (this.markers[i].category == category) {
            this.markers[i].hide();
        }
    }
}

GMapWrapper.prototype.togglebox = function (category,control) {
    if (control.checked) {
        this.show(category);
        control.checked = true;
    }
    else {
        this.hide(category);
        control.checked = false;
    }
    //alert(control.checked);
    //this.manager.refresh();
    //alert('end');

}

GMapWrapper.prototype.openMarkerById = function (markerId) {

    var pos = 0;
    // First evaluate the center
    for (var i = 0; i <this.points.length; i++) {
        var p = this.points[i];
        if (p.descid == markerId) {
            pos = i;
            break;
        }
    }
    this.openMarker(pos);
}

GMapWrapper.prototype.openMarker = function (markerIdx) {
    // from econym.co.uk
    var m = this.markers[markerIdx];
    if (m.getPoint().lat() < 90) {
        var iwAnchor = m.getIcon().infoWindowAnchor;
        var iconAnchor = m.getIcon().iconAnchor;
        var offset = new GSize(iwAnchor.x-iconAnchor.x,iwAnchor.y-iconAnchor.y);
        this.map.openInfoWindow(m.getLatLng(), m.html, {pixelOffset:offset});
    }
}

function loadMaps() {
    var gmap
    for (var i=0;i<Global.length;i++) {
        gmap = Global[i];
        gmap.map = new GMap2(document.getElementById(gmap.mapid));
        gmap.addControls();
        gmap.processQueue(gmap);
    }
}

function getDefaultIcon() {

    return getGIcon({
                        image: "http://www.google.com/mapfiles/marker.png",
                        shadow: "http://www.google.com/mapfiles/shadow50.png",
                        iconSize: new GSize(20, 34),
                        shadowSize: new GSize(37, 34),
                        iconAnchor: new GPoint(10, 34),
                        infoWindowAnchor: new GPoint(9, 2),
                        infoShadowAnchor: new GPoint(18, 25)                
                    })

}

function GMWMarker(coords,postcode,category,descid,minzoom,maxzoom,tooltip) {
    this.coords = typeof coords != 'undefined' ? coords : [];
    this.postcode = typeof postcode != 'undefined' ? postcode : '';
    this.category = typeof category != 'undefined' ? category : 'default';
    this.descid = typeof descid != 'undefined' ? descid : '';
    this.minzoom = typeof minzoom != 'undefined' ? minzoom : 0;
    this.maxzoom = typeof maxzoom != 'undefined' && maxzoom <= 17 ? maxzoom : 17;
    this.tooltip = typeof tooltip != 'undefined' ? tooltip : '';
}    

function getGIcon( oGArg ) {

    var icon = new GIcon(G_DEFAULT_ICON);
    icon.image = typeof oGArg.image != 'undefined'? oGArg.image : "http://www.google.com/mapfiles/marker.png";
    icon.iconSize = typeof oGArg.iconSize != 'undefined'? oGArg.iconSize : new GSize(20, 34);
    icon.iconAnchor = typeof oGArg.iconAnchor != 'undefined'? oGArg.iconAnchor : new GPoint(10, 34);
    icon.infoWindowAnchor = typeof oGArg.infoWindowAnchor != 'undefined'? oGArg.infoWindowAnchor : new GPoint(9, 2);
    icon.shadow = typeof oGArg.shadow != 'undefined'? oGArg.shadow : "";
    icon.shadowSize = typeof oGArg.shadowSize != 'undefined'? oGArg.shadowSize : new GSize(37, 34);
    icon.infoShadowAnchor = typeof oGArg.infoShadowAnchor != 'undefined'? oGArg.infoShadowAnchor : new GPoint(18, 25);
    return icon;
}   
   
            function addLoadEvent(func) {
              var oldonload = window.onload;
              if (typeof window.onload != 'function') {
                window.onload = func;
              } else {
                window.onload = function() {
                  oldonload();
                  func();
                }
              }
            }

            function addUnLoadEvent(func) {
	            var oldonunload = window.onunload;
	            if (typeof window.onunload != 'function') {
	              window.onunload = func;
	            } else {
	              window.onunload = function() {
	                oldonunload();
	                func();
	              }
	            }
            } 
            
  GMarker.prototype.hide = function () 
  { 
    if (this.getPoint().lat() < 90) 
      try { 
        this.savePoint = this.getPoint(); 
        this.setPoint(new GLatLng(90, 0)); 
      } catch(e) {} 
  } 
  GMarker.prototype.show = function () 
  { 
    if (this.getPoint().lat() == 90) 
    if (this.savePoint) 
      try { 
        this.setPoint(this.savePoint); 
        this.savePoint = null; 
      } catch(e) {} 
  } 
            