/**************************************************
 *	calendar.js
 *
 *	Calendar Functionality, using JQuery 1.2.6
 *
 *	$("#calTs").val() = Current calendar timestamp
 *	$("#currM").val() = current month on calenar
 *	$("#currY").val() = current Year for calendar
 */
$(document).ready(function()
{	
	init();				
	//no need for submit if we have js
	$('#filterSubmit').remove();
	/***************************************************
	 *	Drop box automatic load
	 */
	$('#clusterDropdown').change(function()
	{
		//$('#filterForm').submit();
		updateCalendar( $(this).val(), $("#currM").html(), $("#currY").html() );
		
	});//change
	
	/*****************************************************
	 *	Show loading message on AJAX start event
	 */
	$('#eventCol').ajaxStart(function()
	{
		$(this).html('<div id="loadingBox"></div>');
	});
	/**
	 *	re-initialise after all ajax ended, 
	 *	need to re-add expand buttons and tooltips after dom update
	 */
	$('#eventCol').ajaxStop(function()
	{
		init();
	});
	
});// Dom ready /////////////////////

/**
 *	Update the calendar with ajax call
 */
function updateCalendar( cluster, month, year )
{
	var changeDate = '';
	if( month != '' && year != '' )
	{
		changeDate = '&m='+month+'&y='+year;
	}
	/**
	 *	Update Calendar
	 */
	$.ajax(
	{
		type: "GET",
		url: "http://www.sunderlandssp.co.uk/events/calendar",
		data: "cal=1&cluster="+cluster+changeDate,
		success: function(msg)
		{
			$('.cal-div').html(msg);
		}
	});
	/**
	 *	Update events column
	 */
	$.ajax(
	{
		type: "GET",
		url: "http://www.sunderlandssp.co.uk/events/calendar",
		data: "events=1&cluster="+cluster+changeDate,
		success: function(msg)
		{
			$('#eventCol').html(msg);
		}
	});
}

/**
 *	show/hide events info
 */
function init()
{
	$(".eventInnerBox").append('<a class="expandLink" href="">Expand</a>');			
	/**
	 *	Show/hide hidden details for events
	 */
	$('.eventInnerBox a').click(function()
	{
		$(this).parent().children("div:last").toggle("slow");
		if( $(this).html() == 'Expand' )
		{
			$(this).html('Minimize');
		}
		else $(this).html('Expand');		
		return false;// don't follow link
	});
	/**
	 *	Tooltip bubble on calendar
	 */
	$('.calendar tr td').hover(
	function() // Hover in /////////////////
	{
		if ( $.browser.msie )
		{
			$(".tooltip", this).show();
		}
		else
		{
			$(".tooltip", this).fadeIn("fast");
		}
	}, 
	function() // Hover out ////////////////
	{
		if ( $.browser.msie )
		{
			$(".tooltip", this).hide();
		}
		else
		{
			$(".tooltip", this).fadeOut("fast");
		}
	}
   	);
	/**
	 *	Handle prev/next month click on calendar
	 */
	$('.calMonthNav').click(function()
	{
		/**
		 *	Updated month and year are in the links on the cal,
		 *	use these to load the new calendar
		 *
		 */
		 var $url = $(this).attr("href");
		 var $m = getVariable( $url, "m" );
		 var $y = getVariable( $url, "y" );
		 var $cluster = $('#clusterDropdown').val();
		 updateCalendar( $cluster, $m, $y );
		 return false;
	});
}
/**
 *	Get a parameter from a GET link string
 *	eg. www.example.com?param=6 (window.location.href)
 */
function getVariable( href , param )
{
	href = unescape(href);// &amp; to &
	if( href.indexOf("?") == -1 ) return false; // no getstring
	href = href.split("?");
	var str = href[1]; // only after the ?
	var vars = str.split("&");
	for (var i=0; i<vars.length; i++) 
	{
		var pair = vars[i].split("=");
    	if (pair[0] == param) 
		{
      		return pair[1];
    	}
  	} 
}

/**
 *	Get the get string from an url
 */
function getGetString( href )
{
	href = href.split("?");
	var str = href[1]; // only after the ?
	return str;
}
