//添加cookie
function addCookie(objName,objValue,objHours){
	var str = objName + "=" + escape(objValue);
	if(objHours > 0){//为0时不设定过期时间，浏览器关闭时cookie自动消失
	var date = new Date();
	var ms = objHours*3600*1000;
	date.setTime(date.getTime() + ms);
	str += "; expires=" + date.toGMTString();
	}
	document.cookie = str;
	
}

//获取指定名称的cookie的值
function getCookie(objName){
	var arrStr = document.cookie.split("; ");
	//alert(arrStr);
	for(var i = 0;i < arrStr.length;i ++){
		var temp = arrStr[i].split("=");
		if(temp[0] == objName){
			if (temp[1]==""){
				addCookie(objName,"");
			}
			else{
			return unescape(temp[1]);
			}
		 }
	} 
	addCookie(objName,"");
	return "";
}

//删除cookie并删除所有的js数组
function delCookie(objName){
	for ($i=0;con_id.length;$i++){
		con_id.shift(); 
		con_pic.shift(); 
		con_title.shift(); 
		con_short.shift(); 
		con_url.shift(); 
	}
	addCookie(objName,""); 
}

//对比，跳到新的页面
function doCompare(objName) {
	obstr = getCookie(objName).split(",");
	var f=document.createElement('form');
	f.action="/goods_compare.php";
	f.method="get";
	f.target="_blank";
	document.body.appendChild(f);
	if (obstr!="undefined" ){
		if(true){
			for (var i = 0; i < obstr.length; i++) {
				if (obstr[i]!="undefined" && obstr[i]!=""){
					var o = "a"+i;
					var o=document.createElement('input');
					o.type='hidden';o.name='goods_ids[]';
					o.value=obstr[i];
					f.appendChild(o);
				}
			}
		}
		f.submit();
	}
}

//移除单个对比框
function clear_compare(str,val,url,pic,title,shorttitle){
	if (document.getElementById("goods"+val)){
		document.getElementById("goods"+val).checked = false;
	}
	if(con_id.chec(val)){
		con_id.chec(val);
	}else {
		con_id[con_id.length] = val;
	}
	if(con_pic.chec(val+pic)){
		con_pic.chec(val+pic);
	}else {
		con_pic[con_pic.length] = val+pic;
	}
	if(con_title.chec(val+title)){
		con_title.chec(val+title);
	}else {
		con_title[con_title.length] = val+title;
	}
	if(con_short.chec(val+shorttitle)){
		con_short.chec(val+shorttitle);
	}else {
		con_short[con_short.length] = val+shorttitle;
	}
	if(con_url.chec(val+url)){
		con_url.chec(val+url);
	}else {
		con_url[con_url.length] = val+url;
	}
	del_div(val);
	if (document.getElementById("goods"+val)){
		document.getElementById("goods"+val).checked = false; 
	}
	if (document.getElementById("goods_id"+val)){
		document.getElementById("goods_id"+val).checked = false; 
	}
	conjoin = con_id.join();
	addCookie(str,conjoin);
	
	conpic = con_pic.join();
	addCookie(str+"pic",conpic);
	
	conshort = con_short.join();
	addCookie(str+"short",conshort);
	
	contitle = con_title.join();
	addCookie(str+"title",contitle);
	
	conurl = con_url.join();
	addCookie(str+"url",conurl);
}




//点击选择对比
function cheCompare(str,val,url,pic,title,shorttitle){
	show_compare();
	create_div(str,val,url,pic,title,shorttitle);
	if(con_id.chec(val)){
		con_id.chec(val);
	}else {
		con_id[con_id.length] = val;
	}
	if(con_pic.chec(val+pic)){
		con_pic.chec(val+pic);
	}else {
		con_pic[con_pic.length] = val+pic;
	}
	if(con_title.chec(val+title)){
		con_title.chec(val+title);
	}else {
		con_title[con_title.length] = val+title;
	}
	if(con_short.chec(val+shorttitle)){
		con_short.chec(val+shorttitle);
	}else {
		con_short[con_short.length] = val+shorttitle;
	}
	if(con_url.chec(val+url)){
		con_url.chec(val+url);
	}else {
		con_url[con_url.length] = val+url;
	}
	if (con_id.length>6){
		var ss = con_id.shift(); 
		con_pic.shift(); 
		con_short.shift(); 
		con_url.shift(); 
		con_title.shift(); 
		del_div(ss);
		if (document.getElementById("goods"+ss)){
			document.getElementById("goods"+ss).checked = false;
			var aa = getCookie("aler");
			if (aa!=1 ){
				alert('对比最多只能选择6个,第7个将会删除首选的对比');
				addCookie("aler","1");
			}
		}
	}
	var che = in_array(con_id,val);
	if (document.getElementById("goods"+val)){
		document.getElementById("goods"+val).checked = che; 
	}
	if (document.getElementById("goods_id"+val)){
		document.getElementById("goods_id"+val).checked = che; 
	}
	
	
	conjoin = con_id.join();
	addCookie(str,conjoin);
	
	conpic = con_pic.join();
	addCookie(str+"pic",conpic);
	
	conshort = con_short.join();
	addCookie(str+"short",conshort);
	
	contitle = con_title.join();
	addCookie(str+"title",contitle);
	
	conurl = con_url.join();
	addCookie(str+"url",conurl);
}

