/*****************************/
/** 产品对比 *****************/
/** 56770.Kingdge 2012-11-25 **/
/*****************************/
var delta=0.8;
var collection;
var isCloseCompareDiv=true;

if (getCookie('56770_compare_isclose') == "true")
{
	isCloseCompareDiv=true;
}else{
	isCloseCompareDiv=false;
}

function floaters() 
{
	this.items = [];
	this.addItem = function(id,x,y,content)
	{
		document.write('<div id="'+id+'" style="display:none;z-index:9999;position:absolute;RIGHT:0px;width:200px;opacity: .90;-moz-opacity:0.90; filter:alpha(opacity=90);-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=90)";"><layer id="followDiv1"><table class="comp_tb1" cellSpacing=0 cellPadding=2><tr><td colspan=2 bgColor=#f2f2f2 height=25>&nbsp;對比欄</td></tr><tr><td>'+content+'</td></tr><tr><td colspan=2 bgColor=#f2f2f2 height=25 align=center><A HREF="#" onclick="return compare();">[對比]</A>&nbsp;&nbsp;<A HREF="#" onclick="return clearCompareDiv();">[清空]</A>&nbsp;&nbsp;<A HREF="#" onclick="return closeCompareDiv();">[隱藏]</A></td></tr></table></layer></div>');

		var newItem				= {};
		newItem.object			= document.getElementById(id);
		newItem.x				= x;
		newItem.y				= y;

		this.items[this.items.length]		= newItem;
	}
	this.play = function()
	{
		collection = this.items
		if (window.addEventListener)
		window.addEventListener("load", Compare_FloatDiv, false)
		else if (window.attachEvent)
		window.attachEvent("onload", Compare_FloatDiv)
		else if (document.getElementById)
		window.onload=Compare_FloatDiv
		//setInterval('Compare_FloatDiv()',30);
	}
}

function Compare_iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
function Compare_FloatDiv(){
	if(screen.width<=800 || isCloseCompareDiv)
	{
		for(var i=0;i<collection.length;i++)
		{
			collection[i].object.style.display	= 'none';
		}
		return;
	}
	var Compare_startX =document.body.clientWidth-200,Compare_startY = 10;
	var Compare_barheight=document.getElementById("followDiv1").offsetHeight
	var Compare_ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
	var Compare_d = document;
	function Compare_ml(id){
		var Compare_el=Compare_d.getElementById(id);
		Compare_el.style.visibility="visible"
		if(Compare_d.layers)Compare_el.style=Compare_el;
		Compare_el.sP=function(x,y){this.style.left=x+"px";this.style.top=y+"px";};
		Compare_el.x = Compare_startX;
		Compare_el.y = Compare_startY;
		return Compare_el;
	}
	window.staycompare=function(){
		var Compare_pY = Compare_ns ? pageYOffset : Compare_iecompattest().scrollTop;
		Compare_ftlObj.y += (Compare_pY + Compare_startY - Compare_ftlObj.y)/8;
		Compare_ftlObj.sP(Compare_ftlObj.x, Compare_ftlObj.y);
		setTimeout("staycompare()", 10);
	}
	if (getCookie('56770_compare_isclose') == "false"){Compare_ftlObj = Compare_ml("followDiv1");Compare_ftlObj.style.display	= '';}
	staycompare();
}

function closeCompareDiv()
{
	isCloseCompareDiv=true;
	setCookie('56770_compare_isclose',"true");
	Compare_FloatDiv();
	return false;
}

var theFloaters	= new floaters();
	theFloaters.addItem('followDiv1','document.body.clientWidth-206',200,'<table cellSpacing=0 cellPadding=2 id="tableUP"></table>');
	theFloaters.play();

