var this_pane = 5;
var timer;

function daysAvailable(date) {
	var day = date.getDay();
	var datesec = date.getTime();
	var today = new Date();
	var todaysec = today.getTime();
	if (datesec < todaysec)
		return [false, 'notcool'];
	if (day > 1 && day < 5) {
		return [true, 'cool'];
	} else {
		return [false, 'notcool'];
	}
}

function resetPanes() {
	$("li.panes").animate({ opacity: "1" }, 400, 'swing');
	$("a.arrow").css("opacity", "1");
	$(".logo").css("opacity", "1");
	
	for (x = 1; x < 8; x++) {
		$('#pane-1-' + x).hide();
		$('#pane-2-' + x).hide();
		$('#pane-3-' + x).hide();
		$('#pane-4-' + x).hide();
	}
	
	for (x = 1; x < 5; x++) {
		$('#pane-' + x + '-0').show();
		$('#pane-' + x + '-0').show();
		$('#pane-' + x + '-0').show();
		$('#pane-' + x + '-0').show();
	}
	$("a.first").addClass("current");	
	
	if (this_pane == 6) {
		$("#jordan-spot").html("<object width='450' height='254'><param name='movie' value='video/video_player-jordan_spot.swf'><embed src='video/video_player-jordan_spot.swf' width='450' height='254'></embed></object>");
	} else {
		$("#jordan-spot").html("");
	}
}

function clearContainers() {
	$("div.abovemenu ul li a").removeClass("current");	
	$(".logo").css("opacity", "0");
	$("#main li.panes").css("opacity", "0.4");
	$("a.arrow").css("opacity", "0");
}

function resetWindow() {
	var height = $(window).height();
	var width = $(window).width();
	var wrapper = width;
	var container_left = 0;
	var container_top = (height - 425) / 2;
	if (width > 920) {
		var wrapper = width * 3.1;
		container_left = (width - 800) / 2;
		$("#main li.panes").css("width", width);
		$("body").css("overflow-x", "hidden");
		$("#wrapper").css("overflow-x", "hidden");
	} else {
		$("#main li.panes").css("width", 800);
		wrapper = "100%";
		if (width > 800) 
			container_left = (width - 800) / 2;
		$("body").css("overflow-x", "auto");
		$("#wrapper").css("overflow-x", "auto");
	}
	
	if (height > 500) {
		$("body").css("overflow-y", "hidden");
		$("#wrapper").css("overflow-y", "hidden");
		$(".logo").show();
		$(".facebook").show();
		$(".superbowl").show();
		$("#main li.panes").css("height", height);
		$("#main li.panes").css("margin-top", "0px");
	} else {
		container_top = 0;
		$("body").css("overflow-y", "auto");
		$("#wrapper").css("overflow-y", "auto");
		$("#main li.panes").css("height", 450);
		$("#main li.panes").css("width", width);
		$("#main li.panes").css("margin-top", "10px");
	}
	
	if (height < 600) 
		$(".logo").hide();
	
	if (height < 550) {
		$(".facebook").hide();
		$(".superbowl").hide();
	}
		
	if (width < 920 || height < 500)
		$(".arrow").hide();
	else
		$(".arrow").show();

	$("div.container-wrap").css("top", container_top);
	$("div.container-wrap").css("left", container_left);
	$("#wrapper").css("width", wrapper);
	$.scrollTo($("#pane-" + this_pane), 100, { margin: true });
}

$(window).resize(function() {
	resetWindow();
});

