// JavaScript Document
var view ;
var window_top;
var minH = 690;
var minW = 960;
var leftFloatArea_marginTop;
var leftFloatArea_scroll_marginTop;
var containerH;

/*判斷瀏覽器版本-------------------------*/
var browserName = navigator.appName;
var browserVersion = navigator.appVersion;
if(browserName.charAt(0) =='M'){
	if(browserVersion.indexOf('MSIE 6') != -1){
		location.href = 'browser_suggest.html';
	}
}
/*---------------------------------------*/

var nowPlatform = navigator.platform;

function viewWindow(){
	return {
		x: $(document).scrollLeft(),
		y: $(document).scrollTop(),
		width: $(window).width(),
		height: $(window).height(),
		documentH: $(document).height()
	};
}

/*處理視窗*/
function resizeWindow(){
	view = viewWindow();
	$('.container').css({height:'auto'});
	containerH = $('.container').height();
	
	/*.container 高度*/
	if(containerH <= view.height){
		
		$('.container').css({height:view.height});
	}else{
		$('.container').css({height:'auto'});
	}
	
	
	if(nowPlatform == 'iPad'){
		window_top = $(window).scrollTop();
		$('.left_content').css({height:view.documentH+'px'});
		
	}else{
		//背景大圖-高
		if(view.height<minH){
			$('.container_bg').css({'height': minH+'px'})
		}else{
			$('.container_bg').css({'height': 100+'%'})
		}
	}
	
	leftFloatArea();
	
}

function leftFloatArea(){
	
	if(nowPlatform == 'iPad'){
		
		
		$(window).bind("scroll", leftFloatAreaMove);
		
	}else{
		/*左邊浮動區塊*/
		if(view.height>=minH){
			leftFloatArea_marginTop = view.height-minH;
			$('.float_main').css({'margin-top': leftFloatArea_marginTop+'px'});
		}else{
			$('.float_main').css({'margin-top': 0+'px'});
		}
	}
	
}

function leftFloatAreaMove(){
	if(nowPlatform == 'iPad'){
		window_top = $(window).scrollTop();
	
		//$('.left_content').css({'padding-top': window_top+'px'});
	}
}

/*for login area-------------------------------------------------------------------------------------------------*/
function checkLogin (){
	
	if(m_session_id){
		
		$('#himember').show();
		$('#join_btn').hide();
		$('#login_btn').hide();
		$('#logout_btn').hide();
		
		
		$('#login_content_logout_btn').bind("click",logout_fun);
		$('#login_content_edit_btn').bind("click",goEdit);
		$('#login_content').hide();
		
		$("#himember p").append(m_session_name);
		$("#himember p").append('&nbsp;');
		$("#himember p").append('&nbsp;');
		$("#himember p").append(m_session_surname);
		
		$("#welcomeBack p").append(m_session_name);
		$("#welcomeBack p").append('&nbsp;');
		$("#welcomeBack p").append(m_session_surname);
		
		
	}else{
		$('#himember').hide();
		$('#join_btn').show();
		$('#login_btn').show();
		$('#logout_btn').hide();
		$('#login_content').hide();
		
		$('#login_btn').bind("click",goLogin);
		
		$('#loginpage_login_btn').bind("click",login_sent_fun);
		
	}
	
	
}

function goLogin(){
	
	location.replace("login.php");
	
	
	
}

function goEdit(){
	location.replace("mydata_edit.php");
}

function goJoin(){
	location.replace("mydata.php");
}

function login_sent_fun(){
	
	var emailStr=$("table.login_form input#email").val();
	var passwordStr=$("table.login_form input#password").val();
	
	
	if(emailStr && passwordStr){
		$.ajax({
			url: 'template/ajax_member_login.php',
			cache: false,
			dataType: 'html',
			type:'POST',
			data: {email: emailStr,password:passwordStr},
			error: function(xhr) {
					alert('Ajax request 發生錯誤');
				},
				
			
			success: function(response) {
				
					if(response){
						$("#login_error_txt").text("");
						alert("Welcome back!");
						location.replace(document.URL);
					}else{
						alert("Your email or password are incorrect! If you forgot your password, please visit the forgot your password page.");
						$("#login_error_txt").text("帳號密碼錯誤！");
					}
				}
		});
	}else{
		$("#login_error_txt").text("請輸入帳號密碼！");
	}
}

function logout_fun(){
	
		$.ajax({
			url: 'template/ajax_member_logout.php',
			cache: false,
			dataType: 'html',
			type:'POST',
			data: {},
			/*error: function(xhr) {
					alert('Ajax request 發生錯誤');
				},*/
			success: function(response) {
			
					if(response){
						$("#login_error_txt").text("");
						alert("You have successfully logged out!");
						location.replace(document.URL);
					}
				}
		});
}

