/*
    mapwrapper - The jQuery plugin which allows you to easily include a map without messing around with maps apis.
    Copyright (C) 2011  Luca Maggioni

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>

    luca.maggioni@gmail.com

----------------
mapwrapper 0.3.0
----------------
If you like this plugin, please consider making a donation following this link:
https://www.paypal.com/uk/cgi-bin/webscr?cmd=_flow&SESSION=GcaIJnWoI3tVK2nHpfJdarhHaIJpo3gaG4YwzM99e_nd4ctszf9l4i8WqeG&dispatch=5885d80a13c0db1f8e263663d3faee8d61ec37c409b56235bed2ddf64505aee9
... or on site :
http://mapwrapper.clanteam.com
Thank you!

Release notes:
-	added dispose function to dispose the map
-	callout positioning css-driven functionality
-	callout fix on pan
-	pushpin dimension handling changed
-	fix route for explorer
-	pushpin offset and pushpin text offset

*/
(function($){$.mapWrapper={conf:{pushpinSelector:".pushpin",doNotHideProcessedPushpins:false,routeAlertSelector:".routeAlert",mapLoadAlertSelector:".mapLoadAlert",routeOriginPushpinSelector:".routeOriginPushpin",routeDestinationPushpinSelector:".routeDestinationPushpin",imgPushpinHoverSelector:".hover",pushpinCalloutSelector:".pushpinCallout",pushpinCalloutLeftSideClassName:"leftSideCallout",routeSelector:".route",pushpinCalloutBeakSelector:".boxCallout-beak"}};function Bing7_MapWrapper(mapRootJQO,settings){this.mapRootJQO=mapRootJQO;this.settings=settings;this.defaultLayer=new Microsoft.Maps.EntityCollection();this.routeLayer=new Microsoft.Maps.EntityCollection();this.divMapClassId=this.mapRootJQO.attr("id");this.routeServiceCulture=this.mapRootJQO.find(this.settings.routeSelector).attr("culture");this.routeColor=this.mapRootJQO.find(this.settings.routeSelector).attr("color");this.routeOpacity=this.mapRootJQO.find(this.settings.routeSelector).attr("opacity");this.routeThickness=this.mapRootJQO.find(this.settings.routeSelector).attr("thickness");var routeAlertJQO=this.mapRootJQO.find(this.settings.routeAlertSelector);routeAlertJQO.css({"visibility":"hidden","position":"absolute","z-index":2000,"top":this.mapRootJQO.height()/2-routeAlertJQO.height()/2+"px","left":this.mapRootJQO.width()/2-routeAlertJQO.width()/2+"px"});this.mapOptions={credentials:this.mapRootJQO.attr("credentials"),center:new Microsoft.Maps.Location(this.mapRootJQO.attr("latitude"),this.mapRootJQO.attr("longitude")),zoom:parseInt(this.mapRootJQO.attr("zoomLevel")),showDashboard:!this.mapRootJQO.hasClass("hideDashboard"),showLogo:this.mapRootJQO.hasClass("showLogo"),disableMouseInput:this.mapRootJQO.hasClass("disableInput"),disableKeyboardInput:this.mapRootJQO.hasClass("disableInput"),disableTouchInput:this.mapRootJQO.hasClass("disableInput"),disableUserInput:this.mapRootJQO.hasClass("disableInput"),showCopyright:this.mapRootJQO.hasClass("showCopyright"),showScalebar:this.mapRootJQO.hasClass("showScalebar"),mapTypeId:eval("Microsoft.Maps.MapTypeId."+(typeof (this.mapRootJQO.attr("mapType"))=="undefined"?"road":this.mapRootJQO.attr("mapType"))),width:function(mapWrapper){var width=mapWrapper.mapRootJQO.attr("width");if(typeof (width)=="undefined"){width=mapWrapper.mapRootJQO.width();}return parseInt(width);}(this),height:function(mapWrapper){var height=mapWrapper.mapRootJQO.attr("height");if(typeof (height)=="undefined"){height=mapWrapper.mapRootJQO.height();}return parseInt(height);}(this)};this.centerAndZoom=function(latitude,longitude,zoom){var position=new Microsoft.Maps.Location(latitude,longitude);this.map.setView({center:position,zoom:zoom});};this.centerAndZoom=function(){this.map.setView({center:this.mapOptions.center,zoom:this.mapOptions.zoom});};this.setMapType=function(mapType){this.map.setView({mapTypeId:eval("Microsoft.Maps.MapTypeId."+mapType)});};this.modifyMapOptions=function(opts){$.extend(this.mapOptions,opts);this.init();};this.addPushpin=function(pushpinJQO,hideProcessedPushpins){var parHideProcessedPushpins=hideProcessedPushpins;if(typeof (parHideProcessedPushpins)=="undefined"){parHideProcessedPushpins=true;}this.addPushpinToLayer(pushpinJQO,this.defaultLayer,parHideProcessedPushpins);};this.addPushpinToRoute=function(pushpinJQO,hideProcessedPushpins){var parHideProcessedPushpins=hideProcessedPushpins;if(typeof (parHideProcessedPushpins)=="undefined"){parHideProcessedPushpins=true;}this.addPushpinToLayer(pushpinJQO,this.routeLayer,parHideProcessedPushpins);};this.addPushpinToLayer=function(pushpinJQO,layer,hideProcessedPushpins){var latitude=pushpinJQO.attr("latitude");var longitude=pushpinJQO.attr("longitude");var width=pushpinJQO.attr("width");var height=pushpinJQO.attr("height");var pushpinStructure={width:width,height:height};var imgPushpinJQO=pushpinJQO.find("img").not(this.settings.imgPushpinHoverSelector);if(imgPushpinJQO.length){$.extend(pushpinStructure,{icon:imgPushpinJQO.attr("src")});}var anchorX=pushpinJQO.attr("anchorX");var anchorY=pushpinJQO.attr("anchorY");if(typeof (anchorX)!="undefined"&&typeof (anchorY)!="undefined"){$.extend(pushpinStructure,{anchor:new Microsoft.Maps.Point(parseInt(anchorX),parseInt(anchorY))});}var textPushpinJQO=pushpinJQO.find("span");if(textPushpinJQO.length){var offsetX=parseInt(textPushpinJQO.attr("offsetX")||"0");var offsetY=parseInt(textPushpinJQO.attr("offsetY")||"0");$.extend(pushpinStructure,{text:textPushpinJQO.text(),textOffset:new Microsoft.Maps.Point(offsetX,offsetY)});}var position=new Microsoft.Maps.Location(latitude,longitude);var pushpin=new Microsoft.Maps.Pushpin(position,pushpinStructure);var mapWrapper=this;var disableMouseOver=this.mapRootJQO.attr("disableMouseOver");if(disableMouseOver!="true"){disableMouseOver="false";}if(disableMouseOver=="false"){Microsoft.Maps.Events.addHandler(pushpin,"mouseover",function(e){Bing7_MapWrapper.pushpinMouseoverHandler(e,mapWrapper,pushpinJQO);});Microsoft.Maps.Events.addHandler(pushpin,"mouseout",function(e){Bing7_MapWrapper.pushpinMouseoutHandler(e,mapWrapper,pushpinJQO);});}layer.push(pushpin);pushpinJQO.find(this.settings.pushpinCalloutSelector).css("display","none");if(hideProcessedPushpins){pushpinJQO.css("display","none");}};this.init=function(){this.map=new Microsoft.Maps.Map(document.getElementById(this.divMapClassId),this.mapOptions);this.map.entities.push(this.defaultLayer);this.map.entities.push(this.routeLayer);this.mapRootJQO.find(this.settings.mapLoadAlertSelector).css("display","none");Bing7_MapWrapper.acquireMapPushpins(this);this.mapRootJQO.find(this.settings.routeOriginPushpinSelector).css("display","none");this.mapRootJQO.find(this.settings.routeDestinationPushpinSelector).css("display","none");this.map.tryLocationToPixel(this.mapOptions.center,Microsoft.Maps.PixelReference.control);};this.dispose=function(){this.routeLayer.clear();this.defaultLayer.clear();this.switchToPushpin();this.map.dispose();};this.route=function(lat1,lon1,lat2,lon2,optimize,callback){var routeServiceCulture=this.routeServiceCulture;if(typeof (routeServiceCulture)=="undefined"){routeServiceCulture="en-GB";}this.mapRootJQO.find(this.settings.routeAlertSelector).css("visibility","");this.calcolaPercorsoCallback=callback;var waypoint1=lat1+","+lon1;var waypoint2=lat2+","+lon2;var routeCallbackName="MapWrapperRouteCallback_"+this.mapRootJQO.attr("id");var routeCallbackScript=document.createElement("script");routeCallbackScript.type="text/javascript";routeCallbackScript.text="function "+routeCallbackName+"(result){"+"	$('#"+this.mapRootJQO.attr("id")+"').data('mapWrapper').routeCallback(result);"+"}";routeCallbackScript.id="MapWrapperRouteCallbackScriptId_"+this.mapRootJQO.attr("id");document.getElementsByTagName("head")[0].appendChild(routeCallbackScript);var routeRequest="http://dev.virtualearth.net/REST/v1/Routes?wayPoint.1="+waypoint1+"&waypoint.2="+waypoint2+"&routePathOutput=Points&output=json&jsonp="+routeCallbackName+"&optimize="+optimize+"&culture="+routeServiceCulture+"&key="+this.mapOptions.credentials;var restCallScript=document.createElement("script");restCallScript.type="text/javascript";restCallScript.src=routeRequest;restCallScript.id="MapWrapperRestScriptId_"+this.mapRootJQO.attr("id");document.getElementsByTagName("head")[0].appendChild(restCallScript);};this.routeCallback=function(result){this.routeLayer.clear();if(result&&result.resourceSets&&result.resourceSets.length>0&&result.resourceSets[0].resources&&result.resourceSets[0].resources.length>0){this.switchToCalcolaPercorso();var bbox=result.resourceSets[0].resources[0].bbox;var viewBoundaries=Microsoft.Maps.LocationRect.fromLocations(new Microsoft.Maps.Location(bbox[0],bbox[1]),new Microsoft.Maps.Location(bbox[2],bbox[3]));this.map.setView({bounds:viewBoundaries});var routeline=result.resourceSets[0].resources[0].routePath.line;var routepoints=new Array();for(var i=0;i<routeline.coordinates.length;i++){routepoints.push(new Microsoft.Maps.Location(routeline.coordinates[i][0],routeline.coordinates[i][1]));}if(typeof (this.calcolaPercorsoCallback)=="function"){this.calcolaPercorsoCallback(result.resourceSets[0].resources[0].routeLegs[0]);
}var color=this.routeColor.split(",");var opacity=this.routeOpacity;var thickness=this.routeThickness;var routeshape=new Microsoft.Maps.Polyline(routepoints,{strokeColor:new Microsoft.Maps.Color(parseInt(opacity),parseInt(color[0]),parseInt(color[1]),parseInt(color[2])),strokeThickness:parseInt(thickness)});this.routeLayer.push(routeshape);this.addPushpinToRoute(this.mapRootJQO.find(this.settings.routeOriginPushpinSelector).attr("latitude",routepoints[0].latitude).attr("longitude",routepoints[0].longitude));this.addPushpinToRoute(this.mapRootJQO.find(this.settings.routeDestinationPushpinSelector).attr("latitude",routepoints[routepoints.length-1].latitude).attr("longitude",routepoints[routepoints.length-1].longitude));this.mapRootJQO.find(this.settings.routeAlertSelector).css("visibility","hidden");}$("#MapWrapperRouteCallbackScriptId_"+this.mapRootJQO.attr("id")).remove();$("#MapWrapperRestScriptId_"+this.mapRootJQO.attr("id")).remove();};this.switchToCalcolaPercorso=function(){this.defaultLayer.setOptions({visible:false});this.routeLayer.setOptions({visible:true});};this.switchToPushpin=function(){this.defaultLayer.setOptions({visible:true});this.routeLayer.setOptions({visible:false});};this.init();}Bing7_MapWrapper.acquireMapPushpins=function(mapWrapper){Bing7_MapWrapper.acquirePushpins((typeof (mapWrapper.settings.pushpinRootSelector)=="undefined"?"#"+mapWrapper.mapRootJQO.attr("id"):mapWrapper.settings.pushpinRootSelector),mapWrapper.settings.pushpinSelector,mapWrapper);};Bing7_MapWrapper.acquirePushpins=function(pushpinRootSelector,pushpinSelector,mapWrapper){$(pushpinRootSelector).find(pushpinSelector).each(function(){mapWrapper.addPushpin($(this),!mapWrapper.settings.doNotHideProcessedPushpins);});};Bing7_MapWrapper.pushpinMouseoverHandler=function(e,mapWrapper,pushpinJQO){if(e.targetType=="pushpin"){var pushpinCalloutClonedJQO=pushpinJQO.find(mapWrapper.settings.pushpinCalloutSelector).css("display","none").clone().appendTo("#"+mapWrapper.mapRootJQO.attr("id")+" .MicrosoftMap").mouseout(function(){Bing7_MapWrapper.removeCallout(mapWrapper);});var pix=mapWrapper.map.tryLocationToPixel(e.target.getLocation(),Microsoft.Maps.PixelReference.control);var pixX=pix.x+e.target.getWidth()/2;var pixY=pix.y-e.target.getHeight()/2-pushpinCalloutClonedJQO.height()/2;if(pushpinCalloutClonedJQO.is(".cssPosDriven")){pixX+=Bing7_MapWrapper.csspixToNumber(pushpinCalloutClonedJQO.css("left"));pixY+=Bing7_MapWrapper.csspixToNumber(pushpinCalloutClonedJQO.css("top"));if(pixX+pushpinCalloutClonedJQO.width()>mapWrapper.mapOptions.width){pushpinCalloutClonedJQO.addClass(mapWrapper.settings.pushpinCalloutLeftSideClassName);pixX+=Bing7_MapWrapper.csspixToNumber(pushpinCalloutClonedJQO.css("left"));pixY+=Bing7_MapWrapper.csspixToNumber(pushpinCalloutClonedJQO.css("top"));}pushpinCalloutClonedJQO.find(mapWrapper.settings.pushpinCalloutBeakSelector).css("top",(pushpinCalloutClonedJQO.height()/2-Bing7_MapWrapper.csspixToNumber(pushpinCalloutClonedJQO.find(mapWrapper.settings.pushpinCalloutBeakSelector).css("height"))/2)+"px");}else{if(pixX+pushpinCalloutClonedJQO.width()>mapWrapper.mapOptions.width){pixX-=(pushpinCalloutClonedJQO.width()+e.target.getWidth());if(pixX<0){pixX+=(pushpinCalloutClonedJQO.width()+e.target.getWidth())/2;pixY-=(pushpinCalloutClonedJQO.height()/2+e.target.getHeight());if(pixY<0){pixY+=(pushpinCalloutClonedJQO.height()+e.target.getHeight());}}}}pushpinCalloutClonedJQO.css({"cssText":"display:block!important","position":"absolute","top":pixY+"px","left":pixX+"px","z-index":"1000"});e.target.setOptions({icon:pushpinJQO.find(mapWrapper.settings.imgPushpinHoverSelector).attr("src")});}};Bing7_MapWrapper.pushpinMouseoutHandler=function(e,mapWrapper,pushpinJQO){if(e.targetType=="pushpin"){Bing7_MapWrapper.removeCallout(mapWrapper);e.target.setOptions({icon:pushpinJQO.find("img").not(mapWrapper.settings.imgPushpinHoverSelector).attr("src")});}};Bing7_MapWrapper.csspixToNumber=function(csspix){if(typeof (csspix)=="undefined"||csspix=="auto"){return 0;}var ret=0;try{ret=parseInt(csspix.replace("px",""));}catch(e){}return ret;};Bing7_MapWrapper.removeCallout=function(mapWrapper){$("#"+mapWrapper.divMapClassId+" .MicrosoftMap "+mapWrapper.settings.pushpinCalloutSelector).remove();};$.fn.mapWrapper=function(conf){var api=this.data("mapWrapper");if(api){return api;}conf=$.extend({},$.mapWrapper.conf,conf);this.each(function(){if($(this).attr("id")==""){$(this).attr("id","MapWrapperId_"+Math.floor(Math.random()*1000000));}if($(this).hasClass("bing7")){api=new Bing7_MapWrapper($(this),conf);}else{api=new Bing7_MapWrapper($(this),conf);}$(this).data("mapWrapper",api);});return conf.api?api:this;};})(jQuery);