//删除对比
function delCompare(form) {	
	for (var i=1;i<form.elements.length;i++)    {
		form.elements[i].checked=false; 
	}
	for (var i=1;i<con_id.length;i++)    {
		var val = con_id[i];
		if (document.getElementById("goods_id"+val)){
			document.getElementById("goods_id"+val).checked = false; 
		}
	}
	var o = document.getElementById('compare_g');
	o.innerHTML ='';
}


//鼠标点击滚动
function start_scroller(l,t){
	this.scrollTimer = window.setInterval('set_scroller('+l+','+t+')', 50);
}

function set_scroller(l,t){
	var o = document.getElementById('compare_g');
	var _top = o.style.top;
	_top = parseInt(_top.replace('px',""));
	var _h = o.offsetHeight;
	_h = parseInt(_h);
	if (t>0 && _top==0){ 
		t=0;
	}
	if (t<0 && _h+_top<=230){ 
		t=0;
	}
	o.style.top =_top+t+"px";
}
function stop_scroller(){
	if (this.scrollTimer) window.clearInterval(this.scrollTimer);
}

function clear_scroller(type,forma){
	delCompare(document.forms[forma]);
	delCookie(type);
}

//自动滚动
lastScrollY=0;
function compare_beta(div){ 
	var diffY;
	if (document.documentElement && document.documentElement.scrollTop)
		diffY = document.documentElement.scrollTop;
	else if (document.body)
		diffY = document.body.scrollTop
	percent=.1*(diffY-lastScrollY); 
	if(percent>0)percent=Math.ceil(percent); 
	else percent=Math.floor(percent); 
	document.getElementById(div).style.top=parseInt(document.getElementById(div).style.top)+percent+"px";
	lastScrollY=lastScrollY+percent; 

}

//
function urldecode(str){ 
    var ret=""; 
    for(var i=0;i<str.length;i++)
    { 
        var chr = str.charAt(i); 
        if(chr == "+")
        { 
            ret+=" "; 
        }
        else if(chr=="%")
        { 
            var asc = str.substring(i+1,i+3); 
            if(parseInt("0x"+asc)>0x7f)
            { 
                ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6))); 
                i+=5; 
            }
            else
            { 
                ret+=asc2str(parseInt("0x"+asc)); 
                i+=2; 
            } 
        }
        else
        { 
            ret+= chr; 
        } 
    } 
    return ret; 
} 

//判断数组里文件是否存在
function in_array(str, finder) {
	var ina = false;
	for(var i=0;i<str.length;i++){
		if (str[i]==finder){
			var ina = true;	
		}
	}
	return ina;
} 




//显示对比
function show_compare(){
	var diffY=0;
	if (document.documentElement && document.documentElement.scrollTop)
		diffY = document.documentElement.scrollTop;
	else if (document.body)
		diffY = document.body.scrollTop;
	if (!this.scrolls){
		diffY=0;
	}
	document.getElementById('compare_pic').style.display = "none";
	document.getElementById('compare_content').style.display = "";
	document.getElementById('compare_content').style.top = diffY+125+'px';
	if (this.scrolls) window.clearInterval(this.scrolls);
	this.scrolls = window.setInterval("compare_beta('compare_content')",1);
}
//关闭对比
function close_compare(){
	document.getElementById('compare_pic').style.display = "none";
	document.getElementById('compare_content').style.display = "none";
}
//隐藏对比
function hidden_compare(){
	var diffY = document.documentElement.scrollTop;
	document.getElementById('compare_pic').style.display = "";
	document.getElementById('compare_content').style.display = "none";
	document.getElementById('compare_pic').style.top =  diffY+215+'px';
	if (this.scrolls) window.clearInterval(this.scrolls);
	this.scrolls = window.setInterval("compare_beta('compare_pic')",1);
}


//创建div，并加入到对比里面去
function create_div(str,id,url,pic,title,shorttitle){
	var o = document.getElementById('compare_g');
	o.style.top="0px";
	var e = document.getElementById("c_"+id);
	if (e){
		e.parentNode.removeChild(e);	
	}else{
		var  div   =  ""; //创建div层
		div =  '<div class="spdk" id="c_'+id+'">';						
		div += '<div class="spk"><a href="'+url+'" target="_blank"><img src="'+pic+'" width="100" height="80" border="0"  title="'+title+'"/></a></div>';
		div += '<div class="spname"><a href="'+url+'" class="hui" target="_blank" title="'+title+'">'+shorttitle+'</a>&nbsp;&nbsp;<a href="javascript:void(0);" onclick="clear_compare(\''+str+'\',\''+id+'\',\''+url+'\',\''+pic+'\',\''+title+'\',\''+shorttitle+'\');"><img src="/skins/default/images/close.png"></a></div>';
		div += '</div>';
		div += '<iframe src="javascript:false" style="position:absolute; visibility:inherit; top:0px; left:0px; width:305; height:206; z-index:-1; filter=progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);"></iframe> '
		document.getElementById('compare_g').innerHTML = div +　document.getElementById('compare_g').innerHTML;
	}
}
//移除div
function del_div(id){
	var e = document.getElementById("c_"+id);
	e.parentNode.removeChild(e);	
}


Array.prototype.chec = function(find){
	for (i=0; i<this.length; i++)
	{
			if (this[i].toString() == find.toString())
			{
					this.splice(i, 1);
					return true;
			}
	}
	return false;
}