//**********************************************************************************************
//**********************************************************************************************
// functions for getting xml data.
//**********************************************************************************************
//**********************************************************************************************
var xmlHttp;					//variable to contain the xml object.
var myXML = ""; 				//set a global varaible to contain the xml data structure
var xmlHttpAccepted = false;	//set a global variable to determine is xml data is loaded correctly.
var itemID;
var productImage;
var productName;
var productDesc;
var firstImage;
var myproducts;
var n;

//function to pull the xml data out of the specified page so it will be ready to use
function loadData(referalPage){
	p_tempPage = referalPage;
	//determine if XMLHttpRequest or ActiveXObject is required to use.
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			if (xmlHttp.status == 200){
				document.getElementById("quickshop-btn").innerHTML = "<img src='/text/content/landing/halloweenhaven/images/entertaining-quickshop.jpg' border='0' />";
				//document recieved
				xmlHttpAccepted = true;
				myXML = xmlHttp.responseXML;

				if (p_tempPage == "products"){
					products();
				}else{
					products();
				}
			}else{
				alert("The page failed to load, please try again.");
			}
		}
	}
	xmlHttp.open( "GET", "/text/content/landing/halloweenhaven/xml/HHEntertain.xml", true );
	xmlHttp.send(null);

	return xmlHttp;
}

function products(){

		if (window.p_product == undefined){
			var tempproduct = "";
		}else{
			var tempproduct = "";
		}

		myproducts = myXML.getElementsByTagName('Product');

		for (n=0; n < myproducts.length; n ++){
			productImage = myproducts[n].getAttribute('S7FamilyImage');
			productName  = myproducts[n].getAttribute('ProductName');
			productDesc  = myproducts[n].getAttribute('ProductDesc');
			firstImage   = myproducts[0].getAttribute('S7FamilyImage');
			itemID	 	 = myproducts[n].getAttribute('ItemID');
			
			tempproduct = tempproduct + "<li>"
			tempproduct = tempproduct + "<a onclick=\"swapImg('" + productImage + "');swapContent('" + n + "');\" title='" + productName + "' style='cursor:pointer'>"
			tempproduct = tempproduct + "<img src='http://s7ondemand1.scene7.com/is/image/frontgate/" + productImage + "?wid=61&hei=61&qlt=80&op_usm=1.0,0.5,0,0&op_brightness=-70' alt='" + productName + "' /></a></li>"
		}

		function imagecarousel_itemLoadCallback(carousel, state) {
			for (var i = carousel.first; i <= carousel.last; i++) {
				if (carousel.has(i)) {
					continue;
				}
				
				if (i > myproducts.length) {
					break;
				}
			}
		};

		jQuery(document).ready(function(){
			document.getElementById("product-carousel").innerHTML = tempproduct;
			swapImg(firstImage);
			swapContent(0);
		});

		jQuery(document).ready(function(){

			// Activate jCarousel
		    function imagecarousel_initCallback(carousel)
		    {
				carousel.stopAuto();
		        // Disable autoscrolling if the user clicks the prev or next button.
		        jQuery('.jcarousel-next').bind('click', function() {
			        carousel.next();
			    });
	
				jQuery('.jcarousel-prev').bind('click', function() {
			        carousel.prev();
			    });
		
			    jQuery('.pause').bind('click', function() {
					carousel.stopAuto();
				});

				jQuery('.play').bind('click', function() {
					carousel.startAuto();
				});

			    // Pause autoscrolling if the user moves with the cursor over the clip.
			    carousel.clip.hover(function() {
		        	carousel.stopAuto();
		        }, function() {
		        	carousel.stopAuto();
		        });
		    };
		
		
		    jQuery('#product-carousel').jcarousel({
				visible: 5,
				scroll: 4,
		        wrap: 'last',
		        animation: 'slow',
		        initCallback: imagecarousel_initCallback,
				itemLoadCallback: imagecarousel_itemLoadCallback
		    });

		});
}

