// JavaScript Document
/*

Get an xml service in the form of:

<root success="1">
	<product>
		<brand></brand>
		<name></name>
		<link></link>
		<price></price>
		<image></image>
		<promoid></promoid>
	</product>
</root>

and use .Show(true) to render them into a slider using javascript and ajax

*/

promoSlider = function(divId, xmlService) {
	if (typeof(divId) != 'string' || !divId) {
		alert('promoSlider Error: invalid parameter divId.');
		return false;
	}
	
	if (typeof(divId) != 'string' || !xmlService) {
		alert('promoSlider Error: invalid parameter xmlService.');
		return false;
	}
	
	this.divId = divId;
	this.xmlService = xmlService;
	this.items = Array();
	this.sliderLength = 4;
	this.startingPoint = 0;
	this.http_request = null;
	
	PSE.promoSliderRegister[PSE.promoSliderRegister.length] = {obj : this, div : divId };
};

promoSlider.prototype.Refresh = function() {
	this.http_request = false;
	
	if (window.XMLHttpRequest) {
		this.http_request = new XMLHttpRequest();
		if (this.http_request.overrideMimeType) {
			this.http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {}
		}
	}
	
	if (!this.http_request) {
		alert( 'Failed to create httpRequest.' );
		return;
	}
	
	this.http_request.open( 'POST', this.xmlService, false );
	this.http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.http_request.send(null);
	
	if (this.http_request.status == 200) {
		var root = this.http_request.responseXML.getElementsByTagName('root').item(0);			
		if (root.getAttribute('success') == '1') {
			this.items.length = 0;
			
			var prods = root.getElementsByTagName('product');
			for (var n=0; n < prods.length; n++) {
				
				var brand = '';
				var name = '';
				var image = '';
				var lnk = '';
				var pId = 0;
				
				var tmp = prods.item(n).getElementsByTagName('brand');
				if (tmp.length && tmp.item(0).firstChild) {
					brand = tmp.item(0).firstChild.data;
				}
				
				var tmp = prods.item(n).getElementsByTagName('name');
				if (tmp.length && tmp.item(0).firstChild) {
					name = tmp.item(0).firstChild.data;
				}
				
				var tmp = prods.item(n).getElementsByTagName('image');
				if (tmp.length && tmp.item(0).firstChild) {
					image = tmp.item(0).firstChild.data;
				}
				
				var tmp = prods.item(n).getElementsByTagName('price');
				if (tmp.length && tmp.item(0).firstChild) {
					price = tmp.item(0).firstChild.data;
				}
				
				var tmp = prods.item(n).getElementsByTagName('link');
				if (tmp.length && tmp.item(0).firstChild) {
					lnk = tmp.item(0).firstChild.data;
				}
				
				var tmp = prods.item(n).getElementsByTagName('promoid');
				if (tmp.length && tmp.item(0).firstChild) {
					pId = tmp.item(0).firstChild.data;
				}
				
				this.items[this.items.length] = {Brand : brand, Name : name, Img : image, Price : price, Link : lnk, promoId : pId };
			}
			
		}
		else {
			alert( root.getAttribute('error') );
		}		
	}
	else {
		alert( 'Se ha producido un error enviando la información.\nInténtelo más tarde (' + this.http_request.status + ')' );
	}
};

promoSlider.prototype.Show = function(doRefresh) {
	if (doRefresh) 
		this.Refresh();
	
	var divObj = document.getElementById(this.divId);
	
	if (!divObj) {
		alert('promoSlider Error: invalid HTML element id specified.');
		return;
	}
	
	while (divObj.firstChild) 
		divObj.removeChild(divObj.firstChild);
	
	var right = document.createElement('a');
	right.className = 'rightArrow';
	right.setAttribute('href', "javascript:PSE.slideRight('"+this.divId+"');");
	right.appendChild(document.createTextNode(' '));
	
	var left = document.createElement('a');
	left.className = 'leftArrow';
	left.setAttribute('href', "javascript:PSE.slideLeft('"+this.divId+"');");
	left.appendChild(document.createTextNode(' '));
	
	divObj.appendChild(left);
	
	var maxLength = this.sliderLength;
	if (this.sliderLength > this.items.length)
		maxLength = this.items.length;
		
	for (var n = 0, curItem = this.startingPoint; n < maxLength; n++, curItem++) {
		if (curItem >= this.items.length) 
			curItem = 0;
				
		var curProd = this.items[curItem];
		
		var prodContainer = document.createElement('a');
		prodContainer.className = 'prodContainer';
		if (curProd.promoId == '0') {
			prodContainer.setAttribute('href', curProd.Link);
		}
		else {
			var pNdx = 0;
			for (var a=0; a < promoList.length; a++) {
				if (promoList[a].id == curProd.promoId) {
					pNdx = a;
					break;
				}
			}
			prodContainer.setAttribute('href', 'javascript:setPromo('+pNdx+')');
		}
		
		var prodImg = document.createElement('img');
		prodImg.setAttribute('src', curProd.Img );
		prodImg.setAttribute('width', '60');
		prodImg.setAttribute('height', '60');
		prodContainer.appendChild(prodImg);
		
		var prodName = document.createElement('p');
		prodName.appendChild(document.createTextNode(curProd.Brand));
		prodName.appendChild(document.createElement('br'));
		prodName.appendChild(document.createTextNode(curProd.Name));
		prodContainer.appendChild(prodName);
		
		var prodPrice = document.createElement('p');
		prodPrice.className = 'prodPrice';
		prodPrice.appendChild(document.createTextNode(curProd.Price));
		prodContainer.appendChild(prodPrice);
		
		divObj.appendChild(prodContainer);
	}
	
	divObj.appendChild(right);
};

promoSlider_engine = function() {
	this.promoSliderRegister = Array();
};

promoSlider_engine.prototype = {
	slideLeft : function(divId) {
		var obj = PSE.__findPromoSlider(divId);
		
		if (!obj) {
			alert('promoSlider Error: promoSlider not found on event.');
			return;
		}
		
		obj.startingPoint--;
		
		if (obj.startingPoint < 0)
			obj.startingPoint = obj.items.length-1;
			
		obj.Show(false);
	},
	
	slideRight : function(divId) {
		var obj = PSE.__findPromoSlider(divId);
		
		if (!obj) {
			alert('promoSlider Error: promoSlider not found on event.');
			return;
		}
		
		obj.startingPoint++;
		
		if (obj.startingPoint > obj.items.length-1)
			obj.startingPoint = 0;
			
		obj.Show(false);
	},
	
	__findPromoSlider : function(divId) {
		var objRet = false;
		for (var n = 0; n < PSE.promoSliderRegister.length; n++) {
			if (PSE.promoSliderRegister[n].div == divId) {
				objRet = PSE.promoSliderRegister[n].obj;
				break;
			}
		}
		
		return objRet;
	}
};

PSE = new promoSlider_engine();
