// Original implimentation by Why The Lucky Stiff
// <http://whytheluckystiff.net/>, described at:
// http://redhanded.hobix.com/inspect/showingPerfectTime.html
// 
// Modified to fit in a single, unobtrusive javascript
// class by Mike West <http://mikewest.org/>
function handleEvent(obj,event,func){try{obj.addEventListener(event,func,false);}catch(e){if(typeof eval("obj.on"+event)=="function"){var existing=obj['on'+event];obj['on'+event]=function(){existing();func();}}else{obj['on'+event]=func;}}};function PerfectTime(){var self=this;self.format='%m/%d/%Y %g:%i:%s %a';self.mytz=(arguments[0])?arguments[0]:0;self.lang=(arguments[1])?arguments[1]:null;self.strftime_funks={Days:['Domingo','Lunes','Martes','Miercoles','Jueves','Viernes','Sabado'],DaysShort:['Dom','Lun','Mar','Mie','Jue','Vie','Sab'],Months:['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],MonthsShort:['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'],zeropad:function(n){return n>9?n:'0'+n;},d:function(t){return this.zeropad(t.getDate())},D:function(t){return this.DaysShort[t.getDay()]},j:function(t){return t.getDate();},l:function(t){return this.Days[t.getDay()]},w:function(t){return t.getDay()},F:function(t){return this.Months[t.getMonth()]},m:function(t){return this.zeropad(t.getMonth()+1)},M:function(t){return this.MonthsShort[t.getMonth()]},n:function(t){return t.getMonth()+1;},Y:function(t){return t.getFullYear()},y:function(t){return this.zeropad(this.Y(t)%100);},a:function(t){return this.H(t)<12?'am':'pm';},g:function(t){return(t.getHours()%12)?t.getHours()%12:12;},G:function(t){return t.getHours();},h:function(t){return this.zeropad((t.getHours()%12)?t.getHours()%12:12)},H:function(t){return this.zeropad(t.getHours())},i:function(t){return this.zeropad(t.getMinutes())},s:function(t){return this.zeropad(t.getSeconds())},c:function(t){return t.toString()},O:function(t){if(self.mytz>0){return "-"+this.zeropad(self.mytz)+"00";}else if(self.mytz<0){return "+"+this.zeropad(Math.abs(self.mytz))+"00";}else if(t.getTimezoneOffset()>0){return "-"+this.zeropad(t.getTimezoneOffset()/60)+"00";}else{return "+"+this.zeropad(Math.abs(t.getTimezoneOffset())/60)+"00";}},'%':function(t){return '%'}};self.strftime=function(theDate,theFormat){var fmt=(theFormat)?theFormat:self.format;fmt=fmt.replace('%f',self.format);for(var s in self.strftime_funks){if(s.length==1){fmt=fmt.replace('%'+s,self.strftime_funks[s](theDate));}};return fmt;};self.instantiate=function(){self.processLang();var spans=document.getElementsByTagName('span');for(i=0,numSpans=spans.length;i<numSpans;i++){if(spans[i].className.match(/PerfectTime/)){self.processSpan(spans[i]);}}};self.processSpan=function(theSpan){var GMT=parseInt(theSpan.getAttribute('gmt_time'))*1000;var newfmt=theSpan.getAttribute('fmt');var newDate=new Date(GMT);if(self.mytz!=0){newDate.setDate(newDate.getUTCDate());var newhr=newDate.getUTCHours()+self.mytz;newDate.setHours(newhr);};theSpan.innerHTML=self.strftime(newDate,newfmt);};self.processLang=function(){if(!self.lang)return;if(self.lang['Days'])self.strftime_funks['Days']=self.lang['Days'];if(self.lang['DaysShort'])self.strftime_funks['DaysShort']=self.lang['DaysShort'];if(self.lang['Months'])self.strftime_funks['Months']=self.lang['Months'];if(self.lang['MonthsShort'])self.strftime_funks['MonthsShort']=self.lang['MonthsShort'];};handleEvent(window,'load',self.instantiate);};
