








/*
eraseCookie('bookings');
*/




var bookings = new Array();
var pid = 


$(document).ready(function() {

	calendar_setup();

});


/*
* call all the functions to setup the calendar and add the lsiteners etc.
*/
function calendar_setup() {

	/* check for and process cookies */
	cookie_jar();

	/* load the onclick listeners to the back and forward links */
	click_listeners();
	
	/* add watchers to the cells */
	cell_listeners();
	
	
	
	
}

function ajax_setup() {

	/* load the cookie data from the persistant form field */
	//ajax_jar();
	
	/* check for and process cookies */
	cookie_jar();
	
	/* load the onclick listeners to teh back and forward links */
	click_listeners();
	
	/* add watchers to the cells */
	cell_listeners();
	
}

/*
* manage the Ajax request
*/

function load_page(year, month, p, f) {
		$.ajax({
		   type: "GET",
		   url: "/inc/pages/calendar.php",
		   data: "year=" + year + "&month=" + month + "&p=" + p + "&f=" + f + "",
		   success: function(html){
			 $("#calendar-holder").html(html);
			 ajax_setup();
		   }
		   
		   
	
		 });
	}
	
	
/*
* Add event listeners to the back and forward links
* To do an Ajax request
*/

function click_listeners() {
	$("#calendar-holder .calendar th a").click(function(){
		var h = $(this).attr("href");
		var t = h.split('/');
		var c = t.length;
		/*
		* year
		* month
		* page id
		* page file name
		*/
		load_page(t[c-2], t[c-1], t[1], t[2]);
		
		pid = t[1]; // set the value of the global variable
		
		return false;
	
	});
}

/*
* Check if the cookie exists - if so load it all up.
*/	
function cookie_jar(){
	var cookie = readCookie('bookings');
	if(cookie != null) {
	
		unserialize(cookie);
		
		set_selected();
		
		update_booking_text();
	}
}

function ajax_jar(){
	var cookie = $('#persist-bookings').val();
	if(cookie != '') {
	
		unserialize(cookie);
		
		set_selected();
		
		update_booking_text();
	}
}



/*
* Add a click event handler to cells where the class != 'cell-booked'
* i.e. this date has not been booked yet
*/

function cell_listeners() {

	$("#calendar-holder .calendar td[class]").click(function(){
		
		if($(this).hasClass('cell-booked')) {
		
			// do nothing
			
		} else if($(this).hasClass('cell-weekend'))  {
		
			// do nothing
		
		} else {
		

				
				var start_date = $(this).parent().children('td:first').attr('title');
				var end_date = $(this).parent().children('td:last').attr('title');
				
				if($(this).parent().hasClass("selected")) {
					
					$(this).parent().removeClass("selected");
					
					
				
						for(var i in bookings) {
						

							
							if(bookings[i]['start_date'] == start_date) {
								/*
								* remove this from the array
								*/
								
								bookings.splice(i, 1);
								

							}
						}
					
				} else {
						
					$(this).parent().addClass("selected");
					/* check if this value is already in the array */
					
					var flag = 0;
					
					for(var i in bookings) {
					

						
						if(bookings[i]['start_date'] == start_date) {
							flag = 1;
							
						}
					
					}
					
					if(flag == 0) {
					
						var c = bookings.length;
						bookings[c] = new Array();
						bookings[c]['start_date'] = start_date;
						bookings[c]['end_date'] = end_date;
					}
			
				}
				
				
		
		}
		
		
		var s = '';
		
		if(bookings) {
			if(bookings.length != 0) {
				s = serialize(bookings);		
				createCookie('bookings', s, 1);
				$('#persist-bookings').val(s);
				$('#test2').val(s);
			} else {
				eraseCookie('bookings');
				$('#persist-bookings').val(s);
				$('#test2').val(s);
			
			}
		} else {
			eraseCookie('bookings');
			$('#persist-bookings').val(s);
			$('#test2').val(s);
		}
		
		update_booking_text();
		
			
	});
}

/*
*	update the text that displays the booking text on the page
*/
function update_booking_text(){

	var str = '';
	
		

		for(var i in bookings) {
		
			
			str = str + '<p>From: ' + bookings[i]['start_date'] + '<br />To: ' + bookings[i]['end_date'] + '</p><input type="hidden" name="booking-date[]["start"]" value="' + bookings[i]['start_date'] + '" />';
		
		}
		
		if(str != '' || (str == '' && pid == 12)) {
			str = '<div><h2>Selected Dates</h2><form id="calendar-booking-form" method="post" action="/booking">' + str;
			
			
			str = str + '<input type="submit" name="book-now" id="book-now" value="' + book_button + '" />';
			str = str + '</form>';
			str = str + '</div>';
		} 
	
	
	$('.booking-info').html(str);

}

function set_selected(){

	for(var i in bookings) {
			
			
			$('#calendar-holder tr').each(function() {
			
				if($(this).children('td:first').attr('title') == bookings[i]['start_date']){
				
					$(this).addClass("selected");
				
				}
			
			});
			
			
	
	}

}

function isArray(obj) {
	return obj.constructor == Array;
}









// cookie functions http://www.quirksmode.org/js/cookies.html
 
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

	
function serialize(data) {

	str = '';

	for(var i in data) {
	
		if(str != '') {
			str = str + '|';
		}
		str = str + data[i]['start_date'] + ':' + data[i]['end_date'];
	
	}
	
	return str;

}

function unserialize(data) {


	var temp = explodeArray(data, '|');
	var b = new Array();
	
	for(var i in temp) {
	
		var t2 = explodeArray(temp[i], ':');
		b[i] = new Array();
		b[i]['start_date'] = t2[0];
		b[i]['end_date'] = t2[1];
	
	}
	bookings = null;
	bookings = b;


}

function explodeArray(item,delimiter)
{
	tempArray=new Array(1);
	var Count=0;
	var tempString=new String(item);
	while (tempString.indexOf(delimiter)>0)
		{
			tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
			tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
			Count=Count+1
		}
	tempArray[Count]=tempString;
	return tempArray;
}



