var listExpress = Class.create();

listExpress.prototype = {
	initialize: function() {
		this.tabList = new Array();
		this.inputId = 0;
		inputSelect = null;
		initInput = false;
		
		$$('#listChoice a').each(this.addEventChoice.bindAsEventListener(this));
		Event.observe($('zoneList'), 'click',this.onEventClick.bind(this));
		Event.observe($('listdefaultTxt'), 'click',this.onEventClick.bind(this));
		Event.observe($('deleteListExpress'), 'click',this.deleteAll.bind(this));
		
		this.initListeExpressSession();
	},
	
	init:function (){
		this.tabList = new Array();
		this.inputId = 0;
		inputSelect = null;
		initInput = false;
	},
	
	addEventChoice:function (e){
		Event.observe(e, 'click',this.onSelectProduct.bind(this,e));
	},
	
	onSelectProduct:function (e){
		PI_swapImg(e);
		
		if(inputSelect){
			this.checkInput(inputSelect);
		}
		this.removeProduct();

		if(PI_getImgStat(e) == 1){
			this.addProduct({id:this.getId(),value:e.rel,define:true,defaultValue:e.rel,select:e,idSelect:e.id});
			
			this.refresh();
		}
		else{
			this.removeProduct(e.id);
		}
		
	},
	
	addProduct:function (o,pos){
		this[o.id] = o;
		if(pos)
			this.tabList.splice(pos,0,this[o.id]);
		else
			this.tabList.push(this[o.id]);
	},
	
	removeProduct:function (id){
		for(var i=0;i<this.tabList.length;i++){
			if((this.tabList[i].o.value == "")){
				$("listExpressContent").removeChild(this.tabList[i].o);
				this.tabList.splice(i,1);
				break;
			}
			if(this.tabList[i].idSelect == id && id){
				$("listExpressContent").removeChild(this.tabList[i].o);
				this.tabList.splice(i,1);
				break;
			}
		}
		this.update();
	},

	onEventClick:function (){
		inputSelect = null;
		
		this.removeProduct();
		
		this.addProduct({id:this.getId(),value:"",define:false,idSelect:-1},null);
		
		this.refresh();
	},
	
	onNewProduct:function (){
		this.removeProduct();
		
		if(inputSelect){
			this.checkInput(inputSelect);
			
			for(var i=0;i<this.tabList.length;i++){
				if(this.tabList[i].o == inputSelect){
					pos = i+1;
					break;
				}
			}
		}

		this.addProduct({id:this.getId(),value:"",define:false},pos);
		
		this.refresh();
	},
	
	deleteAll:function (){
		this.init();
		this.refresh();

		$$('#listChoice a').each(function(e){
			if(PI_getImgStat(e) == 1) PI_swapImg(e);
		});
		
		this.refreshListSession();
	},

	refreshListSession:function(){
		var url = '/magasin/componentMagasin/refreshListExpress.jsp';
		var listeParameters = 'ipListeExpress=';
		var myAjax = new Ajax.Request(url, {
			onComplete:this.callBackRefresh.bindAsEventListener(this), parameters: listeParameters});
	},
	
	callBackRefresh:function(e){
	},

	refresh:function (){
		$('listExpressContent').innerHTML = "";
		
		for(var i=0;i<this.tabList.length;i++){
			inp = document.createElement("input");
			inp.type = "text";
			inp.id=this.tabList[i].id;
			inp.value=this.tabList[i].value;

			$("listExpressContent").appendChild(inp);

			var oInput = $(this.tabList[i].id);
			this.tabList[i].o = oInput;
			
			Event.observe(oInput, 'blur',this.checkInput.bind(this,oInput));
			Event.observe(oInput, 'focus',this.addEventFocus.bind(this,oInput));
			
			if(oInput.value == ""){
				initInput = true;
				setTimeout(this.focused.bind(this,oInput),80);
			}
		}
		this.update();
		
		return false;
	},
	
	focused:function (oInput){
		try {
			oInput.focus();
		}
		catch (e) {
		}
	},
	
	addEventFocus:function (e){
		inputSelect = e;
		Event.observe(e, 'keydown',this.checkEnterKey.bind(this));

		e.addClassName("PI_on");

		if(!initInput){
			this.removeProduct("");
		}
		else{
			initInput = false;
		}
		
	},
	
	checkEnterKey:function (e){
		if(e.keyCode == 13){
			this.onNewProduct();
		}
	},
	
	checkInput:function (e){
		e.removeClassName("PI_on");
		
		if(this[e.id].define){
			var value = ""+e.value;
			var defaultValue = ""+this[e.id].defaultValue;
			if(value.indexOf(defaultValue) == -1){
				this[e.id].define = false;
				PI_swapImg(this[e.id].select);
				this[e.id].select = null;
				this[e.id].idSelect = -1;
			}
		}
		if(e.value != ""){
			this[e.id].value = e.value;
		}
	},
	
	getId:function (){
		this.inputId++;
		return "n"+this.inputId;
	},
	
	update:function (){
		if($("listExpressContent").offsetHeight<245){
			$('zoneList').style.height = "285px";
		}
		else{
			$('zoneList').style.height = $("listExpressContent").offsetHeight+20+"px";
		}
		
		
	},
	
	validListeExpress:function (){
		$('ipListeExpress').value="";
		for(var i=0;i<this.tabList.length;i++){
			if (this.tabList[i].o.value.length>=3)
			{
				if($('ipListeExpress').value.toUpperCase().indexOf(this.tabList[i].o.value.toUpperCase())==-1){
					if ($('ipListeExpress').value.length>0) $('ipListeExpress').value += "#";
					$('ipListeExpress').value += htmlentities(this.tabList[i].o.value);
				}
			}
		}
		if ($('ipListeExpress').value.length>0)
		{
			$('listExpressForm').submit();
		}
		else
		{
			alert("Vous devez saisir au moins un produit!");
		}
	},
	
	initListeExpressSession:function (){
		var listeSession = $('ipListeExpress').value;
		
		if (listeSession!=null && listeSession!='')
		{
			var listeSessionTab=listeSession.split("#");
			for (var i=0; i<listeSessionTab.length; i++) {
				this.removeProduct();
				this.addProduct({id:this.getId(),value:""+listeSessionTab[i],define:false,idSelect:-1},i);
				this.refresh();
			}
		}
	}
}
