/*
	
	-------------------------------------------------
	Calendar List Date Formatter v0.1 
	-------------------------------------------------
	
	
	Before
	-------------------------------------------------
	
		<div class="eventDate">Tue, February 5, 2008</div>
	
	
	After
	-------------------------------------------------
	
		<div class="eventDateFormatted">
			<span class="dayofweek">Tue</span>
			<span class="month">February</span>
			<span class="day">5</span>
			<span class="year">2008</span>
		</div>
		
	
	Tokens Reference
	-------------------------------------------------
	
	{dayofweek} = Day of Week (full)
	{dayofweek_abbr} = Day of Week (abbreviation)
	{month} = Month (full)
	{month_abbr} = Month (abbreviation)
	{year} = Year (XXXX format)


	To Do
	-------------------------------------------------
	
	* Convert date markup into timestamp for full date formatting. ( eg. YYYY-MM-DD-HH-MM-SS )
		Inspired by the Svend Tofte Date Formatter - the php date() clone 
		link: http://www.svendtofte.com/code/date_format/
	
	* Fast Initialization with "DOM Loaded" (instead of page load)
	
	* Convert CalendarList object to Prototype.js Extended Class (Protype <1.6) to allow multiple instances of formatting
	
	* Remove Protype.js dependency completely? (eh, 122kb removed would be nice)
	
	* Add conversion methods for WSL's Blog Element ".postDate" classes
	
*/


var CalendarList = {
	
	newDateFormat: '<span class="dayofweek">{dayofweek}</span> <span class="month">{month_abbr}</span> <span class="day">{day}</span> <span class="year">{year}</span>',
	
	daysOfWeek: {  // abbr : full
		Mon: "Monday", 
		Tue: "Tuesday", 
		Wed: "Wednesday", 
		Thu: "Thursday", 
		Fri: "Friday", 
		Sat: "Saturday", 
		Sun: "Sunday" 
	},
	
	months: { // full : abbr
		Janurary: "Jan",
		February: "Feb",
		March: "Mar",
		April: "Apr",
		May: "May",
		June: "Jun",
		July: "Jul",
		August: "Aug",
		September: "Sep",
		October: "Oct",
		November: "Nov",
		December: "Dec"
	},
	
	tokenMap: [
		{ "type": "dayofweek", "value": 1 },
		{ "type": "dayofweek_abbr", "value": 1 },
		{ "type": "month", "value": 3 },
		{ "type": "month_abbr", "value": 3 },
		{ "type": "day", "value": 5 },
		{ "type": "year", "value": 7 }
	],
						 
						 
	
	initialize: function()
	{
		this.formatString();
		this.getDates();
	},
	
	formatString: function()
	{
		
		this.tokenMap.each( function(t){ 
			
			var replace_exp = '/{' + t.type + '}/';
			
			CalendarList.newDateFormat = CalendarList.newDateFormat.replace(eval(replace_exp), '$' + t.value );
		
		});

	},
	
	getDates: function()
	{
		
		var dates = $$('.eventsList li .eventDate');
		
		if( dates.length > 0 )
		{
					
			dates.each( function(d,index){
				
				d.className = 'eventDateFormatted';
				
				var new_format = CalendarList.formatDate(d.innerHTML);
				
				d.innerHTML = new_format;
				
			});
			
		}
		
	},
	
	formatDate: function(string)
	{
		
		var reg = new RegExp(/(.*)(, )(.*)( )(.*)(, )(.*)/);
		return string.replace(reg, CalendarList.newDateFormat );
		
	}
	
}






Event.observe(window, 'load', function() { CalendarList.initialize(); });