
// Example:
// alert( readCookie("myCookie") );
var promo=new Date().getTime();
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  //alert(cookieValue);
  return cookieValue;
}
// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  //if(hours != null)
  //{
    expire = new Date((new Date()).getTime() + 10 * 3600000);
    expire = "; expires=" + expire.toGMTString();
  //}
  document.cookie = name + "=" + escape(value) + expire + "; path=/;";
}

var lastScrollY = 0 ;
var scrollNum = 0 ;
/*
function heartBeat()
{
	diffY = document.body.scrollTop + document.body.clientHeight - document.getElementById("float").scrollHeight - 2;

	if(scrollNum != 1){
		document.all.float.style.display = "none" ;
	}

	if( scrollNum == 0 && document.all.float.style.pixelTop == (document.body.scrollTop + document.body.clientHeight - document.getElementById("float").scrollHeight - 2) ){
		scrollNum = 1;
		//alert("0")
		document.all.float.style.pixelTop = 0; 
		lastScrollY = 0 ;
		document.all.float.style.display = "" ;
	}

		percent =0.1*(diffY-lastScrollY);
		if( percent > 0 ) {
			percent = Math.ceil(percent);
		}else{
			percent = Math.floor(percent);
		}
		document.all.float.style.pixelTop += percent;
		lastScrollY = lastScrollY + percent;

}
*/
//window.setInterval("heartBeat()",1);


function hide(){
  obj=document.getElementById("float");
  obj.style.display='none';
}

function show(){

  obj=document.getElementById("float");
  //position:absolute;right:1px;top:100;filter:alpha(opacity=50);z-index:99;width:140;
  obj.style.position='absolute';
  obj.style.right='1px';
  obj.style.top='100px';
  obj.style.filter='alpha(opacity=50)';
  //obj.style.z-index='99';
  obj.style.width='180';
  obj.style.display='';
}
function redraw(promo)
{   
	$("float").style.display = "" ;
    tmp=readCookie("pro_str"); 

	item_arr=tmp.split("###");

	floatstr = '<table width="170" border="0" cellspacing="0" cellpadding="0">';
	floatstr += '<form method="post" action="../../newcar/compare/?P='+promo+'" name="compare" target="_blank" style="margin:0px;">';
	floatstr += '<tr>';
	floatstr += '<td height="28" align="right" background="../../images/dbnew_03.gif"><table width="160" border="0" cellspacing="0" cellpadding="0">';
	floatstr += '<tr>';
	floatstr += '<td align="left" class="font14px fontbold fontgrey2">车型对比框</td>';
	floatstr += '<td width="45" align="center"><img src="../../images/dbnew_13.gif" width="13" height="13"  style="cursor: pointer;" onclick="return hide()"></td>';
	floatstr += '</tr>';
	floatstr += '</table></td>';
	floatstr += '</tr>';
	floatstr += '<tr>';
	floatstr += '<td align="center" style="border:1px solid #D2D2D2;background-color:#FFF;" id="compare_bar">';

	if (!tmp){
		floatstr += "请添加对比车型";		
		//return;
	}else{
		floatstr += '<table width="150" border="0" cellspacing="0" cellpadding="0" >';
		floatstr += '<tr>';
		floatstr += '<td height="27" align="left" class="fontgrey1">已添加' + (item_arr.length-1) + '款车型</td>';
		floatstr += '<td width="26" align="center">';
		floatstr += '<a class="fontred" href="javascript:;" onclick="empty();">清空</a>';
		floatstr += '</td>';
		floatstr += '</tr>';

		for(i=0;i<=item_arr.length;i++) {
			if (item_arr[i]){
			tmp_info_arr=item_arr[i].split("|");
			pid  = tmp_info_arr[0];
			names= tmp_info_arr[1];
				if (pid && names){
					ft  = '<tr>';
					ft += '<td height="27" align="left" class="upline">';
					ft += '<div style="width:124px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"><a href="../../newcar/html/?' + pid.substr(3) +'.html" title="' + names + '" class="blacknl" target="_blank">' + names + '</a></div>';												      								
					ft += '</td>';
					ft += '<td align="center" class="upline">';
					ft += '<a href="#" onclick="del(\''+pid+'\',\''+names+'\',\'0\');return false;"><img src="../../images/dbnew_07.gif" width="13" height="13" border="0"></a>';
					ft += '<input type="hidden" name="pro_id[]" value="'+pid+'"></td>';
					ft += '</tr>';	
					floatstr = floatstr+ft;
				}	
			}	
		}
	
		floatstr += "<input type='hidden' name='subcatid' value='"+readCookie("subcat")+"'></table>";
	}
	floatstr += '</td>';
	floatstr += '</tr>';
	floatstr += '<tr>';
	floatstr += '<td><img onclick="comparecheck();" style="cursor:pointer;" src="../../images/dbnew_10.gif" width="170" height="28" border="0"></td>';
	floatstr += '</tr>';
	floatstr += '</form></table>';
	$("float").innerHTML = floatstr;
}
function heartBeat()
{
	//diffY = document.body.scrollTop + document.body.clientHeight - document.getElementById("float").scrollHeight - 2;
	diffY = document.body.scrollTop + 160  - 2;

	if(scrollNum != 1){
		document.getElementById("float").style.display = "none" ;
	}
	//alert("变量document.getElementById('float').style.pixelTop"+document.getElementById("float").style.pixelTop);
	if( scrollNum == 0 )
	{
		scrollNum = 1;
		//alert("条件2成立");
		document.getElementById("float").style.pixelTop = 0; 
		lastScrollY = 0 ;
		document.getElementById("float").style.display = "" ;
	}

		percent =0.1*(diffY-lastScrollY);
		if( percent > 0 ) {
			percent = Math.ceil(percent);
		}else{
			percent = Math.floor(percent);
		}
		//pixelTop是IE专有属性
		//document.getElementById("float").style.pixelTop += percent;
		//style.top是IE和Firefox共同都支持属性
		document.getElementById("float").style.top= lastScrollY
		lastScrollY = lastScrollY + percent;
		//alert(document.getElementById("float").style.pixelTop);
		

}

