
//	Basic open window
function openWindow(url,width,height) {
	var rand = Math.floor(Math.random()*1001)
	var PopUpWin=window.open(url,"popup"+rand,'width=' + width + ',height=' + height + ',resizeable=0,scrollbars=yes,menubar=no,status=no');
	PopUpWin.focus()
}

function fLogoSwap() {
	if ( $("#logo_english").length > 0 ) {
		setTimeout("$('#logo_english').fadeToggle('slow');fLogoSwap();",5000)
	}
}

$(document).ready(function() {

	fLogoSwap(); //Logo Swap

	$('img[rollover]').hover(function() {
        $(this).attr('tmp', $(this).attr('src')).attr('src', $(this).attr('rollover')).attr('rollover', $(this).attr('tmp')).removeAttr('tmp');
    }).each(function() {
        $('<img />').attr('src', $(this).attr('rollover'));
    });
	
	$('input[rollover]').hover(function() {
        $(this).attr('tmp', $(this).attr('src')).attr('src', $(this).attr('rollover')).attr('rollover', $(this).attr('tmp')).removeAttr('tmp');
    }).each(function() {
        $('<input />').attr('src', $(this).attr('rollover'));
    });


	 // Equal heights function
	 jQuery.fn.equalHeights=function() {
		var maxHeight=0;
		this.each(function(){
			if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
		});
		this.each(function(){
			$(this).height(maxHeight + "px");
			if (this.offsetHeight>maxHeight) {
				$(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
			}
		});
	};
	
	// navigation rollover function
	$("#navigation_list li ul").hover(function(){
		$(this).parent("li").children("a").addClass("nav_link_over");
	},function(){
		$(this).parent("li").children("a").removeClass("nav_link_over");
	})
	
	$("#site_search_form #search_keywords").focus(function(){
		if($("#site_search_form #search_keywords").val()=="Enter search"){
			$("#site_search_form #search_keywords").val('');
			$("#site_search_form #search_keywords").removeClass('default_search');
		}else{
			$("#site_search_form #search_keywords").select();
		}
	});
	
	$("#navigation_list li").not("#navigation_list li ul li, #navigation_list li.right_nav_link").hover(
		function() { 
			$("#navigation_list li").removeClass("nav_link_hover");
			$("#navigation_list li").not(".nav_link_hover").find("ul").fadeOut(0);
			$(this).not(".nav_link_hover").find("ul").fadeIn(0);
			$(this).not(".nav_link_on").addClass("nav_link_hover");
		}
	);
	
	$(".login_box").click(
		function() { 
			$("#update_box").slideUp(300, function() {
				$("#login_box").slideToggle(300);
			});
			return false;
		}
	);
	
	$(".update_box").click(
		function() { 
			$("#login_box").slideUp(300, function() {
				$("#update_box").slideToggle(300);
			});
			return false;
		}
	);
	// PNG fix for IE
	//$(document).pngFix();


	// Clears an input of default value on click - to use add class of: clear_on_focus
	var aClearPrev = new Array();
	$.each($('.clear_on_focus'), function(){
		aClearPrev[this.name] = this.title;
	});
	// clear input on focus
	$('.clear_on_focus').focus(function(){
		if($(this).val()==$(this).attr('title')) {
			$(this).removeClass("grey");
			aClearPrev[this.name] = $(this).val();
			$(this).val('');
		}
	});
	// if field is empty afterward, add text again
	$('.clear_on_focus').blur(function() {
		if($(this).val()=='') {
			$(this).val( aClearPrev[this.name] );
			$(this).addClass("grey");
		}
	});
	
});

// Hotel Favourites
function fHotelFav(vAction,hotelId) {
	$("#fav" + hotelId).load("/registration/accommodation_ajax.cfm",{action:vAction, hotel_id: hotelId});
	return false;
}
function fHotelSelect(hotelId) {
	$("#select"+hotelId+" a").html("Registration Form Updated").addClass("button_on");
	window.opener.populateHotel(hotelId);
	return false;
}

// ----------			NEWS MODULE			----------
nNewsModCurrent = 1;
function fNewsModNext() {
	if (nNewsModCurrent != nNewsModTotal) {
		if (nNewsModCurrent == 1) { $("#newsmod_button_prev").fadeTo(0,1); }
		$("#news_mod_"+nNewsModCurrent).hide();
		$("#news_mod_"+(nNewsModCurrent+1)).show();
		nNewsModCurrent += 1;
		if (nNewsModCurrent == nNewsModTotal) { $("#newsmod_button_next").fadeTo(0,0.4); }
	}
	return false;
}
function fNewsModPrev() {
	if (nNewsModCurrent != 1) {
		if (nNewsModCurrent == nNewsModTotal) { $("#newsmod_button_next").fadeTo(0,1); }
		$("#news_mod_"+nNewsModCurrent).hide();
		$("#news_mod_"+(nNewsModCurrent-1)).show();
		nNewsModCurrent -= 1;
		if (nNewsModCurrent == 1) { $("#newsmod_button_prev").fadeTo(0,0.4); }
	}
	return false;
}

//----------------- share module ---------------------//
//	Basic open window
function openWindow(url,width,height) {
	var rand = Math.floor(Math.random()*1001)
	var PopUpWin=window.open(url,"popup"+rand,'width=' + width + ',height=' + height + ',resizeable=0,scrollbars=yes,menubar=no,status=no');
	PopUpWin.focus()
}



// ----------			MINI FORM VALIDATIONS		-------------
function fValidateEmailAlerts() {
	if ($.trim($("#email_updates_email").val()) == "" || !fValidateIsEmail($("#email_updates_email").val()) ) {
		fErrorAlert('<h3 class="icon_excl">Get Email Updates</h3><p><strong>Please enter your email address.</strong></p>')
		return false;
	} else {
		return true;
	}
}
function fValidateHeaderLogin() {
	if ($.trim($("#site_login_email").val()) == "" || $.trim($("#site_login_password").val()) == "" || !fValidateIsEmail($("#site_login_email").val())) {
		fErrorAlert('<h3 class="icon_excl">Login</h3><p><strong>Enter your email address and password to log in.</strong></p>')
		return false;
	} else {
		return true;
	}
}


// Generic Email Validation
function fValidateIsEmail(email) {
	email = $.trim(email);
	var filter = new RegExp(/^([a-zA-Z0-9_\.\-'])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/);
	if ( email == "" || !filter.test(email) ) {
		return false;
	} else { return true; }
}

/* ----    ----    ----   ----		ALERT BOX LIGHTBOX	----    ----    ----   ---- */

//		HTML LINE BREAKS
// =--  replaces Javascript new lines with HTML breaks.
function HTMLizeJS(vMsg) {
	vMsg = vMsg.replace(/\n/g,"<br />")
	return vMsg;
}

//		Variables
var sAlertBoxID = "TBAlertBox"; // Identity of alert box wrapper div
var nAlertBoxOffset = 30; // The gap between window and top of the box
var bAlertLimitScroll = false; // Limit scrolling when page is long? (removes page content
var sPageWrapperID = "mainWrapper"; // ID of an element that contains the entire page for heights
var nPageHeightCorrection = 40; // Amount that the sPageWrapperID is wrong by if there's oddly positioned divs

//			Replacement Functions
// Fix for IE 7 needing clearfix, but Firefox and IE8 breaking with it.
var IEstyles = "";
if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) == "7") { IEstyles=" clearfix"} //clearfix

function fErrorAlert(vMsg,sCloseFunction) {
	if(!sCloseFunction) { var sCloseFunction = ""; }
	vMsg = HTMLizeJS(vMsg);
	vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="javascript:tb_alert_close();'+sCloseFunction+'" class="button_style button_cross'+IEstyles+'">Close</a></div>';
	tb_alertbox(vMsg);
}

// =--  Confirm Alert Replacement  --=
// Displays a Confirm and Cancel button.
// This doesn't work like a JS confirm as the code that calls it doesn't wait for a return.
// Therefore code to call on cancel can be put into the vCode variable, which can also be a function.
function fErrorConfirm(vMsg,vCode) {
	vMsg = HTMLizeJS(vMsg)
	vMsg = vMsg;
	vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="#" onclick="tb_alert_close();'+vCode+';return false" style="margin-right:50px;" class="button_style button_tick'+IEstyles+'">Confirm</a><a href="#" onclick="tb_alert_close();return false" class="button_style button_cross'+IEstyles+'">Cancel</a></div>';
	tb_alertbox(vMsg);
}

function fErrorConfirmLabels(vMsg,vCode,sLConfirm,sLCancel) {
	vMsg = HTMLizeJS(vMsg)
	vMsg = vMsg;
	vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="#" onclick="tb_alert_close();'+vCode+';return false" style="margin-right:50px;" class="button_style'+IEstyles+'">'+sLConfirm+'</a><a href="#" onclick="tb_alert_close();return false" class="button_style'+IEstyles+'">'+sLCancel+'</a></div>';
	tb_alertbox(vMsg);
}

function fErrorConfirmLink(vMsg,vLink) {
	vMsg = HTMLizeJS(vMsg)
	vMsg = vMsg;
	//vMsg = "<h3>Please Note...</h3>"+vMsg;
	vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="'+vLink+'" class="button_style icon_tick'+IEstyles+'" style="margin-right:40px;">Confirm</a><a href="#" onclick="tb_alert_close();return false" class="button_style button_cross'+IEstyles+'">Cancel</a></div>';
	return tb_alertbox(vMsg);
}

/*=- As Above but reversed so confirm does nothing -=*/
function fErrorConfirmReversed(vMsg,vCode) {
		vMsg = HTMLizeJS(vMsg)
		vMsg = vMsg;
		vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="#" onclick="tb_alert_close();return false" style="margin-right:50px;" class="button_style icon_tick'+IEstyles+'">Confirm</a><a href="#" onclick="tb_alert_close();'+vCode+';return false;" class="button_style button_cross'+IEstyles+'">Cancel</a></div>';
	return tb_alertbox(vMsg);
}

/*=- As Above but two actions -=*/
function fErrorConfirmTwoAction(vMsg,vCodeConfirm,vCodeCancel) {
	vMsg = HTMLizeJS(vMsg)
	vMsg = vMsg;
	vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="#" onclick="tb_alert_close();'+vCodeConfirm+';return false;" style="margin-right:50px;" class="button_style button_tick'+IEstyles+'">Confirm</a><a href="#" onclick="tb_alert_close();'+vCodeCancel+';return false;" class="button_style button_cross'+IEstyles+'">Cancel</a></div>';
	return tb_alertbox(vMsg);
}

function fErrorConfirmTwoActionLabels(vMsg,vCodeConfirm,vCodeCancel,sLConfirm,sLCancel) {
	vMsg = HTMLizeJS(vMsg)
	vMsg = vMsg;
	vMsg += '<div id="TBAlertButtons" class="center clearfix"><a href="#" onclick="tb_alert_close();'+vCodeConfirm+';return false;" style="margin-right:50px;" class="button_style'+IEstyles+'">'+sLConfirm+'</a><a href="#" onclick="tb_alert_close();'+vCodeCancel+';return false;" class="button_style'+IEstyles+'">'+sLCancel+'</a></div>';
	return tb_alertbox(vMsg);
}

var AllowSubmit = 0; // Switch used in IAF specific validation
function fPressSubmit() { AllowSubmit = 1; document.getElementById('submit').click(); }
function fPressSubmitForm(vFormID) { AllowSubmit = 1; document.getElementById(vFormID).submit(); }
function fPressYes() { AllowSubmit = 1; document.getElementById('button_yes').click(); }
function fAllowSubmit() { AllowSubmit = 1; }


/* ----    ----    ----		Alert Box ENGINE		----    ----    ---- */

// IE6 - WARNING: THIS DETECTION IS DEPRECIATED IN JQUERY 1.3
var isIE6 = jQuery.browser.msie && jQuery.browser.version.substr(0,1)=="6" ? true : false;

// For IE 6 we use a fudge to mimic position:fixed
function tb_ie6_pos() {
	if ($("#"+sAlertBoxID)){
	$("#"+sAlertBoxID).css("top", ($(window).scrollTop()+nAlertBoxOffset) + "px");
	}
}
if(isIE6) {
	$(window).scroll(function() {
		if ($("#"+sAlertBoxID).height() < $(window).height()-nAlertBoxOffset) {
	    	tb_ie6_pos();
		}
	});
}

// window resize listener
$(window).resize(function(){ tb_alert_check_sizing(); });

// Check size of lightbox to switch fixed or absolute positioning
function tb_alert_check_sizing() {
	var pageWrapperHeight = parseInt($("#"+sPageWrapperID).height()) + parseInt(nPageHeightCorrection);
	if ($("#"+sAlertBoxID).height() > $(window).height()-nAlertBoxOffset) {
		if (bAlertLimitScroll) {
			// Alert is taller than window
			$("#"+sAlertBoxID).css("position","absolute").css("top", nAlertBoxOffset+"px") ;
		}
		if(!isIE6) {
			$("#TBBoxBlackOut").height($(window).height() > $("#"+sAlertBoxID).height() ? $(window).height()+(nAlertBoxOffset*2) : $("#"+sAlertBoxID).height()+(nAlertBoxOffset*2) );
		}
	} else {
		// Alert is shorter than window
		pageWrapperHeight > $(window).height() ? $("#TBBoxBlackOut").height(pageWrapperHeight) : $("#TBBoxBlackOut").height($(window).height());
		if(!isIE6) {
			// centre position
			var topPx = ($(window).height()-$("#"+sAlertBoxID).height())/2;
			topPx >  $(window).height()/4 ? topPx = topPx-$(window).height()/6 : topPx=topPx;
			if (jQuery.browser.msie) { topPx += document.body.scrollTop; } // Hack for CL2011 as some muppet used HTML Doc Type, not XHTML
			$("#"+sAlertBoxID).css("top",topPx+"px");
		}
	}
}

function tb_alertbox(sContent) {
	var pageWrapperHeight = parseInt($("#"+sPageWrapperID).height()) + parseInt(nPageHeightCorrection);
	if ( $("#"+sAlertBoxID).length > 0 ) { tb_alert_close(); }
	$("body").append('<div id="'+sAlertBoxID+'" class="TBPosFixed"><div id="TBAlertBoxInner">'+sContent+'</div><!--[if lte IE 6.5]><iframe></iframe><![endif]--></div><div id="TBBoxBlackOut"></div>'); // Add Container
	pageWrapperHeight > $(window).height() ? $("#TBBoxBlackOut").height(pageWrapperHeight) : $("#TBBoxBlackOut").height($(window).height());
	if(isIE6) { tb_ie6_pos(); } else { tb_alert_check_sizing(); }
	return false;
}

function tb_alert_close() {
	$("#"+sAlertBoxID).remove();
	$("#TBBoxBlackOut").remove();
}

/* ----    ----    ----		END Alert Box ENGINE		----    ----    ---- */

