var GiftBox = Class.create({

	id:0
	,addressid:0
	,products:null// typeOf=ProductInAddress
	,parent_element:'addresses'
	,manager:null
	,did:''
	,empty_text:'Glissez les produits ici pour les ajouter à ce cadeau'
	,eth1:"<font style='color:#ccc;font-size:11px;'>"
	,eth2:"</font>"

	,initialize: function(id2) {
		this.id = id;
		this.products = new Array();
		this.did = "dropable_address_" + this.id;
	}


	,addProduct: function(productid,quantity){
		//console.log("Adding productid="+productid+" in quantity="+quantity+" in me (id="+this.id+")");
		var productinaddress = new ProductInAddress(productid,this.id,quantity);
		productinaddress.parent_element = 'contents_'+this.did;
		productinaddress.manager = this.manager;
		productinaddress.address = this;
		productinaddress.product = manager.getProduct(productid);
		this.products.push(productinaddress);
		this.products[this.products.length-1].Render();
		this.products[this.products.length-1].modifyMainQuantityOnProduct();
		//this.updateEmptyText();

		this.manager.anythingHappens();
	}

	,unlinkProduct: function(productid){
		for(i=0;i<this.products.length;i++)
			if(this.products[i].productid==productid){
				this.products[i].unRender();
				this.products.splice(i,1);
				this.manager.anythingHappens();
				return true;
			}
		this.manager.anythingHappens();
		return false;
	}

	,Render: function(){
		//console.log("DID="+this.did);
		var caption = this.city + (this.address1.trim()!='' ? ('<br>' + this.address1):'') + (this.address2.trim()!='' ? ('<br>' + this.address2):'') + (this.zip.trim()!='' ? ('<br>' + this.zip):'');

		var div_mare = new Element('div',{'id':this.did,'style':'float:right;width:340px;padding:10px;border:2px solid #ddd;margin-top:10px;'});
		var div1 = new Element('div',{'id':'actualaddress_'+this.did,'style':'float:left;font-size:12px;'}).update(caption);
		var div2 = new Element('div',{'id':'contents_'+this.did,'style':'float:left;width:100%;border-top:1px solid #eee;padding-top:3px;'}).update();
		var div_empty = new Element('div',{'id':'emptytext_'+this.did,'style':'float:left;font-size:12px;'}).update(this.eth1+this.empty_text+this.eth2);

		//-gift
		var div3 = new Element('div',{'id':'isgift_div_'+this.did,'style':'position:relative;float:left;border:0px solid red;width:100%;' + (this.manager.allow_giftables?"":"display:none;")});
		var checkbox = new Element("input",{'id':'isgift_checkbox_'+this.did,'type':'checkbox'});
		checkbox.setStyle("position:relative;float:left;");
		checkbox.observe('click',this.onGiftClick.bindAsEventListener(this));
		var label = new Element('label',{'for':'isgift_checkbox_'+this.did}).update("Ecrire un message");
		label.setStyle('position:relative;float:left;margin-top:2px;');

		var div3message = new Element('div',{'id':'isgift_div_message_'+this.did,'style':'display:none;position:relative;float:left;border:0px solid red;width:100%;'});
		//var label2 = new Element('label',{'for':'isgift_checkbox_'+this.did}).update("You can fill in a message for the gift:");
		//label2.setStyle('position:relative;float:left;margin-top:2px;');

		var textarea = new Element('textarea',{'id':'isgift_message_'+this.did});
		textarea.setStyle('width:343px;height:37px;position:relative;float:left;');

		//div3message.appendChild(label2);
		div3message.appendChild(textarea);


		//-gift message

		div3.appendChild(checkbox);
		div3.appendChild(label);
		div3.appendChild(div3message);

		div_mare.appendChild(div1);
		div_mare.appendChild(div3);
		div_mare.appendChild(div2);
		div_mare.appendChild(div_empty);
		$(this.parent_element).appendChild(div_mare);
		Droppables.add(this.did,{
			accept: 'product'
			,onDrop: this.dropSuccess.bindAsEventListener(this)

		});
		this.manager.anythingHappens();
	}

	,onGiftClick: function(){
		if($('isgift_checkbox_'+this.did).checked)
			$('isgift_div_message_'+this.did).show();
		else
			$('isgift_div_message_'+this.did).hide();
	}

	,dropSuccess: function(element){
		var tokens1 = element.id.split('_');
		var tokens2 = this.did.split('_');
		var productid = parseInt(tokens1[1].trim());
		var addressid = parseInt(tokens2[2].trim());
		available = this.manager.getProductRemaining(productid);
		if(available>0){
			ahi = this.alreadyHasIt(productid);
			if(ahi!==false){
				ahi.plusQuantity();
			}else{
				manager.assignProductToGiftBox(productid,addressid);
			}
		}else{
			alert('Vous n\'avez plus de produit disponible dans votre panier!');
		}
		this.manager.anythingHappens();
	}

	,alreadyHasIt: function(productid){
		for(i=0;i<this.products.length;i++)
			if(this.products[i].productid==productid){
				return this.products[i];
			}
		return false;
	}
	,updateEmptyText: function(){
		//console.log("ProdusE:"+this.products.length + ' inh='+$('contents_'+this.did).innerHTML);
		if(this.products.length<=0){
			//console.log("adaugam textul gol");
			$('emptytext_'+this.did).show();
		}else if(this.products.length==1){
			//console.log("scoatem textul gol");
			$('emptytext_'+this.did).hide();
		}
		this.manager.anythingHappens();
	}

	,enablePlusButton: function(productid){
		for(var i=0;i<this.products.length;i++)
			if(this.products[i].productid==productid)
				this.products[i].enablePlusButton();
	}

	,disablePlusButton: function(productid){
		for(var i=0;i<this.products.length;i++)
			if(this.products[i].productid==productid)
				this.products[i].disablePlusButton();
	}

});
