lastScrollY = 0;
var floatid = "curfloat";

function drag(o,s)
{
 if (typeof o == "string") o = document.getElementById(o);
 o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
 o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
 o.orig_index = o.style.zIndex;
  
 o.onmousedown = function(a)
 {
  this.style.cursor = "move";
  this.style.zIndex = 10000;
  var d=document;
  if(!a)a=window.event;
  var x = a.clientX+d.body.scrollLeft-o.offsetLeft;
  var y = a.clientY+d.body.scrollTop-o.offsetTop;
  //author: www.longbill.cn
  d.ondragstart = "return false;"
  d.onselectstart = "return false;"
  d.onselect = "document.selection.empty();"
    
  if(o.setCapture)
   o.setCapture();
  else if(window.captureEvents)
   window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);

  d.onmousemove = function(a)
  {
   if(!a)a=window.event;
   o.style.left = a.clientX+document.body.scrollLeft-x;
   o.style.top = a.clientY+document.body.scrollTop-y;
   o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
   o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
  }

  d.onmouseup = function()
  {
   if(o.releaseCapture)
    o.releaseCapture();
   else if(window.captureEvents)
    window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
   d.onmousemove = null;
   d.onmouseup = null;
   d.ondragstart = null;
   d.onselectstart = null;
   d.onselect = null;
   o.style.cursor = "normal";
   o.style.zIndex = o.orig_index;
  }
 }
 
 if (s)
 {
  var orig_scroll = window.onscroll?window.onscroll:function (){};
  window.onscroll = function ()
  {
   orig_scroll();
   o.style.left = o.orig_x + document.body.scrollLeft;
   o.style.top = o.orig_y + document.body.scrollTop;
  }
 }
}

function floating() {
	//alert(lastScrollY);
	var floatobj = document.getElementById(floatid);
	diffY = document.body.scrollTop;
	percent =.2*(diffY-lastScrollY);
	if(percent>0){
		percent = Math.ceil(percent);
	}else{
		percent = Math.floor(percent);
	}
	floatobj.style.pixelTop+= percent;
	lastScrollY = lastScrollY+percent;
}
window.setInterval("floating()",1);
document.write("<div id='curfloat' style='left:5px;POSITION:absolute;TOP:320;FILTER:ALPHA(opacity=80);z-index:99;width:120;' align='center'><iframe style='position:absolute;z-index:-1;width:87;height:100%;top:0;left:0;scrolling:no;' frameborder='0' src='about:blank'></iframe><img src='images/compare.gif' width='87' height='23' alt='点击比较' onclick='comchk();' style='cursor:pointer;'><br /><div id='com_item'></div></div>");

var sel = new Array();
var sel_num = 0;

//Cookie存储对比数据begin
function GetCookieVal (offset){
	var endstr = document.cookie.indexOf (";", offset);
  	if (endstr == -1)
    	endstr = document.cookie.length;
  	return unescape(document.cookie.substring(offset, endstr));

}

function GetCookie (name){
  	var arg = name + "=";
  	var alen = arg.length;
  	var clen = document.cookie.length;
  	var i = 0;
  	while (i < clen){
    		var j = i + alen;
    		if (document.cookie.substring(i, j) == arg)
      			return GetCookieVal (j);
    		i = document.cookie.indexOf(" ", i) + 1;
    		if (i == 0) break; 
  	}
  	return null;
}

function SetCookie (name,value,expires,path,domain,secure){
  	document.cookie = name + "=" + escape (value) +
    	((expires) ? "; expires=" + expires.toGMTString() : "") +
    	((path) ? "; path=" + path : "") +
    	((domain) ? "; domain=" + domain : "") +
    	((secure) ? "; secure" : "");
         return value;
}

function DeleteCookie (name){
	if(GetCookie(name) != null)
	{
		SetCookie(name,"");
	}
}

//Cookie存储对比数据end 

function cookie_content(){
	i = 0;
	content = "";
	for(key in sel){
		if(i == 0)
		{
			content += key + "[" + sel[key] + "]";
		}
		else
		{
			content += "," + key + "[" + sel[key] + "]";
		}
		i++;
	}
	//alert(content);
	return content;
}

function add(id,nm) {
	if(!sel[id]){
		if(sel_num >= 4) 
		{
			alert('您好,为了保证对比查看效果您最多只能选择4个产品参加比较.');
		}
		else 
		{
			sel_num++;
			sel[id] = nm;
		}
	}
	else 
	{
		alert( nm+' 已经被选择,请不要重复添加同一产品(*^__^*');
	}
	SetCookie("HFHOUSE_TMP_COMPARE_COOKIE",cookie_content(),null,"/");
	draw();
}

function del(id) {
	sel2 = new Array();
	for(key in sel){
		if(id!=key) 
		{
			sel2[key] = sel[key];
		}
	}
	sel = sel2;
	sel_num--;
	SetCookie("HFHOUSE_TMP_COMPARE_COOKIE",cookie_content(),null,"/");
	draw();
}
function draw() {
	out = '';
	for(key in sel) {
		out += "<font color=#FFAD5D size=1><b>|</b></font><br><input type=button onclick=\"del('"+key+"')\" value='"+sel[key]+"' style='border:1px solid #FFAD5D ;background-color:#ffffff;height:24;width:120;cursor:pointer;color:'black';'><br>";
	}
	$("com_item").innerHTML = out;
}

function comchk() {
	if(sel_num < 2) {
		alert('您好,您至少需选择两个产品才能进行对比.')
	}else {
		out = '';
		i=0;
		for(key in sel){
			++i;
			out += key+'__';
			//out +=key+'&';
			
		}
		out = "proId=" + out;
		out = out.replace(/\w{2}$/,"");   

		var str = "propk.php?"+out;
		window.open(str);
	}
}

function inni_data()
{
	//alert(GetCookie("HFHOUSE_TMP_COMPARE_COOKIE"));
	var cookie_sel = new Array();
	cookie_str = GetCookie("HFHOUSE_TMP_COMPARE_COOKIE");
	if(cookie_str != "" && cookie_str != null)
	{
		cookie_sel = cookie_str.split(",");
		for( key in cookie_sel)
		{
			//alert(cookie_sel[key]);
			i = cookie_sel[key].indexOf("[");
			j = cookie_sel[key].indexOf("]");
			//alert(cookie_sel[key].substring(0, i));
			//alert(cookie_sel[key].substring(i+1, j));
			sel[cookie_sel[key].substring(0, i)] = cookie_sel[key].substring(i+1, j);
			sel_num++;
		}
		draw();
	}
}
window.onLoad=inni_data();
