var ImoCart= Class.create(); 

ImoCart.prototype={

    initialize: function(options) { 
        this.url = "/index.php?section=cart&screen=receiver"; 
    },
	
	add_item: function(object_id,object_type){
		var a=new Ajax.Request(this.url, {   
			method: 'post',   
			parameters:'action=add_item&object_id='+object_id+'&object_type='+object_type,
			onSuccess: function(transport) {
				$('favorite_'+object_id+'_'+object_type).immediateDescendants().each(function (el){el.remove()});
				new_element_link = Builder.node('a',{href:'javascript:void(0)',onclick:'mycart.delete_item(\''+object_id+'\',\''+object_type+'\')'},["Sterge din favorite"]);
				$('favorite_'+object_id+'_'+object_type).appendChild(new_element_link);
				if($('icon_favorite_'+object_id+'_'+object_type)!=undefined)
					$('icon_favorite_'+object_id+'_'+object_type).src = $('icon_favorite_'+object_id+'_'+object_type).src.substr(0,$('icon_favorite_'+object_id+'_'+object_type).src.lastIndexOf('.'))+'_act'+$('icon_favorite_'+object_id+'_'+object_type).src.substr($('icon_favorite_'+object_id+'_'+object_type).src.lastIndexOf('.'));
				//$('imgfavorite_'+object_id+'_'+object_type).className = 'icon_normal icon_cos3';
				//$$('.numar_oferte_cos').each(function(el){el.innerHTML=transport.responseText;});
			} 
		});
	},
	delete_item: function(object_id,object_type){
		var a=new Ajax.Request(this.url, {   
			method: 'post',   
			parameters:'action=delete_item&object_id='+object_id+'&object_type='+object_type,
			onSuccess: function(transport) {     
				$('favorite_'+object_id+'_'+object_type).immediateDescendants().each(function (el){el.remove()});
				new_element_link = Builder.node('a',{href:'javascript:void(0)',onclick:'mycart.add_item(\''+object_id+'\',\''+object_type+'\')'},["Adauga in favorite"]);
				$('favorite_'+object_id+'_'+object_type).appendChild(new_element_link);
				//$('imgfavorite_'+object_id+'_'+object_type).className = 'icon_normal icon_cos3 icon_gray';
				//if($('cos_item_'+object_id+'_'+object_type)) $('cos_item_'+object_id+'_'+object_type).fade();
				//$$('.numar_oferte_cos').each(function(el){el.innerHTML=transport.responseText;});
				if($('icon_favorite_'+object_id+'_'+object_type)!=undefined)
					$('icon_favorite_'+object_id+'_'+object_type).src = $('icon_favorite_'+object_id+'_'+object_type).src.substr(0,$('icon_favorite_'+object_id+'_'+object_type).src.lastIndexOf('_act'))+$('icon_favorite_'+object_id+'_'+object_type).src.substr($('icon_favorite_'+object_id+'_'+object_type).src.lastIndexOf('.'));
			} 
		});
	},
	
	set_callback:function(functor){
		this.callback_action=functor;
	},
	
	fill_cart_count:function(text_total){
		//$$('.numar_oferte_cos').each(function(el){el.innerHTML=text_total;});
	},
	delete_favorit:function(type,id){
		$('delete_'+type+'_'+id).innerHTML = '';
		new_img = Builder.node('img',{src:'/images/delete_btn.gif',onclick:'mycart.delete_item_spec(\''+id+'\',\''+type+'\')'});
		$('delete_'+type+'_'+id).appendChild(new_img);
		new_img.style.cursor='pointer';
	},
	delete_item_spec:function(object_id,object_type){
		var a=new Ajax.Request(this.url, {   
			method: 'post',   
			parameters:'action=delete_item&object_id='+object_id+'&object_type='+object_type,
			onSuccess: function(transport) {
				$('item_'+object_type+'_'+object_id).parentNode.removeChild($('item_'+object_type+'_'+object_id));
			}
		});
	}
}

mycart=new ImoCart();

