﻿// JavaScript Document
var activedMenu = null;
$(function(){
  $('#search .searchBar').val("請輸入品名關鍵字")  
  $('#search .searchBar').focus(function (){$(this).val("")})  
  $('#search .searchBar').blur(function (){
		if($(this).val()==""){
		  $(this).val("請輸入品名關鍵字")
	    }
  })
  $('#onlineService').bind("click",function(){popWindow('onlineService')});
  $('#status a ,#mapStatus a').bind("click",function(){popWindow('login')});
  
  
  var tURL='http://www.green-n-safe.com/';
  var tTtile='永豐餘生技GREEN&SAFE購物網站';
  
  $('body').find('.icon_facebook:eq(0)').click(function(){
     shareFB(tURL,tTtile);
  })
  
  $('body').find('.icon_plurk:eq(0)').click(function(){
     sharePlurk(tURL,tTtile);
  })
  
   $('body').find('.icon_twitter:eq(0)').click(function(){
     shareTwitter(tURL,tTtile);
  })  
  
  $(window).bind('resize', adjustfooter)
  
  
  if($('body').attr('type')=="login"){
    detectlogin($('body').attr('name'),$('body').attr('vip'));

	$('#shoppingCart a').attr('href','index.php?p=shop0')
	$('#memberCenter a').attr('href','index.php?p=m1')
  }
  
  $('body').find('.dBtn').each(function(){
   if($('body').attr('type')!="login"){
     //$(this).mousedown(function(){alert("你尚未登入，無法使用!")})
	 $(this).mousedown(function(){popWindow('login')})//popWindow('login');
   } 
  })
  
  adjustfooter();  
  flyOut();
  menuFlyOut();

	
  if($('#multi_tab')){
    $('#multi_tab').switch_btn('.btn','.tc','.tab_content'); 
  }
  
  
  $('.aboutDeal.btn').click(function(){
	  var idName=$(this).attr('id');
	  switch (idName){
		case 'securityDeal':
		    $(this).attr('href','index.php?p=b10&num=0');
			break;
		case 'privacy':
		  	$(this).attr('href','index.php?p=b10&num=1');
		    break;	
		case 'buyDes':
			$(this).attr('href','index.php?p=b10&num=2');
			break;		
		default:
		  }  
  })
  
 
  if(jQuery.browser.msie && !$('#IE_main').hasClass('ieDetect')){
	if(String(jQuery.browser.version).indexOf("6")!=-1){
	  var tempC=confirm("歡迎您進入GREEN & SAFE購物網站,系統己自動偵測到您瀏覽器為IE 6.0以下版本\n，為能順利於購物網站進行線上購物,建議將您的瀏覽器(Microsoft Internet Explore)升級至7.0以上之版本")
	  if(tempC){
	    window.location="../index.php?p=id6"
	  }	  
	}
  }
  

})

/*WinNameArr for pop function : login, onlineService, register, fareDesciption, verify  */


function adjustfooter(){
  var dtH=$(document).height();
  var boH=$('body').height();
  if(boH < dtH){
	  $('body #wrap').css('height',(dtH-205) + 'px');
  }
}


function closePop(){
  $('#popContainer').fadeOut()
  /*$('#popContainer').css('display','none');*/
  $('.popup, .popup2').remove();
}