function swapImg(img) {
	document.getElementById('product').style.backgroundImage = "url(http://s7ondemand1.scene7.com/is/image/frontgate/" + img + "?hei=549&wid=549&qlt=70&op_usm=1.0,0.5,0,0&align=0,0)";
}

function swapContent(id) {
	document.getElementById('product_name').innerHTML		 = myproducts[id].getAttribute('ProductName');
	document.getElementById('product_description').innerHTML = myproducts[id].getAttribute('ProductDesc');
	document.getElementById('shop-now').href				 = "/item.jsp?item=" + myproducts[id].getAttribute('ItemID') + "";
}

//function to pull the xml data out of the specified page so it will be ready to use
function getData(referalPage){
	p_tempPage = referalPage;
	//determine if XMLHttpRequest or ActiveXObject is required to use.
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			if (xmlHttp.status == 200){
				//document recieved
				xmlHttpAccepted = true;
				myXML = xmlHttp.responseXML;

				if (p_tempPage == "top10"){
					top10();
				}else{
					top10();
				}
			}else{
				alert("The page failed to load, please try again.");
			}
		}
	}
	xmlHttp.open( "GET", "/text/content/landing/halloweenhaven/xml/CustomerFavorites_HalloweenHaven.xml", true );
	// xmlHttp.open( "GET", "/text/content/landing/halloweenhaven/xml/customerFavorites.xml", true );
	xmlHttp.send(null);

	return xmlHttp;
}

function top10(){
		if (window.p_product == undefined){
			var tempproduct = "";
		}else{
			var tempproduct = "";
		}

		var myproducts = myXML.getElementsByTagName('Products');

		for (var n=0; n < myproducts.length; n ++){
			var productImage = myproducts[n].getAttribute('S7FamilyImage');
			var productName = myproducts[n].getAttribute('ProductName');
			var productLink = myproducts[n].getAttribute('ProductId');
			var productCat = myproducts[n].getAttribute('SubCategoryName');
			var catName = myproducts[n].getAttribute('CategoryName');
			var categoryCat = myproducts[n].getAttribute('CategoryID');
			var categorySubCat = myproducts[n].getAttribute('SubCategoryId');

			if (categorySubCat == "2763") {
				tempproduct = tempproduct + "<div class='favorite'>"
				if (window.p_product == undefined){
					tempproduct = tempproduct + "<div class='arrow'><img src='/text/content/landing/halloweenhaven/images/arrow.gif' alt='" + productName + "' border='0' /></div><div class='item'><a title=\"header=[] body=[<img src='http://s7ondemand1.scene7.com/is/image/frontgate/" + productImage + "?wid=300&qlt=80&op_usm=2.0,0.5,0,0' />] fixedrelx=[-335] fixedrely=[-150] delay=[300] fadespeed=[0.2] fade=[on]\" href='/jump.jsp?itemType=PRODUCT&itemID=" + productLink + "&path=1%2C2%2C2759%2C" + categoryCat + "%2C" + categorySubCat + "'>"
				}else{
					tempproduct = tempproduct + "<div class='arrow'><img src='/text/content/landing/halloweenhaven/images/arrow.gif' alt='" + productName + "' border='0' /></div><div class='item'><a title=\"header=[] body=[<img src='http://s7ondemand1.scene7.com/is/image/frontgate/" + productImage + "?wid=300&qlt=80&op_usm=2.0,0.5,0,0' />] fixedrelx=[-335] fixedrely=[-150] delay=[300] fadespeed=[0.2] fade=[on]\" href='/jump.jsp?itemType=PRODUCT&itemID=" + productLink + "&path=1%2C2%2C2759%2C" + categoryCat + "%2C" + categorySubCat + "'>"
				}
				if (myproducts[n].getAttribute('TopCrop') == null) {
	                tempproduct = tempproduct + "" + productName + "</a></div></div>"
				}else{
	                tempproduct = tempproduct + "" + productName + "</a></div></div>"
	            }
			}
        }
		document.getElementById("top10").innerHTML = tempproduct;
}