$(document).ready(function(){
	//sevenup
	sevenUp.plugin.black.test( { showToAllBrowsers: false }, false);
	
	$('input.date').datepicker({
		dateFormat:'dd-mm-yy',
		changeMonth: true,
		changeYear: true
	});
	
	$('.readmore').readmore();
	$('.readmore2').readmore({ substr_len: 100});
	
	$('.highligh_box .textbox').each(function(){
		this_box = $(this);
		content_height = this_box.find('.content').height();
		content_height = content_height+20;
		this_box.find('.bg').height(content_height);
		this_box.css({'bottom':20});
	
		$(this).find('.more').click(function(){
			stopHero();
			box = $(this).parent().parent().parent();
			setHeroHeight(box);
		});
	});
	
	
	//hero
	$('#hero .image_box:eq(0)').addClass('active');
	$('#hero .image_box:not(:eq(0))').hide().addClass('inactive');
	$('#hero_nav a:not(:eq(0))').fadeTo('fast', 0.5);
	hero_total = $('#hero .image_box').length;
	hero_current = 0;
	autoHero();
	
	
	//nav
	$('#hero_nav a').click(function(){
		stopHero();
		index = $(this).index();
		heroTo(index);
	});
	
	$('#hero_nav a.inactive').fadeTo('fast', 0.5);
	
	//quick check
	$('input[name=checkin], input[name=checkout]').change(function(){
		pass = true;
		pass = checkNight();
	});
	$('form[name=quick_hotel]').submit(function(){
		chkin = $('.hotel input[name=checkin]').val();
		chkout = $('.hotel input[name=checkout]').val();
		if(chkin == '' || chkout == ''){
			alert("Please make sure all required fields are filled out correctly.");
			return false;
		}else{
			pass = checkNight();
			if(pass){
				return true;
			}else{
				return false;
			}
		}
	});
	
	$('.spa input[name=treatment_date]').change(function(){
		checkTreatmentDateQuick();
	});
	$('form[name=quick_spa]').submit(function(){
		treatment_date = $('.spa input[name=treatment_date]').val();
		if(treatment_date == ''){
			alert("Please make sure all required fields are filled out correctly.");
			return false;
		}else{
			pass = checkTreatmentDateQuick();
			return pass;
		}
	});
	
	
	//quick
	$('.quick_box .spa').hide();
	
	
	$('.gold_ribbon').click(function(){
		return false;
	});
	$('.gold_ribbon.hotel').addClass('active');
	$('.gold_ribbon.spa').addClass('inactive');
	$('.gold_ribbon').hover(function(){
		if($(this).hasClass('inactive')){
			$(this).stop().animate({'margin-top':0}, 'slow', 'easeInOutBack');
		}
	},
	function(){
		if($(this).hasClass('inactive')){
			$(this).stop().animate({'margin-top':-15}, 'fast');
		}
	});
	$('.gold_ribbon').click(function(){
		if($(this).hasClass('spa')){
			$('.quick_box .hotel').fadeOut();
			$('.quick_box .spa').fadeIn();
		}else{
			$('.quick_box .hotel').fadeIn();
			$('.quick_box .spa').fadeOut();
		}
		
		if($(this).hasClass('inactive')){
			$('.gold_ribbon.active').removeClass('active').addClass('inactive').animate({'margin-top':-15}, 'fast');;
			$(this).removeClass('inactive').addClass('active');
		}
		
	});
	
	//restaurant
	$('.float_label').parent().hover(function(){
		$(this).find('.float_label').stop(true,true).fadeIn();
	},function(){
		$(this).find('.float_label').stop(true,true).fadeOut('fast');
	});
	
	//gallery
	$('#gallery .tn').hover(function(){
		$(this).stop(true, true).fadeTo('slow', 1);	
	}, function(){
		$(this).stop(true, true).fadeTo('slow', 0.3);
	});
	
	
	//contact
	$('form[name=contactform]').submit(function(){
		pass = true;
		pass = requireCheck();
		return pass;
	});
	
	
	//spa booking
	$('form[name=spabooking]').submit(function(){
		//treatment check
		pass_check = false;
		$('.treatment input[type=checkbox]').each(function(){
			if($(this).is(':checked')){
				//alert($(this).val());
				pass_check = true;
			}
		});
		$('.package input[type=checkbox]').each(function(){
			if($(this).is(':checked')){
				//alert($(this).val());
				pass_check = true;
			}
		});
		if(!pass_check){
			alert("Please select at least 1 treatment or package.");
			return false;
		}else{
			//check treatment date
			advance_days = 2;
			start_date = Date.today().add(advance_days).days();
			treatment_date = Date.parse(converted($('input[name=treatment_date]').val()));
			if(treatment_date < start_date){
				alert("Reservations should be made "+advance_days+" days in advance.");
				return false;
			}else{
				//require
				pass = true;
				pass = requireCheck();
				return pass;
			}
		}
	});
	
	
	//plugin
	
	//$("img").lazyload({threshold : 100, effect : "fadeIn"});
	//$('.setcenter').setcenter();
	
	//datejs
	//Date.parse('today')
	
	//fitvidjs
	//$(".fitvid").fitVids();
	
	//star rating
	/*$('#stars-wrapper').stars({
        inputType: "select",
		captionEl: $("#stars-cap")
    });*/
});
function convertDate(date){
	converted = date.split("-");
	converted = converted[2]+"-"+converted[1]+"-"+converted[0];
	return converted;
}
function checkNight(){
	maxnight = 10;
	//check range date
	chkin = Date.parse(convertDate($('input[name=checkin]').val()));
	chkout = Date.parse(convertDate($('input[name=checkout]').val()));
	today = Date.parse('today');
	night = (chkout - chkin)/60/60/24/1000;
	
	if($('input[name=checkin]').val() != '' && $('input[name=checkout]').val() == ''){
		if(chkin < today){
			alert("invalid check-in date");
			return false;
		}
	}
	
	if($('input[name=checkout]').val() != '' && $('input[name=checkin]').val() == ''){
		if(chkout < today){
			alert("invalid check-out date");
			return false;
		}
	}
	
	if($('input[name=checkout]').val() != '' && $('input[name=checkin]').val() != ''){
		if(chkin < today){
			alert("invalid check-in date");
			return false;
		}else if(chkout < today){
			alert("invalid check-out date");
			return false;
		}else if(chkout < chkin){
			alert("check-out date must be greater than check-in date");
			return false;
		}else{
			if(night > maxnight){
				alert("maximum nights per booking is "+maxnight);
				return false;
			}else{
				return true;
			}
		}
	}
}
function checkTreatmentDateQuick(){
	advance_days = 2;
	treatment_date = Date.parse(convertDate($('.spa input[name=treatment_date]').val()));
	start_date = Date.today().add(advance_days).days();
	if(treatment_date < start_date){
		alert("Reservations should be made "+advance_days+" days in advance.");
		return false;
	}else{
		return true;
	}
}
function requireCheck(){
	$('.require').each(function(){
		if($(this).val() == ''){
			pass = false;
			//alert($(this).attr('name'));
		}else{
		}
	});
	if(!pass){
		alert('Please make sure all required fields are filled out correctly');
	}
	return pass;
}

/* hero */
function setHeroHeight(object){
	content_height = object.find('.content').height();
	content_height = content_height+20;
	//object.find('.bg').height(content_height);
	object.find('.bg').animate({'height':content_height}, 'slow', 'easeInOutBack');
	object.css({'bottom':20});
}
	
function autoHero(){
	interval = setInterval( heroToAuto, 5000);
}
function stopHero(){
	clearInterval(interval);
}
function heroToAuto(){
	if(hero_current < hero_total - 1){
		hero_current++;
	}else{
		stopHero();
		hero_current = 0;
	}
	heroTo(hero_current);
}
function heroTo(num){
	$('#hero .image_box:eq('+num+')').removeClass('inactive').addClass('active').fadeIn('slow');
	$('#hero .image_box:not(:eq('+num+'))').removeClass('active').addClass('inactive').fadeOut('slow');
	
	$('#hero_nav a:eq('+num+')').fadeTo('fast', 1);
	$('#hero_nav a:not(:eq('+num+'))').fadeTo('fast', 0.5);
	
	current_hero = num;
}
