$(document).ready(function() {
	
	if ($.browser.msie){
		$("a.arrowLink").each(function() {
			if ($(this).height() > 13 ){
				$(this).addClass("arrowLinkFix");
	        	$(this).append('&nbsp;<span class="iconArrowLink"></span>');
			}
	     });
	}
	
	// Fix thickbox when a tab changes
	function tbPreload() {
		$(".thickbox, area.thickbox, input.thickbox").unbind();
		tb_init("a.thickbox, area.thickbox, input.thickbox");
		imgLoader = new Image();
		imgLoader.src = tb_pathToImage;
	}
	// Tabs
	//alert($("#apdTabsInnerR").children().size());
	if ($("#apdTabsInnerR").children().size() > 0) {
		$("#apdTabsInnerR > ul").tabs({
			//cache: true,
			spinner: '',
			load: function() {
				tbPreload();
				expander();
			}
		});
	}
	/* Dynamically display commerce boxes for buy buttons that go to a tab rather than add-to-cart */
	$('ul#apdTabsUl > li > a').click(function(){
		var currentTabText = $(this).attr('title').toLowerCase();
		if (currentTabText == 'how to buy' || currentTabText == 'sign up' || currentTabText == 'apply now' || currentTabText == '2 ways to buy') {
			$('.buttonHowApply').fadeOut(500);
		} else {
			$('.buttonHowApply').fadeIn(500);
		}
		
		// Send Omniture data to custom links
		var whichTab = $(this).attr('title').replace(/ /,'_');
		sendClickEvent(pageTitle + '_' + whichTab);
	});
	$('.buttonHowApply').click(function() {
		var $tabs = $("#apdTabsInnerR > ul").tabs();
		var whichTab = $(this).attr('id');
		var whichTabLn = whichTab.length;
		var whichTabCt = whichTab.charAt(whichTabLn-1);
		$tabs.tabs('select', parseFloat(whichTabCt));
		$('.buttonHowApply').fadeOut(500);
		return false;
	});
	
	// Commerce display handlers
	var anchor=location.hash;
	if(anchor=="" && anchor != "#New" && anchor != "#Upgrade"){
		$('div.cdTabList div:eq(0)').addClass('activeTab');
		$('div.tabPanel:eq(0)').show();
	}
	else{
		anchor=anchor.replace(/#/,"");
		if (anchor=="") {
			$('div.cdTabList div:eq(0)').addClass('activeTab');
			$('div.tabPanel:eq(0)').show();
		} else if (anchor == "New" || anchor == "Upgrade") {
			$('#tab'+anchor).addClass('activeTab');
			$('#'+anchor).show();
		} else {
			$('div.cdTabList div:eq(0)').addClass('activeTab');
			$('div.tabPanel:eq(0)').show();
		}
	};
	
	
	// Commerce display tabs		
	$('#tabbedCD .cdTabList a').click(function(){ //When any link is clicked
		$('#tabbedCD div').removeClass('activeTab'); // Remove active class from all links
		$(this).parent().addClass('activeTab'); //Set clicked link class to active
		var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
		$('#tabbedCD div.tabPanel').hide(); // Hide all divs
		$(currentTab).show(); // Show div with id equal to variable currentTab
		return false;
	});
	
});
/* Stop flickering, stupid IE6 */
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
	var ieversion=new Number(RegExp.$1)
	if (ieversion>=6) {
		try {
		  document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {}
	}
}
//this code is for capturing the clicks on the expanding/contracting content pieces.  
//the arguement 'clickEvent' is the name you want omniture to capture.
var howManyClicks =  0;
function addEvent(clickEvent) {
	for (var x = 0; x <= 5; x++) {
	   	if (x == 0) {
			if (howManyClicks == 1) {
				break;
			}
			sendClickEvent(pageTitle + clickEvent);
			howManyClicks = 1;
			break;
		}
	}
}
var howManyClicks = howManyClicks + 0;
/* Function for opening popup windows */
function OpenT(name,childX,childY) {
	popupWin = window.open(name,'open_window',',status=0,resizable=0,scrollbars=1,width=' + childX + ',height=' + childY);
}
/* Stop errors from other sites */
function pushEvent(foo) {}
function pushProduct(foo) {}
function MM_swapImage() {}
function MM_swapImgRestore() {}
/* Set the current year */
var date = new Date();
var currentDate = date.getFullYear();

function addToCartWithPopup(skuId, prodId, priorityCode, targetDiv) {
	$.ajax({
		url: "/commerce/checkout/redirectToCart.jsp?skuId=" + skuId + "&prodId=" + prodId + "&successUrl=/quotes/minishoppingcart-frame.jsp&priorityCode=" + priorityCode +"/>",
		  success: function(data) {
			$('#minicart1').html(data);
		    $(targetDiv).html(data);
		    $(targetDiv).css('display', 'block');
		  }
	});	
}
function hideAddToMiniCart() {
	if ($('#addtominicart')) {
		$('#addtominicart').css('display', 'none');
	}
}
/*
 * Function used to add 1 product to the shopping cart
 * prodId = 1 product id, which should have a matching sku id
 * skuId = 1 sku id, which should have a matching product id
 * miniCartRefresh = true or false depending on if you want to automatically refresh the mini-cart iframe.
 * successUrl = If you want the user to remain on the same page, set this to false.  If you want the user to go somewhere after the ajax, set a relative url
 */
function productToCartAJAX(prodId, skuId, miniCartRefresh, successUrl) {
	$.post(
		'/commerce/checkout/redirectToCart.jsp', 
		{ 
			prodId:		prodId,
			skuId:		skuId
		},
		function() {
			if (miniCartRefresh == true) {
				var currentUrl = $('#minicart1 iframe').attr('src');
				var date = new Date();
				var epoch = date.getTime();
				var newUrl;
				if (currentUrl.indexOf('?') > 0) {
					newUrl = currentUrl + "&no-cache=" + epoch
				} else {
					newUrl = currentUrl + "?no-cache=" + epoch
				}
				$('#minicart1 iframe').attr('src', newUrl);
			}
			if (successUrl != false) {
				var colon=":";
				var parent_protocol=parent.location.protocol;
				var parent_host=parent.location.hostname;
				var parent_port=parent.location.port;
				var parent_url;
				if (parent_port == '' || parent_port == null) {
					parent_url=parent_protocol + "//" + parent_host + successUrl;
				} else {
					parent_url=parent_protocol + "//" + parent_host + colon + parent_port + successUrl;
				}
				document.location.href=parent_url;
			}
		}
	);
}
/*
 * Function used to add more than 1 product to the shopping cart
 * prodSkuIds = A double array containing product and sku pairs.  Example 'prod01,sku01|prod02,sku02|prod03,sku03'
 * miniCartRefresh = true or false depending on if you want to automatically refresh the mini-cart iframe.
 * successUrl = If you want the user to remain on the same page, set this to false.  If you want the user to go somewhere after the ajax, set a relative url
 */
function multipleProductToCartAJAX(prodSkuIds, miniCartRefresh, successUrl) {
	var prodSkuIdMap = prodSkuIds.split('|');
	for (i = 0; i < prodSkuIdMap.length; i++) {
		var currProdSku = prodSkuIdMap[i].split(',');
		if (successUrl != false) {
			if (parseFloat(prodSkuIdMap.length - 1) == i) {
				productToCartAJAX(currProdSku[0], currProdSku[1], false, successUrl);
			} else {
				productToCartAJAX(currProdSku[0], currProdSku[1], false, false);
			}
		} else {
			if (parseFloat(prodSkuIdMap.length - 1) == i) {
				productToCartAJAX(currProdSku[0], currProdSku[1], miniCartRefresh, false);
			} else {
				productToCartAJAX(currProdSku[0], currProdSku[1], false, false);
			}
		}
	}
}
