var pageXML = null;
var sticky = 0;
var pageTimeout = null;

// ------------------ TURNPAGE ------------------
function turnPage(catalog, page) {
	pageXML = null;
	var url = "../ajax/ajax_getPage.cfm";
	var pars = "catalog=" + catalog + "&page=" + page;
	var myAjax = new Ajax.Request(
		url,
		{
			method: 'get',
			parameters: pars,
			onComplete: acceptPageResponse,
			onFailure: ajaxFailure
		});
	// clear the productViewer
	clearText($("productViewer_title"));
	clearText($("productViewer_smallinfo"));
	clearText($("productViewer_blurb"));
	clearText($("productViewer_sku"));
	$("productViewer_img").style.display = "none";
	$("productViewer").style.display = "block";
	$("productViewer_loading").style.display = "block";
}

// ------------------ ACCEPTPAGERESPONSE ------------------
function acceptPageResponse(ajaxResponse) {
	$("productViewer_loading").style.display = "none";
	$("productViewer").style.display = "none";
	pageXML = ajaxResponse.responseXML;
}

// ------------------ VIEWTITLE ------------------
function viewTitle(product, stuck) {
	// titleStuck variable is for getting around the problem of viewTitle 
	// not working when something is "stickied" while the XML is still loading
	var titleStuck = stuck || 0;
	if ((sticky == 0 || titleStuck == 1) && pageXML != null) {
		// get rid of instructions and show productViewer
		$("instructions").style.display = "none";
		$("productViewer").style.display = "block";
		// --
		var productInfo = getProductArray(product);
		var imgEl = $("productViewer_img");
		var titleEl = $("productViewer_title");
		var smallinfoEl = $("productViewer_smallinfo");
		var blurbEl = $("productViewer_blurb");
		var skuEl = $("productViewer_sku");
		// clear out the sku element
		clearText(skuEl);
		// input the new text and image
		if (!document.all) { // calling spinner img behaves oddly in IE
			imgEl.src = "../skins/graphics/spinner.gif";
		}
		imgEl.src = productInfo.imgsrc;
		imgEl.style.display = "block";
		replaceText(titleEl, productInfo.title);
		replaceText(smallinfoEl, productInfo.smallinfo);
		replaceText(blurbEl, productInfo.blurb);
		// closed caption?
		if (productInfo.closedcaption == 1) {
			newElement = document.createElement("span");
			newElement.className = "closedCaption";
			newText = document.createTextNode(" [cc]");
			newElement.appendChild(newText);
			blurbEl.appendChild(newElement);
		}
		if (productInfo.sku != null) {
			for (var i = 0; i < productInfo.sku.length; i++) {
				// create outer div (container)
				newElement = document.createElement("div");
				newElement.className = "sku";
				// create anchor tag (with image)
				newElementAnchor = document.createElement("a");
				newElementAnchor.href = "frameCart.cfm?sku=" + escape(productInfo.sku[i].sku) + "&catalog=" + escape(productInfo.catalog);
				newElementAnchor.onclick= addSkuToCart;
				newElementImg = document.createElement("img");
				newElementImg.className = "sku";
				newElementImg.src = "../skins/graphics/buy.gif";
				newElementAnchor.appendChild(newElementImg);
				newElement.appendChild(newElementAnchor);
				// create text node (space)
				newText = document.createTextNode(" ");
				newElement.appendChild(newText);
				// create anchor tag (with price)
				newElementAnchor = document.createElement("a");
				newElementAnchor.className = "price";
				newElementAnchor.href = "frameCart.cfm?sku=" + escape(productInfo.sku[i].sku) + "&catalog=" + escape(productInfo.catalog);
				newElementAnchor.onclick = addSkuToCart;
				newText = document.createTextNode(productInfo.sku[i].catalogPrice);
				newElementAnchor.appendChild(newText);
				newElement.appendChild(newElementAnchor);
				// create text node (title)
				newText = document.createTextNode(" " + productInfo.sku[i].title);
				newElement.appendChild(newText);
				skuEl.appendChild(newElement);
			}
		}
	} else if (sticky == 0 || titleStuck == 1) {
		if (pageTimeout != null) {
			window.clearTimeout(pageTimeout);
		}
		pageTimeout = window.setTimeout("viewTitle(" + product + ", " + titleStuck + ")", 1000);
	}
}

