$(document).ready(function(){
	setupCalendar();
	setupHistory();
	setupPackages();
	setupSubmitButton();
	
	$("#form-signup").validate();
});

function setupSubmitButton()
{
	$('.button-submit').hover(function() {
		$(this).css('background-position','0 -40px');
	},
	function() {
		$(this).css('background-position','0 0');
	});
}

function setupCalendar()
{
	//$("#calendar table tr td[background] a.image").css("opacity", "0");
	
	$("#calendar table tr td").each(function(index){
		var i = 50-index;
//		$(this).css("z-index",i).find("span.number").attr("id",index).text(index);
		$(this).css("z-index",i).find("span.number").attr("id",index);
	});
/*if (jQuery.browser.msie) {
	$("#calendar table tr td div").closest("td").each(function(index) {
		$(this).hover( function (){
//			$(this).closest("tr").nextAll("tr").find("td div").hide();
			$(this).closest("tr").nextAll("tr").find("td div").css("visibility","hidden");
//			$(this).closest("tr").nextAll("tr").find("td div").stop().animate({opacity: 0}, 500);
		},
		function (){
//			$(this).closest("tr").nextAll("tr").find("td div").show();
			$(this).closest("tr").nextAll("tr").find("td div").css("visibility","visible");
//			$(this).closest("tr").nextAll("tr").find("td div").stop().animate({opacity: 1}, 500);
		});
	});
}*/

	$('#calendar table tr td').mouseenter(function() {
		$('#tooltip').css('display', 'none');
	});

	$("#calendar table tr td.multiple").mouseenter(function() {
		var x, y = 0;
		var tooltipContent = $(this).find('ol').html();
		
		x = $(this).offset().left;
		y = $(this).offset().top + 84;
		
		x += 'px';
		y += 'px';
		
		$('#tooltip ol').remove();
		$('#tooltip').append('<ol>' + tooltipContent + '</ol>');
		
		$('#tooltip').css({
			left: x,
			top: y
		});
		$('#blank').css('display', 'block');
		// alert($('#tooltip').text());
		$('#tooltip, #tooltip ol').fadeIn();
		
		$("#tooltip a").click( function (e){
			e.preventDefault();
			var updatedCopy = $(this).next('.copy').html();
			replaceText.html(updatedCopy);
			
			$("a.return").click( function (){
				replaceText.html(originalCopy);
			});			
			
		});
	});
	
	$('#tooltip').mouseleave(function() {
		$('#tooltip').css('display', 'none');												 
	});
	
	$("#calendar table tr td.multiple").mouseenter(function() {
	});
	
	$("#calendar table tr td[background] a.image").hover( function (){
		$(this).stop().animate({opacity: 0}, 500);
	}, 
	function (){
		$(this).stop().animate({opacity: 1}, 500);
	});
	
	$("#calendar .copy").hide();
	var replaceText = $(".sidebar-description .replace-text");
	var originalCopy = replaceText.html();
		
	$("#calendar a:not(.arrow, .button)").click( function (){
		var updatedCopy = $(this).next('.copy').html();
		replaceText.html(updatedCopy);
		
		$("a.return").click( function (){
			replaceText.html(originalCopy);
		});
	});
	
}

function setupHistory()
{
	$("#history-visuals").scrollable({size: 1, loop:true});
}

function setupPackages()
{        
	var currentPrimary = 0;
	var totalPrimary = $("#slideshow-nav .top-level").size();
	
	var secondary = $("#slideshow-nav li .sub-nav").tabs(".sub-nav li .pane", {
		tabs: 'a:not(.link)',
		effect:'fade',
		initialIndex: -1,
		api:true
	});
	
	var primary = $("#slideshow-nav").tabs("#slideshow-nav li .sub-nav", {
		tabs: 'li a.top-level',
		effect:'fade',
		initialIndex: 0,
		onClick: function(event, tabIndex) { 
			//$(".sub-nav li .pane").hide();
			currentPrimary = this.getIndex();
		},
		api:true
	});
	
	$("#slide-prev").click(function (){
		if(currentPrimary - 1 == -1){
			currentPrimary = totalPrimary - 1;
		} else {
			currentPrimary -= 1;
		}
		$(".top-level:eq(" + currentPrimary + ")").click();
	});
	
	$("#slide-next").click(function (){
		if(currentPrimary + 1 == totalPrimary){
			currentPrimary = 0;
		} else {
			currentPrimary += 1;
		}
		$(".top-level:eq(" + currentPrimary + ")").click();
	});
	
	var slideshow = $("#slideshow-visuals").scrollable({
			size: 1,
			loop:true,
			globalNav:true,
			clickable:false
		}).circular().navigator({ 
			navi: "#slideshow-nav",
			naviItem: 'a:not(.link)', 
			activeClass: 'current',
			api:true
	});
}