function preLoader(imgObj,imgSrc) {
	if (document.images) {
		eval(imgObj+' = new Image()')
		eval(imgObj+'.src = "'+imgSrc+'"')
	}
}
function selectProductType(item)
{
	var thisUrl = window.location + "";
	var url = thisUrl.split("?")[0] + "";
	var param = thisUrl.split("?")[1] ? thisUrl.split("?")[1] : "";
	param = param.replace(/pid=\d*[&]*/i, ""); 
	param = param.replace(/page=\d*[&]*/i, ""); 

	if(param != "") {
		param = param + "&pid=" + item.value;
	}else {
		param = "pid=" + item.value;
	}
	window.location = url + "?" + param;
}
function goCounter()
{
	window.location='order_counter.html';
	/*
	if(!cehckCartPrice()) {
		var flag = confirm('對不起，您訂購商品合計金額不滿 '+_env_min_order_price+' 元，本站將不處理，敬請見諒。\n\n點擊[確定]繼續選購，點擊[取消]進入結帳櫃台可修改訂購數量')
		if(!flag) {
			window.location='order_counter.html';
		}
	}*/
}

function cehckCartPrice()
{
	var flag = false;
	if($("cart_total_price").innerHTML != "") {
		var price_str = $("cart_total_price").innerHTML;
		var price = parseFloat(price_str);
		if(price >= _env_min_order_price) {
			flag = true;
		}
	}
	if(flag) {
		return true;
	}else {
		return false;
	}
}

function checkLoginForm(item)
{
	if(item.login_id.value == "") {
		alert("請填寫賬號！");
		item.login_id.focus();
		return false;
	}
	if(item.password.value == "") {
		alert("請填寫密碼！");
		item.password.focus();
		return false;
	}
	return true;
}
if(document.all) {
	var tags=document.all.tags("a")
	for (var i=0;i<tags.length;i++)
	tags(i).outerHTML=tags(i).outerHTML.replace(">"," hidefocus=true>") 
}

function favority(favName,favUrl) {

	var myWebName;
	var myAddress;
	myWebName = favName;
	myAddress = favUrl;

	if (navigator.appName!="Netscape"){
	window.external.AddFavorite(myAddress, myWebName);
	}else{
	window.location = myAddress;
	}
}

getNewWindow=null;
function openWindow(ContentUrl,theWidth,theHeight,theScroll,theStatusbar,theMenubar,theResizable) {

		if(getNewWindow==null){
					var CoordX = (screen.width / 2) - (theWidth / 2);
					var CoordY = (screen.height / 2) - (theHeight / 2);
					getNewWindow = window.open(ContentUrl, 'PMD', 'Left='+CoordX+',Top='+CoordY+',Width='+theWidth+',Height='+theHeight+',ScrollBars='+theScroll+',Status='+theStatusbar+',Menubar='+theMenubar+',Resizable='+theResizable+'');
		}else{
				try{
						getNewWindow.status;
						alreadyOpenMsg = "_______________________________________________________\t\n";
						alert(alreadyOpenMsg + "\n 該子視窗已經打開, 若將重新開啟請先關閉子視窗 !\n 檢查一下是否隱藏到背後了 ! \n" + alreadyOpenMsg + "\n");
				}catch(e){
						getNewWindow=null;
				   openWindow(ContentUrl,theWidth,theHeight,theScroll,theStatusbar,theMenubar,theResizable);
				}
		}
}

// 滚动列表

