﻿//=======================get element X/Y value;=======================
//(function($) {

jQuery.fn.backgroundPosition = function() {
  var bgPosition = $(this).css('background-position');
  if(typeof(bgPosition) == 'undefined') {
   return $(this).css('background-positionX') + ' ' + $(this).css('background-positionY');
  }else{
   return bgPosition;
  }
};
//X值：$(’#Element’).backgroundPosition().split(” “)[0];
//Y值：$(’#Element’).backgroundPosition().split(” “)[1];
//})(jQuery);
//=======================END get element X/Y value;=======================


//=======================check element is exists;=======================
jQuery.fn.exists = function(){return jQuery(this).length>0;}
//=======================END check element is exists;=======================


// JavaScript Document
var closeTimer;
var curName = "";
var preName = "";
var curPos = "";
var curObj;

/*
var handybioList = new Array(8850,100,667,1001,1002,1008,1007,1009,8577);
var satysaList = new Array(331,332,333,334,335,336,337,338,345,349,348,347,339,343,710,351,352,341,344,325,350,317,329);
var specialList = new Array(2640);
var foodList = new Array(100,101,150,88,99,50,86,89);
*/




$().ready(function(){
		if ($("#productDetailBg").exists()){	
			if ($("#productDetailBg").height() > 0){
				$("#contentDetailRight").height($("#productDetailBg").height()+200);
			}
		}

	if($.browser.msie){
		if ( parseInt($.browser.version, 10) <9){
			i=1;
			$('div').each(function(i) {
				if($(this).css('position')!='absolute') $(this).css('zIndex', 1000 - i);
				i++;
			});
		}
	}
		
		if (typeof(curPage) !== 'undefined'){
			tmpId = curPrefix+curPage;
			lmTmp = "-130px " + $("#"+tmpId).backgroundPosition().split(" ")[1];
			$("#"+tmpId).css('backgroundPosition', lmTmp);
		}
		
		setupTopMenu();
		
		$("#memberLogin #username").focus(function(){
			if($(this).val() == "會員名稱"){
				$(this).val("");
			}
		}).blur(function(){
			if ($('#memberLogin #userpassword').val() == ""){
			//	$("#memberLogin #passwordText").css("color","#BBB");
				$("#memberLogin #passwordText").show();
				$('#memberLogin #userpassword').hide();
			}
			if ($('#memberLogin #username').val() == ""){
			//	$("#memberLogin #username").css("color","#BBB");
				$('#memberLogin #username').val("會員名稱");
			}
		});

		$("#memberLogin #passwordText").focus(function(){
				$(this).hide();
				$('#userpassword').show();
				$('#userpassword').focus();				
		});
		$("#memberLogin #userpassword").blur(function(){
			if ($('#memberLogin #userpassword').val() == ""){
			//	$("#memberLogin #passwordText").css("color","#BBB");
				$("#memberLogin #passwordText").show();
				$('#memberLogin #userpassword').hide();
			}
		});
		
		if ($("#faq").exists()){
			$(".faqAnswer").attr('s','close');
			$(".faqAnswer").each(function(){
				$(this).attr('h',$(this).height());
				$(this).hide();
			});
			$(".faqQuestion").click(function(){
				tmp = $(this).next().attr('h');
				tmps = $(this).next().attr('s');
				if (tmps == 'close'){
					$(this).next().attr('s','open');
					$(this).next().attr('h',tmp);
					$(this).next().css('padding','20px');
					$(this).next().height(0);
					$(this).next().show();
					$(this).next().animate({"height":tmp},500);
					$(this).parent().css("list-style-image","url(../images/expanded.png)");
				}else{
					$(this).parent().css("list-style-image","url(../images/collapsed.png)");
					$(this).next().attr('s','close');
					//$(this).next().animate({"height":0},500);
					$(this).next().hide();
				}
			});
		}
		
		if ($(".colorbox").exists()){
			$(".colorbox").colorbox();
		}
		if ($(".colorboxTNC").exists()){
			$(".colorboxTNC").colorbox({width:"80%", height:"80%", iframe:true});
		}
	
		if ($(".contPics").exists()){
			$(".contPics img").css("cursor","pointer").click(function(){
				tmp = $(this).attr("src").replace("contpics/","contpics/popup/");
				$.colorbox({href:tmp});
			})
		}
		
		if ($("#productDetailBg").exists()){
			if (curPrefix == "adplus_" || curPrefix == "satys_"){
				appendNetvigation(curPage,curId);
			}
		}
		
		
});