window.setInterval("heartBeat()",1);

function empty(){
    var f1 = document.plist;
	for( var i=0;i<f1.elements.length;i++)
	{
		if(f1.elements[i].type=="checkbox") {
			f1.elements[i].checked=false;
		}
    }
   writeCookie("subcat","");
   writeCookie("pro_str","");
   redraw();

   //window.location="search_good.jsp";
}
function check(chkradio,subcatid,pid,names,promo){
    var f1 = document.plist;
    var chkID =document.getElementById("compare"+pid);
    var i=chkradio;
	if(f1.elements.length>1)
	{
		if(chkradio.checked)
		{
		add(subcatid,pid,names,promo);        
		}
		else
		{
		del(pid,names,promo);
		}
	}
	else
    {
		if(f1.compare.checked)
		{
		add(subcatid,pid,names,promo);        
		}
		else
		{
		del(pid,names,promo);
		}
		//alert("对不起，该页只有一个产品时不能比较！");
        //chkID.checked =false;
	}
}
function comparecheck(){
    obj=document.compare;
	mark=0;
	for(k=0;k<obj.elements.length;k++)
        {
	  if (obj.elements[k].name=="pro_id[]"    &&  obj.elements[k].value!="")
          {
	    //alert(obj.elements[k].name+"===第"+k+"个元素==="+obj.elements[k].value);
            if (obj.elements[k].value.indexOf("undefined")!=(-1))
            {
		   obj.elements[k].value=obj.elements[k].value.replace("undefined","");
            }
	    mark++;
	  }
	}
	if(mark<2) {
		//alert ('请至少选择两款产品进行对比!');
		alert ('系统提示：请至少选择两款产品进行对比！！！\r\n选择方法：点击对应产品的“选框”即可添加该产品到产品对比篮。 ');
	}
        else 
        {
            obj.submit();
	}
}

//pro_str:产品字符串
//subcat :子类字符串
function add(subcatid,pid,names,promo){
  var chkID =document.getElementById("compare"+pid);
  //检查是否为相同的子类 
  subcat_id_change(subcatid,pid,names) 
  //读出产品串后用函数返回一个数组 
  tmp=readCookie("pro_str"); 
  //检查数组的个数（即选择产品的个数）
  tmp_arr=stringToArray(tmp,"###");
  if (tmp_arr.length<6)
  {
     //看是否有相同的产品已被选择
	 //alert(tmp+"=="+pid+"==="+tmp.indexOf(pid));
	 if (tmp.indexOf(pid)!=-1)
        {
	    alert("（"+names+"）已经选择了！！！");
            chkID.checked =false;
	}
        else
        {
            writeCookie("pro_str",tmp+pid+"|"+names+"###"); 
        }
  }
  else
  {
    alert("对不起，您只能选择五款同类产品比较！！！");
    chkID.checked =false;
  }
  
   redraw(promo);
}
//该函数功能是 
function subcat_id_change(subcat,id,name){
  //alert("函数参数subcatid="+subcat);
  //change=false;

  tmp_subcat=readCookie("subcat");
  if (tmp_subcat==null || tmp_subcat==""){
    //还没有选择产品呢！！！
	writeCookie("subcat",subcat);
	//alert(readCookie("zol_pro_cmp_subcat"));
  } else {
	writeCookie("subcat",subcat);
  }
  /*else if (subcat != tmp_subcat){
    //选择另类产品 
	if (confirm("您选择的产品和以前选择的产品不是同类产品，无法比较,确实要这样做吗？（单击确定，将清掉以前选择的产品）")){
        var f1 = document.plist;
        //var chkID =document.getElementById("compare"+pid);
	for( var i=0;i<f1.elements.length;i++)
	{ 
            if(f1.compare[i].value!=id){  
                f1.compare[i].checked=false;
            }   
        }
	  writeCookie("subcat",subcat);
	  writeCookie("pro_str","");
	  //change=true;
	}
	//end of if 
  }*/
  //return change;
}
//用指定的字符串div切开str字符串后返回一个数组 
function stringToArray(str,div){
  objArr=str.split(div);
  return objArr;
}
function del(pid,name,promo){
  tmp=readCookie("pro_str"); 
  if($('compare'+pid).type=="checkbox") {
	$('compare'+pid).checked=false;
  }
  //alert(tmp);
  //alert(pid+"|"+name+"###");
  //alert(tmp.replace(pid+"|"+names+"###"));
  writeCookie("pro_str",tmp.replace(pid+"|"+name+"###",""));
  tmp=readCookie("pro_str"); 
  //如果没有产品了，自动清空subcat
  if (tmp=="" || tmp==null){
    writeCookie("subcat","");
  }
  redraw(promo);
}
redraw(promo);