function marqueeLine(id, amount)
{
    var e = document.getElementById(id); 
	if(!e) return;
    var s=[], a=[]; 
	for(var i=0; i<e.childNodes.length; i++)
    	if(e.childNodes[i].nodeType==1)
		a[a.length]=s[s.length]=e.childNodes[i];
    if(s.length==0)
		return; 
	function getAbsPointY(e) {
		var y=e.offsetTop;
    	while(e=e.offsetParent) 
			y+=e.offsetTop;
		return y;
	}

    var width  = e.offsetWidth;
    var height = e.offsetHeight;
	
    // var div = document.createElement("DIV");
    // div.style.width=width +"px";
    // div.style.height=height +"px";

    e.style.clip = "rect(0 "+ width +"px "+ height +"px 0)";
    e.style.position = "absolute"; e.style.zIndex = 1;
    // e.parentNode.insertBefore(div, e);
    // e.style.top = getAbsPointY(div) +"px";
    e.style.overflow = "scroll"; e.style.overflowX = "hidden";
    var scrollHeight = e.scrollHeight;
    e.style.width=(width + 20) +"px";
    e.style.height=(height + 20) +"px";

    var step  = 1;   //px
    var speed = 30;  //milliseconds
    var delay = 3000;//milliseconds
    var timer = null;
    var index = a.length;
    if("number"!=typeof(amount) || amount==0)amount=1;
    else amount=Math.ceil(amount);

    var k=scrollHeight*Math.ceil(amount/s.length)-s[amount%s.length].offsetTop;
    (function mm()
    {
        for(i=0;i<s.length;i++)e.appendChild(a[a.length]=s[i].cloneNode(true));
        if(e.scrollHeight<(height*2+k+20)) setTimeout(mm, 0);
    })(); var n=0; k=amount;

    function cycle()
    {
        e.scrollTop=Math.floor(n); n+=step;
        if(k<a.length && e.scrollTop>=a[k].offsetTop)
        {
            k+=amount;
            timer=setTimeout(cycle, delay);
        }
        else timer=setTimeout(cycle, speed);
        if(e.scrollTop>=a[s.length].offsetTop)
        {
            e.scrollTop=n=0;
            k=Math.max(0, k-Math.ceil(height/scrollHeight)*s.length);
        }
    }cycle();

    e.onmouseover=function(E)
    {
        E=window.event||E; E=E.srcElement||E.target;
        clearTimeout(timer);
    }
    e.onmouseout=function(E)
    {
        E=window.event||E; E=E.srcElement||E.target;
        timer=setTimeout(cycle, delay);
    }

    this.setStep =function(n){step=n;}
    this.setDelay=function(n){delay=n;}
    this.setSpeed=function(n){speed=n;}
    return this;
}

Event.onReady(function()
{
	var eles = $$("a.dg");
	eles.each(function(ele){
		var item_id = ele.id.replace("p", "");
		if(cart[item_id]) {
			ele.className = "jz";
		}
	})
	buildCartList();
});

function orderItem(item_id)
{
	/*
	if(_env_member_id == "0") {
		alert("對不起，登入後才可以訂購\n\n如果您還沒有註冊賬號請先進行註冊");
		return false;
	}*/
	var ele = $("p" + item_id);	
	var operation = 'cart_ajaxadditem.html';
	var data = "item_id=" + item_id;
	new Ajax.Request(operation, {
	  method: 'post',
	  parameters: data,
	  onSuccess: function(transport) {
		switch (transport.responseText)	{
			case "1":
				buildCartList();
				ele.className = "jz";
				break;
			case "0":
				goCounter();
				// window.location = "order_counter.html";
				break;
			default:
				alert("訂購失敗！\n" + transport.responseText);
		}
	  },
	  onComplete: function() {}

	});
}

function buildCartList() 
{
	if(!$('cart_list'))return false;
	var operation = "cart_ajaxgetcart"
	new Ajax.Request(operation, {
	  onSuccess: function(transport) {
		var cart = transport.responseText.evalJSON(true);
		if (cart == "")	{
			cart = new Object();
		}
		var cart_item_num = 0;
		var cart_total_price = 0;
		var cart_item_piece = 0;
		$('cart_list').innerHTML = "";
		for (var key_var in cart) {
			cart_item_num += cart[key_var].quantity;
			cart_item_piece += parseFloat(cart[key_var].piece) * cart[key_var].quantity;
			cart_total_price += (parseFloat(cart[key_var].price) * cart[key_var].quantity);
			var listItem = $li($em('x ' + cart[key_var].quantity), $a({ href : 'product_detail-'+key_var+'.html' }, cart[key_var].name)); 
			$('cart_list').appendChild(listItem);
		}
		
		if(cart_item_num == 0) {
			var listItem = $li($span("您還沒有訂購商品")); 
			$('cart_list').appendChild(listItem);
		}
		$("cart_item_piece").innerHTML = cart_item_piece;
		$("cart_total_price").innerHTML = cart_total_price;
		
		// 額外
		if($("integral_display")) {
			$("integral_display").innerHTML = $("cart_item_piece").innerHTML;
		}
	  }

	});
}

// preLoader('allbg','/web/normal/images/image/allbg.gif');
preLoader('allbg','/web/normal/images/image/new_pbutton.gif');
preLoader('allbg','/web/normal/images/image/hot_pbutton.gif');
