/* 	misc.js - Miscellaneous javascript functions used throughout the site
------------------------------------------------------------------------*/

// -------------------------------------------------------------------
// Apply preferred text size (using cookies)

// Run init function when dom is loaded - uses dLite.js library
DOMReady(initTextSize);

// Gather preferred text size from cookie and apply
function initTextSize(){
	var textSize = readCookie('textsizecookie')
	if (textSize)
	{
		changetext(textSize);
	}
}

//Change the base font size using the text size links.
function changetext(size) {
	var text = document.getElementById('centerCol');
	text.style.fontSize = size;
	createCookie('textsizecookie',size,999);

}


// -------------------------------------------------------------------
//Prevent right-click on images
var clickmessage="Please do not attempt to download this image by using the right-click menu."

document.oncontextmenu = function (event) {
	event = event || window.event;
	var target = event.target || event.srcElement;

	if (target.tagName=="IMG"){
		if (event.preventDefault) {	
		event.preventDefault();
		}	
	alert(clickmessage);
	return false;
	}								
}

// -------------------------------------------------------------------
//popup windows (e.g. terms on booking form)
function link_popup(src, features) {
  var url = src.getAttribute('href');
  var features = 'location=0,statusbar=0,scrollbars=1,resizable=1,menubar=0,width=400,height=350';
  var theWindow = window.open(url, '_blank', features);
  theWindow.focus();
  return theWindow;
}


//popup windows (variable size) - used for videos and misc
//eg <a href="/en/contact/privacy-policy-popup.html" target="_blank" onclick="link_popup_variable(this,300,1000); return false">

function link_popup_variable(src, width, height) {
// (has scrollbars)
  var url = src.getAttribute('href');
  var features = "location=0,statusbar=0,scrollbars=1,resizable=1,menubar=0,width="+width+",height="+height;
  var theWindow = window.open(url, "_blank", features);
  theWindow.focus();
  return theWindow;
}

function link_popup_video(src, width, height) {
// (no scrollbars)
  var url = src.getAttribute('href');
  var features = "location=0,statusbar=0,scrollbars=0,resizable=0,menubar=0,width="+width+",height="+height;
  var theWindow = window.open(url, "_blank", features);
  theWindow.focus();
  return theWindow;
}


// -------------------------------------------------------------------
// Cookie manipulation functions from http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// -------------------------------------------------------------------
// Rotating special offers code - makes use of jquery and cycle lite plugin
function switchDivs() {
	// use jquery cycle lite plugin to cycle through special offer divs
	jQuery('#holidays').cycle({ 
		timeout:	6000,
		speed: 	 	500, 
		before: 	onBefore 
	}); 
}

function onBefore(curr, next, opts, fwd){
	// Deals with a change in height of the containing element
	var currentHeight = jQuery(curr).height();
	//get the height of the next slide
	var nextHeight = jQuery(next).height();
	// set minimumn height as 160px;
	if (nextHeight < 160) {
		nextHeight = 160;
	}
	//set the container's height to that of the next slide
	jQuery(this).parent().animate({ 
		height: nextHeight
	}, 500 );
}
