//var button_compare = "進行比拼";
var exist = "您已經選擇了 %s ";
var count_limit_num = 3;
var count_limit = "最多只能選擇"+ count_limit_num +"張";
//var compare_no_goods = "您沒有選定任何車輛 或者 比較的車輛少於 2 架。";

var Voted = new Object();

Voted = {
  add : function(goodsId, goodsName)
  {
	 // alert(goodsName);
	 
    var count = 0;
    for (var k in this.data)
    {
      if (typeof(this.data[k]) == "function" || this.data[k] == "" )
      continue;
      count++;
    }

	if(count >= count_limit_num) {
		alert(count_limit);
		return ;
	}
    
    if (this.data[goodsId])
    {
      alert(exist.replace("%s",goodsName)); 
      return;
    }
    else
    {
     // this.data[goodsId] = {n:goodsName};
	  this.data[goodsId] = goodsName;
    }
	
    this.save();
    this.init();
  },
  relocation : function()
  {
    if (this.voteBox.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.voteBox.style.top = parseInt(this.voteBox.style.top)+ percent + "px";

    this.lastScrollY = this.lastScrollY + percent;
  },
  init : function(){
	  
	  
    this.data = new Array();
	
    var cookieValue = document.getCookie("voteItems");
	var submitBtn = document.getElementById("voteSubmit");
    if (cookieValue != null) {
     // this.data = cookieValue.parseJSON();
	  this.data = cookieValue.split(",");
	  var url = "venetian_ndhand_car_vote.php?keepThis=true";
	  for (var k in this.data){
	  	if(typeof(this.data[k]) == "function" || this.data[k] == "" )
	  		continue;
          url += "&option_id[]=" + k;
	  }
	  submitBtn.href = url+"&TB_iframe=true&modal=true&width=700&height=450";
    }
	else{
		submitBtn.href = "venetian_ndhand_car_vote.php?keepThis=true&TB_iframe=true&modal=true&width=700&height=450";
	}
    if (!this.voteBox)
    {
      this.voteBox = document.getElementById("voteBox");
      this.voteList = document.getElementById("voteList");
	  this.voteBox.style.right = "0px";
	  this.voteBox.style.top = "100px";
	  this.voteBox.style.display = "none";
	  
    }
	
	
    this.voteList.innerHTML = "";
    var self = this;
    for (var key in this.data)
    {
		//alert(key);
      if(typeof(this.data[key]) == "function" || this.data[key] == "" )
        continue;
      var li = document.createElement("LI");
      var span = document.createElement("SPAN");
      span.style.overflow = "hidden";
      span.style.width = "120px";
      span.style.height = "20px";
      //span.style.display = "block";
      span.innerHTML = this.data[key];
      li.appendChild(span);
      li.style.listStyle = "none";
	  li.style.margin="2px";
      var delBtn = document.createElement("IMG");
      delBtn.src = "images/www/racing_girl_photo/close.gif";
	  delBtn.align="right";
      delBtn.className = key;
      delBtn.onclick = function(){
        document.getElementById("voteList").removeChild(this.parentNode);
        delete self.data[this.className];
        self.save();
        self.init();
      }
      li.insertBefore(delBtn,li.childNodes[0]);
      this.voteList.appendChild(li);
    }
    if (this.voteList.childNodes.length > 0 )
    {
      this.voteBox.style.display = "";
      this.timer = window.setInterval(this.relocation.bind(this), 50);
    }
    else
    {
      this.voteBox.style.display = "none";
      window.clearInterval(this.timer);
      this.timer = 0;
    }
  },
  save : function()
  {
    var date = new Date();
    date.setTime(date.getTime() + 99999999);
    //document.setCookie("voteItems", this.data.toJSONString());
	//alert(this.data.join(","));
	document.setCookie("voteItems", this.data.join(","));
  },
  lastScrollY : 0
}