function setupTopMenu(){
		
		$("#subHandybio").find(".submenuBg").height($("#subHandybio").height());
		$("#subSatys").find(".submenuBg").height($("#subSatys").height());
		$("#subAdplus").find(".submenuBg").height($("#subAdplus").height());
		$("#subPampani").find(".submenuBg").height($("#subPampani").height());
		$("#subJosephine").find(".submenuBg").height($("#subJosephine").height());
		
		$(".outer a").hover(function(){
			if (curName != ""){
				resetMenu(curName);
			}

			if ($(this).parent().attr("id")){
				tmp = $(this).parent().backgroundPosition().split(" ")[0]+" -42px";
				$(this).parent().css('backgroundPosition', tmp);
				if (curName == $(this).parent().attr("id").replace("m","sub")){
					clearMenuTimer();	
				}else{
					curName = $(this).parent().attr("id").replace("m","sub");
				}
				
			}
			if ($("#"+curName)){
				$("#"+curName).css('display','inline');
			}
		}, function (){
			
			curPos = $(this).parent().backgroundPosition().split(" ")[0]+" 0px";
			curObj = $(this).parent();
			startMenuTimer(curName, curObj, curPos);
		});
		
		
		$(".subText").hover(function(){
			clearMenuTimer();
		}, function (){
			startMenuTimer(curName, curObj, curPos);
		});
}

function resetMenu(curName){
	if($("#"+curName).exists()){
		$("#"+curName).css('display','none');
		preName = curName.replace("sub","m");
		tmp = $("#"+preName).backgroundPosition().split(" ")[0]+" 0px";
		$("#"+preName).css('backgroundPosition', tmp);
	}
}

function startMenuTimer(curName, obj, pos){
	closeTimer = setTimeout(function(){
					resetMenu(curName);
					obj.css('backgroundPosition', pos);
				},500);
}

function clearMenuTimer(){
	clearTimeout(closeTimer);
}

function submitSearch(){
	if (document.fSearch.txt_search.value == "" && document.fSearch.cat_search.value == ""){
		alert("請選擇類別或輸入關鍵字");
	}else{
		document.fSearch.submit();	
	}
}

