
$(document).ready(function() {
	$('a[rel=external]').each(function() {
		this.target = '_blank';
	});

	$('#intlCurrencyDropdown a').each(function() {
		var href = $(this).attr("href");
		href += "&pathname=" + document.URL;
		$(this).attr("href", href);
	});
	showCurrencyDisclaimer();
});

function initializeBasicShopHeader() {
	_showBasicShopSelectedCurrencyHeader();
}

function _showBasicShopSelectedCurrencyHeader() {
	var selectedCurrency = window.cafepress.currencyCode;
	
	$(".intlBasicMenuLink").css('display','none');
	if (selectedCurrency == 'AUD') { $("a.aud").css('display', 'block'); }
	else if (selectedCurrency == 'CAD') { $("a.cad").css('display', 'block'); }
	else if (selectedCurrency == 'GBP') { $("a.gbp").css('display', 'block'); }
	else if (selectedCurrency == 'EUR') { $("a.eur").css('display', 'block'); }
	else { $("a.usd").css('display', 'block'); }
}

var productImageToUrlMapping = {};

function addProductImage(product, perspective, size, color, url) {
	var productToPerspectiveMap = productImageToUrlMapping;
	
	if (! productToPerspectiveMap[product]) productToPerspectiveMap[product] = {};
	var perspectiveToSizeMap = productToPerspectiveMap[product];
	
	if (! perspectiveToSizeMap[perspective]) perspectiveToSizeMap[perspective] = {};
	var sizeToColorMap = perspectiveToSizeMap[perspective];
	
	if (! sizeToColorMap[size]) sizeToColorMap[size] = {};
	var colorToUrlMap = sizeToColorMap[size];
	
	var imageObj = {
		url: url
	};
	
	colorToUrlMap[color] = imageObj;
}

function getProductImageUrl(product, perspective, size, color)
{
	if (! perspective) perspective = getFirstKeyFromMap(productImageToUrlMapping[product]);
	if (! size) size = getFirstKeyFromMap(productImageToUrlMapping[product][perspective]);
	if (! color) color = getFirstKeyFromMap(productImageToUrlMapping[product][perspective][size]);
	
	return productImageToUrlMapping[product][perspective][size][color].url;
}

function getFirstKeyFromMap(map) {
	for (key in map) {
		return key;
	}
}

function initializeTracking(themeId){
	$(window).load(function() {
		var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
		var gaUrl = gaJsHost + 'google-analytics.com/ga.js';
		$.getScript(gaUrl, function() {
			$.get('/content/shop/theme/1/googleanalyticsconfig.txt', function(data) {
				var googleAnalyticsThemeId = data;
				_gat._getTracker("' + googleAnalyticsThemeId + '")._trackPageview();
			})
		});
	});
}

function launchHelp(newURL, newFeatures)
{
	launchHelpInShop(newURL, newFeatures);
}

function launchHelpInShop(newURL, newFeatures)
{
	if (window.HelpWindow) HelpWindow.close();
	HelpWindow = open(newURL, "HelpWindow", newFeatures + ",screenX=0,left=0,screenY=0,top=0,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,status=0,toolbar=0,scroll=1");
	if (HelpWindow.opener == null) HelpWindow.opener = window;
	HelpWindow.focus();
}

function showCurrencyDisclaimer() {
	var selectedCountry = window.cafepress.countryCode;
	var currencyObj = $("#currencyDisclaimer");
	if (selectedCountry) {
		if (selectedCountry == "US") {
			currencyObj.hide();
		} else {
			currencyObj.show();
		}
	} else {
		currencyObj.hide();
	}
}
