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(2)').append(" ...");
		$('.box_promotion .detail ul').find('li:gt(2)').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();
	}// end doucument ready
)
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();
		}
	});
}