var spa_num = 1;
var hotel_num = 1;
var clickstatus = "first";
var myintervalID;

var headnum = 1;
var head_total = 4;
var auto_status = true;
var myHeadInterval;

var editstatus = true;

var tnpagenum = tnnum = 1;
var total_tnbox;

var awardnum = 0;

/* browser */
var Browser = {
  Version: function() {
	var version = 999; // we assume a sane browser
	if (navigator.appVersion.indexOf("MSIE") != -1)
	  // bah, IE again, lets downgrade version number
	  version = parseFloat(navigator.appVersion.split("MSIE")[1]);
	return version;
  }
}
		
$('document').ready(
	function(){
		npronum = 0;
		nprobox_total = $('#banner_hotel_box').children('.npro_box').size();
		$('.npro_box:eq('+npronum+')').css({"z-index":1});
		$('.npro_box:eq('+npronum+')').addClass('current').fadeIn(2000);
		$('.npro_box:not(:eq('+npronum+'))').hide();
		npro_check();
		nprointervalID = setInterval("npro_auto()",3000);
		$('#banner_hotel_box .nav').click(function(){
			clearInterval(nprointervalID);
		});
		
		
		$('.step1 .data01 select').change(function(){
			get_treatment_date();									
		});
		
		if (Browser.Version() < 8) {
			$('#upgrade').show();
		}else{
			$('#upgrade').hide();
		}
		st=$("#countperson").val();
		rno=$("#countroom").val();	
		getRoomperson(rno);
		get_selectTreatment(st);//àÅ×Í¡treatment µèÒ§æ ¢Í§¤¹áÃ¡¡èÍo

	//	rno=$("#countroom").val();
	//	list=$("#roomIdsc").val();
	//	if(list!=""){getroom_description(list);}
	//	getRoomperson(rno);
	

		/* test edit
		$('.profile_box .val').click(
			function(){
				if(editstatus == true){
					myval = $(this).text();
					$(this).html("<input type='text' id='edit_realname' value="+myval+"/>");
					editstatus = false;
				}
			}
		);*/
		//$('#our_relate_service .show_promini:last').addClass('last');
		$('.hilight_box:first').css({"z-index":1});
		$('.hilight_box:first').fadeIn(2000).addClass('current');
		$('#hilight_menu li:first').addClass('current');

		$('.banner_box:first').fadeIn(1000);
		$('.banner_box:first').addClass('current');
		
		
		$('.wide_banner .nav').hide();
		
		$('.wide_banner').fadeIn();
		$('.wide_banner').mouseover(
			function(){
				clearInterval(myintervalID);
				numofbanner = $(this).find('div.banner_box').size();
				if(numofbanner > 1){
					mytype = $(this).attr('class').split(' ').pop(); // get type from element
					if(mytype == "hotel"){
						tocheck = hotel_num;
					}else{
						tocheck = spa_num;
					}
				}
				if(tocheck == 1){
					$(this).find('.left_nav').hide();
					$(this).find('.right_nav').show();
				}else if(tocheck == numofbanner){
					$(this).find('.left_nav').show();
					$(this).find('.right_nav').hide();
				}else{
					$(this).find('.nav').show();
				}
			}
		);
		$('.wide_banner').mouseout(
			function(){
				$(this).find('.nav').hide();
			}
		);
		$('#login_link').click(
			function(){
				//alert(clickstatus);
				if(clickstatus == "first" || clickstatus == "closed"){
					$('#show_flogin').fadeIn().fadeTo("slow",0.9);
					clickstatus = "clicked";
				}else{
					$('#show_flogin,#show_fforgot,#showhint').fadeOut();
					clickstatus = "closed";
					setLogin();
				}
			}
		);
		$('.forgotlink').click(
			function(){
				$('#show_flogin,#showhint').fadeOut();
				$('#show_fforgot').fadeIn().fadeTo("slow",0.9);
				clickstatus = "forgotclick";
			}
		)
		$('#profile_link').click(
			function(){
				$('#profile_mini').fadeIn().fadeTo("slow",0.9);
			}
		)
		$('#head_menu,#mainbody').mouseover(
			function(){
				$('#show_flogin,#show_fforgot,#profile_mini,#showhint').fadeOut();
				clickstatus = "closed";
				setLogin();
			}
		);
		$('.box_promotion .detail ul').find('li:eq(1)').append("...");
		$('.box_promotion .detail ul').find('li:gt(1)').hide();
		autoBanner("spa");
		autoBanner("hotel");
		
		/* auto header image */
		$('#header_box .headsubbox:first').addClass('current').fadeIn(1000);
		
		myHeadInterval = setInterval("auto_header()",3000);
		
		
		/* quick reservation */
		$('#hotelQuick').hide();
		$('#tapspa').click(function(){
			$(this).parent().parent().find('.current').removeClass('current');						
			$(this).addClass('current');
			$('#hotelQuick').fadeOut(function(){
				$('#spaQuick').fadeIn();
			});
	
		});
		$('#taphotel').click(function(){
			$(this).parent().parent().find('.current').removeClass('current');						
			$(this).addClass('current');
			$('#spaQuick').fadeOut(function(){
				$('#hotelQuick').fadeIn();
			});
		});
		
		/* gallery */
		galbranch = $('#gallery').attr('rel');
		$('.rightimg .previewPane img').width('100%');
		total_tnbox = $('#tn_box_all').children('.imgperbox').size();
		total_tn = $('#tn_box_all').find('img').size();
		set_tn_nav(tnpagenum,total_tnbox);
		$('#tn_nav_prev').click(function(){
			tn_goto('prev');
		});
		$('#tn_nav_next').click(function(){
			tn_goto('next');
		});
		$('.link').children('a').hide();
		$('.previewPane').mouseover(function(){
			if(tnnum == 1){
				$(this).find('.prev').hide();
				$(this).find('.next').show();
			}else if(tnnum == total_tn){
				$(this).find('.prev').show();
				$(this).find('.next').hide();	
			}else{
				$(this).find('.prev').show();
				$(this).find('.next').show();
			}
		});
		$('.previewPane').mouseout(function(){
			$(this).find('.prev').hide();
			$(this).find('.next').hide();
		});
		$('.link .prev').click(function(){
			if(tnnum > 1){
				tnnum--;
				showGal(tnnum,"num");
			}
		});
		$('.link .next').click(function(){
			if(tnnum < total_tn){
				tnnum++;
				showGal(tnnum,"num");
			}
		});
		$('.treatment_desc').hide();
		
		//award
		awardbox_total = $('#award').children('.award_box').size();
		/*$('#award .left_nav').click(function(){
			award_prev();
		});
		$('#award .right_nav').click(function(){
			award_next();
		});
		$('#award .nav').show();*/
		$('#award .award_box:eq('+awardnum+')').addClass('current');
		$('#award .award_box:not(:eq('+awardnum+'))').hide();
		awardintervalID = setInterval("award_auto()",3000);
		award_check();
		
		
		//branch box
		var branch1 = 0;
		var branch2 = 70;
		var branch3 = 140;
		$('#branch_box .branch2').animate({top:branch2},1000,"easeOutBack");
		$('#branch_box .branch3').animate({top:branch3},1000,"easeOutBack");
		
		$('#branch_box .branch1').click(function(){
			$('.branch_description').fadeIn();
			$('#branch_box .branch2, #branch_box .branch3').animate({top:'320'},1000,"easeOutBack");
			$('#branch_box .quick_back').animate({left:0},1000,"easeOutQuad");
		});
		
		$('#branch_box .branch2').click(function(){
			$('.branch_description').fadeIn();
			$(this).animate({top:'0'},1000,"easeOutBack");
			$('#branch_box .branch3').animate({top:'320'},1000,"easeOutBack");
			$('#branch_box .quick_back').animate({left:0},1000,"easeOutQuad");
		});
		
		$('#branch_box .branch3').click(function(){
			$('.branch_description').fadeIn();
			$(this).animate({top:'0'},1000,"easeOutBack");
			$('#branch_box .quick_back').animate({left:0},1000,"easeOutQuad");
		});
		$('#branch_box .quick_back').click(function(){
			$('.branch_description').fadeOut();
			$('#branch_box .quick_back').animate({left:-88},800,"easeOutQuad");
			$('#branch_box .branch1').animate({top:branch1},1000,"easeOutBack");
			$('#branch_box .branch2').animate({top:branch2},1000,"easeOutBack");
			$('#branch_box .branch3').animate({top:branch3},1000,"easeOutBack");
		});
		
		//new hilight
		current_hilight = 1;
		hilight_total = $('.hilight_new_box .hilight_box').size();
		check_hilight_nav();
		
		$('#hilight_new .left_nav').click(function(){
			$('.hilight_new_box .current').fadeOut().removeClass('current');
			current_hilight--;
			$('.hilight_new_box .box'+current_hilight).fadeIn().addClass('current');
			check_hilight_nav();
		});
		$('#hilight_new .right_nav').click(function(){
			$('.hilight_new_box .current').fadeOut().removeClass('current');
			current_hilight++;
			$('.hilight_new_box .box'+current_hilight).fadeIn().addClass('current');
			check_hilight_nav();
		});
		
		
		$('.new_hilight #menu1').click(function(){
			$('.hilight_new_box').load('../ajax.php?fn=hotelpro',function(){
				$('#hilight_sub1').fadeIn().addClass('current');
				new_hilight_nav();
			});
			new_hilight_class($(this));
		});
		$('.new_hilight #menu2').click(function(){
			$('.hilight_new_box').load('../ajax.php?fn=deck1',function(){
				$('#hilight_sub1').fadeIn().addClass('current');
				new_hilight_nav();
			});
			new_hilight_class($(this));
		});
		$('.new_hilight #menu3').click(function(){
			$('.hilight_new_box').load('../ajax.php?fn=awards',function(){
				$('#hilight_sub1').fadeIn().addClass('current');
				new_hilight_nav();
			});
			new_hilight_class($(this));
		});
		function new_hilight_class(object){
			$('.new_hilight .current').removeClass('current');
			$(object).addClass('current');
		}
		function new_hilight_nav(){
			hilight_total = $('.hilight_new_box .hilight_box').size();
			//alert("hilight_total "+hilight_total);
			current_hilight = 1;
			//alert("current_hilight "+current_hilight);
			check_hilight_nav();
		}
		function check_hilight_nav(){
			if(current_hilight == 1){
				$('#hilight_new .left_nav').fadeOut(100);
				$('#hilight_new .right_nav').show();
			}else if(current_hilight == hilight_total){
				$('#hilight_new .left_nav').show();
				$('#hilight_new .right_nav').fadeOut(100);
			}else{
				$('#hilight_new .left_nav').show();
				$('#hilight_new .right_nav').fadeIn(100);
			}
		}
		
		// christmas
		$('.prize_box').hover(function(){
			$(this).stop().animate({top:'-=10'},500,"easeOutBack");
		},function(){
			$(this).stop().animate({top:'+=10'},500,"easeOutQuad");
		});
		$('.prize_box').click(function(){
			getChristmasGift();
		});
		$('#christmas_contact #christmas_send').click(function(){
			return christmas_contact_check();
		});
		$('#submit_customer_id').attr('disabled', 'disabled');
		$('#customer_id_input').bind('change keyup',function(){
			customerid = $('#customer_id_input').val();
			$('#christmascode .status').removeClass('red green')
			$('#christmascode .status').html("checking...");
			$('#submit_customer_id').attr('disabled', 'disabled');
			$.get('../ajax.php', {fn : "checkcustomerid", customer_id : customerid}, function(data, textStatus, XMLHttpRequest){
				if(data == true){
					status = "ID available";
					$('#christmascode .status').removeClass('red').addClass('green');
					$('#submit_customer_id').removeAttr('disabled');
				}else{
					status = "ID already exist";
					$('#christmascode .status').removeClass('green').addClass('red');
					$('#submit_customer_id').attr('disabled', 'disabled');
				}
				$('#christmascode .status').html(status);
			});
		});
		
		//submityoursite
		$('#submityoursite a.submit_website_link').click(function(){
			$('.submit_website_form').fadeIn();
			$(this).hide();
		});
		$('textarea[name=code]').click(function(){
			$(this).focus().select();
		});
		$('form[name=submityoursite]').submit(function(){
			return checkSubmitWebsiteForm();
		});
		//check form
		
		
	}// end doucument ready
)
function checkSubmitWebsiteForm(){
	pass = true;
	$('.submit_website_form input, .submit_website_form textarea').each(function(){
		if($(this).attr('name') == 'url' && $(this).val() == 'http://'){
			pass = false;
		}else if($(this).val() == ''){
			pass = false;
		}
	});
	if(!pass){
		alert("please input all the fields");
	}
	return pass;
}
function getChristmasGift(){
	$('#christmas_left .popup').load('../ajax.php?fn=getchristmasgift',function(){
		$(this).fadeIn(500);
	})
}
function christmas_contact_check(){
	customer_id = $('#christmas_contact input[name|="customer_id"]').val();
	
	email = $('#christmas_contact input[name|="email"]').val();
	firstname = $('#christmas_contact input[name|="firstname"]').val();
	lastname = $('#christmas_contact input[name|="lastname"]').val();
	gender = $('#christmas_contact select[name|="gender"]').val();
	age = $('#christmas_contact select[name|="age"]').val();
	country_id = $('#christmas_contact input[name|="country_id"]').val();
	
	birthdate_date = $('#christmas_contact select[name|="birthdate_date"]').val();
	birthdate_month = $('#christmas_contact select[name|="birthdate_month"]').val();
	birthdate_year = $('#christmas_contact select[name|="birthdate_year"]').val();
	
	//lasttime_visit_date = $('#christmas_contact select[name|="lasttime_visit_date"]').val();
	lasttime_visit_month = $('#christmas_contact select[name|="lasttime_visit_month"]').val();
	lasttime_visit_year = $('#christmas_contact select[name|="lasttime_visit_year"]').val();
	enjoy_service = $('#christmas_contact select[name|="enjoy_service"]').val();
	
	if(email != "" && firstname != "" && lastname != "" && gender != "" && age != "" && country_id != "" && lasttime_visit_month != "" && lasttime_visit_year != "" && enjoy_service != "" && birthdate_date != "" && birthdate_month != "" && birthdate_year != ""){
		/*if(customer_id == ""){
			if(confirm("no customer id right?") == true){
				$('#christmas_contact').submit();
				return true;
			}else{
				return false;
			}
		}else{
			$('#christmas_contact').submit();
			return true;
		}*/
		return true;
	}else{
		alert("Please input all the require fields");
		return false;
	}
	
}
function checkemail(email){
	var regex = new RegExp("/(.*)@([a-zA-Z0-9_-]{2,})\.(.*)/");
	if(regex.test(email)){
		return true;
	}else{
		return false;
		alert("Please input the correct email format");
	}
}
function award_check(){
}

