/*
	Adds click handlers to the tabs to make sure they stay active upon clicking the months for the event calendar.
	We will first reset all backgroud positioning and then set the active one thats clicked afterwards.  Makes for
	ease of handling and prevents anything from going wrong if we just reset the active button each time
*/

$(document).ready(function() {
	//define a default tab here to be displayed on page load.
	var default_active_tab = gup("month");
		if (default_active_tab == "") {
			default_active_tab = getMonth();
		}
	//loop through all of our tabs
	$('.calendarNav a').each(function() {
		//set the default tab to active
		if($('span', this).html() == default_active_tab) {
			$(this).css({'background-position' : '0% -26px'});
			$('span', this).css({'background-position' : '100% -26px'});
		}
		//lets add our onclick handlers for each tab
		$(this).click(function() {
			//lets reset our tabs to default
			tab_reset();
			//now lets make our tab that was clicked active
			$(this).css({'background-position' : '0% -26px'});
			$('span', this).css({'background-position' : '100% -26px'});
		});
	});
	
	function tab_reset() {
		$('.calendarNav a').each(function() {
			$(this).css({'background-position' : 'left top'});
			$('span', this).css({'background-position' : 'right top'});
		});
	}
	
	var loc = window.location.pathname;
	if (loc.match(/news.jsp$/) || loc.match(/events.jsp$/)){
		readURLString(loc);
	}	
	
	function readURLString (loc)	{
		var searchString = document.location.search;
		var month = getMonth();
		if (searchString == '' && loc.match(/news.jsp$/)) {
			loadintoIframe('/DiscoverCenter/community/news/newsMonth.jsp?month=' + month);
		} else if (searchString == '' && loc.match(/events.jsp$/)){
			loadintoIframe('/DiscoverCenter/community/events/eventsMonth.jsp?month=' + month);
		}
	}	
	
	function loadintoIframe(url){
	  	$("#targetFrame").attr({src : url});
	}	
	
	function gup( name ) {
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
		return "";
	  else
		return results[1];
	}	
	
	
	function getMonth(){
		var d=new Date();
		var month=new Array(12);
		month[0]="Jan";
		month[1]="Feb";
		month[2]="Mar";
		month[3]="Apr";
		month[4]="May";
		month[5]="Jun";
		month[6]="Jul";
		month[7]="Aug";
		month[8]="Sep";
		month[9]="Oct";
		month[10]="Nov";
		month[11]="Dec";
		return month[d.getMonth()];
	}	
	


	

	
});