﻿// Setup global vars
var model;
var svcLocator;
var vwLocator;
var sortByCriteria = "";

function handleAddClick()
{
	var q;
	var respCtx = {
		onSuccess:handleAddSuccess,
		onFailure:handleAddFault
	}
	var svc = svcLocator.getService("addToBagService");
	q = document.forms[0].ctlSkuGroupType6$txtQuantity.value;
	svc.processAddToBag(SKU, SELECTED_SKU, q, CATEGORY_ID, MASTER_CATEGORY_ID, ORDER_ORIGINATION, respCtx);	
	//document.getElementById("addMessage").innerHTML = "<img src='/Shared/Images/misc/progress.gif' alt='' /> Loading... ";
	//document.getElementById("addMessage").style.display = "block";
	omnitureTrackActionAdd("Gift Card", "Add to ShoppingBag");
}
function handleAddSuccess() {
//	Debug.windowTrace("handleAddSuccess");
	//d. if error message is displayed, hide it
	var errorNode = document.getElementById("ajaxError");
	if (errorNode) {
		errorNode.innerHTML = "";
	}
	//e. update Add to Cart button to Proceed to Checkout (assumes item page)
	document.getElementById("btnPurchase").style.display = "none";
	document.getElementById("btnGoToSB").style.display = "block";
	//f. add message below button
	document.getElementById("addMessage").innerHTML = LABEL_ADD_SUCCESS;
	document.getElementById("addMessage").style.display = "block";
}
function handleAddFault(argResult) {
//	Debug.windowTrace("handleAddFault:"+typeof(argResult));
	var msg;
	if (typeof(argResult) == "string") {
		//some other error occurred
		msg = argResult;
	} else {
		//set the error message from the status key
		for (var skuCnt=0;skuCnt< argResult.length;skuCnt++){
			var currSku = argResult[skuCnt];
			msg = "";
			var currMsg=null;
			try{eval("currMsg = "+currSku.status+";");}catch (err){}
			if (currMsg) {
				msg += currMsg;
			} else {
				msg += currSku.status;
			}
			currSku.errorString = msg;
//			Debug.windowTrace(currSku.id+": Status = "+currSku.status+"; "+currSku.errorString);
		}
	}
	var errorDiv = document.getElementById("ajaxError");
	if (errorDiv) {
		errorDiv.innerHTML = msg;
	}
	//hide progress indicator
	var addMessageDiv = document.getElementById("addMessage");
	if (addMessageDiv) {
		addMessageDiv.innerHTML = "";
		addMessageDiv.style.display = "none";
	}
}
function handleSaveClick() {
	var svc = svcLocator.getService("addSavedItemService");
	svc.processSavedItem(SKU, SELECTED_SKU, CATEGORY_ID, MASTER_CATEGORY_ID);
	omnitureTrackAction("Gift Card", "Add to SavedItems");
}
function initAddButton()
{
//	Debug.windowTrace("initAddButton");
	if (isAjaxEnabled()) {
		var addLink = document.getElementById("btnPurchase");
		if (addLink) {
			addLink.href="javascript:handleAddClick()";
		}
		
		var saveLink = document.getElementById("ancSaveForLater");
		if (saveLink) {
			saveLink.href="javascript:handleSaveClick()";
		}
	}
}	
var itemZoomProperties = {
	maskColor: "#FFFFFF",
	thumbnailHilightColor: "#66CCCC",
	thumbnailBorderColor: "#EAEAEA",
	thumbnailBackgroundColor: "#FFFFFF",
	backgroundColor: "#FFFFFF"
};