function npro_auto(){
	if(npronum < (nprobox_total-1)){
		npro_next();
	}else{
		//clearInterval(awardintervalID);
		npro_to(0);
	}
}
function npro_to(num){
	$('#npro .current').removeClass("current").fadeOut();
	npronum = num;
	$('#npro .npro_box:eq('+(npronum)+')').addClass('current').fadeIn();
	npro_check();
}
function npro_next(){
	if(npronum < (nprobox_total-1)){
		$('.npro_box.current').removeClass("current").fadeOut();
		npronum++;
		$('.npro_box:eq('+(npronum)+')').addClass('current').fadeIn();
	}
	npro_check()
}
function npro_prev(){
	if(npronum > 0){
		$('.npro_box.current').removeClass("current").fadeOut();
		npronum--;
		$('.npro_box.npro_box:eq('+(npronum)+')').addClass('current').fadeIn();
	}
	npro_check()
}
function npro_check(){
	if(npronum == 0){
		$('#banner_hotel_box .left_nav').hide();
	}else if(npronum == (nprobox_total-1)){
		$('#banner_hotel_box .right_nav').hide();
	}else{
		$('#banner_hotel_box .left_nav').show();
		$('#banner_hotel_box .right_nav').show();
	}
}

function award_auto(){
	if(awardnum < (awardbox_total-1)){
		award_next();
	}else{
		//clearInterval(awardintervalID);
		award_to(0);
	}
}
function award_to(num){
	$('#award .current').removeClass("current").fadeOut();
	awardnum = num;
	$('#award .award_box:eq('+(awardnum)+')').addClass('current').fadeIn();
	award_check();
}
function award_next(){
	if(awardnum < (awardbox_total-1)){
		$('#award .current').removeClass("current").fadeOut();
		awardnum++;
		$('#award .award_box:eq('+(awardnum)+')').addClass('current').fadeIn();
		award_check();
	}
}
function award_prev(){
	if(awardnum > 0){
		$('#award .current').removeClass("current").fadeOut();
		awardnum--;
		$('#award .award_box:eq('+(awardnum)+')').addClass('current').fadeIn();
		award_check();
	}
}
function setLogin(){
	$('#emaillogin').val('Username or Email');
	$("#emaillogin,#password").addClass("fontxt");
	$('#password').val("Enter password");
	$("#remember").val("");
	$("#showresult").html("");
	$("#remember").removeAttr("checked");
}
function show_box(toshow){
	$('div.hilight_box:is(.current)').css({"z-index":0});
	$('div.hilight_box:is(.current)').fadeOut(1000);
	$('div.hilight_box:is(.current)').removeClass('current');
	$('#hilight_menu li:is(.current)').removeClass('current');
	
	$('.hilight_box:eq('+(toshow-1)+')').css({"z-index":1});
	$('.hilight_box:eq('+(toshow-1)+')').fadeIn(1000);
	
	$('.hilight_box:eq('+(toshow-1)+')').addClass('current');
	$('#hilight_menu li:eq('+(toshow-1)+')').addClass('current');
	if (Browser.Version() <= 8) {
		$('#hilight_menu li:eq('+(toshow-1)+') a').width(150);
		$('#hilight_menu li:eq('+(toshow-1)+') a').height(22);
	}
}
function setvar(type,direction){
	if(type =="hotel"){
		if(direction == "get"){
			current_num = hotel_num;
		}else{
			hotel_num = current_num;
		}
	}else{
		if(direction == "get"){
			current_num = spa_num;
		}else{
			spa_num = current_num;
		}
	}
}
function goNext(type){
	setvar(type,"get");
	if(current_num < $('#banner_group_'+type).children().size()){
		lefttobe = current_num*(-340);
		$('#banner_group_'+type).animate({marginLeft:lefttobe},800,"easeOutBack");
		current_num++;
		setvar(type,"pass");
	}
}
function goPrev(type){
	setvar(type,"get");
	if(current_num > 1){
		current_num--;
		righttobe = (current_num-1)*(-340);
		$('#banner_group_'+type).animate({marginLeft:righttobe},800,"easeOutBack");
		setvar(type,"pass");
	}
}
function goTo(type,num){
	current_num = num;
	lefttobe = (num-1)*(-340);
	$('#banner_group_'+type).animate({marginLeft:lefttobe},800,"easeOutBack");
}
function autoBanner(type){
	total = $('#banner_group_'+type).children().size();
	if(total != 1){
		myintervalID = setInterval("autoGo('"+type+"')",3000);
	}
}
function autoGo(type){
	setvar(type,"get");
	total = $('#banner_group_'+type).children().size();
	if(current_num == $('#banner_group_'+type).children().size()){
		clearInterval(myintervalID);
		current_num = 1;
		setvar(type,"pass");
		lefttobe = (current_num-1)*(-700);
		$('#banner_group_'+type).animate({marginLeft:lefttobe},7000);
	}else{
		current_num++;
		setvar(type,"pass");
		goTo(type,current_num);
	}
}