//=================================================
function addCompare(pId,pName,isServerReturn)
{
	if (!isServerReturn)
	{
		isCloseCompareDiv=false;
		setCookie('56770_compare_isclose',"false");		
	}
	Compare_FloatDiv();

	var uPidArr = document.getElementsByName("uPid");
	if (uPidArr.length > 20)
	{
		alert("每次最多可對比20件商品。");
		return false;
	}
	if (checkSub(pId))
	{
		var td0= document.createElement("td");
		var td1= document.createElement("td");
		
		td0.setAttribute("width","190px");
		td1.setAttribute("width","10px");
		td1.setAttribute("align","center");
		
		td0.innerHTML = '&nbsp;<A class="compare" HREF="'+EshopDomin+''+LangPath+'product/?id='+pId+'" target="_blank">'+pName+'</A>'+'<INPUT TYPE="hidden" id="uPid" NAME="uPid" value="'+pId+'"><INPUT TYPE="hidden" id="uPName" NAME="uPName" value="'+pName+'">';
		td1.innerHTML = '<A HREF="#" class="compare" style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; COLOR: #ff0000" onclick="return delCompareRows(event);">×</a>&nbsp;';

		var trNode = document.createElement("tr");
		trNode.appendChild(td0);
		trNode.appendChild(td1);
		var trBody = document.createElement("tbody");
		trBody.appendChild(trNode);
		document.getElementById("tableUP").appendChild(trBody);
		setCookie('56770_Product_comp',getPidStr());
		carttocomp.innerHTML=getCompCount();
	}
}
function LoadCompare(pId,pName,isServerReturn)
{
	if (!isServerReturn)
	{
		isCloseCompareDiv=false;
		setCookie('56770_compare_isclose',"false");		
	}
	Compare_FloatDiv();

	var uPidArr = document.getElementsByName("uPid");
	if (uPidArr.length > 20)
	{
		alert("每次最多可對比20件商品。");
		return false;
	}
	if (checkSub(pId))
	{
		var td0= document.createElement("td");
		var td1= document.createElement("td");
		
		td0.setAttribute("width","190px");
		td1.setAttribute("width","10px");
		td1.setAttribute("align","center");
		
		td0.innerHTML = '&nbsp;<A class="compare" HREF="'+EshopDomin+''+LangPath+'product/?id='+pId+'" target="_blank">'+pName+'</A>'+'<INPUT TYPE="hidden" id="uPid" NAME="uPid" value="'+pId+'"><INPUT TYPE="hidden" id="uPName" NAME="uPName" value="'+pName+'">';
		td1.innerHTML = '<A HREF="#" class="compare" style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; COLOR: #ff0000" onclick="return delCompareRows(event);">×</a>&nbsp;';

		var trNode = document.createElement("tr");
		trNode.appendChild(td0);
		trNode.appendChild(td1);
		var trBody = document.createElement("tbody");
		trBody.appendChild(trNode);
		document.getElementById("tableUP").appendChild(trBody);
		carttocomp.innerHTML=getCompCount();
	}
}
function delCompareRows(event) 
{
	if(event == null)
	{
		event = window.event; // For IE
	}
		
	var eventObj = event.srcElement? event.srcElement : event.target;   
	var tbodyNode = eventObj.parentNode.parentNode.parentNode;
	var trNode = eventObj.parentNode.parentNode
	var rowIndex = trNode.rowIndex;
	var objTable = document.getElementById("tableUP");
	objTable.removeChild(tbodyNode);
	setCookie('56770_Product_comp',getPidStr());
	Compare_FloatDiv();
	carttocomp.innerHTML=getCompCount();
	return false;
}

//===============================
function LoadCompareList()
{
	var respoCompare_nse = getCookie('56770_Product_comp')
	if (respoCompare_nse != "")
	{
		var tArr = respoCompare_nse.split('||$$||');
		for (i = 0; i < tArr.length; i++)
		{
			var sArr = tArr[i].split('|$|');
			if (sArr.length > 1)
			{
				LoadCompare(sArr[0],sArr[1],true);
			}
		}
	}else{
		isCloseCompareDiv = true;
	}
}

