$(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);
				
				$(".drop-shadow-narrow").css("display", "block");
				$(".drop-shadow-narrow").css("top", $(window).scrollTop()-268);

			} else {

				$("#compare-header").css("position", "static");
				$("#compare-header").css("padding-top", "0");

				$(".drop-shadow").css("display", "none");
				
				$(".drop-shadow-narrow").css("display", "none");
			}
		});

	}



	
	//Get external links to open in a new window
	$("a").click(function()
	{
		if($(this).hasClass("event-btn-next") || $(this).hasClass("image-viewer") || $(this).hasClass("event-btn-prev")){
		}else{
			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");
	}
	


	//Exit Survey added to site examines <a> links and the onunload
	//event to determine when to launch a popup.  Added these functions
	//to prevent clicks in flash from launching popups.
	$("object").mouseover(function() {
		swfClick = 1;
	});
	$("object").mouseout(function() {
		swfClick = 0;	
	});


});



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;
}

$(".close2").click(function () {
		closeDialog();		
	});

function launchPressPlayer(videoPath,videoTitle) {
	
	var playerVersion = swfobject.getFlashPlayerVersion(); // returns a JavaScript object
	if(playerVersion != "9")
	{
		document.getElementById("no-flash").style.display='block';	
	}
	
	trackPage('/press_player');
	
	$("#press-player-header h2").html(videoTitle);

	//Embed press player
	var params = {quality: "high", scale: "noscale", wmode: "window", allowscriptaccess: "always", bgcolor: "#FFFFFF"};
	var flashvars = {video: videoPath};
	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 launchVideoPlayer(videoTitle,videoURL) {

	var playerVersion = swfobject.getFlashPlayerVersion(); // returns a JavaScript object
	if(playerVersion != "9")
	{
		document.getElementById("no-flash").style.display='block';	
	}

	//Stretch dimming layer to full height of the page
	$("#page-dimmer").css("height", document.body.clientHeight);
	
	//Scroll page to top
	window.scrollTo(0,0);
	
	$("#generic-player-header h2").html(videoTitle);
	
	//Embed press player
	var params = {quality: "high", scale: "noscale", wmode: "window", allowscriptaccess: "always", bgcolor: "#FFFFFF"};
	var flashvars = {video: videoURL};
	var attributes = {id:"generic-player"};
	swfobject.embedSWF("/flash/vidplayer.swf", "generic-player", "640", "380", "9.0.124", "expressInstall.swf", flashvars, params, attributes);
	
	$("#page-dimmer").show();
	$(".dialog-wrapper-video").fadeIn();
	
	$("*").bind("keydown", function(e) {
		if(e.keyCode == 27) {
			$(".dialog-wrapper-video").fadeOut("fast");
			$("#page-dimmer").hide();
			$("*").unbind("keypress");
		}
	});
	
	return false;
	
}

function launchProfileViewer() {
	//trackPage('/offer_details');
	
	launchDialog("profile-viewer-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");
		
}

function closePressPlayer() {
	$(".dialog-press-player").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";
		var div2 = document.createElement("div");
		div2.id = "no-flash"; 
		div2.innerHTML += "   <p>The functionality of this page requires the Adobe Flash Player to be installed. Click on the link below to download and install the current version.</p>";
        div2.innerHTML += "   <p><a href=\"http://www.adobe.com/go/getflashplayer\" target=\"_blank\"><img src=\"/images/get_flash_player.gif\" alt=\"Get Adobe Flash Player\" width=\"112\" height=\"33\" /></a></p>";
		document.getElementById("press-player-wrapper").appendChild(div);
		document.getElementById("press-player").appendChild(div2);
	}		
	
}

function closeGenericPlayer() {
	$(".dialog-content").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("generic-player-wrapper")) {
		document.getElementById("generic-player-wrapper").innerHTML = "";

		var div = document.createElement("div");
		div.id = "generic-player";
		var div2 = document.createElement("div");
		div2.className = "no-flash"; 
		div2.id = "no-flash"; 
		div2.innerHTML += "   <p>The functionality of this page requires the Adobe Flash Player to be installed. Click on the link below to download and install the current version.</p>";
        div2.innerHTML += "   <p><a href=\"http://www.adobe.com/go/getflashplayer\" target=\"_blank\"><img src=\"/images/get_flash_player.gif\" alt=\"Get Adobe Flash Player\" width=\"112\" height=\"33\" /></a></p>";
		document.getElementById("generic-player-wrapper").appendChild(div);
		document.getElementById("generic-player").appendChild(div2);
	}		
	
}

function trackPage(tag) {
	pageTracker._trackPageview(tag);
}

function trackEvent(category, action, label) {
	pageTracker._trackEvent(category, action, label);
}

// Toggles Events on the Homepage Event Carousel
function toggleEvent(direction,current,total) {
	var nextNumber = 0;
	if (direction == "next"){
		nextNumber = current + 1;
		if(nextNumber > total) { nextNumber = 1; }		
	}else{
		nextNumber = current - 1;
		if(nextNumber == 0) { nextNumber = total; }
	}
	// Toggle Event Information
	$("#event" + current).addClass("hidden");
	$("#event" + nextNumber).removeClass("hidden");
	// Toggle Event Square Icon
	$("#event-box" + current).removeClass("active");
	$("#event-box" + nextNumber).addClass("active");
}

function switchProfile(curr,next)
{
	$("#profile" + curr).hide();
	$("#profile" + next).show();
}
