2 lines
6.2 KiB
JavaScript
2 lines
6.2 KiB
JavaScript
(function($){var methods={};var opts={};var selectedTimzone=null;var imgElement=null;var mapElement=null;var $pin=null;var changeHandler=null;methods.init=function(initOpts){var $origCall=this;opts=$.extend({},$.fn.timezonePicker.defaults,initOpts);selectedTimzone=opts.timezone;changeHandler=opts.changeHandler;return $origCall.each(function(index,item){imgElement=item;mapElement=document.getElementsByName(imgElement.useMap.replace(/^#/,""))[0];$(imgElement).wrap('<div class="timezone-picker"></div>').parent().css({position:"relative",width:$(imgElement).width()+"px"});if(opts.pinUrl){$pin=$('<img src="'+opts.pinUrl+'" />').appendTo(imgElement.parentNode).css("display","none")}else if(opts.pin){$pin=$(imgElement).parent().parent().find(opts.pin).appendTo(imgElement.parentNode).css("display","none")}$(mapElement).find("area").click(function(){var areaElement=this;if($pin){$pin.css("display","block");var pinCoords=$(areaElement).attr("data-pin").split(",");var pinWidth=parseInt($pin.width()/2);var pinHeight=$pin.height();$pin.css({position:"absolute",left:pinCoords[0]-pinWidth+"px",top:pinCoords[1]-pinHeight+"px"})}var timezoneName=$(areaElement).attr("data-timezone");var countryName=$(areaElement).attr("data-country");var offset=$(areaElement).attr("data-offset");if(typeof changeHandler==="function"){changeHandler(timezoneName,countryName,offset)}if(opts.target){if(timezoneName)$(opts.target).val(timezoneName)}if(opts.countryTarget){if(countryName)$(opts.countryTarget).val(countryName)}return false});if(opts.target){$(opts.target).bind("change",function(){$origCall.timezonePicker("updateTimezone",$(this).val())})}if(opts.countryTarget&&opts.countryGuess){$(opts.countryTarget).bind("change",function(){var countryCode=$(this).val();if(opts.countryGuesses[countryCode]){$(mapElement).find('area[data-timezone="'+opts.countryGuesses[countryCode]+'"]').click()}else{$(mapElement).find("area[data-country="+countryCode+"]:first").click()}})}if(opts.responsive){var resizeTimeout=null;$(window).resize(function(){if(resizeTimeout){clearTimeout(resizeTimeout)}resizeTimeout=setTimeout(function(){$origCall.timezonePicker("resize")},200)})}setTimeout(function(){if(opts.responsive&&parseInt(imgElement.width)!==parseInt(imgElement.getAttribute("width"))){$origCall.timezonePicker("resize")}else if(opts.maphilight&&$.fn.maphilight){$(imgElement).maphilight(opts)}if(opts.target){$(opts.target).triggerHandler("change")}},500)})};methods.updateTimezone=function(newTimezone){selectedTimzone=newTimezone;$pin.css("display","none");$(mapElement).find("area").each(function(m,areaElement){if(areaElement.getAttribute("data-timezone")===selectedTimzone){$(areaElement).triggerHandler("click");return false}});return this};methods.detectLocation=function(detectOpts){var detectDefaults={success:undefined,error:undefined,complete:undefined};detectOpts=$.extend(detectDefaults,detectOpts);if(navigator.geolocation){navigator.geolocation.getCurrentPosition(showPosition,handleErrors)}function showPosition(position){var $imgElement=$(imgElement);var imageXY=convertXY(position.coords.latitude,position.coords.longitude,$imgElement.width(),$imgElement.height());$(mapElement).find("area").each(function(m,areaElement){var coords=areaElement.getAttribute("coords").split(",");var shape=areaElement.getAttribute("shape");var poly=[];for(var n=0;n<coords.length/2;n++){poly[n]=[coords[n*2],coords[n*2+1]]}if(shape==="poly"&&isPointInPoly(poly,imageXY[0],imageXY[1])||shape==="rect"&&isPointInRect(coords,imageXY[0],imageXY[1])){$(areaElement).triggerHandler("click",detectOpts["success"]);return false}});if(detectOpts["complete"]){detectOpts["complete"](position)}}function handleErrors(error){if(detectOpts["error"]){detectOpts["error"](error)}if(detectOpts["complete"]){detectOpts["complete"](error)}}function convertXY(latitude,longitude,map_width,map_height){var x=Math.round((longitude+180)*(map_width/360));var y=Math.round((latitude*-1+90)*(map_height/180));return[x,y]}function isPointInPoly(poly,x,y){var inside=false;var bbox=[1e6,1e6,-1e6,-1e6];for(var i=0,j=poly.length-1;i<poly.length;j=i++){var xi=poly[i][0],yi=poly[i][1];var xj=poly[j][0],yj=poly[j][1];bbox[0]=Math.min(bbox[0],xi);bbox[1]=Math.min(bbox[1],yi);bbox[2]=Math.max(bbox[2],xi);bbox[3]=Math.max(bbox[3],yi);var intersect=yi>y!=yj>y&&x<(xj-xi)*(y-yi)/(yj-yi)+xi;if(intersect)inside=!inside}return inside&&isPointInRect(bbox,x,y)}function isPointInRect(rect,x,y){rect=[Math.min(rect[0],rect[2]),Math.min(rect[1],rect[3]),Math.max(rect[0],rect[2]),Math.max(rect[1],rect[3])];return x>=rect[0]&&x<=rect[2]&&y>=rect[1]&&y<=rect[2]}return this};methods.resize=function(){$(mapElement).find("area").each(function(m,areaElement){if(!areaElement.originalCoords){areaElement.originalCoords={timezone:areaElement.getAttribute("data-timezone"),country:areaElement.getAttribute("data-country"),coords:areaElement.getAttribute("coords"),pin:areaElement.getAttribute("data-pin")}}var rescale=imgElement.width/imgElement.getAttribute("width");$(imgElement).parent().css({width:$(imgElement).width()+"px"});var originalCoords=areaElement.originalCoords.coords.split(",");var newCoords=new Array;for(var j=0;j<originalCoords.length;j++){newCoords[j]=Math.round(parseInt(originalCoords[j])*rescale)}areaElement.setAttribute("coords",newCoords.join(","));var pinCoords=areaElement.originalCoords.pin.split(",");pinCoords[0]=Math.round(parseInt(pinCoords[0])*rescale);pinCoords[1]=Math.round(parseInt(pinCoords[1])*rescale);areaElement.setAttribute("data-pin",pinCoords.join(","));if(opts.target){$(opts.target).triggerHandler("change")}});return this};$.fn.timezonePicker=function(method){if(methods[method]){return methods[method].apply(this,Array.prototype.slice.call(arguments,1))}else if(typeof method==="object"||!method){return methods.init.apply(this,arguments)}else{$.error("Method "+method+" does not exist on jQuery.timezonePicker")}};$.fn.timezonePicker.defaults={pin:".timezone-pin",pinUrl:null,timezone:null,maphilight:true,target:null,countryTarget:null,countryGuess:true,countryGuesses:{AU:"Australia/Sydney",BR:"America/Sao_Paulo",CA:"America/Toronto",CN:"Asia/Shanghai",ES:"Europe/Madrid",MX:"America/Mexico_City",RU:"Europe/Moscow",US:"America/New_York"},responsive:false,fade:false,stroke:true,strokeColor:"FFFFFF",strokeOpacity:.4,fillColor:"FFFFFF",fillOpacity:.4,groupBy:"data-offset"}})(jQuery);
|