function addCompareList()
{
	var respoCompare_nse = getCookie('56770_Product_comp')
	if (respoCompare_nse != "")
	{
		var tArr = respoCompare_nse.split('||$$||');
		for (i = 0; i < tArr.length; i++)
		{
			var sArr = tArr[i].split('|$|');
			if (sArr.length > 1)
			{
				addCompare(sArr[0],sArr[1],true);
			}
		}
	}else{
		isCloseCompareDiv = true;
	}
}

function clearCompareDiv()	// 清空对比框
{
	var rowLength = document.getElementById("tableUP").rows.length;
	for(i=1;i<rowLength;i++)
	{
		document.getElementById("tableUP").deleteRow(1);
	}
	setCookie('56770_Product_comp','');
	Compare_FloatDiv();
	carttocomp.innerHTML="0";
	return false;
}

function checkSub(upid) // 检测此ID是否已被加入对比框
{
	var uPidArr = document.getElementsByName("uPid");
	for(i=0;i<uPidArr.length;i++)
	{
		if (upid==uPidArr[i].value)
		{
			return false;
			break;
		}
	}
	return true;
}

function getCompCount()
{
	var uPidArr = document.getElementsByName("uPid");
	if (uPidArr.length > 0)
	{
		return uPidArr.length;
	}else
	{
		return 0;
	}
}

function compare()	// 开始对比
{
	var uPidArr = document.getElementsByName("uPid");
	if (uPidArr.length < 2)
	{
		alert("您至少要選擇2件商品進行對比。");
		return false;
	}
	var uPidStr = "";
	for(i=0;i<uPidArr.length;i++)
	{
		if (uPidStr=="")
		{
			uPidStr = uPidArr[i].value;
		}else{
			uPidStr = uPidStr + "," + uPidArr[i].value;
		}
	}
	window.open(EshopDomin+''+LangPath+'comp.asp?com_id='+uPidStr);
	closeCompareDiv(); //提交对比时关闭对比栏 2009-4-13 by 56770.kingdge
	return false;
}

function getPidStr()
{
	pidStr = "";
	var uPidArr = document.getElementsByName("uPid");
	var uPNameArr = document.getElementsByName("uPName");
	for(i=0;i<uPidArr.length;i++)
	{
		if (pidStr == "")
		{
			pidStr = ""+uPidArr[i].value +"|$|"+ uPNameArr[i].value +"";
		}else
		{
			pidStr = pidStr +"||$$||"+uPidArr[i].value +"|$|"+ uPNameArr[i].value +"";
		}
	}
	return pidStr;
}

function sunet_addEvent(obj, evType, fn)
{
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	}else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}else{
		return false;
	}
}
LoadCompareList();

function delCompId(delid)	// 对比栏
{
	var str = getCookie('56770_Product_comp');
	var compidstr = "";
	if (str != "")
	{
		var cookieIdStr = "";
		var tArr = str.split('||$$||');
		for (i = 0; i < tArr.length; i++)
		{
			var sArr = tArr[i].split('|$|');
			if (sArr.length > 0)
			{
				if (sArr[0] != delid)
				{
					if (cookieIdStr == "")
					{
						cookieIdStr = tArr[i];
						compidstr = sArr[0];
					}else{
						cookieIdStr = cookieIdStr + "||$$||" + tArr[i];
						compidstr = compidstr + "," + sArr[0];
					}
				}
			}
		}
//		document.write(cookieIdStr)
//		document.write(compidstr)
		setCookie('56770_Product_comp', cookieIdStr)
	}
	location.href=EshopDomin+''+LangPath+'comp.asp?com_id='+compidstr;
	return false;
}

function tocompare()
{  
   var i = 0;   
   var chkArr = document.getElementsByTagName("input"); 
   var id = document.getElementById("com_id").value;
   var name = document.getElementById("com_name").value;
   var param = "";   
   for (i=0;i<chkArr.length;i++)   
   if (chkArr[i].checked)   
   param+=chkArr[i].value +",";  
   if (param.length == 0)
   {
       alert("請先選擇要對比的商品！");
   }else{
	   //addCompare(id,name,true)
       location.href=EshopDomin+''+LangPath+'comp.asp?com_id='+param+id;  
   }
}