function reset() {   	       

	whatsOnDestroyCookie('eventCategory');
	whatsOnDestroyCookie('dateObject');
	whatsOnDestroyCookie('easyDateObject');
	whatsOnDestroyCookie('monthOffset');
	
	$('select#category').val('');

	$('div#calendarHeading').html('Loading calendar...');
	$.ajax({
	    url: encodeURI(httpHost+'/app/whatson/calendarView.php'),
	    type: 'GET',
	    dataType: 'html',
	    timeout: 10000,
	    error: function(){
			$('div#monthlyView').html('Problem rendering calendar view, please try again later');
	    },
	    success: function(html) {				
    
				$('div#monthlyView').html(html);
			        
	    }
	    
	});	
	
	loadThisWeek();
		
} 

function whatsOnSetCookie(cookieName, cookieValue, life, path, domain, isSecure) {
	if (!cookieName)
		return false;
	document.cookie = escape(cookieName) + '=' + escape(cookieValue) + 
						(life ? ';expires=' + (new Date((new Date())).getTime() + (1000*life)).toGMTString() : '') +
						(path ? ';path=' + path : '') + (domain ? ';domain=' + domain : '') + 
						(isSecure ? ';secure' : '');
	if (life < 0) {
		if (typeof(whatsOnGetCookie(cookieName)) == 'string')
			return false
		return true
	}
	
	if (typeof(whatsOnGetCookie(cookieName)) == 'string' )
		return true;
	return false;
}

function whatsOnGetCookie(cookieName) {
	var cookieJar = document.cookie.split('; ');
	for (var x=0; x<cookieJar.length; x++) {
		var thisCookie = cookieJar[x].split('=');
		if (thisCookie[0] == escape(cookieName))
			return thisCookie[1] ? unescape(thisCookie[1]) : '';
	}
	return null;
}

function whatsOnDestroyCookie(name) {
	var expiryDate = new Date();
	expiryDate.setTime(expiryDate.getTime() - 1);
	document.cookie = name += "=; expires=" + expiryDate.toGMTString();
}

$(document).ready(function() {  		
		reset();		
		//rotating images
		$('#rotatingImages').innerfade({ 
			speed: 'slow', 
			timeout: 4000, 
			type: 'sequence',
			containerheight: '180px' 
		}); 
	}
);

function concatObject(obj) {
  str='';
  for(prop in obj)
  {
    str+='['+prop+'=>'+obj[prop]+"]\n";
  }
  return(str);
}

function setCategory() {
	
	var category = $('select#category').val();
	var categoryInCookie = whatsOnGetCookie('eventCategory');
	var viewType = whatsOnGetCookie('viewType');
	
	if (category != categoryInCookie) {
	
		whatsOnSetCookie('eventCategory', category);
		
		var categoryInCookie = whatsOnGetCookie('eventCategory');
		var monthOffsetInCookie = whatsOnGetCookie('monthOffset');
		
		if (!monthOffsetInCookie)
			monthOffsetInCookie = 0;
	
		$('div#calendarHeading').html('Loading calendar...');
		$.ajax({
		    url: encodeURI(httpHost+'/app/whatson/calendarView.php?cat='+categoryInCookie+'&offset='+monthOffsetInCookie),
		    type: 'GET',
		    dataType: 'html',
		    timeout: 10000,
		    error: function(){
				$('div#monthlyView').html('Problem rendering calendar view, please try again later');
		    },
		    success: function(html) {				
	    
					$('div#monthlyView').html(html);
				        
		    }
		    
		});
		
		var viewType = whatsOnGetCookie('viewType');	
		if (viewType == 'monthly') {			
			var dateObject = whatsOnGetCookie('dateObject');			
			montlyViewShow(dateObject);			
		}	
		if (viewType == 'daily') {	
			var date = 	whatsOnGetCookie('dateObject');
			var easyDate = whatsOnGetCookie('easyDateObject');				
			dayView(date, easyDate)		
		}						
		if (viewType == 'thisWeek') {					
			loadThisWeek();			
		}										
		
	}
	
}

function montlyViewShow(dateObject) {

	whatsOnSetCookie('viewType', 'monthly');
	whatsOnSetCookie('dateObject', dateObject);
	
	var dateObjectText = dateObject.split(" ");
	var month = dateObjectText[0];
	var yyyy = dateObjectText[1];	
	
	var categoryInCookie = whatsOnGetCookie('eventCategory');
	
	$('div#viewType').empty();
	$('div#viewType').html('<h2><span style="font-size:60%; color:#666;">Showing all events for</span><br />'+month+' '+yyyy+' | <span><a href="javascript: loadThisWeek();" style="color:#ccc;">this week</a></span></h2>');		        	

	$('div#eventListing').empty();
	$('div#eventListing').html('<img src="images/loading.gif"></img> Loading events...');	
	
	$.ajax({
	    url: encodeURI(httpHost+'/app/whatson/monthView.php?month='+month+'&yyyy='+yyyy+'&cat='+categoryInCookie),
	    type: 'GET',
	    dataType: 'html',
	    timeout: 10000,
	    error: function(){
			$('div#eventListing').html('Problem rendering monthly view, please try again later');
	    },
	    success: function(html) {
    
				$('div#eventListing').html(html);
			        
	    }
	    
	});	

}

