$(document).ready(function() {

	$("#navigation h3").mouseover(function () {
	
		$("#navigation").addClass("hover");
		$("#navigation li").removeClass("hover");
		
		if ($(this).parent().hasClass("selected")) {
			$("#navigation li.selected div").show();
		} else {
			$(this).parent().addClass("hover");
			$("#navigation li.selected div").hide();
		}	
	});


	$("#navigation").mouseleave(function () {
	
		$("#navigation").removeClass("hover");
		$("#navigation li").removeClass("hover");
		$("#navigation li.selected div").show();
		
	});

	$("#card-details-tabs li").click(function () {

		//Turn off all tabs
		$("#card-details-tabs li").each(function (i) {
		
		    if ($(this).css("background-position-x")) {
				//Internet Explorer
				this.style.backgroundPositionX = "0px";
				
		    } else {
				//All other browsers
				if (this.style.backgroundPosition.substring(0,3) == "-15") {
					this.style.backgroundPosition = this.style.backgroundPosition.substring(3);
				}
			}
		});
		
		//Turn on selected tab
		if ($(this).css("background-position-x")) {
			$(this).css("background-position-x", "-150px");
		} else {
			$(this).css("background-position", "-15" + $(this).css("background-position"));
		}
	
		//Hide all details panes
		$("#card-details > li").addClass("hidden");
		
		//Show selected detail pane
		$("#card-details > li." + $(this).attr("class")).hide();
		$("#card-details > li." + $(this).attr("class")).removeClass("hidden");
		$("#card-details > li." + $(this).attr("class")).fadeIn();
		
		trackPage("/" + $("body").attr("class").replace("cards-", "cards/") + "/" + $(this).attr("class"));
		
	});



	$("#compare-data th").click(function () {
		
		var th = $("th:first", $(this).parent());
		
		if (th.hasClass("expanded")) {
			th.removeClass("expanded");
			th.addClass("collapsed");
			$("tr.row", th.closest("tbody").next("tbody")).hide();
		
		} else {
			th.removeClass("collapsed");
			th.addClass("expanded");
			$("tr.row", th.closest("tbody").next("tbody")).show();

			trackEvent('Compare Cards', 'Expand', th.html());
		
		}
	});



	$(".close").click(function () {
		closeDialog();		
	});



	//If we're on a card-details page, click the first tab so it's visible,
	//and then add a scroll listener for the table header.
	if ($("#card-details-tabs")) {
		($("#card-details-tabs li:first")).trigger("click");

		$(window).scroll(function () { 
			if ($(window).scrollTop() > 268) {

				$("#compare-header").css("position", "relative");
				$("#compare-header").css("padding-top", "2em");
				$("#compare-header").css("top", $(window).scrollTop()-268);

				$(".drop-shadow").css("display", "block");
				$(".drop-shadow").css("top", $(window).scrollTop()-268);

			} else {

				$("#compare-header").css("position", "static");
				$("#compare-header").css("padding-top", "0");

				$(".drop-shadow").css("display", "none");
			}
		});

	}



	
	//Get external links to open in a new window
	$("a").click(function()
	{
		if (((this.href != "#") && 
			(this.href != "") && 
			(this.href.indexOf("localhost") == -1) && 
			(this.href.indexOf("6990kc1") == -1) && 
			(this.href.indexOf("t-3.com") == -1) && 
			(this.href.indexOf("inkfromchase.com") == -1)) || 
			(this.href.indexOf("/assets/") != -1)) 
		{
			window.open(this.href);
			return false;
		}
	});



	//If we're on the compare page, expand first section.
	if ($("#compare-data")) {
		($("#compare-data th:first")).trigger("click");
	}
	



});



function launchBlueprint() {
	trackPage('/blueprint');
	launchDialog("blueprint-dialog");
	return false;
}

function launchUltimateRewards() {
	trackPage('/ultimate_rewards');
	launchDialog("ultimate-rewards-dialog");
	return false;
}

function launchFlexibleSpending() {
	trackPage('/flexible_spending_capacity');
	launchDialog("flexible-spending-capacity-dialog");
	return false;
}

function launchOfferDetails() {
	trackPage('/offer_details');
	launchDialog("offer-details-dialog");
	return false;
}

function launchPressPlayer() {
	trackPage('/press_player');

	//Embed press player
	var params = {quality: "high", scale: "noscale", wmode: "window", allowscriptaccess: "always", bgcolor: "#FFFFFF"};
	var flashvars = {video: "/assets/HD_ISCI_YCYC5058H.flv"};
	var attributes = {id:"press-player"};
	swfobject.embedSWF("/flash/vidplayer.swf", "press-player", "640", "380", "9.0.124", "expressInstall.swf", flashvars, params, attributes);

	launchDialog("press-player-dialog");
	return false;
}

function launchDialog(obj) {
	
	//Stretch dimming layer to full height of the page
	$("#page-dimmer").css("height", document.body.clientHeight);
	
	//Scroll page to top
	window.scrollTo(0,0);
	
	$("#page-dimmer").show();
	$("#" + obj).parent().fadeIn();


	$("*").bind("keydown", function(e) {
		if(e.keyCode == 27) {
			closeDialog();
		}
	});
	
	//Need to hide any Flash
	$("#card-flash").css("visibility", "hidden");
	$("#card-finder").css("visibility", "hidden");
}

function closeDialog() {
	$(".dialog").parent().fadeOut("fast");
	$("#page-dimmer").hide();
	$("*").unbind("keypress");

	//Need to show any Flash that was hidden
	$("#card-flash").css("visibility", "visible");
	$("#card-finder").css("visibility", "visible");
	
	
	//If we're closing the Press Player dialog, we need to unembed
	//the Flash to get it to stop playing.
	if (document.getElementById("press-player-wrapper")) {
		document.getElementById("press-player-wrapper").innerHTML = "";

		var div = document.createElement("div");
		div.id = "press-player";
		document.getElementById("press-player-wrapper").appendChild(div);
	}		
	
}

function trackPage(tag) {
	pageTracker._trackPageview(tag);
}

function trackEvent(category, action, label) {
	pageTracker._trackEvent(category, action, label);
}