function addItemZoom()
{
	var hasReqestedFlashVersion = true;
	var imageZoomData = "";

	var imageURL = "";
	var imageMaskX = "";
	var imageMaskY = "";
	var width = 0;
	var height = 0;
	var splitURL;
	var splitItem;
	
	if (document.forms[0].imageZoomData)
	{
		imageZoomData = document.forms[0].imageZoomData.value;
		imageZoomData += "&maskColor=" + itemZoomProperties.maskColor;
		imageZoomData += "&thumbnailHilightColor=" + itemZoomProperties.thumbnailHilightColor;
		imageZoomData += "&thumbnailBorderColor=" + itemZoomProperties.thumbnailBorderColor;
		imageZoomData += "&thumbnailBackgroundColor=" + itemZoomProperties.thumbnailBorderColor;
		
		splitURL = imageZoomData.split("&");
		
		for (var i = 0; i < splitURL.length; i++)
		{
			splitItem = splitURL[i].split("=");
			switch(splitItem[0])
			{
				case "x0":
					imageMaskX = Number(splitItem[1]);
					break;
				case "y0":
					imageMaskY = Number(splitItem[1]);
					break;
				case "im0":
					imageURL = splitItem[1];
					break;
				case "w0":
					width = Number(splitItem[1]);
					break;
				case "h0":
					height = Number(splitItem[1]);
					break;
			}
		}
		
		if (DetectFlashVer)
		{
			hasReqestedFlashVersion = DetectFlashVer(8, 0, 0);
		}

		var imageHTML = "";
		if (hasReqestedFlashVersion == true)
		{
			imageHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"'
			+ 'width="475" height="440" align="middle" '
			+ 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">'
			+ '<param name="allowScriptAccess" value="sameDomain" />'
			+ '<param name="movie" value="/Shared/flash/itemZoom.swf?v=5" />'
			+ '<param name="quality" value="high" />'
			+ '<param name="scale" value="noscale" />'
			+ '<param name="bgcolor" value="' + itemZoomProperties.backgroundColor + '" />'
			+ '<param name="wmode" value="opaque" />'
			+ '<param name="flashvars" value="' + imageZoomData + '" />'
			+ '<embed src="/Shared/flash/itemZoom.swf?v=5" quality="high" scale="noscale" '
			+ 'width="475" height="440" name="itemZoom" align="middle" '
			+ 'quality="high" '
			+ 'scale="noscale" '
			+ 'bgcolor="' + itemZoomProperties.backgroundColor + '" '
			+ 'name="itemZoom" '
			+ 'play="true" '
			+ 'loop="false" '
			+ 'wmode="opaque" '
			+ 'allowScriptAccess="sameDomain"'
			+ 'type="application/x-shockwave-flash" '
			+ 'flashvars="' + imageZoomData + '" '
			+ 'pluginspage="http://www.macromedia.com/go/getflashplayer" />'
			+ '<\/obj' + 'ect>';
		}
		else
		{
			if (imageMaskX == "")
			{
				imageMaskX = (width - 475) / 2;
			}
			
			if (imageMaskY == "")
			{
				imageMaskY = (height - 440) / 2;
			}
			
			imageHTML = '<div style="width:475px;height:440px;overflow:hidden;position:relative;"><img src="' + imageURL + '" alt="Necklace" title="" galleryimg="no" style="position:relative;left:' + (-imageMaskX) + 'px;top:' + (-imageMaskY) + 'px" width="' + width + '" height="' + height + '" /></div>';
		}
		vwLocator.getView("itemImage").innerHTML = imageHTML;
	}	
}

// ----------------------------------------------
// Function init
// Author:		Nathan Derksen
// Description:	First function to be called after page load
// Inputs:		<none>
// Returns:		<nothing>
// ----------------------------------------------
function init()
{
	// Suppress errors - only known workaround for Flash 9 ExternalInterface bug
	window.onerror = function(message, URL, line)
	{
		return true;
	};
	
	// Initialize Ajax services
	svcLocator = ServiceLocator.getInstance();
	svcLocator.registerService("addToBagService", new AddShoppingBagItemService());
	svcLocator.registerService("addSavedItemService", new AddSavedItemService());
	
	// Register some useful visual components for later access
	vwLocator = ViewLocator.getInstance();
	vwLocator.registerView("itemImage", document.getElementById("itemImage"));
	
	addItemZoom();
	mozillaDragFix();
	initAddButton();

}

function cancelEvent(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function mozillaDragFix()
{
	// Disable bug where dragging out of the Flash component can hilight text on the page
	// and where a mouse up event outside the Flash component is not registered by the component
	var isSafari = (navigator.userAgent.indexOf("Safari") > -1);
	if (document.addEventListener)
	{
		if (isSafari == false)
		{
			vwLocator.getView("itemImage").onmousedown = function() 
			{
				return false;
			};
			vwLocator.getView("itemImage").addEventListener("mouseup", cancelEvent, false);
			document.onmouseup = function() 
			{
				// Call an AS function within the Flash component to trigger a reset
				var flashElements = document.getElementsByName("itemZoom");
				if (flashElements.length > 0)
				{
					if (flashElements[flashElements.length-1].sendClick)
					{
						flashElements[flashElements.length-1].sendClick();
					}
				}
			};
		}
	}
}

BrowserUtils.addOnLoadHandler(init);