var mycarts = new Hash.Cookie('mycarts', {duration: 0});

window.addEvent('domready', function(){ 
	mycarts.each(function(item,key){
			var s = "<div class='cartot'>"+item.offertitle+"</div>";
			    s += "<div class='cartono'><input type='hidden' name='offerId' value='"+key+"'>促销编号："+item.offerno+"</div>";
			    s += "<div class='cartdel' onclick='delCartOffer(this)' style='cursor:hand'>删除</div>";
			var ci = new Element("div", {
             "class": "offercartitem"
      }).setHTML(s);			

		  $('cart').adopt(ci);
		  truncateContent(".cartot","25");
	});	
	
	$ES('.item').each(function(item){
		item.addEvent("click", function(e) {
			e = new Event(e).stop();
			$("comparemain").style.display="";
			var offertitle = $("offertitle").getText();
			var offerno = $("offerno").getText();			
			var offerId = $("t_offerId").value;
			if ($ES(".offercartitem").length>3){
				alert("一次最多只能比较4个促销信息。");
				return;
			}
			
			var s=false;
			$ES(".offercartitem").each(function(item) {
				if ($E("input",item).value==offerId){
					alert("当前促销已经添加到对比列表。");
					s = true;
				}
			});
			if (s) return;

			var s = "<div class='cartot'>"+offertitle+"</div>";
			    s += "<div class='cartono'><input type='hidden' name='offerId' value='"+offerId+"'>促销编号："+offerno+"</div>";
			    s += "<div class='cartdel' onclick='delCartOffer(this,"+offerId+")' id='"+offerId+"' style='cursor:hand'>删除</div>";

			var ci = new Element("div", {
             "class": "offercartitem"
      }).setHTML(s);			
			
			
			$('cart').adopt(ci);
			truncateContent(".cartot","25");
			var t = "{";
			t += "'offertitle':'"+offertitle+"',"
		    t += "'offerno':'"+offerno+"',"  
		    t += "'offerId':'"+offerId+"'"
		    t += "}";

		    t = eval('('+t+')')
			
			//放入Cookie
			mycarts.set(offerId, t);
		});
	});
});

function delCartOffer(co,offerId){
	mycarts.erase(offerId);
	$(co.parentNode).remove();
}


