Ext.onReady(function(){


/************************************************/
/* 		HOME PAGE SLIDESHOW 			*/
/************************************************/
cartEcommerce = function() {
	var divViewer = false;
	var divContent = false;
	var divTitle = false;
	var divMain = false;
	var formProduct = false;
	var addButton = false;
	var buyButton = false;
	var seeButton = false;
	var hiddeButton = false;
	var updateButton = false;
	var cartData=false;
	var actionData=false;

	this.constructor = function() {
		this.divViewer=Ext.get('popup');
		this.divContent=Ext.get('cartBkg');
		this.divTitle=Ext.get('cartTitle');
		this.divMain=Ext.get('cart');
		this.formProduct=Ext.get('productChoice');
		if(Ext.query("form").length>0){
			this.addButton=Ext.get(Ext.query("form")[0]['add']);
		}
		this.seeButton=Ext.get('btnPanier');
		this.hiddeButton=Ext.get('cartContinue');
		this.buyButton=Ext.get('cartBuy');
		this.updateButton=Ext.get('cartUpdate');
		this.activeBtn();
	};

	this.activeBtn=function(){
		if(Ext.query("form").length>0){
			this.addButton.on('click', this.addGetFade, this);
		}
		this.seeButton.on('click', this.getFade, this);
		this.hiddeButton.on('click', this.getHidde, this);
		this.buyButton.on('click', this.goCart, this);
		this.updateButton.on('click', this.sendXml, this);
	}

	this.addGetFade = function() {
		this.actionData="update";
		this.divTitle.dom.innerHTML="VOTRE COMMANDE";
		Ext.get('popupBkg').setHeight(Ext.get('content').getHeight()+80);
		this.divViewer.fadeIn({callback: this.addGetCart, scope:this, duration:.35});
	};

	this.getFade = function(e) {
		e.stopEvent();
		this.actionData="see";
		this.divTitle.dom.innerHTML="VOTRE COMMANDE";
		Ext.get('popupBkg').setHeight(Ext.get('content').getHeight()+80);
		this.divViewer.fadeIn({callback: this.getCart, scope:this, duration:.35});
	};

	this.getCart = function() {
		this.divViewer.stopFx();
		this.getContent();
	};

	this.addGetCart = function() {
		this.divViewer.stopFx();
		this.addContent();
	};

	this.getContent = function(){
		this.xmlData=new Array('mmm', 'ppp');
		Ext.Ajax.request({
		url: './libs/php/cart.php',
		success: this.parseXml,
		failure: function () {
			alert('Ajax Fail !');
		},
		params: { xml: 'true'},
		scope:this
		});
	}

	this.addContent = function(){
		this.seeButton.addClass('full');
		this.seeButton.removeClass('empty');
		this.xmlData=new Array('mmm', 'ppp');
		Ext.Ajax.request({
		url: './libs/php/cart.php',
		success: this.parseXml,
		failure: function () {
			alert('Ajax Fail !');
		},
		params: { xml: 'true' , ID: Ext.get(Ext.query("form")[0]['ID']).dom.value, ID2: Ext.get(Ext.query("form")[0]['IDSelected']).dom.value, QTT: Ext.get(Ext.query("form")[0]['quantiteSelected']).dom.value},
		scope:this
		});
	}

	this.parseXml=function(response){
		this.divMain.dom.innerHTML="";
		this.cartData='<div id="allItems"><form name="cartItems"  id="cartItems"method="POST">';
		var total=0;

		for (var counter=0; counter<response.responseXML.getElementsByTagName('product').length; counter++) {
			Ensemble=response.responseXML.getElementsByTagName('product').item(counter);
			this.cartData+='<div class="cartItem">';
			this.cartData+='<input type="hidden"  name="ID_'+counter+'" value="'+Ensemble.getElementsByTagName('ID').item(0).firstChild.nodeValue+'" />';
			this.cartData+='<input type="hidden"  name="ID2_'+counter+'" value="'+Ensemble.getElementsByTagName('ID2').item(0).firstChild.nodeValue+'" />';
			this.cartData+='<div class="infoItem">';
			this.cartData+='<div class="titleItem">'+Ensemble.getElementsByTagName('titre').item(0).firstChild.nodeValue+'</div>';
			if(Ensemble.getElementsByTagName('intitule').item(0).firstChild.nodeValue!=""){
				this.cartData+='<div class="subTitleItem">'+Ensemble.getElementsByTagName('intitule').item(0).firstChild.nodeValue+'</div>';
			}
			this.cartData+='</div>';
			this.cartData+='<div class="qttItem"> x <input type="text" class="littleInput" name="QTT_'+counter+'" value="'+Ensemble.getElementsByTagName('qtt').item(0).firstChild.nodeValue+'" /></div>';
			if(Ensemble.getElementsByTagName('promo').length>0){
				this.cartData+='<div class="prixItem"><span>'+Ensemble.getElementsByTagName('prix').item(0).firstChild.nodeValue+' &euro; </span>'+ Ensemble.getElementsByTagName('promo').item(0).firstChild.nodeValue+' &euro;</div>';
				total+=(Ensemble.getElementsByTagName('promo').item(0).firstChild.nodeValue*Ensemble.getElementsByTagName('qtt').item(0).firstChild.nodeValue);
			}else{
				this.cartData+='<div class="prixItem">'+Ensemble.getElementsByTagName('prix').item(0).firstChild.nodeValue+' &euro;</div>';
				total+=(Ensemble.getElementsByTagName('prix').item(0).firstChild.nodeValue*Ensemble.getElementsByTagName('qtt').item(0).firstChild.nodeValue);
			}
			this.cartData+='<div class="spacer"></div>';
			this.cartData+='</div>';
		}
		this.cartData+='<input type="hidden"  name="totalItems" value="'+counter+'" />';
		this.cartData+='</form></div>';
		this.cartData+='<div id="cartTotal">TOTAL : '+ (Math.round(total*100)/100) +' &euro;</div>';
		if(total==0){
			this.seeButton.addClass('empty');
			this.seeButton.removeClass('full');
		}
		this.createNextDiv();
	}

	this.createNextDiv=function(){
		this.divMain.createChild(this.cartData);
		this.resizeDiv();
	};

	this.resizeDiv=function(){
		this.divContent.scale(this.divContent.getWidth(), Ext.get('cartMsg').getHeight() , {callback:this.animDiv, scope:this, easing: 'easeIn',duration: .5});

	}

	this.animDiv=function(){
		this.divContent.stopFx();
		Ext.get('cartMsg').setStyle('visibility', 'visible');
	}

	this.getHidde=function(){
		this.divMain.dom.innerHTML="";
		this.divContent.setHeight(120);
		Ext.get('cartMsg').setStyle('visibility', 'hidden');
		this.divViewer.fadeOut();
	}

	this.goCart=function(){
		document.location.href="cart.php";
	}

	this.sendXml = function(){
		Ext.get('cartMsg').setStyle('visibility', 'hidden');
		this.divContent.scale(this.divContent.getWidth(), 120 , {callback:this.updateContent, scope:this, easing: 'easeIn',duration: .5});
	}

	this.updateContent = function(){
		this.divViewer.stopFx();
		this.xmlData=new Array('mmm', 'ppp');
		Ext.Ajax.request({
		url: './libs/php/cart.php',
		success: this.parseXml,
		failure: function () {
			alert('Ajax Fail !');
		},
		form: Ext.get('cartItems'),
		scope:this
		});
	}
};

var cartEcommerce = new cartEcommerce;
cartEcommerce.constructor();
});