function popWindow(WinNameArr,x,y){
	var WinName=WinNameArr
	 
	$.ajax({
	url:"popContent/"+WinName+".html",
	cache:false,
	success:function(html){
	 $('body').append(html)
	 $('#popContainer').fadeIn()	 
	 /*$('#popContainer').css('display','block')	*/	 
	 behaviour();
	 
	},
	error:function(){
	  alert("Please contact with us, let us know the error" )
	}	
  })
  
  function behaviour(){
	var outside=false
	
	switch(WinName)
	{
	  case "onlineService":
	  position() 
	  break;
	  
	  case "login":
	  position()
	  login() 
	  break;
	  
	  case "waiting":
	  position()
	  break;
	  
	  case "register":
	  position() 
	  break;
	  
	  case "fareDesciption":
      position2(x,y)
	  break;
	  
	  case "verify":
	  position() 
	  break;	    
	}
	
	$('.popup .btnClose').click(function(){     	
		$('#popContainer').fadeOut()
		/*$('#popContainer').css('display','none');*/
	    $('.popup').remove();
		outside=false;
    })
	
	$('body').click(function(){
     	if(outside){
		$('#popContainer').css('display','none');
	    $('.popup').remove();
		outside=false;
		}
	})
	
	$('.popup').mouseenter(function(){outside=false});
	$('.popup').mouseleave(function(){outside=true})
	
  }
  
  function position(){
     var target=$('.popup')
	 var wH=$(document).height();
	 var wW=$(document).width();
	 var pwH=target.height();
	 var pwW=target.width();	 
	 $('#popContainer').height(wH);
	 target.css("left",(wW-pwW)*.5+'px');
	 target.css("top",150+'px');
  }
  
  function position2(x,y){
     var target=$('.popup')
	 var wH=$(document).height();
	 var wW=$(document).width();
	 var pwH=target.height();
	 var pwW=target.width();	 
	 $('#popContainer').height(wH);
	 target.css("left",x+'px');
	 target.css("top",y+'px');
  }
  
  function login(){
	var result
	var target=$('.popup');
	$('#grayContent .account').val("您的身分證字號")  
  $('#grayContent .account').focus(function (){$(this).val("")})  
  $('#grayContent .account').blur(function (){
		if($(this).val()==""){
		  $(this).val("您的身分證字號")
	    }
  })

	target.find('.btnLogIn').click(function(){	  
	  target.find('#account').val(target.find('#account').val().toUpperCase());
	  var tempD="id="+target.find('#account').val()+"&pw="+target.find('#password').val();
	  verify('login.php',0,"mamberName",tempD,correct,incorrect);  
	})
	
	function incorrect(){
	  target.find('.wrongMsg').fadeIn();
	}
	
	function correct(d){
	  detectlogin(d)
	  $('#popContainer').css('display','none');
	  $('.popup').remove();
	  outside=false;
	  
	  var tempS=String(window.location).split('/')[4]
	  if(tempS!="index.php"){
	    window.location='../index.php'
	  }
	}	
  }

  
}

function detectlogin(username,vip){
	  if(vip=='Y'){vipTitle=' (VIP會員)';}else{vipTitle=' (會員)';} //PJ
	  $('#member').text('您好！ '+username+vipTitle);   //PJ
	  $('#status').html('<a href="gnsPHP/logout.php">登出</a>');/*BE登出路徑*/
	  $('#member').fadeIn();
	  $('#join').remove();
	  $('body').attr("type","login");
	  $('#shoppingCart a').attr('href','index.php?p=shop0');
	  $('#memberCenter a').attr('href','index.php?p=m1');
	  $('#status a').unbind("click");
	  $('#status a').bind("click",function(){logOut()});
	  $('body').find('.dBtn').unbind("mousedown")  	  	  
	  
	  	if($('body').attr('vatime')=='0' && vip=='Y'){
	    alert('您是GREEN&SAFE VIP會員，可享有本網站VIP價格');
	
	}
}

function logOut(){
	  $('#status a').bind("click",function(){popWindow('login')});
	  $('#shoppingCart a').attr('href','');
	  $('#memberCenter a').attr('href','');
	  $('body').find('.dBtn').each(function(){
		 if($('body').attr('type')!="login"){
		   $(this).click(function(){alert("你尚未登入，無法使用")})
		 } 
      })
	  
}


function flyOut(){
	$('#nav .cate').mouseover(function(e){
		  $(this).addClass('active');
		  $(this).find('.flyOut').show();
	  })
	  
	  $('#nav .cate').mouseout(function(e){
		  $(this).removeClass('active');
		  $(this).find('.flyOut').hide();
		 
	  })
}

var nowdom;
function menuFlyOut(){
	 $('.menu .menuLi').mouseenter(function(){
		if ( activedMenu != null ) {
		 activedMenu.removeClass('active');
		 activedMenu.find('.subMenu').hide();
		 
		}
		  
		  $(this).addClass('active');
		  $(this).find('.subMenu').show();
		  
		 nowdom = $(this);
		 setTimeout(resizeSubMenu, 10);
		 activedMenu = $(this);
		  
	  })
}

function resizeSubMenu() {
	var subHt=nowdom.find('.subMenu ul').height();
	var flyHt=nowdom.parent().parent().height();
	if( flyHt < subHt){
		nowdom.parent().parent().css('height',subHt + 32);
		nowdom.parent().css('height',subHt);
	}
}

function shareFB(fburl,fbtitle){
  fburl=encodeURIComponent(fburl);
  fbtitle=encodeURIComponent('+('+fbtitle+')');
  window.open('http://www.facebook.com/share.php?u='+fburl+'&amp;t='+fbtitle)
}


function sharePlurk(plurl,pltitle){
  plurl=encodeURIComponent(plurl);
  pltitle=encodeURIComponent('+('+pltitle+')');
  window.open('http://www.plurk.com/?status='+plurl+pltitle+'&qualifier=shares');
}


function shareTwitter(twurl,twtitle){
 window.open('http://twitter.com/home/?status='.concat(encodeURIComponent(twtitle)) .concat(' ') .concat(encodeURIComponent(twurl)))
}








