$(document).ready(function() {
	//$('p.unavailable').show();
	
	// javascript is required on TS side for online orders, so we only enable starting the order process if javascript is enabled on our side
	$('.day-content p:not(.unavailable)').each(function() {
		$(this).wrapInner('<a href="' + $(this).attr("id") + '"/>');
	});

	// the user click on a table cell with class .calendar-day that has a link
	$("td.calendar-day:has(a)").click(
		function(event) { 
			// if the click wasn't on a link itself, help out the user a bit...
			if (event.target.nodeName.toLowerCase() != 'a') {

				// track this in Google Analytics
				pageTracker._trackEvent('Calendar', 'Click on empty space');
				
				// if the click was on the day-number div...
				if ($(event.target).attr("class") == 'day-number') {
					// check for one link like this...
					if ($(event.target).parent().find('a').length == 1)
						window.location.href = $(event.target).parent().find('a').first().attr("href");
					else
						sendToDaySuccess(event);
				}
				// otherwise check like this...
				else if ($(event.target).find('a').length == 1)
					window.location.href = $(event.target).find('a').first().attr("href");
					
				// otherwise, send them to daySuccess
				else
					sendToDaySuccess(event);
			}
		}
	);
	
	function sendToDaySuccess(event) {
		// get year and month from url; if they don't exist, default to current year and month
		var urlparts = window.location.pathname.split('/');
		if (urlparts[urlparts.length - 1] == 'calendar') {
			var d = new Date();
			var year = d.getFullYear();
			var month = d.getMonth() + 1;
			if (month < 10)
				month = "0" + month;
		}
		else {
			var month = urlparts[urlparts.length - 1];
			var year = urlparts[urlparts.length - 2];
		}
		
		// get day from class day-number and pad with zero if necessary
		var day = $(event.currentTarget).find(".day-number").first().html();
		while (day.toString().length < 2)
			day = "0" + day.toString();
					
		window.location.href = window.location.pathname.replace(/calendar.*/, "calendar/"+year+"/"+month+"/"+day);
	}
	
	// hide descriptions
	$(".discount_details").hide();
	
	// create click handler to show descriptions
	$(".discount a").click(function() {
		if ($(".discount a").html() == 'Show Details')
			$(".discount a").html('Hide Details');
		else
			$(".discount a").html('Show Details');
		$(".discount_details").toggle();

		return false;
	});
	
});

