// BOF GOOGLE FONT
//
WebFontConfig = {
    google: { families: [ 'Open+Sans:300,400,600,700,400italic,600italic,700italic:latin' ] }
};
(function() {
	var wf = document.createElement('script');
	wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
	  '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
	wf.type = 'text/javascript';
	wf.async = 'true';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(wf, s);
})();
//
// EOF GOOGLE FONT
//
// RE-RENDER PAGE ON RESIZE
$(window).resize(function() {
	//window.location.reload();
	setBodyWidth();
});
//
function setBodyWidth() {
	// SET BODY WIDTH IF BELOW 980
	var win_w = $(window).width();
	//
	if(win_w < 980 && $('body').css('width') == ''){
		$('body').width(980);
	} else {
		$('body').width(win_w);	
	}
}
//
function get_val(fld){
	if($(fld).length){
		v = $(fld).val();
		return v;
	} else {
		alert('No Matching Field!\nID:'+fld);
		return false;
	}
}
//
function is_email(eml) {
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if(eml=='' || !emailReg.test(eml)) {
		return false;
	} else {
		return true;
	}
}
//
function input_err(id){
	if(id.substring(0,1) != "#"){
		var id = "#"+id;
	}
	//
	var input_fld = $(id);
	//alert(input_fld.hasClass('err'));
	//
	if(!input_fld.hasClass('err')){
		input_fld.addClass('err');
		var curr_width = input_fld.width();
		input_fld.width(curr_width-23);
		input_fld.css('background', 'url(/images/ui/ico_err.png) right 3px no-repeat #ffffff');
		input_fld.css('padding-right', '30px');
	}
}
//
$(window).load(function () {
	// SET BODY WIDTH
	setBodyWidth();
	//
	// SET TOP MENU CURR
	this_index = parseInt(sec_index);
	//
	$('#brand_nav li:eq('+this_index+')').addClass('curr');
	//
	// TOP MENU FUNCTIONS
	$('#brand_nav li').hover(function() {
		$(this).addClass('curr');
		$(this).css('cursor', 'pointer');
	}, function(){
		if($(this).index() != sec_index){
			$(this).removeClass('curr');
		}
	});
	//
	$('#brand_nav li').bind('click', function() {
		var this_link = $(this).find("a").attr('href');
		document.location.href = this_link;
	});
	//
	// SET IMG BTN ROLLOVERS
	$('div.btn').each(function(index) {		
		$(this).css('display', 'block');
		//
		if($(this).css('width')){
			var btn_w = $(this).css('width');
		} else {
			var btn_w = $(this).find("img").width();
		}
		var btn_h = ($(this).find("img").height())*0.5;
		//
		//alert('btn:'+btn_w+'x'+btn_h+';');
		//		
		$(this).css('width', btn_w+'px');
		$(this).css('height', btn_h+'px');
		//
		$(this).mouseover(function() {
			$(this).find("img").css('top', '-'+btn_h+'px');
		}).mouseout(function() {
			$(this).find("img").css('top', '0px');
		});
	});
	//
	// ADD FUNCTIONALITY TO CHKBOX DIVS
	$('div.chkbox').bind('click', function() {
		var real_input = $(this).attr('id').replace('chk_', '');
		//
		if($(this).hasClass('chkd')){
			$(this).removeClass('chkd');
			$('#'+real_input).val(0);
		} else {
			$(this).addClass('chkd');
			$('#'+real_input).val(1);
		}
	});
	//
	// SET FOCUS VALS FOR NEWSLETTER
	$('#nl_email').focus(function(){
		var eml = $(this).val();
		if(eml == 'Enter your email address here...'){
			$(this).val('');
		}
	});
	//
	$('#nl_email').blur(function(){
		var eml = $(this).val();
		if(eml == ''){
			$(this).val('Enter your email address here...');
		}
	});
	//
	// ALIGN FOOTER ICONS
	var ftr_ul_w = $('#ftr #btm ul').width();
	var ftr_ul_cntnt_w = 0;
	$('#ftr #btm ul li').each(function(index) {
		ftr_ul_cntnt_w += $(this).outerWidth();
	});
	//
	var h_margin = (ftr_ul_w-ftr_ul_cntnt_w)*0.5;
	//
	$('#ftr #btm ul').css('margin', '0px '+h_margin+'px 0px '+h_margin+'px');		
	$('#ftr #btm').css('display', 'block');
	//
	// ADD FUNCTIONALITY TO TABS
	$('ul.tabs li').bind('click', function(){
		do_tab($(this).attr('id'));
	});
	//
	// ADD CLICK TO ANY A TAG IN A DEF BTN
	$('.btn_def').bind('click', function() {
		var this_href = $(this).find("a").attr('href');
		var this_clss = $(this).find("a").attr('class');
		if(this_clss == 'anchorLink'){
				var destination = $(this_href).offset().top;
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 700, function() {
					window.location.hash = elementClick
				});
				return false;
		} else {
			if(this_href){
				document.location.href = this_href;
			}
		}
	});
	//
	// SET ROLLOVER ON BTM BANNER
	$('#cu').hover(function(){
		$(this).css('background-position', '0px -85px');						
	}, function(){
		$(this).css('background-position', '0px 0px');						
	});
});
//
function do_tab(id){
	$('ul.tabs li').each(function(){
		$('div#inf_'+$(this).attr('id')).css('display', 'none');
		$(this).removeClass('curr');							  
	});
	$('ul.tabs li#'+id).addClass('curr');
	$('div#inf_'+id).css('display', 'block');
	setBodyWidth();
}
//
// INIT COLORBOX
$(document).ready(function(){
	// SET BODY WIDTH
	setBodyWidth();
	//
	// SET COLORBOX LINKS
	$("a[rel='tmb']").colorbox({current:'Photo {current} of {total}'});
	//
	$("a[rel='gal']").colorbox({current:'Photo {current} of {total}'});
	$("a[rel='lnk']").colorbox({current:'Page {current} of {total}'});
	$("a[rel='lnk_sngl']").colorbox({rel:'nofollow', initialHeight:'400', initialWidth:'400'});
	//
	var $gallery = $('a[rel=tmb]').colorbox();
	$('a#openGallery').click(function(e){
	  e.preventDefault();
	  $gallery.eq(0).click();
	});
	//
	$('div.imgs ul').jcarousel({
		visible: 1,
		scroll: 1,
		auto: 0,
		wrap: 'circular',
		buttonPrevHTML:'<div class="p"><img src="/images/ui/btns_lr_thumbs.png"></div>',
		buttonNextHTML:'<div class="n"><img src="/images/ui/btns_lr_thumbs.png"></div>',
		buttonNextEvent:'click',
		buttonPrevEvent:'click'
	});
	//
	$("#gallery_switch").click(function(){
		if($("#gallery_holder").height() > 220){
			$("#gallery_switch p").html('Show Gallery');
			//
			$("#gallery_holder").animate({
				height: "207px",
			}, 300, function(){
					// do when done
				}
			);
			// MOVE UP GALLERY
			$("#gallery_holder div.gallery").animate({
				top: "-67px",
			}, 300, function(){
					// do when done
				}
			);
		} else {
			$("#gallery_switch p").html('Hide Gallery');
			//
			$("#gallery_holder").animate({
				height: "390px",
			}, 300, function(){
					$('.ttl').css('border-bottom', '1px solid #cccccc');
				}
			);
			// MOVE DOWN GALLERY
			$("#gallery_holder div.gallery").animate({
				top: "-20px",
			}, 300, function(){
					// do when done
				}
			);
		}
	});
});
//
$(function() {
	$('#nl_signup').bind('click', function() {
		var eml = $('#nl_email').val();
		//
		if(is_email(eml)){
			$('#nl_email').val('Enter your email address here...');
			$(this).colorbox({
				href:'nl_signup.php?eml='+eml,
				rel:'nofollow',
				initialHeight:'200',
				initialWidth:'200',
				innerWidth:'500',
				escKey:false,
				overlayClose:false,
				onLoad: function() {
					$('#cboxClose').remove();
				},
				onComplete: function() {
					var c_height = $('#cboxLoadedContent').height();
					$('#cboxContent').height(c_height);
					$('#cboxLoadedContent').height(c_height);
					$('#cboxMiddleLeft').height(c_height);
					$('#cboxMiddleRight').height(c_height);
				}
			});
		} else {
			alert('Please enter a valid email address');	
		}
	});
});
//
$(document).ready(function(){
	$('#frm_sub').bind('click', function() {
		do_enq('acc');
	});
	//
	$('#stf_sub').bind('click', function() {
		do_enq('stf');
	});
	//
	$('#lwu_sub').bind('click', function() {
		do_enq('lwu');
	});
});
//
function do_enq(typ) {
	//
	switch(typ)
	{
	case 'acc':
		// VALIDATE ACCOMMODATION ENQUIRY
		var sub_form = true;
		$('#form_errors ul').empty();
		$('#form_div .err').each(function(){
			$(this).removeClass('err');
			var curr_width = $(this).width();
			$(this).width(curr_width+23);
			$(this).css('background', 'none');
			$(this).css('padding-right', '7px');
		});
		//
		if($("#enq_fname").val() == ""){
			input_err('#enq_fname');
			$('#form_errors ul').append('<li>Please enter your first name</li>');
			sub_form = false;
		}
		//
		if($("#enq_lname").val() == ""){
			input_err('#enq_lname');
			$('#form_errors ul').append('<li>Please enter your last name</li>');
			sub_form = false;
		}
		//
		if(!is_email($("#enq_email").val())){
			input_err('#enq_email');
			$('#form_errors ul').append('<li>Please enter a valid email address</li>');
			sub_form = false;
		}
		//
		if($("#enq_telnum").val() == ""){
			input_err('#enq_telnum');
			$('#form_errors ul').append('<li>Please enter a phone number</li>');
			sub_form = false;
		}
		//
		if($("#acc_id").val() == "notallowed"){
			//input_err('#acc_id');
			$("#acc_id").css('padding-right', '30px');
			$("#acc_id").css('background', 'url(/images/ui/ico_err.png) right 3px no-repeat #ffffff');
			$('#form_errors ul').append('<li>Please pick a property or set is to "Choose an Accommodation"</li>');
			sub_form = false;
		} else {
			$("#acc_id").css('padding-right', '4px');
			$("#acc_id").css('background', '');
		}
		//
		if (!sub_form) {
			pop_msg('form_errors');
			return false;
		} else {
			//alert('Success');
			$("#form_div").wrap('<form id="enq_info" />');
			$('#enq_info').attr('action', '/sys_mailer.php');
			$('#enq_info').attr('method', 'post');
			$('#enq_info').submit();
			return true;
		}
		//
	break;
	//
	case 'gen':
		// VALIDATE GENERAL ENQUIRY
		var sub_form = true;
		$('#form_errors ul').empty();
		$('#form_div input.err').each(function(){
			$(this).removeClass('err');
			var curr_width = $(this).width();
			$(this).width(curr_width+23);
			$(this).css('background', 'none');
			$(this).css('padding-right', '7px');
		});
		//
		if($("#enq_fname").val() == ""){
			input_err('#enq_fname');
			$('#form_errors ul').append('<li>Please enter your first name</li>');
			sub_form = false;
		}
		//
		if($("#enq_lname").val() == ""){
			input_err('#enq_lname');
			$('#form_errors ul').append('<li>Please enter your last name</li>');
			sub_form = false;
		}
		//
		if(!is_email($("#enq_email").val())){
			input_err('#enq_email');
			$('#form_errors ul').append('<li>Please enter a valid email address</li>');
			sub_form = false;
		}
		//
		if($("#enq_telnum").val() == ""){
			input_err('#enq_telnum');
			$('#form_errors ul').append('<li>Please enter a phone number</li>');
			sub_form = false;
		}
		//
		if (!sub_form) {
			pop_msg('form_errors');
			return false;
		} else {
			//alert('Success');
			$("#form_div").wrap('<form id="enq_info" />');
			$('#enq_info').attr('action', '/sys_mailer.php');
			$('#enq_info').attr('method', 'post');
			$('#enq_info').submit();
			return true;
		}
		//
	break;
	//
	case 'lwu':
		// VALIDATE LIST WITH US ENQUIRY
		var sub_form = true;
		$('#form_errors ul').empty();
		$('#form_div input.err').each(function(){
			$(this).removeClass('err');
			var curr_width = $(this).width();
			$(this).width(curr_width+23);
			$(this).css('background', 'none');
			$(this).css('padding-right', '7px');
		});
		//
		$('#how_hear_txt img').remove();
		//
		if($("#enq_fname").val() == ""){
			input_err('#enq_fname');
			$('#form_errors ul').append('<li>Please enter your first name</li>');
			sub_form = false;
		}
		//
		if($("#enq_lname").val() == ""){
			input_err('#enq_lname');
			$('#form_errors ul').append('<li>Please enter your last name</li>');
			sub_form = false;
		}
		//
		if(!is_email($("#enq_email").val())){
			input_err('#enq_email');
			$('#form_errors ul').append('<li>Please enter a valid email address</li>');
			sub_form = false;
		}
		//
		if($("#enq_telnum").val() == ""){
			input_err('#enq_telnum');
			$('#form_errors ul').append('<li>Please enter a phone number</li>');
			sub_form = false;
		}
		//
		if($("#how_hear").val() == ""){
			$('#how_hear_txt').append('<img src="/images/ui/ico_err.png" style="float:right;">');
			//
			$('#form_errors ul').append('<li>Please tell us how you heard about us</li>');
			sub_form = false;
		}
		//
		if (!sub_form) {
			pop_msg('form_errors');
			return false;
		} else {
			//alert('Success');
			$("#form_div").wrap('<form id="enq_info" />');
			$('#enq_info').attr('action', '/sys_lwu.php');
			$('#enq_info').attr('method', 'post');
			$('#enq_info').submit();
			return true;
		}
		//
	break;
	//
	case 'stf':
		// VALIDATE ACCOMMODATION ENQUIRY
		var sub_form = true;
		$('#form_errors ul').empty();
		$('#form_div input.err').each(function(){
			$(this).removeClass('err');
			var curr_width = $(this).width();
			$(this).width(curr_width+23);
			$(this).css('background', '#ffffff');
			$(this).css('padding-right', '7px');
		});
		//
		if($("#eml_name").val() == ""){
			input_err('#eml_name');
			$('#form_errors ul').append('<li>Please enter your name</li>');
			sub_form = false;
		}
		//
		if($("#stf_email_01").val() == '' && $("#stf_email_02").val() == '' && $("#stf_email_03").val() == ''){
				$('#form_errors ul').append('<li>Please enter at least one email address</li>');
				sub_form = false;
		} else {
			if($("#stf_email_01").val() != '' && !is_email($("#stf_email_01").val())){
				input_err('#stf_email_01');
				$('#form_errors ul').append('<li>Please enter a valid email address in box one</li>');
				sub_form = false;
			}
			if($("#stf_email_02").val() != '' && !is_email($("#stf_email_02").val())){
				input_err('#stf_email_02');
				$('#form_errors ul').append('<li>Please enter a valid email address in box two</li>');
				sub_form = false;
			}
			if($("#stf_email_03").val() != '' && !is_email($("#stf_email_03").val())){
				input_err('#stf_email_03');
				$('#form_errors ul').append('<li>Please enter a valid email address in box three</li>');
				sub_form = false;
			}
		}
		//
		if (!sub_form) {
			pop_msg('form_errors');
			return false;
		} else {
			//alert('Success');
			$("#form_div").wrap('<form id="enq_info" />');
			$('#enq_info').attr('action', '/sys_stf.php');
			$('#enq_info').attr('method', 'post');
			$('#enq_info').submit();
			return true;
		}
		//
	break;
	//
	default:
		alert('no form validation code:'+typ);
	}
}
//
// MSG COLORBOX
function pop_msg(id){
	$.colorbox({
		inline:true,
		href:"#"+id,
		transition:'none',
		initialWidth:50,
		initialHeight:14,
		onLoad: function() {
			//alert('onLoad');
			//$('#cboxClose').remove();
		},
		onComplete: function() {

		}
	});	
}
//
// HOME PAGE HOTSTEPPER
function hs_thumbs_do(carousel) {
	var main_carousel = $('#hotstepper #main').data('jcarousel');
	//$('#hotstepper .thumbs_ctrl .dot_bar ul li:eq('+(main_carousel.first-1)+')').addClass('curr');
	//
    $('#hotstepper .thumbs_ctrl .next').bind('click', function() {
		$('#hotstepper .thumbs_ctrl .dot_bar ul li.curr').removeClass('curr');
        carousel.next();
		main_carousel.next();
        return false;
    });
	//
    $('#hotstepper .thumbs_ctrl .prev').bind('click', function() {
		$('#hotstepper .thumbs_ctrl .dot_bar ul li.curr').removeClass('curr');
        carousel.prev();
		main_carousel.prev();
        return false;
    });
	//
    $('#hotstepper .thumbs_ctrl .dot_bar ul li').bind('click', function() {
		$('#hotstepper .thumbs_ctrl .dot_bar ul li.curr').removeClass('curr');
		//alert($(this).index());
		carousel.scroll($(this).index()+2);
		main_carousel.scroll($(this).index()+1);
        return false;
    });
	//
    $('#hotstepper #thumbs li').bind('click', function() {
		$('#hotstepper .thumbs_ctrl .dot_bar ul li.curr').removeClass('curr');
		//alert($(this).attr('id'));
		var idx = parseInt($(this).attr('id'));
		carousel.scroll(idx+2);
		main_carousel.scroll(idx+1);
        return false;
    });
}
//
function hs_thumbs_done(carousel) {
	var main_carousel = $('#hotstepper #main').data('jcarousel');
	//var thumb_carousel = $('#hotstepper #thumbs').data('jcarousel');
	//
	var curr_idx = main_carousel.first;
	//
	if(parseInt(curr_idx) <= 0){
		iterations = Math.abs(parseInt(curr_idx/4));
		curr_idx = 4+((4*iterations)+curr_idx);
		//curr_idx = (4*iterations)+parseInt(curr_idx);
	} else {
		iterations = Math.abs(parseInt(curr_idx/4));
		curr_idx = curr_idx-(4*iterations);
		//
		if(curr_idx == 0){
			curr_idx = 4;
		}
	}
	//
	$('#hotstepper .thumbs_ctrl .dot_bar ul li:eq('+(curr_idx-1)+')').addClass('curr');
	//
	var htmlstr = 'Current main:'+curr_idx;
	$('#inf').html(htmlstr);
}
//
$(function() {
	$('#hotstepper #main').jcarousel({
		visible: 1,
		//auto: 5,
		scroll: 1,
		wrap:'circular',
		//initCallback: hs_thumbs_do,
		easing: 'easeInOutQuart',
		animation: 700,
		buttonPrevHTML: null,
		buttonNextHTML: null
	});
	//
	$('#hotstepper #thumbs').jcarousel({
		vertical: true,
		//auto: 5,
		scroll: 1,
		start:2,
		wrap:'circular',
		initCallback: hs_thumbs_do,
		itemLoadCallback: {
			onAfterAnimation:hs_thumbs_done
		},
		easing: 'easeInOutQuart',
		animation: 700,
		buttonPrevHTML:null,
		buttonNextHTML:null
	});
});
//
// DETAILS GALLERY CAROUSEL STUFF
function gallery_initCallback(carousel) {
	$('.gallery .thumbs li:eq(0)').addClass('curr');
	$('.gallery .thumbs img').bind('click', function() {
		carousel.scroll($.jcarousel.intval($(this).parent().index()+1));
		$(this).parent().addClass('curr').siblings('.curr').removeClass('curr');
	});
	//
    $('.gallery #slides_next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('.gallery #slides_prev').bind('click', function() {
        carousel.prev();
        return false;
    });
}
//
function thumbs_off(carousel) {
	// TURN OFF THE CURRENT THUMBNAIL
	var thumb_carousel = $('.gallery .thumbs').data('jcarousel');
	if(thumb_carousel){
		$('.gallery .thumbs li').removeClass('curr');
	}
	
}
//
function thumbs_on(carousel) {
	// MAKE SURE CURRENT SLIDE IS VISIBLE IN THUMBS
	var curr_slide = carousel.first;
	var thumb_carousel = $('.gallery .thumbs').data('jcarousel');
	if(thumb_carousel){
		var first_thumb = thumb_carousel.first;
		var last_thumb = thumb_carousel.last;
		//
		if(curr_slide > last_thumb){
			thumb_carousel.next();
		}
		//
		if(curr_slide < first_thumb){
			thumb_carousel.prev();
		}
		//
		//FADE IN THE NEW ONE
		$('.gallery .thumbs li:eq('+(curr_slide-1)+') div').animate({
			opacity: 0
		}, 250, function() {
			// Animation complete.
			$('.gallery .thumbs li:eq('+(curr_slide-1)+')').addClass('curr');
			$(this).css({ opacity: 0.6 });
		});
	}
	
}
//
function thumbs_initCallback(carousel) {
    $('.gallery #thumbs_next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('.gallery #thumbs_prev').bind('click', function() {
        carousel.prev();
        return false;
    });
}
//
$(function() {
	$('.gallery').css('display', 'block');
	//
	$('.gallery .slides').jcarousel({
		visible: 1,
		scroll: 1,
		initCallback: gallery_initCallback,
		itemLoadCallback: {
			onBeforeAnimation:thumbs_off,
			onAfterAnimation:thumbs_on
		},
		easing: 'easeInOutQuart',
		animation: 700,
		buttonPrevHTML: null,
		buttonNextHTML: null
	});
	//
	var gall_width = $('.gallery .thumbs').outerWidth();
	//alert(gall_width);
	if(gall_width == 727){
		var v_num = 6;
		var s_num = 6;
	} else {
		var v_num = 4;
		var s_num = 4;
	}
	//
	$('.gallery .thumbs').jcarousel({
		visible: v_num,
		scroll: s_num,
		initCallback: thumbs_initCallback,
		easing: 'easeInOutQuart',
		animation: 700,
		buttonPrevHTML:null,
		buttonNextHTML:null
	});
});
//
function rev_initCallback(carousel) {
	$('#rev_slider .ctrl li:eq(0)').addClass('curr');
	$('#rev_slider .ctrl li').bind('click', function() {
		carousel.scroll($.jcarousel.intval($(this).index()+1));
		$(this).addClass('curr').siblings('.curr').removeClass('curr');
	});
}
//
$(function() {
	$('#rev_slider').jcarousel({
		visible: 1,
		scroll: 1,
		initCallback: rev_initCallback,
		easing: 'easeInOutQuart',
		animation: 700,
		buttonPrevHTML: null,
		buttonNextHTML: null,
		itemFallbackDimension: 380
	});
});
//
// INIT FADE SLIDESHOW
$(document).ready(function(){
	$('#apt_ss').cycle('fade');
	$('#vla_ss').cycle('fade');
	$('#revs').cycle('fade');
	//
	$('.fade_slide_show').css('display', 'block');
});
//
// SET DYN LIST FUNCTIONS
$(window).load(function () {
	$('.dyn_list li h2').bind('click', function() {
		$(this).parent().addClass('open');
		var this_sub = $(this).parent().children().eq(1);
		if(this_sub.css('display') == 'none'){
			this_sub.css('display', 'block');
		} else {
			$(this).parent().removeClass('open');
			this_sub.css('display', 'none');
		}
	});
});
