YAHOO.namespace("productScroller");
var DOM = YAHOO.util.Dom;
var EVT = YAHOO.util.Event;

YAHOO.productScroller.featuredProduct = function(featuredProductElementID) {
	this.init(featuredProductElementID);
};

YAHOO.productScroller.featuredProduct.prototype = {
	highlightClass : 'outline',
	productNames : new Array(),
	productIds : new Array(),
	curIndex : 1,
	prevIndex : 1,
	
	autoPlay : null,
	scrollDelay : 7000,
	dataUrl : './includes/getproductinfo.php?id=',
	data : null,
	
	init: function(featuredProductElementID) {
		this.data = new Object();
		var me = this;
		var products = DOM.getElementsBy(function(el) { return true; }, 'li', featuredProductElementID);
		
		for(var i = 0; i < products.length; i++) {
			this.productNames[i] = products[i].firstChild.lastChild.nodeValue;
			this.productIds[i] = products[i].firstChild.firstChild.value;
			var count = i + 1;
			ele = products[i].firstChild;

		}
		
		function callbackProductClick1(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollProduct(1), me; }
		EVT.on(DOM.get('featured-product-1').firstChild, 'click', callbackProductClick1);
		function callbackProductClick2(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollProduct(2), me; }
		EVT.on(DOM.get('featured-product-2').firstChild, 'click', callbackProductClick2);
		function callbackProductClick3(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollProduct(3), me; }
		EVT.on(DOM.get('featured-product-3').firstChild, 'click', callbackProductClick3);
		function callbackProductClick4(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollProduct(4), me; }
		EVT.on(DOM.get('featured-product-4').firstChild, 'click', callbackProductClick4);
		
		function callbackNext(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollNext(), me; }
		function callbackPrev(e) { EVT.preventDefault(e); me.stopAutoScroll(); me.scrollPrev(), me; }
		EVT.on('next', 'click', callbackNext);
		EVT.on('prev', 'click', callbackPrev);
		
		this.startAutoScroll();
	},
	
	startAutoScroll: function() {
		if(this.autoPlay !== null)
			return;		
		var me = this;
		this.autoPlay = setInterval(function() { me.scrollNext(); }, this.scrollDelay); //setInterval
	},
	
	stopAutoScroll: function() {
		if(this.autoPlay !== null) {
			clearTimeout(this.autoPlay);
			this.autoPlay = null;
		}
	},
	
	scrollNext: function() {
		var me = this;
		this.prevIndex = this.curIndex;
		this.curIndex++;
		
		this.removeHighlight(this.prevIndex, this.highlightClass);
		if(this.curIndex != 1 && this.curIndex > 4) {
			this.curIndex = 1;
		}
		
		this.addHighlight(this.curIndex, this.highlightClass);
		setTimeout(function() { me.changeFeaturedProduct(me.productNames[me.curIndex-1],me.productIds[me.curIndex-1]); }, 350);
	},
	
	scrollPrev: function() {
		var me = this;
		this.prevIndex = this.curIndex;
		this.curIndex--;
		
		this.removeHighlight(this.prevIndex, this.highlightClass);
		if(this.curIndex != 4 && this.curIndex < 1) {
			this.curIndex = 4;
		}
		
		this.addHighlight(this.curIndex, this.highlightClass);
		setTimeout(function() { me.changeFeaturedProduct(me.productNames[me.curIndex-1],me.productIds[me.curIndex-1]); }, 350);
	},
	
	scrollProduct: function(productIndex) {
		var me = this;
		this.prevIndex = this.curIndex;
		
		if(productIndex < 1) this.curIndex = 4;
		else if(productIndex > 4) this.curIndex = 1;
		else this.curIndex = productIndex;
		
		this.removeHighlight(this.prevIndex, this.highlightClass);
		
		this.addHighlight(this.curIndex, this.highlightClass);
		setTimeout(function() { me.changeFeaturedProduct(me.productNames[me.curIndex-1],me.productIds[me.curIndex-1]); }, 350);
	},
	
	removeHighlight: function(index, nodeClass) {
		DOM.removeClass('featured-product-' + index, nodeClass);
		
		attributes = { opacity: { from: 1, to: 0 } };
		anim = new YAHOO.util.Anim(DOM.get('featuredNewProduct').parentNode, attributes, 0.6, YAHOO.util.Easing.backOut);
		anim.animate();
	},
	
	addHighlight: function(index, nodeClass) {
		DOM.addClass('featured-product-' + index, nodeClass);
	},
	
	changeFeaturedProduct: function(productName,productId) {
		featuredProductDiv = DOM.get('featuredNewProduct');
		var callback = {
			success : this.handleSuccess,
			failure : this.handleFailure,
			argument: { name: productName },
			scope : this
		}
		if(!this.data[this.curIndex])
			var request = YAHOO.util.Connect.asyncRequest('POST', this.dataUrl+ productId, callback, 'productName=' + productName);
		else
			this.updatePage();
			
		attributes = { opacity: { from: 0, to: 1 } };
		anim = new YAHOO.util.Anim(featuredProductDiv.parentNode, attributes, 0.8, YAHOO.util.Easing.easeBoth);
		anim.animate();
	},
	
	updatePage: function() {
		var featuredProductDiv = DOM.get('featuredNewProduct');
		var featuredProductDivChildren = DOM.getChildren(featuredProductDiv);
		var playBtn = DOM.getChildren(featuredProductDivChildren[1])[0];
		featuredProductDivChildren[0].innerHTML = this.data[this.curIndex]['NAME'];
		featuredProductDivChildren[1].childNodes[0].nodeValue  = this.data[this.curIndex]['SHORT_DESCRIPTION']; //desc
		
		featuredProductDiv.previousSibling.href = "product.php?id="+this.data[this.curIndex]['ID']; // productImg link
		featuredProductDiv.previousSibling.firstChild.src = this.data[this.curIndex]['IMAGE_URL']; //productImg
		featuredProductDiv.previousSibling.firstChild.alt = this.data[this.curIndex]['NAME']; //productImg	Name	
	},
	
	handleSuccess: function(o) {
	var str =o.responseText;
		if(o.responseText !== undefined) {
                var data = this.makeObject(str);
                        
			this.data[this.curIndex]			= new Object();
				this.data[this.curIndex]['NAME']		= data.NAME;
				this.data[this.curIndex]['IMAGE_URL']		= data.IMAGE_URL;
				this.data[this.curIndex]['SHORT_DESCRIPTION']	= data.SHORT_DESCRIPTION;
				this.data[this.curIndex]['ID']		= data.ID;
				
				this.updatePage();
		}
	},
	
	
	
	handleFailure: function(o) {
		if(o.responseText !== undefined) {
			alert('An error occurred, Please try again later');
		}
		
		},
		
		makeObject: function(str) {
	var productsObject = new Object();
		var strSplit = str.split("^^");
		for (var i=0; i<strSplit.length; i++)
		{
		 		var strFieldsValues = strSplit[i].split("##");
				var fieldName = strFieldsValues[0];
				var fieldValue = strFieldsValues[1];
				productsObject[fieldName] = fieldValue;
		}
		return productsObject;
	}

};