function memberLogin(){
	if ($("#username").val() == "會員名稱" || $("#username").val() == "" || $("#userpassword").val() == ""){
		alert("請填寫會員名稱及會員密碼.");		
	}else{
		var url = '/proc_login.aspx';
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		stra = "username="+$("#username").val()+"&password=" + $("#userpassword").val();
		req.onreadystatechange = processMemberLogin;
		req.open("POST", url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(stra);	
	}
}

function processMemberLogin() {
        if (req.readyState == 4 || req.readyState=="complete") {
               if (req.responseText =="error"){
				   alert("對不起! 會員密碼錯誤,或是這個會員名稱尚未註冊!");
			   }else if (req.responseText =="ok"){
				   window.location.href = window.location.href;
			   }else{
					alert(req.responseText);
					alert("Server error, please try again later.");   
			   }
        }
}
function memberLogout(){
		var url = '/proc_logout.aspx';
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		stra = "logout=true";
		req.onreadystatechange = processMemberLogout;
		req.open("POST", url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(stra);		
}
function processMemberLogout() {
        if (req.readyState == 4 || req.readyState=="complete") {
			if (req.responseText =="ok"){
			   window.location.href = window.location.href;
			}
        }
}
function getList(pQu){
	for (i=1;i<=10;i++){
		if (pQu == i){
			document.write("<option value='"+i+"' selected>"+i+"</option>");
		}else{
			document.write("<option value='"+i+"'>"+i+"</option>");
		}
	}
}
function changeQu(pId,pQu){
		$("#btnNextStep").attr("disabled", true);
		var url = 'changequ.aspx';
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		pId = pId.replace("+","^");
		stra = "itemid="+pId+"&newqu="+pQu;
		req.onreadystatechange = processChangeQu;
		req.open("POST", url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(stra);
}
function processChangeQu() {
        if (req.readyState == 4 || req.readyState=="complete") {
			if (req.responseText =="ok"){
			   window.location.href = window.location.href;
			}else{
				alert("server error, please try again later.");	
			}
        }
}

function checkArea(value){
	if (value != "a0" && value != "hk"){
		$("#menu2").css("display","inline");
	}else{
		if (value != "a0"){
			$("#btnNextStep").attr("disabled", true);
			window.location.href = "delivery.aspx?delivery=a1&a=hk";
		}
	}
}

function changeDelivery(value){
	if (value != "a0"){
		$("#btnNextStep").attr("disabled", true);
		areaId = $("#menu1").val();
		window.location.href = "delivery.aspx?delivery="+value +"&a="+areaId;
	}
}
function spNextStep(){
	if (!$("#agree").attr('checked')){
		alert("必須同意購物條款方可繼續購物");
		return false;
	}else{
		if ($("#menu1").val() == "a0"){
			alert("請選擇地區");
			return false;
		}else if($("#menu1").val() != "hk" && $("#menu2").val() == "a0"){
			alert("請選擇郵寄方式");
			return false;
		}else{
			window.location.href = "step2.aspx";
		}
	}
}

function alertMax(){
	alert("每筆網上訂購可接受之購買金額(包括運費在內)由最低HK$100至最高HK$5,000");
	return false;
}
function cloneAddress(){
	$("#to").val($("#contact").val());
	$("#address2").val($("#address1").val());
	$("#to_tel").val($("#telno").val());
}

function chkstep2form(){
	if ($("#contact").val() == "" || $("#address1").val() == "" || $("#telno").val() == "" ||  $("#to").val() == "" || $("#address2").val() == "" || $("#to_tel").val() == "" || $("#email").val() == ""){
		alert ("有 * 為必需填寫.");
		return false;
	}else{
		alert("你將會進入 Paydollar Payment System 請填寫閣下之信用咭資料以完成交易.");
		$("#step2form").submit();
	}

}

function delme(id){
	if (confirm("刪除這項物品?")){
		$("#btnNextStep").attr("disabled", true);
		var url = 'del.aspx';
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		stra = "itemid="+id;
		req.onreadystatechange = processDelItem;
		req.open("POST", url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(stra);
	}
}
function processDelItem() {
        if (req.readyState == 4 || req.readyState=="complete") {
			if (req.responseText =="ok"){
			   window.location.href = window.location.href;
			}else{
			    alert(req.responseText);
				//alert("server error, please try again later.");	
			}
        }
}
function showBeautyTips(p){
	if (p==2){
		$("#beautytipsP1").fadeOut(0);
		$("#beautytipsP2").fadeIn("slow");
	}else{
		$("#beautytipsP2").fadeOut(0);
		$("#beautytipsP1").fadeIn("slow");
	}
}


function appendNetvigation(p,cid){
			
			//$(document).scrollTo(350, "slow");

			curtIndex = 99;
			curtList = eval("p"+p+"List");
			
			for (i=0;i<curtList.length;i++){
				if (cid == curtList[i]){
					curtIndex = i;
				}
			}

			if (curtIndex != 99){
				prevId = 0;
				nextId = 0;
				if (curtIndex == 0){
					prevId = curtList[curtList.length-1];
					nextId = curtList[curtIndex+1];
				}else if (curtIndex == curtList.length-1){
					prevId = curtList[curtIndex-1];
					nextId = curtList[0];
				}else{
					prevId = curtList[curtIndex-1];
					nextId = curtList[curtIndex+1];
				}
				prevLink = curPrefix+"detail.aspx?p="+p+"&id="+prevId;
				nextLink = curPrefix+"detail.aspx?p="+p+"&id="+nextId;
			}

			
			$("#productDetailBg").append("<br /><br /><br />"+
				"<div id='pnProductGroup'>"+
				"	<div id='prevProduct' style='float:left;width:40%'><a href='"+prevLink+"'><img src='/images/btn_prev.png' border='0' onmouseover='this.src=\"/images/btn_prev_o.png\"' onmouseout='this.src=\"/images/btn_prev.png\"' /></a></div>"+
				"	<div id='nextProduct' style='float:right;width:40%; text-align:right'><a href='"+nextLink+"'><img src='/images/btn_next.png' border='0' onmouseover='this.src=\"/images/btn_next_o.png\"' onmouseout='this.src=\"/images/btn_next.png\"' /></a></div>"+
				"</div>");
		
}