$(document).ready(function() {
	resetPanes();
	resetWindow();
	$("#ouroffice").hide();
	$("#appt_place").hide();
	$("#superbowl-text").hide();
	
	$("a.arrow").click(function() {
		clearContainers();
		$.fancybox.close();
		var number = $(this).data('number');
		$.scrollTo($("#pane-" + number), 800, { 
			margin: true, 
			onAfter: function() {
				resetPanes();
			}
		});
		this_pane = number;
		return false;
	});
	
	$("a > img.superbowl").hover(function() {
		$("#superbowl-text").toggle();
	});
	
	$("#superbowl-link").click(function(e) {
		$.fancybox({
			href: this.href,
			padding: 0,
			width: 480,
			height: 360,
			overlayShow: false,
			type: 'swf',
			swf: {
				'wmode': 'transparent',
				'allowFullScreen': 'false'
			}
		});
		e.preventDefault();
	});
	
	$("div.submenu ul li a").click(function() {
		var number = $(this).data('number');
		$.fancybox.close();
		clearContainers();
		$.scrollTo($("#pane-" + number), 800, { 
			margin: true, 
			onAfter: function() {
				resetPanes();
			}
 		});
		this_pane = number;
		return false
	});
	
	$("div.abovemenu ul li a").click(function() {
		$("div.abovemenu ul li a").removeClass("current");
		var number = $(this).data('number');
		$("div.pane").hide();
		$("#pane-" + number).fadeIn(400);
		$(this).addClass("current");
		return false
	});
	
	$("#main-video").fancybox({
		href: this.href,
		padding: 0,
		width: 642,
		height: 361,
		overlayShow: false,
		type: 'swf',
		swf: {
			'wmode': 'transparent',
			'allowFullScreen': 'false'
		},
		onStart: function() {
			$("#main li.panes").hide();
			$("#wrapper").css("opacity", "0");
			$("#main-video").blur();
		},
		onClosed: function() {
			$("#main li.panes").show();
			$.scrollTo($('#pane-5'), 0, {
				margin: true,
				onAfter: function() {
					$("#wrapper").animate({
						opacity: "1.0"
					}, 400, "swing", function() {
						$("#main li.panes").animate({
							opacity: "1.0"
						}, 400, "swing");
					});
					resetPanes();
					resetWindow();
					$(window).resize();
				}
			});
		}
	});
	
	$("#main-video").trigger("click");
	
	$("#main-video").blur();
	
	$("a.photo-gallery").fancybox({
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		href: this.href,
		opacity: 1,
		overlayOpacity: 0,
		type: 'image'
	});
	
	$("a.video-player").each(function() {
		var height = 253;
		if ($(this).hasClass("wide"))
			var height = 209;
		$(this).fancybox({
			transitionIn: 'elastic',
			transitionOut: 'elastic',
			href: this.href,
			padding: 0,
			width: 450,
			height: height,
			overlayOpacity: 0,
			type: 'swf'
		});
	}); 
	
	$("a.pane-trigger").click(function() {
		var pane_number = $(this).data("pane-number");
		$("div.pane").hide();
		$("#pane-" + pane_number).fadeIn();
		$("#mainContent" + last).css("width", "489px");
		$("#mainContent" + last + " > div.jspContainer").css("width", "489px");
	});
	/*
	$("#wrapper").css("opacity", "0.0");
	$("#logo").css("opacity", "0.0");
	*/
	if (!$.browser.ie) {
		$(window).mousemove(function(e) {
			var difference = $(window).width() - 810;
			var measurement = (e.pageX / 8) + (difference / 5);
			$("#background-logo").css("right", measurement);
		});
		var difference = ($(window).width() - 830) * 0.4;
		$("#background-logo").css("right", difference);
	} else {
		if ($.browser.version < 7) {
			$("#background-logo").hide();
			$(".arrow").remove();
		}
	}
	
	$("a.logo-wrap").click(function() {
		$("#main-video").trigger("click");
		return false;
	});
	
	$("a.vp").hover(function() {
		var name = $(this).data("name");
		$("#vp-name").fadeOut("fast", function() {
			$("#vp-name").html(name).fadeIn()
		});
	}, function() {
		$("#vp-name").html("Click an image below for a look at our Xtra Entertainment.");
	});

	$("#date").datepicker({
		beforeShowDay: daysAvailable,
		dateFormat: 'yy-mm-dd'
	}); 
	
	$(".hasdemo").hover(function() {
		$(".hasdemo p a.demovideo").show();
	}, function() {
		if (timer) {
      clearTimeout(timer);
      timer = null
    }
    timer = setTimeout(function() {
			$(".hasdemo p a.demovideo").hide();
		}, 500);
	});
	
	$("input[name=home_office]").change(function() {
		if ($("input[name=home_office]:checked").val() == "Home") {
			$("#ouroffice").hide();
			$("#appt_place").show();
		} else {
			$("#appt_place").hide();
			$("#ouroffice").show();
		}
	});
	
	$("#doesntwork").click(function() {
		if ($("#doesntwork").is(":checked")) {
			$(".calendar").attr("disabled", true);
		} else {
			$(".calendar").removeAttr("disabled");
		}
	})
	
	$('#appt-submit').click(function() {
		
		$("#error").hide();
		
		var name = $("#client_name").val();
		var email = $("#email_address").val();
		var phone = $("#phone_number").val();
		var date = $("#date").val();
		var time = $("#time").val();
		var info = $("#info").val();
		var party_type = $("#party_type").val();
		var party_for = $("#party_for").val();
		var home_office = $("input[name=home_office]:checked").val();
		var place = $("#place").val();
		var refer = $("#refer").val();
		var doesntwork = $("#doesntwork").is(":checked");
		
		if (name == "" || email == "") {
			$("#error").show();
			if (name == "")
				$("#client_name").focus();
			else
				$("#email_address").focus();
			return false;
		}
		
		var dataString = 'client_name=' + name + 
			'&email_address=' + email + 
			'&phone_number=' + phone + 
			'&date=' + date + 
			'&time=' + time + 
			'&info=' + info +
			'&party_type=' + party_type +
			'&party_for=' + party_for +
			'&home_office=' + home_office +
			'&place=' + place +
			'&doesntwork=' + doesntwork +
			'&refer=' + refer;
		
		$.ajax({
			type: 'POST',
			url: '/appointment.php',
			data: dataString,
			success: function() {
				$('#appointment').html("<p></p>");
				$('#appointment').html("<p>Thanks for making an appointment!</p>").hide().fadeIn(1500, function() {
					$('#appointment').append("<p>You'll be contacted soon.</p>");
				});
			},
			error: function() {
				$('#appointment').html("<p>An error has occurred</p>");
			}
		});
		return false;
	});
	
	$('.trackable').bind('click', function(event){
    var action = $(this).data('action');
    var category = $(this).data('category');
    _gaq.push(['_trackEvent', category, action]);
  });

});