/* auto header image */
function auto_header(){
	if(headnum < head_total){
		headnum++;
	}else{
		headnum = 1;
	}
	$('#header_box .headsubbox:hasClass("current")').removeClass('current').css('z-index',1).fadeOut(200);
	$('#header_box .headsubbox:eq('+(headnum-1)+')').addClass('current').css('z-index',2).fadeIn(1000);
}

/* gallery */
function showGal(img,type){
	$('#loadingicon').remove();
	if(type == "num"){
		realimg = "../images/"+galbranch+"/gallery/"+galbranch+"_"+img+".jpg";
		tnnum = img;
	}else{
		realimg = "../images/"+galbranch+"/gallery/"+img;
	}
	loadingicon = "../images/ajax-loader.gif";
	$('.rightimg .previewPane img').fadeOut(function(){
		$(this).parent().prepend('<img id=loadingicon src="'+loadingicon+'">');
		
		$('.rightimg .caption').fadeOut().html('');
		
		$(this).attr('src',''+realimg+'').load(function(){
			$('#loadingicon').remove();
			/* set image dimension */
			scale = $(this).width() / $(this).height();
			if(scale > 1){
				$(this).width($('.rightimg').width()).fadeIn();
				$(this).height('');
			}else{
				$(this).width('');
				$(this).height($('.rightimg').height()).fadeIn();
			}
			imgwidth = $(this).width();
			$('.previewPane').width(imgwidth);
			getcaption(img,1);
    	});
	});
}
function tn_goto(tndirection){
	if(tndirection == "prev"){
		if(tnpagenum <= total_tnbox && tnpagenum > 1){
			tnpagenum--;
		}
	}else{
		if(tnpagenum < total_tnbox){
			tnpagenum++;
		}
	}
	set_tn_nav(tnpagenum,total_tnbox);
	tnlefttobe = -((tnpagenum-1)*215);
	$('#tn_box_all').animate({marginLeft:tnlefttobe},700,"easeOutBack");
}
function set_tn_nav(tnpagenum,total_tnbox){
	if(total_tnbox == 1){
		$('#tn_nav_prev').hide();
		$('#tn_nav_next').hide();	
	}else{
		if(tnpagenum == 1){
			$('#tn_nav_prev').fadeOut();
			$('#tn_nav_next').fadeIn();
		}else if(tnpagenum == total_tnbox){
			$('#tn_nav_next').fadeOut();
			$('#tn_nav_prev').fadeIn();
		}else{
			$('#tn_nav_next').fadeIn();
			$('#tn_nav_prev').fadeIn();
		}
	}
}
function getcaption(num,branchid){
	$('.rightimg .caption').fadeIn().load("gallery.php",{tn:num,bid:branchid});
/*	$.ajax({
		url:"gallery.php",
		type: "POST",
		data:({tn:num,bid:branchid}),
		async: false,
		success: function(data){
			$('.rightimg .caption').fadeIn().html(''+data+'');
		}
	});*/
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function get_treatment_date(){
	treatmentdate = $('#reserv_year').val() + "-" + $('#reserv_mon').val() + "-" +$('#reserv_day').val() + "  " + $('#thour').val() +":"+ $('#tmin').val()+":00";
	idpro = $('#idpro').val();
	//treatmentdate = $('#reserv_year').val() + "-" + $('#reserv_mon').val() + "-" +$('#reserv_day').val();
	$.post('ajax.php',{treatment_date:treatmentdate,id_promotion:idpro},function(data){
		if(data == 'OK'){
			$('.step1 #inform_reserv_box').html('');
			$('.right_reserv').fadeIn();
			$('.thaicontact').fadeIn();
			$('.reserv_info').fadeIn();

		}else if(data == 'donotcheck'){
		}else{
			$('.step1 #inform_reserv_box').html(data);
			$('.right_reserv').fadeOut();
			$('.thaicontact').fadeOut();
			$('.reserv_info').fadeOut();
		}
	});
}


