
/*********************************** UPDATE Home Page Product Feature Section*****************************************************/
	
	var prodImgs = [];
	var prodID = [];
	var i = 0;
	var currentCard = i;
	var paused = false;
	var pauseName = "Paused";
	var mytime;
	var Dir = 'intial';
	
	
												   
	function init(){
		
		//Uncomment function below to show overlay
		//STFinderPanel();
		
		
		
		
		ProdFeatureObject.startRequest("xml/FeaturedProducts.aspx");
		
		
		RecipeFeatureObject.startRequest("xml/featuredrecipes.aspx");
		
		//ExtrasObject.startRequest("xml/hmExtra.xml");
	}

	
	function refreshData(){
		var $U=YAHOO.util;
		var $E=$U.Event;
		var $D=$U.Dom;
		var $C=$U.CustomEvent;
		if (Dir == 'backward'){
			newCard = getPreCard();
		} else if (Dir == 'intial'){
			newCard = 0;
		} else {
			newCard = getNextCard();
		}
		Dir = 'forward';
		var newDiv = document.createElement("div");
			newDiv.setAttribute("id", "curDiv");
			
		var newA = document.createElement("a");
		newA.href = "RedirectPantry.aspx?id=" + prodID[newCard];
		var sp1 = document.createElement("img");
		//alert(newCard);
		sp1.setAttribute("id", "curImg");
		sp1.src = prodImgs[newCard];
		sp1.setAttribute("border", 0);
		sp1.useMap = "#productmap";
		newDiv.appendChild(sp1);
		
		//newDiv.appendChild(newA);
		
		var newMap = document.createElement("map");
			newMap.name = "productmap";
			newMap.id = "productmap";
		var newArea = document.createElement("area");
			newArea.shape="rect";
			newArea.coords="5,183,383,212";
			newArea.href="RedirectProductCategory.aspx?id="+ prodID[newCard];
			newArea.alt="View Recipes with this product";
			
			var topArea = document.createElement("area");
			topArea.shape="rect";
			topArea.coords="0,0,389,181";
			topArea.href="RedirectPantry.aspx?id=" + prodID[newCard];
			topArea.alt="View Products";
			
		newMap.appendChild(newArea);
		
		newMap.appendChild(topArea);
		
		
		newDiv.appendChild(newMap);
		
		var oldDiv = document.getElementById("curDiv");
		var ProparentDiv = oldDiv.parentNode;
		//var collapseAnim2 = new YAHOO.util.Anim(ProparentDiv, { opacity: {to: 30} }, 0.2 );
		
		if(this.profadeIn&&this.profadeIn.isAnimated()){
			this.profadeIn.stop();
		}
		if(this.profadeOut&&this.profadeOut.isAnimated()){
			this.profadeOut.stop();
		}

		this.profadeOut=new YAHOO.util.Anim(ProparentDiv,{opacity:{from:0.99,to:0}},1.0);
		this.profadeOut.onStart.subscribe(function(){
									
									}
		);
		this.profadeOut.onComplete.subscribe(function(){
											
											ProparentDiv.replaceChild(newDiv, oldDiv);
											profadeIn.animate();
										   }
		);
		this.profadeOut.animate();
		
		this.profadeIn=new YAHOO.util.Anim(ProparentDiv,{opacity:{from:0,to:0.99}},1.0);
		this.profadeIn.onStart.subscribe(function(){
									   
									  }
		);
		
		this.profadeOut.onComplete.subscribe(function(){
											
											currentCard = newCard;
											clearTimeout(mytime)
											if(!paused){
												mytime=setTimeout(refreshData,10000)
											}
										   }
		);
		
		if(this.autoPlay){this.startAutoPlay();}

		} 
		
		function updateFWD(){
			Dir = 'forward';
			refreshData();
		}
		
		function updateBack(){
			Dir = 'backward';
			refreshData();
		}
		function btnPause(){
			if(paused){
				pauseName = "Pause";
				paused = false;
				mytime=setTimeout(refreshData,10000)
				
			} else {
				paused = true;
				pauseName = "Start";
				clearTimeout(mytime);
			}
			var newLink = document.createElement("a");
				newLink.appendChild(document.createTextNode(pauseName));
				newLink.id =  "pausebtn";
				newLink.className =  "pausebtn";
				newLink.href = "javascript:btnPause()"
				var oldDiv = document.getElementById("pausebtn");
				var parentLi = oldDiv.parentNode;
				parentLi.replaceChild(newLink, oldDiv);
			
		}
		
		function getNextCard(){
			if(currentCard >= prodImgs.length-1){
				currentCard = 0;
			} else {
				currentCard=currentCard+1
			}
			return(currentCard);
		}
		function getPreCard(){
			if(currentCard <= 0){
				currentCard = prodImgs.length-1;
			} else {
				currentCard=currentCard-1
			}
			return(currentCard);
		}
														
/*********************************** Home Page Product Feature Section*****************************************************/

var ProdFeatureObject = {

	handleSuccess:function(k){
		this.removeLoaders('loaderdiv');
		this.processResult(k);
		//this.callAlert(k);
	},

	handleFailure:function(k){
		//alert('fail');
		// Failure handler
	},

	processResult:function(k){
		this.parseprodFeature(k);
		
		// This member is called by handleSuccess
	},
	
	
	// parse and then display the data
	parseprodFeature:function(k) {
		var rList = k.responseXML.getElementsByTagName("Name");

		for (var i = 0; i < rList.length; i++) {
			prodImgs.push(rList[i].firstChild.nodeValue);
			prodID.push(rList[i].getAttribute("ID"));
		};
		refreshData();
		mytime=setTimeout(refreshData,10000);
		
		
		
	},
	
	//loading graphic
	loader:function(){

			var loadFragment = document.createDocumentFragment('t');
			var xdivContainer = document.createElement("div");
			var xdivContent = document.createElement("div");
			//give container div a css class
			xdivContainer.id = "loaderdiv";	
			var loadTxt = document.createElement("p");
			loadTxt.appendChild(document.createTextNode("Loading Featured Products"));
			
			xdivContent.appendChild(loadTxt);
			
			var loadImg = document.createElement("img");
			loadImg.src = "images/common/loader.gif";
			xdivContent.appendChild(loadImg);
			//append content to container
			xdivContainer.appendChild(xdivContent);
			//append container to doc object
			loadFragment.appendChild(xdivContainer);
			
			var loadBody = document.getElementById('curDiv');
				//alert(loadBody);
			loadBody.appendChild(loadFragment);

	},
	
		// remove loading grpahuic from the current list
	removeLoaders:function (divNum) {
			var d = document.getElementById('curDiv');
			var olddiv = document.getElementById(divNum);
			d.removeChild(olddiv);		
	},
	
	
	startRequest:function(myURL) {
	   this.loader();
	   YAHOO.util.Connect.asyncRequest("GET", myURL, ProdFeatureCallBack);
	}

};

var ProdFeatureCallBack =
{
	success:ProdFeatureObject.handleSuccess,
	failure:ProdFeatureObject.handleFailure,
	scope: ProdFeatureObject
};




YAHOO.util.Event.addListener(window, "load", init);