var dates = [];

function regroupDates () {
		
		$('#fullSideCalSet').find('.calItem').each(function (i) {
			var t = $(this);
			var date = t.find('.calDate').css('display','none').html();
			
			//console.log(date);
			
			date = date.replace(/[^a-z0-9]/gi, '');

			// t.addClass(date);
						
			if(dates[date] == null) {
				dates[date] = [];
				$('.sidebarCalendar').append('<ul class="dayList '+date+'"></ul>');
			}
			
			dates[date].push(this);
			t.remove().appendTo($('.'+date));
		});
	};
	
	
function addEnhancedControls () {
		var d = new Date();
		
		var months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];
		
		for (var i=0; i<7; i++) {
			
			var month = d.getMonth();
			var date = d.getDate();
			var year = d.getFullYear();
						
			$('#sidecalControls').append('<div class="sideNavControlItem" id="'+months[month]+date+year+'"><a href="#sidecal"><span class="m">'+months[month].substr(0,3)+'</span><br /><span class="d">'+date+'</span></a></div>');
			
			d.setTime(d.getTime()+1000*60*60*24);
			
		}
		
		$('.sideNavControlItem').hover(function () { $(this).addClass('sideNavOver'); }, function () { $(this).removeClass('sideNavOver'); });
		$('.sideNavControlItem').click (function () {
			var t = $(this);
			t.siblings().removeClass('sideNavActive');
			t.addClass('sideNavActive');
			
			var curClass = t.attr('id');
			
			if (!$('ul.'+curClass).length) {
				$('.sidebarCalendar').append('<ul class="dayList '+curClass+'"><li><strong>There are no events listed for this date.</strong></li></ul>');
				
			}
			$('.dayList').each(function (i) {
				
				if ($(this).hasClass(curClass)) {
					$(this).css('display', 'block');
				} else {
					$(this).css('display', 'none');
				}
				
			});
			t.find('a').blur();
			return false;
		});
	};
	
function enhance () {
		
		regroupDates();
		
		addEnhancedControls();
		
		$('.sideNavControlItem:first').click();
		
	}
	

//  pass the url  and  optionally a width, height, and window name
function spawnWindow(url,name,w,h){
	if(w == undefined) w = 320;
	if(h == undefined) h = 240;
	if(name == undefined) name = "popWindow";
	var newWindow = window.open(url,name,"toolbar=0,scrollbars=1,resizable=1,width="+w+",height="+h+"\"");		
 }
 function spawnJukebox(){
	var newWindow = window.open("content/jukebox.php",'jb',"toolbar=0,scrollbars=1,resizable=1,width=300,height=250");		
 }
 function ifExists(val, htmlOut, other){
	if (typeof other=='undefined') other = "";
	
	if (val.length && val != "false" && val !="/echotools/images/spacer.gif"){
	document.write (htmlOut);
	}else{
		if(other.length)
		{
			document.write (other);
		}
	}
}


$(document).ready(enhance);