/* JS Date tag
		by Eric @ 5.May.2008
-----------------------------------
Sample Tag:
	<span id="time">
	    <js:Date format="GMT:jS M Y H:i \G\MT" 
	        updateInterval="500" updateId="time" />
	</span>
-----------------------------------
required
	format : attribute
		read PHP date() function.
		*** This implementation without timezone functions.
*/

var JsDate = {
	
	currentDate : new Date(),
	
	_initialize : function()
	{
		var tags = $$('js:Date');
		
		for(var i=0; i<tags.length; i++)
		{
			if( !tags[i].getAttribute('format') ) continue;
			
			this.currentDate = new Date();
			
			with( tags[i] )
			{
				var formatString = getAttribute('format');
				
				if(/^GMT:/.test(formatString))
					with( this.currentDate )
					{
						var offset = getTimezoneOffset();
						
						setHours( getHours() + (offset / 60) );
						setMinutes( getMinutes() + (offset % 60) );
						
						formatString = formatString.substr(4);
					}
				
				var updateInterval = getAttribute('updateInterval');
				
				if(!isNaN(updateInterval))
				{
				    var intString = 'document.getElementById("'+getAttribute('updateId')+'").innerHTML = ' + 
				        'JsDate.Format("'+formatString.replace(/\\/g, '\\\\')+'");';
				    
				    setInterval(intString, parseInt(updateInterval));
				}
				
				tags[i].parentNode.replaceChild( Text(this.Format( formatString )), tags[i] );
			}
		}
	},
	
	Format : function( formatString )
	{
	    this.currentDate = new Date();
	    
		var result = '';
		
		for(var i=0; i<formatString.length; i++)
			result += ( formatString.charAt(i) == '\\'? 
				formatString.charAt(++i) : this.mapChr( formatString.charAt(i) ) );
		
		return result;
	},
	
	mapChr : function( chr )
	{
		var d = this.currentDate;
		
		var weekNames = [
			'Monday', 
			'Tuesday', 
			'Wednesday', 
			'Thursday', 
			'Friday', 
			'Saturday', 
			'Sunday'
		];
		
		var monthNames = [
			'January', 
			'February', 
			'March', 
			'April', 
			'May', 
			'June', 
			'July', 
			'August', 
			'September', 
			'October', 
			'November', 
			'December'
		];
		
		with(this)
		switch( chr )
		{
			case 'd': return padZero(mapChr('j'));
			case 'D': return mapChr('l').substr(0,3);
			case 'j': return d.getDate();
			case 'l': return weekNames[mapChr('w')];
			case 'N': return mapChr('j') || 7;
			case 'S': return (mapChr('j') < 4? ['st','nd','rd'][mapChr('j') - 1] : 'th');
			case 'w': return d.getDay();
			case 'z':
				for(var z=0, m=0; m<d.getMonth(); m++)
					z += ( mapChr('t') - 0 );
				return z;
			// case 'W':
			case 'F': return monthNames[d.getMonth()];
			case 'm': return padZero(mapChr('n'));
			case 'M': return mapChr('F').substr(0,3);
			case 'n': return d.getMonth() + 1;
			case 't': var t = mapChr('n'); return (t==2? 28 + mapChr('L') : 30 + ((t + (t > 7)) % 2));
			case 'L': return !!(y%3200 && !(y%400) || (!(y%4) && y%100));
			// case 'o':
			case 'Y': return d.getFullYear();
			case 'y': return String(mapChr('Y')).substr(2);
			case 'a': return mapChr('G') < 12? 'am' : 'pm';
			case 'A': return mapChr('a').toUpperCase();
			// case 'B':
			case 'g': return mapChr('G') % 12;
			case 'G': return d.getHours();
			case 'h': return padZero(mapChr('g'));
			case 'H': return padZero(mapChr('G'));
			case 'i': return padZero(d.getMinutes());
			case 's': return padZero(d.getSeconds());
			case 'u': return d.getMilliseconds();
			// case 'e':
			// case 'I':
			case 'O': case 'P': var o = d.getTimezoneOffset(); return (o / 60) + (chr=='P'?':':'') + (o % 60);
			case 'Z': return d.getTimezoneOffset() * 60 + ( mapChr('s') - 0 );
			// case 'c':
			// case 'r':
			case 'U': return d.getTime() / 1000;
			default:  return chr;
		}
	},
	
	padZero : function( val ) { if( parseInt( val ) < 10 ) val = '0' + val; return val; }
};

System.initialize( JsDate );
