-
Notifications
You must be signed in to change notification settings - Fork 10
/
jquery.timezone-picker.min.js
1 lines (1 loc) · 4.22 KB
/
jquery.timezone-picker.min.js
1
(function(e){var t={},n={},r=null,i=null,s=null,o=null;t.init=function(t){var u=this;return n=e.extend({},e.fn.timezonePicker.defaults,t),r=n.timezone,u.each(function(t,r){i=r,s=document.getElementsByName(i.useMap.replace(/^#/,""))[0],e(i).wrap('<div class="timezone-picker"></div>').parent().css({position:"relative",width:e(i).width()+"px"}),n.pinUrl?o=e('<img src="'+n.pinUrl+'" />').appendTo(i.parentNode).css("display","none"):n.pin&&(o=e(i).parent().parent().find(n.pin).appendTo(i.parentNode).css("display","none")),e(s).find("area").click(function(){var t=this;if(o){o.css("display","block");var r=e(t).attr("data-pin").split(","),i=parseInt(o.width()/2),s=o.height();o.css({position:"absolute",left:r[0]-i+"px",top:r[1]-s+"px"})}if(n.target){var u=e(t).attr("data-timezone");u&&e(n.target).val(u)}if(n.countryTarget){var a=e(t).attr("data-country");a&&e(n.countryTarget).val(a)}return!1}),n.target&&e(n.target).bind("change",function(){u.timezonePicker("updateTimezone",e(this).val())}),n.countryTarget&&n.countryGuess&&e(n.countryTarget).bind("change",function(){var t=e(this).val();n.countryGuesses[t]?e(s).find('area[data-timezone="'+n.countryGuesses[t]+'"]').click():e(s).find("area[data-country="+t+"]:first").click()});if(n.responsive){var a=null;e(window).resize(function(){a&&clearTimeout(a),a=setTimeout(function(){u.timezonePicker("resize")},200)})}setTimeout(function(){n.responsive&&parseInt(i.width)!==parseInt(i.getAttribute("width"))?u.timezonePicker("resize"):n.maphilight&&e.fn.maphilight&&e(i).maphilight(n),n.target&&e(n.target).triggerHandler("change")},500)})},t.updateTimezone=function(t){return r=t,o.css("display","none"),e(s).find("area").each(function(t,n){if(n.getAttribute("data-timezone")===r)return e(n).triggerHandler("click"),!1}),this},t.detectLocation=function(t){function r(n){var r=e(i),o=u(n.coords.latitude,n.coords.longitude,r.width(),r.height());e(s).find("area").each(function(n,r){var i=r.getAttribute("coords").split(","),s=r.getAttribute("shape"),u=[];for(var l=0;l<i.length/2;l++)u[l]=[i[l*2],i[l*2+1]];if(s==="poly"&&a(u,o[0],o[1])||s==="rect"&&f(i,o[0],o[1]))return e(r).triggerHandler("click",t.success),!1}),t.complete&&t.complete(n)}function o(e){t.error&&t.error(e),t.complete&&t.complete(e)}function u(e,t,n,r){var i=Math.round((t+180)*(n/360)),s=Math.round((e*-1+90)*(r/180));return[i,s]}function a(e,t,n){var r=!1,i=[1e6,1e6,-1e6,-1e6];for(var s=0,o=e.length-1;s<e.length;o=s++){var u=e[s][0],a=e[s][1],l=e[o][0],c=e[o][1];i[0]=Math.min(i[0],u),i[1]=Math.min(i[1],a),i[2]=Math.max(i[2],u),i[3]=Math.max(i[3],a);var h=a>n!=c>n&&t<(l-u)*(n-a)/(c-a)+u;h&&(r=!r)}return r&&f(i,t,n)}function f(e,t,n){return e=[Math.min(e[0],e[2]),Math.min(e[1],e[3]),Math.max(e[0],e[2]),Math.max(e[1],e[3])],t>=e[0]&&t<=e[2]&&n>=e[1]&&n<=e[2]}var n={success:undefined,error:undefined,complete:undefined};return t=e.extend(n,t),navigator.geolocation&&navigator.geolocation.getCurrentPosition(r,o),this},t.resize=function(){return e(s).find("area").each(function(t,r){r.originalCoords||(r.originalCoords={timezone:r.getAttribute("data-timezone"),country:r.getAttribute("data-country"),coords:r.getAttribute("coords"),pin:r.getAttribute("data-pin")});var s=i.width/i.getAttribute("width");e(i).parent().css({width:e(i).width()+"px"});var o=r.originalCoords.coords.split(","),u=new Array;for(var a=0;a<o.length;a++)u[a]=Math.round(parseInt(o[a])*s);r.setAttribute("coords",u.join(","));var f=r.originalCoords.pin.split(",");f[0]=Math.round(parseInt(f[0])*s),f[1]=Math.round(parseInt(f[1])*s),r.setAttribute("data-pin",f.join(",")),n.target&&e(n.target).triggerHandler("change")}),this},e.fn.timezonePicker=function(n){if(t[n])return t[n].apply(this,Array.prototype.slice.call(arguments,1));if(typeof n=="object"||!n)return t.init.apply(this,arguments);e.error("Method "+n+" does not exist on jQuery.timezonePicker")},e.fn.timezonePicker.defaults={pin:".timezone-pin",pinUrl:null,timezone:null,maphilight:!0,target:null,countryTarget:null,countryGuess:!0,countryGuesses:{AU:"Australia/Sydney",BR:"America/Sao_Paulo",CA:"America/Toronto",CN:"China/Shanghai",ES:"Europe/Madrid",MX:"America/Mexico_City",RU:"Russia/Moscow",US:"America/New_York"},responsive:!1,fade:!1,stroke:!0,strokeColor:"FFFFFF",strokeOpacity:.4,fillColor:"FFFFFF",fillOpacity:.4,groupBy:"data-offset"}})(jQuery);