var Product = Class.create({

	id				:0 // - cartitems.id
	,count_total	:0 // - nr curent de produse
	,count_available:0 // - nr. total de produse
	,name			:''
	,type			:1
	,isgift			:0 // - daca produsul este gift (fie produs, fie bon cadeaux)
	,parent_element	:'products'
	,manager:null
	,dragable:null
	,isgiftbox		:0 // - in tab-ul SHIPPING: daca e GIFTBOX
	,baseid			:''
	,baseprefix:''

	,initialize: function(id,quantity,name,type,isgift,isgiftbox) {
		this.id 				= id;
		this.count_total 		= quantity;
		this.count_available 	= quantity;
		this.name 				= name;
		this.type 				= type;
		this.isgift 			= isgift;
		this.isgiftbox 			= isgiftbox ? isgiftbox : 0;
		if(isgiftbox>0){
			this.count_total = 1;
			this.count_available =1;
		}

	}

	,conditiaDeDragabil: function(){
		return (this.isgift>0 && this.type==1) || this.type==2 || this.isgiftbox==1;
	}

	,Render: function(){
		w = '350px';
		//console.log(this.name+': gift='+ this.isgift + ', type=' + this.type);
		var conditia_de_dragabil = this.conditiaDeDragabil();
		this.baseprefix = this.isgiftbox>0 ? 'giftbox_' : 'product_';
		var baseid = this.baseprefix + this.id;
		var css_addon = 'background:#eee;cursor:default;';
		if(conditia_de_dragabil){
			css_addon = "background:#fff;cursor:move;";
		}


		var div_mare = new Element('div',{'id':baseid,'style':css_addon + 'width:'+w+';float:left;position:relative;border:2px solid #ddd;padding:4px;margin-top:10px;'});
		div_mare.addClassName('product');
		var bifa = new Element('img',{'id':'bifa_'+baseid,'src':'img/cartcheck.png','style':'position:absolute;left:-33px;top:-14px;display:none;'});
		//var icon_giftbox = new Element('img',{'style':'float:left;','src':'img/gift.gif'});
		var imgs = this.isgift>0 ? "<img src='img/gift.gif' border='0' style='float:left;padding-right:5px;'>" : "";
		var div1 = new Element('div',{'id':'quantity_'+baseid,'style':'float:left;width:36px;font-size:18px;font-weight:bold;padding-top:3px;'}).update(this.count_available + ' x ');
		var div2 = new Element('div',{'id':'contents_'+baseid,'style':'position:relative;float:left;width:300px;'}).update(imgs + this.name);

		div2.appendChild(bifa);
		div_mare.appendChild(div1);
		div_mare.appendChild(div2);
		$(this.parent_element).appendChild(div_mare);

		if(conditia_de_dragabil){
			this.enableDrag();
			disableSelection($(baseid));
		}else{
			bifa.show();
		}

		this.manager.anythingHappens();
	}

	,enableDrag: function(){
		//console.log('Il facem dragabil');
		var baseid = this.baseprefix+this.id;
		this.dragable = new Draggable(baseid,{revert:true,scroll:window,scrollSensitivity :50});

		this.manager.anythingHappens();
	}
	,disableDrag:function(){
		//console.log('oprim drag-ul');
		this.dragable.destroy();
		this.manager.anythingHappens();
	}
	,UpdateRender:function(){

		var baseid = this.baseprefix + this.id;
		//console.log('UPDATING RENDER');
		//console.log('avail=' + this.count_available);
		$('quantity_'+baseid).update(this.count_available + ' x ');
		//console.log('upd qty');
		if(this.count_available<=0){
			//console.log('before if');
			if(!$('bifa_'+baseid).visible()){
				$('bifa_'+baseid).show();
				$(baseid).setStyle("background-color:#EEEEEE;cursor:default;");
				//console.log('aaa');
				this.disableDrag();
				//console.log('bbb');
				this.manager.disablePlusButtons(this.id,this.isgiftbox);
				//console.log('ccc');
			}
			//console.log('after if');
		}else{
			if($('bifa_'+baseid).visible()){
				$('bifa_'+baseid).hide();
				//console.log('aaaa');
				$(baseid).setStyle("background-color:#FFFFFF;cursor:move;");
				//console.log('bbbb');
				this.enableDrag();
				//console.log('cccc');
				this.manager.enablePlusButtons(this.id,this.isgiftbox);
				//console.log('dddd');
			}
		}
		//console.log('UPDATING RENDER FINIT');
		this.manager.anythingHappens();
		//console.log('UPDATING RENDER END');
	}


});
