/*
	bMap - © 2010 Darren Dignam
	darren.dignam@blocsoft.com
	http://www.blocsoft.com/bMap
	Released under the GPL License
	http://www.gnu.org/licenses/gpl-3.0.txt
*/
(function(a){a.fn.bMap=function(b){eachOptions=b;return this.each(function(){obj=a(this);var d={mapCenter:[51,0],mapZoom:1,mapCanvas:obj.attr("id"),mapSidebar:"none",mapLayerbar:"none",mapType:G_NORMAL_MAP,loadMsg:"<h2>Loading...</h2>"};var c=a.extend(d,eachOptions);obj.data("bMap",new bMap(c))});return this}})(jQuery);function bMap(a){if(!GBrowserIsCompatible()){alert("This browser is unable to render the map!");return}var b={mapCenter:[51,0],mapZoom:1,mapCanvas:"map",mapSidebar:"none",mapLayerbar:"none",mapType:G_NORMAL_MAP,loadMsg:"<h2>Loading...</h2>"};var a=$.extend(b,a);this.mapSidebar=a.mapSidebar;this.useSidebar=(this.mapSidebar!="none")?true:false;this.mapLayerbar=a.mapLayerbar;this.useLayerbar=(this.mapLayerbar!="none")?true:false;this.layerMgrArray=[];this.map=new GMap2(document.getElementById(a.mapCanvas));this.map.setUIToDefault();this.map.setCenter(new GLatLng(a.mapCenter[0],a.mapCenter[1]),a.mapZoom);this.map.setMapType(a.mapType);this.mapCanvas=a.mapCanvas;$("#"+this.mapCanvas).append("<div id='"+this.mapCanvas+"bMapLoadMsg' class='bMapLoadMsg'>"+a.loadMsg+"</div>");$("#"+this.mapCanvas+"bMapLoadMsg").css("left",($("#map").width()/2)-50);$("#"+this.mapCanvas+"bMapLoadMsg").css("top",($("#map").height()/2)-50);if(a.icons){this.icons=a.icons}if(a.markers){this.insertMarkers(a.markers)}this.geoCoder=new GClientGeocoder()}bMap.prototype.insertMarkers=function(a){tmpThis=this;var b=tmpThis.layerMgrArray.length;var c={name:"Layer"+b,type:"marker",visible:"true"};var a=$.extend(c,a);tmpThis.layerMgrArray[b]=a;tmpThis.layerMgrArray[b].toggleLayer=function(){if(this.visible!="false"){this.visible="false";for(i=0,j=this.data.length;i<j;i++){this.data[i].hide()}$("#bMapLyr"+b).addClass("bLyrHide");return false}else{this.visible="true";for(i=0,j=this.data.length;i<j;i++){this.data[i].show()}$("#bMapLyr"+b).removeClass("bLyrHide");return true}};jQuery.each(a.data,function(f,g){var d=new GLatLng(g.lat,g.lng);if(g.icon){tmpThis.layerMgrArray[b].data[f]=new GMarker(d,tmpThis.icons[parseInt(g.icon)])}else{tmpThis.layerMgrArray[b].data[f]=new GMarker(d)}if(g.title){var e="<h2>"+g.title+"</h2>";if(g.body){e+=g.body}GEvent.addListener(tmpThis.layerMgrArray[b].data[f],"click",function(){tmpThis.layerMgrArray[b].data[f].openInfoWindowHtml(e);$("#"+tmpThis.mapSidebar+" div").removeClass("bSideSelect")})}tmpThis.map.addOverlay(tmpThis.layerMgrArray[b].data[f]);if(tmpThis.useSidebar){$('<div rel="'+b+'">'+g.title+"</div>").click(function(){GEvent.trigger(tmpThis.layerMgrArray[b].data[f],"click");$("#"+tmpThis.mapSidebar+" div").removeClass("bSideSelect");$(this).addClass("bSideSelect")}).appendTo("#"+tmpThis.mapSidebar)}});this.refreshLayerbar();return this};bMap.prototype.AJAXMarkers=function(c){var b={serviceURL:"mapService.php",action:"getMarkers",vars:[]};var c=$.extend(b,c);var a=this;$("#"+this.mapCanvas+"bMapLoadMsg").show();$.post(c.serviceURL,{action:c.action,vars:c.vars},function(d){a.insertMarkers(d);$("#"+a.mapCanvas+"bMapLoadMsg").hide()},"json");return this};bMap.prototype.insertLine=function(c){tmpThis=this;var a=tmpThis.layerMgrArray.length;var d={name:"Layer"+a,type:"line",visible:"true",color:"#00F",weight:5,opacity:1};var c=$.extend(d,c);tmpThis.layerMgrArray[a]=c;tmpThis.layerMgrArray[a].toggleLayer=function(){if(this.visible!="false"){this.visible="false";this.data.hide();$("#bMapLyr"+a).addClass("bLyrHide");return false}else{this.visible="true";this.data.show();$("#bMapLyr"+a).removeClass("bLyrHide");return true}};var b=[];jQuery.each(c.data,function(e,f){b.push(new GLatLng(f.lat,f.lng))});tmpThis.layerMgrArray[a].data=new GPolyline(b,tmpThis.layerMgrArray[a].color,parseInt(tmpThis.layerMgrArray[a].weight),parseFloat(tmpThis.layerMgrArray[a].opacity));tmpThis.map.addOverlay(tmpThis.layerMgrArray[a].data);if(c.visible!="true"){tmpThis.layerMgrArray[a].data.hide()}this.refreshLayerbar();return this};bMap.prototype.AJAXLine=function(b){var c={serviceURL:"mapService.php",action:"getLine",vars:[]};var b=$.extend(c,b);var a=this;$("#"+this.mapCanvas+"bMapLoadMsg").show();$.post(b.serviceURL,{action:b.action,vars:b.vars},function(d){a.insertLine(d);$("#"+a.mapCanvas+"bMapLoadMsg").hide()},"json");return this};bMap.prototype.insertPolygon=function(d){tmpThis=this;var b=tmpThis.layerMgrArray.length;var a={name:"Layer"+b,type:"polygon",visible:"true",color:"#00F",weight:5,opacity:0.5};var d=$.extend(a,d);tmpThis.layerMgrArray[b]=d;tmpThis.layerMgrArray[b].toggleLayer=function(){if(this.visible!="false"){this.visible="false";this.data.hide();$("#bMapLyr"+b).addClass("bLyrHide");return false}else{this.visible="true";this.data.show();$("#bMapLyr"+b).removeClass("bLyrHide");return true}};var c=[];jQuery.each(d.data,function(e,f){c.push(new GLatLng(f.lat,f.lng))});tmpThis.layerMgrArray[b].data=new GPolygon(c,tmpThis.layerMgrArray[b].color,parseInt(tmpThis.layerMgrArray[b].weight),1,tmpThis.layerMgrArray[b].color,parseFloat(tmpThis.layerMgrArray[b].opacity));tmpThis.map.addOverlay(tmpThis.layerMgrArray[b].data);if(d.visible!="true"){tmpThis.layerMgrArray[b].data.hide()}this.refreshLayerbar();return this};bMap.prototype.AJAXPolygon=function(b){var c={serviceURL:"mapService.php",action:"getPolygon",vars:[]};var b=$.extend(c,b);var a=this;$("#"+this.mapCanvas+"bMapLoadMsg").show();$.post(b.serviceURL,{action:b.action,vars:b.vars},function(d){a.insertPolygon(d);$("#"+a.mapCanvas+"bMapLoadMsg").hide()},"json");return this};bMap.prototype.removeAllLayers=function(){for(i=0,j=this.layerMgrArray.length;i<j;i++){if(this.layerMgrArray[i].type=="marker"){for(i2=0,j2=this.layerMgrArray[i].data.length;i2<j2;i2++){this.map.removeOverlay(this.layerMgrArray[i].data[i2])}}else{this.map.removeOverlay(this.layerMgrArray[i].data)}this.layerMgrArray[i].data=0;if(this.useSidebar){$("#"+this.mapSidebar+' div[rel="'+i+'"]').remove()}}this.layerMgrArray.length=0;this.refreshLayerbar();return this};bMap.prototype.removeLayer=function(a){if(this.layerMgrArray[a].type=="marker"){for(i2=0,j2=this.layerMgrArray[a].data.length;i2<j2;i2++){this.map.removeOverlay(this.layerMgrArray[a].data[i2])}}else{this.map.removeOverlay(this.layerMgrArray[a].data)}this.layerMgrArray[a].data=0;if(this.useSidebar){$("#"+this.mapSidebar+' div[rel="'+a+'"]').remove()}this.refreshLayerbar();return this};bMap.prototype.popLayer=function(){var a=this.layerMgrArray.length-1;var b=this.layerMgrArray.pop();if(b.type=="marker"){for(i2=0,j2=b.data.length;i2<j2;i2++){this.map.removeOverlay(b.data[i2])}}else{this.map.removeOverlay(b.data)}b.data=0;if(this.useSidebar){$("#"+this.mapSidebar+' div[rel="'+a+'"]').remove()}this.refreshLayerbar();return this};bMap.prototype.shiftLayer=function(){for(i3=0,j3=this.layerMgrArray.length;i3<j3;i3++){if(this.layerMgrArray[i3].data!=0){var a=i3;break}}if(this.layerMgrArray[a].type=="marker"){for(i2=0,j2=this.layerMgrArray[a].data.length;i2<j2;i2++){this.map.removeOverlay(this.layerMgrArray[a].data[i2])}}else{this.map.removeOverlay(this.layerMgrArray[a].data)}this.layerMgrArray[a].data=0;this.layerMgrArray[a].name="";this.layerMgrArray[a].type="";if(this.useSidebar){$("#"+this.mapSidebar+' div[rel="'+a+'"]').remove()}this.refreshLayerbar();return this};bMap.prototype.refreshLayerbar=function(){if(this.mapLayerbar){var b=this;$("#"+this.mapLayerbar).html("");for(var d=0,c=this.layerMgrArray.length;d<c;d++){if(this.layerMgrArray[d].data!=0){if(this.layerMgrArray[d].visible!="false"){var a=""}else{a="class='bLyrHide' "}$("<div "+a+"id='bMapLyr"+d+"' rel='"+d+"'>"+this.layerMgrArray[d].name+"</div>").click(function(){b.layerMgrArray[$(this).attr("rel")].toggleLayer()}).appendTo("#"+this.mapLayerbar)}}}};bMap.prototype.centerAtAddress=function(b){var a=this;this.geoCoder.getLatLng(b,function(c){if(c){a.map.setCenter(c)}});return this};
