/* $Id: compare.js 15469 2008-12-19 06:34:44Z testyang $ */
var Compare = new Object();
var num_on = 8; //可開數量
Compare = {
  add : function(goodsId, goodsName, type)
  {
    //alert(goodsId +"  " + goodsName+ "  " + type);
    var count = 1;
    for (var k in this.data)
    {

      if (typeof(this.data[k]) == "function")
	     continue;
      if (this.data[k].t != type) {
        alert(goods_type_different.replace("%s", goodsName));
	    return;
      }
      count++;
	  	//alert("test " + count);
    }
	
		
    if (this.data[goodsId])
    {
      alert(exist.replace("%s",goodsName));
      return;
    }
    else
    {
		
		if(count > num_on)
		{
			alert(goods_full);
			return;
		}
	
      this.data[goodsId] = {n:goodsName,t:type};
	 // alert(this.data[goodsId]);
    }
	//alert(count);
	
    this.save();
    this.init();
	//alert("save()  ??");
  },
  relocation : function()
  {
	//alert("relocation");
    if (this.compareBox.style.display != "") return;
    var diffY = Math.max(document.documentElement.scrollTop,document.body.scrollTop);

    var percent = .2*(diffY - this.lastScrollY);
    if(percent > 0)
      percent = Math.ceil(percent);
    else
      percent = Math.floor(percent);
    this.compareBox.style.top = parseInt(this.compareBox.style.top)+ percent + "px";

    this.lastScrollY = this.lastScrollY + percent;
		  	
  },
  init : function(){
	//alert("init compare");
    this.data = new Object();
    var cookieValue = document.getCookie("compareItems");
	//var cookieValue = getCookie("compareItems");
	
	//alert(cookieValue);
	
    if (cookieValue != null) {
	  //mars modfix
      //this.data = cookieValue.parseJSON();
		this.data = $.evalJSON(cookieValue);
    }
    if (!this.compareBox)
    {
      this.compareBox = document.createElement("DIV");
      var submitBtn = document.createElement("INPUT");
      this.compareList = document.createElement("UL");
      this.compareBox.id = "compareBox";
      this.compareBox.style.display = "none";
      this.compareBox.style.top = "200px";
	  
	 // this.compareBox.style.z-index = "200";
	  
      this.compareBox.align = "center";
      this.compareList.id = "compareList";
      submitBtn.type = "button";
      submitBtn.value = button_compare;
			this.compareBox.appendChild(this.compareList);
      this.compareBox.appendChild(submitBtn);
      submitBtn.onclick = function() {
	  	
        var cookieValue = document.getCookie("compareItems");
		var obj = $.evalJSON(cookieValue);
	    //var cookieValue = getCookie("compareItems");
		//mars modfix
    	//var obj = cookieValue.parseJSON();
        var url = document.location.href;
        url = url.substring(0,url.lastIndexOf('/')+1) + "compare.php";
        var i = 0;
        for(var k in obj)
        {
          if(typeof(obj[k])=="function")
          continue;
          if(i==0)
            url += "?goods[]=" + k;
          else
            url += "&goods[]=" + k;
          i++;
        }
        if(i<2)
        {
          alert(compare_no_goods);
          return ;
        }
		
		window.open(url);
		
        //document.location.href = url;
      }
      document.body.appendChild(this.compareBox);
    }
    this.compareList.innerHTML = "";
    var self = this;
    for (var key in this.data)
    {
	  	//alert("test");
      if(typeof(this.data[key]) == "function")
		continue;
	
      var li = document.createElement("LI");
	  
      var span = document.createElement("SPAN");
      span.style.overflow = "hidden";
      span.style.width = "100px";
      span.style.height = "30px";
      span.style.display = "block";
      span.innerHTML = this.data[key].n;
      li.appendChild(span);
      li.style.listStyle = "none";
	
      var delBtn = document.createElement("IMG");
      delBtn.src = "themes/bluesky/images/drop.gif";
      delBtn.className = key;
	  
      delBtn.onclick = function()
	  {
        document.getElementById("compareList").removeChild(this.parentNode);
        delete self.data[this.className];
        self.save();
        self.init();
	
      }
	  
      li.insertBefore(delBtn,li.childNodes[0]);
	   
      this.compareList.appendChild(li);
    }
	
	 //alert("now for");
	
	//mars modfix ie6這不進不去
    if (this.compareList.childNodes.length > 0)
    {
	
      this.compareBox.style.display = "";
	  this.timer = window.setInterval(this.relocation.bind(this), 50);

    }
    else
    {
      this.compareBox.style.display = "none";
      window.clearInterval(this.timer);
      this.timer = 0;
    }
  },
  save : function()
  {
	//alert("now save");
    var date = new Date();
	
	//mars test
	//alert(date);
	
    date.setTime(date.getTime() + 99999999);
   // document.setCookie("compareItems", this.data.toJSONString());
	document.setCookie("compareItems",  $.toJSON(this.data));
	// setCookie("compareItems", 123);
	//document.cookie = 1234;
	//alert(this.data.toJSONString());
  },
  lastScrollY : -120
}

/*
function SetCookie(sName, svalue)
{
	documents.cookie = sName + "=" + escape(svalue) + "; ";
}

function GetCookie(sName)
{
	var aCookie = documents.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		var aCrumb = aCookie.split("=");
		if (sName == aCrumb[0])
		return unescape(aCrumb[1]);
	}

}
*/

function SubmitTo(formId, url, target)
{ 
	  window.forms(formId).action = url; 
	  window.forms(formId).target = target; 
	  window.forms(formId).submit; 
} 