function previousMonth() {
	$('div#calendarHeading').html('Loading calendar...');
	var offset = $('input#monthOffset').val();
	var category = $('select#category').val();
	offset--;	
	whatsOnSetCookie('monthOffset', offset);
	$.ajax({
	    url: encodeURI(httpHost+'/app/whatson/calendarView.php?offset='+offset+'&cat='+category),
	    type: 'GET',
	    dataType: 'html',
	    timeout: 10000,
	    error: function(){
			$('div#monthlyView').html('Problem rendering calendar view, please try again later');
	    },
	    success: function(html) {				
    
				$('div#monthlyView').html(html);
			        
	    }
	    
	});
}

function nextMonth() {
	$('div#calendarHeading').html('Loading calendar...');
	var offset = $('input#monthOffset').val();
	var category = $('select#category').val();
	offset++;
	whatsOnSetCookie('monthOffset', offset);
	$.ajax({
	    url: encodeURI(httpHost+'/app/whatson/calendarView.php?offset='+offset+'&cat='+category),
	    type: 'GET',
	    dataType: 'html',
	    timeout: 10000,
	    error: function(){
			$('div#monthlyView').html('Problem rendering calendar view, please try again later');
	    },
	    success: function(html) {				
    
				$('div#monthlyView').html(html);
			        
	    }
	    
	});
}

// draw view of events on a given day
function dayView(date, easyDate) {

	whatsOnSetCookie('viewType', 'daily');
	whatsOnSetCookie('dateObject', date);
	whatsOnSetCookie('easyDateObject', easyDate);
	
	var categoryInCookie = whatsOnGetCookie('eventCategory');	

	$('div#viewType').empty();
	$('div#viewType').html('<h2><span style="font-size:60%; color:#666;">Showing all events for</span><br />'+easyDate+' | <span><a href="javascript: loadThisWeek();" style="color:#ccc;">this week</a></span></h2>');		        	

	$('div#eventListing').empty();
	$('div#eventListing').html('<img src="images/loading.gif"></img> Loading events...');
	
	$.ajax({
	    url: encodeURI(httpHost+'/app/whatson/dayView.php?q='+date+'&cat='+categoryInCookie),
	    type: 'GET',
	    dataType: 'html',
	    timeout: 10000,
	    error: function(){
			$('div#eventListing').html('Problem rendering calendar view, please try again later');
	    },
	    success: function(html) { 
				$('div#eventListing').html(html);
			        
			    $('td.selection').removeClass("selection");
				$('td#'+date).addClass("selection");			        
	    }
	    
	});
	
		
}

function loadThisWeek() {
	
	whatsOnSetCookie('viewType', 'thisWeek');
	var categoryInCookie = whatsOnGetCookie('eventCategory');
	
	$('div#eventListing').empty();
	$('div#eventListing').html('<img src="images/loading.gif"></img> Loading events...');
	
	$.ajax({
	    url: encodeURI(httpHost+'/app/whatson/weekView.php?cat='+categoryInCookie),
	    type: 'GET',
	    dataType: 'html',
	    timeout: 10000,
	    error: function(){
			$('div#eventListing').html('Problem rendering calendar view, please try again later');
	    },
	    success: function(html) {
    
				$('div#eventListing').html(html);
				$('div#viewType').html('<div id="viewType"><h2><span style="font-size:60%; color:#666;">Showing all events for</span><br />this week</h2></div>');
				
				//refreshing calendar to current month
				$('div#calendarHeading').html('Loading calendar...');
				$.ajax({
				    url: encodeURI(httpHost+'/app/whatson/calendarView.php?cat='+categoryInCookie),
				    type: 'GET',
				    dataType: 'html',
				    timeout: 10000,
				    error: function(){
						$('div#monthlyView').html('Problem rendering calendar view, please try again later');
				    },
				    success: function(html) {				
			    
							$('div#monthlyView').html(html);
						        
				    }
				    
				});	
				
				whatsOnDestroyCookie('dateObject');
				whatsOnDestroyCookie('easyDateObject');
				whatsOnDestroyCookie('calElement');
				whatsOnDestroyCookie('monthOffset');
			        
	    }
	    
	});

}

function toggleAllThisWeek() {
	
    if ($("input#toggleHidden").is(":checked")) {
       	$("#eventListing").find("div.past").each(function() {
			$(this).show("fast");
		});
       }
       else {     
           $("#eventListing").find("div.past").each(function() {
			$(this).hide("fast");
		});
       }

}


