/*
 * epiClock 2.1 - Create Epic Clocks Easily
 *
 * Copyright (c) 2008 Eric Garside (http://eric.garside.name)
 * Dual licensed under:
 * 	MIT: http://www.opensource.org/licenses/mit-license.php
 *	GPLv3: http://www.opensource.org/licenses/gpl-3.0.html
 */
var EC_HALT="disable",EC_RUN="enable",EC_KILL="destroy",EC_CLOCK=0,EC_COUNTDOWN=1,EC_COUNTUP=2,EC_ROLLOVER=3,EC_EXPIRE=4,EC_LOOP=5,EC_STOPWATCH=6,EC_HOLDUP=7;(function(C){C.epiclocks={};var F={epiClock:{offset:{hours:0,minutes:0,seconds:0,days:0,years:0},arbitrary:{days:0,years:0},gmt:false,target:null,onTimer:null,onKill:null,onRender:function(H,I){H.html(I)},format:null,frame:{},dead:false,displace:0,modifier:0,variance:0,daysadded:0,paused:0,tolerance:0,selfLoc:-1,mode:EC_CLOCK,onSetup:null,stylesheet:null,containerClass:null,tpl:"<span></span>"},formats:["F j, Y, g:i:s a","V{d} x{h} i{m} s{s}","Q{y} K{d} x{h} i{m} s{s}","V{d} x{h} i{m} s{s}","x{h} i{m} s{s}","i{m} s{s}","x{h} i{m} s{s}","Q{y} K{d} x{h} i{m} s{s}"]},E=null,A=null,D=[];C.cssIncludes={};C.cssInclude=function(H,I){if(C.cssIncludes[H]){return false}C.cssIncludes[H]=true;I=I||"screen";C('<link type="text/css" rel="stylesheet" href="'+H+'" media="'+I+'"/>').appendTo("head")};C.epiclock=C.fn.clocks=function(J,H,I){J=J||EC_RUN;H=H||500;if(J==E){return}switch(J){case EC_KILL:C.each(D,function(){this.removeData("epiClock")});D=[];case EC_HALT:if(A){clearInterval(A);A=null}E=J;break;case EC_RUN:if(!A){B();A=setInterval(B,H)}E=J;break}return this};function B(){C.each(D,function(H){this.data("epiClock").render()})}C.fn.epiclock=function(H,I){if(typeof H=="string"&&C.epiclocks&&C.epiclocks[H]){H=C.epiclocks[H]}else{if(I&&C.epiclocks&&C.epiclocks[I]){H=C.extend(true,{},C.epiclocks[I],H)}}switch(H){case"destroy":return this.each(function(){var J=C(this);if(J.data("epiClock") instanceof G){J.data("epiClock").kill()}});case"disable":return this.each(function(){var J=C(this);if(J.data("epiClock") instanceof G){J.data("epiClock").pause()}});case"enable":return this.each(function(){var J=C(this);if(J.data("epiClock") instanceof G){J.data("epiClock").resume()}});default:H=C.extend(true,{},F.epiClock,H);break}this.each(function(){var L=C(this),O=(H.format||F.formats[H.mode]).split(""),N=false,K=H.tpl||F.tpl,J="",M=new G(H,L);L.data("epiClock",M);C.each(O,function(){x=this+"";switch(x){case" ":if(!N){C(K).addClass("epiclock epiclock-spacer").appendTo(L)}else{J+=x}break;case"{":N=true;break;case"}":N=false;C(K).addClass("epiclock").html(J).appendTo(L);J="";break;default:if(N){J+=x}else{if(Date.prototype[x]||M[x]){M.frame[x]=C(K).addClass("epiclock epiclock-digit").data("ec-encoding",x).appendTo(L)}else{C(K).addClass("epiclock epiclock-separator").html(x).appendTo(L)}}break}});M.selfLoc=D.push(L)-1;if(C.isFunction(M.onSetup)){M.onSetup.call(M,[])}if(M.stylesheet){C.cssInclude(M.stylesheet)}if(M.containerClass){L.addClass(M.containerClass)}});return this};function G(H,I){if(this instanceof G){return this.init(H,I)}else{return new G(H,I)}}G.prototype={Q:function(){return this.arbitrary.years},E:function(){return this.arbitrary.days},e:function(){return this.arbitrary.days.pad(0)},zero:new Date(0),pause:function(){if(this.dead){return}this.paused=new Date().valueOf();this.dead=true},resume:function(){if(!this.dead){return}if(this.mode==EC_STOPWATCH){this.displace+=(this.paused-new Date().valueOf())}this.paused=0;this.dead=false},kill:function(){D.splice(this.selfLoc,1);C.each(D,function(H){this.data("epiClock").selfLoc=H});if(C.isFunction(this.onKill)){this.onKill()}this.dead=true},init:function(H,J){if(H.mode<EC_CLOCK||H.mode>EC_HOLDUP){throw new Exception("Invalid Clock Mode.")}var I=this;C.each(H,function(L,K){I[L]=K});switch(this.mode){case EC_LOOP:case EC_EXPIRE:this.target=this.target||new Date();case EC_COUNTDOWN:case EC_ROLLOVER:this.modifier=-1;this.variance=1;break;case EC_STOPWATCH:this.displace+=-1*new Date().valueOf();return;case EC_HOLDUP:this.variance=-1;this.modifier=1;break;default:this.modifier=1;this.variance=0;break}if(this.gmt){this.normalize()}switch(true){case this.target instanceof Date:this.target=this.target.valueOf();break;case typeof this.target=="string":this.target=new Date(this.target).valueOf();break}this.displace+=this.modifier*this.calculateOffset()},calculateOffset:function(H){H=H||this.offset;return(H.years*31570560000+H.days*86400000+H.hours*3600000+H.minutes*60000+(this.variance+H.seconds)*1000)},normalize:function(){this.displace+=new Date().getTimezoneOffset()*60000},render:function(){if(!this.tick()){return}var H=this,I=(this.mode==EC_HOLDUP)?this.zero:this.now;C.each(this.frame,function(K,J){var L=(C.isFunction(I[K])?I[K]():H[K]())+"";if(J.data("last")!=L){H.onRender(J,L)}J.data("last",L)})},tick:function(){if(this.dead){return false}var H=new Date().valueOf()+this.displace;switch(this.mode){case EC_HOLDUP:if(this.target<H){this.mode=EC_COUNTUP}case EC_COUNTUP:H-=this.target;break;case EC_ROLLOVER:if(H>this.target){H=H-this.target}else{H=this.target-H}break;case EC_COUNTDOWN:case EC_EXPIRE:case EC_LOOP:H=this.target-H;if(H<this.tolerance){return this.timerEnd()}break}this.now=new Date(H);var I=this.now.V();if(I<=this.daysadded){return true}this.daysadded=I;this.arbitrary.days+=I;if(this.arbitrary.days<365){return true}this.arbitrary.years+=Math.floor(this.arbitrary.days/365.4%365.4);this.arbitrary.days=Math.floor(this.arbitrary.days%365.4);return true},timerEnd:function(){if(C.isFunction(this.onTimer)){this.onTimer()}switch(this.mode){case EC_COUNTDOWN:case EC_EXPIRE:this.kill();break;case EC_LOOP:this.displace+=this.modifier*this.calculateOffset();return this.render();case EC_ROLLOVER:this.mode=EC_COUNTUP;return true}this.now=new Date(0);return true}};C.extend(String.prototype,{pad:function(I,H){H=H||2;return this.length<H?new Array(1+H-this.length).join(I)+this:this},rpad:function(I,H){H=H||2;return this.length<H?this+new Array(1+H-this.length).join(I):this}});C.extend(Number.prototype,{pad:function(I,H){return(this+"").pad(I,H)},rpad:function(I,H){return(this+"").rpad(I,H)}});C.extend(Date.prototype,{modCalc:function(I,H){return(Math.floor(Math.floor(this.valueOf()/1000)/I)%H)},months:["January","February","March","April","May","June","July","August","September","October","November","December"],days:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],suffix:[null,"st","nd","rd"],V:function(){return this.modCalc(86400,100000)},v:function(){return this.V().pad(0)},K:function(){return this.V()%365},k:function(){return this.K().pad(0)},X:function(){return this.modCalc(3600,24)},x:function(){return this.X().pad(0)},d:function(){return this.getDate().pad("0")},D:function(){return this.days[this.getDay()].substring(0,3)},j:function(){return this.getDate()},l:function(){return this.days[this.getDay()]},N:function(){return this.getDay()+1},S:function(){return this.suffix[this.getDate()]||"th"},w:function(){return this.getDay()},z:function(){return Math.round((this-this.f())/86400000)},W:function(){return Math.ceil(((((this-this.f())/86400000)+this.f().w())/7))},F:function(){return this.months[this.getMonth()]},m:function(){return(this.getMonth()+1).pad(0)},M:function(){return this.months[this.getMonth()].substring(0,3)},n:function(){return this.getMonth()+1},L:function(){var H=this.Y();return H%4?false:H%100?true:H%400?false:true},f:function(){return new Date(this.getFullYear(),0,1)},Y:function(){return this.getFullYear()},y:function(){return(""+this.getFullYear()).substr(2)},a:function(){return this.getHours()<12?"am":"pm"},A:function(){return this.a().toUpperCase()},B:function(){return Math.floor((((this.getHours())*3600000)+(this.getMinutes()*60000)+(this.getSeconds()*1000))/86400).pad(0,3)},g:function(){return this.getHours()%12||12},G:function(){return this.getHours()},h:function(){return this.g().pad("0")},H:function(){return this.getHours().pad("0")},i:function(){return this.getMinutes().pad(0)},s:function(){return this.getSeconds().pad("0")},u:function(){return this.getTime()%1000},O:function(){var H=this.getTimezoneOffset()/60;return(H>=0?"+":"-")+Math.abs(H).pad(0).rpad(0,4)},P:function(){var H=this.O();return H.substr(0,3)+":"+H.substr(3)},Z:function(){return this.getTimezoneOffset()*60},c:function(){return this.Y()+"-"+this.m()+"-"+this.d()+"T"+this.H()+":"+this.i()+":"+this.s()+this.P()},r:function(){return this.toString()},U:function(){return this.getTime()/1000}})})(jQuery);
