var firstTime = true;
var urlDate = getUrlDate();

$(function(){
	var sessionOn = false;

	
	$("#datepicker").datepicker({
		beforeShowDay: beforeShowDay,
		onChangeMonthYear:onChangeMonthYear,
		onSelect: onSelect
	});

	if(urlDate){
		var month = urlDate.month;
		var year = urlDate.year;
		var day = urlDate.day;
	}else{
	var currentDate = $("#datepicker").datepicker('getDate');
	var month = currentDate.getMonth()+1;
	var year = currentDate.getFullYear();
	var day = currentDate.getDate();
	
	var date = new Date();
	
	DWRHelper.getLastMonthEvents(
	{
		callback:function( value ) 
		{
			if( value != null ) {
				sessionOn = true;
			    month = value[0];
				day = value[1];
				year = value[2];
				
				
				date.setYear( year );
				date.setMonth( month-1);
				date.setDate( day );
			
				$("#datepicker").datepicker( 'setDate' , date );
			}
		},
		async:false
	});
	}
	
	DWRHelper.getMonthsEvents( month, year, true, 
	{
		callback:function( value )
		{
			dwr.util.setEscapeHtml(false); 
			dwr.util.setValue( document.getElementById('eventContainer'), value );

			if( !sessionOn ) {
				var id = getCurrentDateId(currentDate);
				
				$.each( $(".eventDay") , function() 
				{
					if( getOnlyDay( $(this).attr("id") ) >= getOnlyDay( id ) ) {
						$(this).show();
					}
					else
						$(this).hide();
				});
			}
		},
		async:false
	});
	
	if(urlDate){
		onSelect(month+"/"+day+"/"+year);
	}
	$(".detailEvent").livequery('click', function()
	{
		var splitedEventDate = $(this).parent().parent( $(".eventDay") ).attr("id").split("/");
		DWRHelper.setLastMonthEvents( splitedEventDate[0], splitedEventDate[1], splitedEventDate[2] );
	});
	
	if(urlDate){
		date = new Date(month+"/"+day+"/"+year);
	}
	$("#datepicker").datepicker( 'setDate' , date );
	firstTime = false;
	$("#datepicker .future a").attr('title','Future Event');
	$("#datepicker .past a").attr('title','Past Event');
	
	$.each( $(".ui-datepicker-calendar a") , function() 
		{
		if($(this).hasClass("ui-state-highlight"))
			$(this).removeClass("ui-state-default");
		});
	
	
});


function getOnlyDay(date)
{
	return date.substring( date.indexOf("/") +1 , date.lastIndexOf("/") );
}

function getCurrentDateId(date)
	{
		if( date == null )
		   date = new Date();
		var day = ( date.getDate() < 10 ? "0" : "" ) + date.getDate();
		var month = ( date.getMonth() < 10 ? "0" : "" ) + (date.getMonth()+1);
		return "" + month + "/" + day + "/" + date.getFullYear() + ""; 
	}

function onSelect( dateText )
	{
		var choosedEventDate = false;
		
		$.each( $(".eventDay") , function() 
		{
			if( $(this).attr("id") == dateText )
				choosedEventDate = true;
		});
		
		if( !choosedEventDate )
			return false;

		$.each( $(".eventDay") , function() 
		{
			if( $(this).attr("id") == dateText )
				$(this).show();
			else
				$(this).hide();
		});

		var date = $('#datepicker').datepicker('getDate');
		DWRHelper.setLastMonthEvents( date.getMonth()+1, date.getDate(), date.getFullYear() );
	}

function onChangeMonthYear(year, month)
	{
		if(!firstTime){
			DWRHelper.getMonthsEvents( month, year, true, 
			{
				callback:function( value )
				{
					dwr.util.setEscapeHtml(false); 
					dwr.util.setValue( document.getElementById('eventContainer'), value ); 
			    },
				async:false
			});
		}
	}

function beforeShowDay(date)
	{
		var css = "";
		
		var currentShowDate = new Date();
		currentShowDate.setFullYear(date.getFullYear(), date.getMonth(), date.getDate() );
		
		var currentDate = new Date();
		var eventDate = new Date();

		$.each( $(".eventDay") , function() 
		{
			var splitedEventDate = $(this).attr("id").split("/");
			eventDate.setFullYear( /*year*/ splitedEventDate[2], /*month*/ splitedEventDate[0]-1, /*day*/ splitedEventDate[1] );
			
			if(  currentShowDate.toString() == eventDate.toString() )
			{	
				 if( currentShowDate < currentDate ){
					css = "past";
				}
				else if( currentShowDate > currentDate ){
					css = "future";
				}
				else
					css = "currentEvent"
			}
			
		});
		
		return [true,css]; 
	}		 

function getUrlDate()
	{
		var a = window.location.toString().match(/Date\/\d\d\/\d\d\/\d\d\d\d/);
		if(a){
			a = a[0].substr(5,10);
			var b = a.split("/");
			var urlDate = {
				month : b[0],
				day : b[1],
				year : b[2]
			};
		};
		return urlDate;
	};