// ------------------ GETPRODUCTARRAY ------------------
function getProductArray(product) {
	var myIndex = null;
	var returnArray = new Array();
	productArray = pageXML.getElementsByTagName("product");
	// find the position of the correct product node
	for (var i = 0; i < productArray.length; i++) {
		if (productArray[i].getAttribute("id") == product) {
			myIndex = i;
			break;
		}
	}
	if (myIndex != null) {
		// build the return array
		returnArray['imgsrc'] = productArray[myIndex].getElementsByTagName("imgsrc")[0].firstChild.nodeValue;
		returnArray['title'] = productArray[myIndex].getElementsByTagName("title")[0].firstChild.nodeValue;
		returnArray['closedcaption'] = productArray[myIndex].getElementsByTagName("closedcaption")[0].firstChild.nodeValue;
		returnArray['catalog'] = productArray[myIndex].getElementsByTagName("catalog")[0].firstChild.nodeValue;
		if (productArray[myIndex].getElementsByTagName("smallinfo")[0].hasChildNodes()) {
			returnArray['smallinfo'] = productArray[myIndex].getElementsByTagName("smallinfo")[0].firstChild.nodeValue;
		} else {
			returnArray['smallinfo'] = "";
		}
		if (productArray[myIndex].getElementsByTagName("blurb")[0].hasChildNodes()) {
			returnArray['blurb'] = productArray[myIndex].getElementsByTagName("blurb")[0].firstChild.nodeValue;
		} else {
			returnArray['blurb'] = "";
		}
		// now time to get the SKU's
		try {
			skuArray = productArray[myIndex].getElementsByTagName("sku");
			returnArray['sku'] = new Array();
			for (i = 0; i < skuArray.length; i++) {
				returnArray['sku'][i] = new Array();
				returnArray['sku'][i]['sku'] = skuArray[i].getAttribute("id");
				returnArray['sku'][i]['title'] = skuArray[i].getElementsByTagName("title")[0].firstChild.nodeValue;
				returnArray['sku'][i]['catalogPrice'] = skuArray[i].getElementsByTagName("catalogPrice")[0].firstChild.nodeValue;
			}
		} catch (noskus) {
			returnArray['sku'] = null;
		}
		return returnArray;
	} else {
		return null;
	}
}

// ------------------ STICKYTITLE ------------------
function stickyTitle(product, stickyState) {
	sticky = stickyState;
	if (sticky == 1) {
		new Effect.Highlight($("productViewer"), {duration:1.5});
	}
	viewTitle(product, 1);
}

// ------------------ ADDSKUTOCART ------------------
function addSkuToCart() {
	window.frameCart.location = this.href + "&timestamp=" + new Date().getTime();
	return false;
}

// ------------------ GETCARTINFO ------------------
function getCartInfo() {
	var url = "../ajax/ajax_getCartInfo.cfm";
	var pars = "timestamp=" + new Date().getTime();
	var myAjax = new Ajax.Request(
		url,
		{
			method: 'get',
			parameters: pars,
			onComplete: updateCart,
			onFailure: ajaxFailure
		});
}

// ------------------ UPDATECART ------------------
function updateCart(ajaxResponse) {
	var cartXML = ajaxResponse.responseXML;
	var itemCount = cartXML.documentElement.getElementsByTagName("items")[0].firstChild.nodeValue;
	var itemTotal = cartXML.documentElement.getElementsByTagName("total")[0].firstChild.nodeValue;
	if (itemCount == 1) {
		replaceText($("cartItemLabel"), 'Item');
	} else {
		replaceText($("cartItemLabel"), 'Items');
	}
	replaceText($("cartItems"), itemCount);
	replaceText($("cartTotal"), itemTotal);
	$("cartItems").style.opacity = "1";
	$("cartTotal").style.opacity = "1";
	new Effect.Pulsate($("cartItems"));
	new Effect.Pulsate($("cartTotal"));
}

// ------------------ TOGGLECART ------------------
function toggleCart() {
	new Effect.toggle($("cartDetails"), 'blind');
	new Effect.BlindUp($("instructions"));
	return false;
}

// ------------------ SHOWINSTRUCTIONS ------------------
function showInstructions() {
	$("productViewer").style.display = "none";
	new Effect.BlindUp($("cartDetails"));
	new Effect.BlindDown($("instructions"));
	return false;
}

// ------------------ AJAXFAILURE ------------------
function ajaxFailure(response) {
	alert("Error processing request, status = " + response.status);
}
