$(document).ready(function() {

	///////////////// HANDLES XMAS THEME SWITCH ////////////////////
		
	var isXmas 			= false;
		xmasBannerStart = new Date(),
		xmasBannerEnd	= new Date(),
		todaysDate		= new Date();

	xmasBannerStart.setFullYear(2011,10,30);
	xmasBannerEnd.setFullYear(2011,11,26);

	if(todaysDate > xmasBannerStart && todaysDate < xmasBannerEnd){	
		isXmas = true;
	}

	if(isXmas){
		$('#pageContainerInner').css({ backgroundImage : 'url(/static-images/xmas-footer-bg.jpg)' });
	}
	
	///////////////////////////////////////////////////////////////

	if ($("#flashClockContent").length > 0) { 
		var flashvars = {}, params = { wmode:"transparent" }, attributes = {};
		swfobject.embedSWF("/static-flash/rsh_clock.swf", "flashClockContent", "125", "50", "9.0.0", false, flashvars, params, attributes);
	}

	if ($("#homeFlashBanner").length > 0) { 
		var flashvars = {}, params = { wmode:"transparent" }, attributes = {};
		swfobject.embedSWF("/static-flash/rsh10.swf", "homeFlashBanner", "710", "274", "9.0.0", false, flashvars, params, attributes);	
	}
	
	if ($("#bookingColumnAd").length > 0) { 
		var flashvars = {}, params = { wmode:"transparent" }, attributes = {};
	
		if(isXmas){
			swfobject.embedSWF('/static-flash/xmas-register.swf', "bookingColumnAd", "240", "100", "9.0.0", false, flashvars, params, attributes);			
		}
		else{
			swfobject.embedSWF('/static-flash/register.swf', "bookingColumnAd", "240", "100", "9.0.0", false, flashvars, params, attributes);	
		}
		

	}/**/
/*		loyalty flash banner
	if ($("#bookingColumnAd").length > 0) { 
		var flashvars = {}, params = { wmode:"transparent" }, attributes = {};
		swfobject.embedSWF("/static-flash/loyalty1.swf", "bookingColumnAd", "246", "63", "9.0.0", false, flashvars, params, attributes);
	}/**/

	if ($("#homeAdSpace").length > 0) { 
		var flashvars = {}, params = { wmode:"transparent" }, attributes = {};
		swfobject.embedSWF("/static-flash/loyalty2.swf", "homeAdSpace", "266", "124", "9.0.0", false, flashvars, params, attributes);	
	}	

	if ($("#scratchFormFlashLeft").length > 0) { 
		var flashvars = {}, params = { wmode:"transparent" }, attributes = {};
		swfobject.embedSWF("/static-flash/side_panel.swf", "scratchFormFlashLeft", "210", "601", "9.0.0", false, flashvars, params, attributes);	
	}

	if ($("#scratchcardPlayFooter").length > 0) { 
		var flashvars = {}, params = { wmode:"transparent" }, attributes = {};
		swfobject.embedSWF("/static-flash/coins_bottom.swf", "scratchcardPlayFooter", "710", "220", "9.0.0", false, flashvars, params, attributes);	
	}
	
	if ($("#scratchIntroPlayButton").length > 0) { 
		var flashvars = {}, params = { wmode:"transparent" }, attributes = {};
		swfobject.embedSWF("/static-flash/single_coin.swf", "scratchIntroPlayButton", "165", "240", "9.0.0", false, flashvars, params, attributes);	
	}
	
	if ($("#corporateFlashBanner").length > 0) { 
		var flashvars = {}, params = { wmode:"transparent" }, attributes = {};
		swfobject.embedSWF("/static-flash/rsh10.swf", "corporateFlashBanner", "710", "274", "9.0.0", false, flashvars, params, attributes);	
	}

	//attachFloatingBG.init();
	doOfferTable.init();
	doCollationNav.init();
	doModuleHeightNormalisation.init();
	jumpMenu.init();

	destinationsCarousel = new miniCarousel($('div.carousel').eq(0)); // currently just works on the first one but we can do an each if we ever need more than one per page
	homeCarousel.init()
	
	normaliseHome();
	hotelDates();
	lightboxesSetUp();
	//checkTableCols();
	
	
	$('a#printMe').click(function(e){
		window.print();	
		e.preventDefault();
	});
	
	$('#locationsForm p').click(function(){
		if( $('#allLocations').is(':visible') ) {
			$('#allLocations').hide();
			$(this).text('Show all');
		}
		else {
			$('#allLocations').show();
			$(this).text('Hide');
		}
	});
	
	mobileUserCheck();
	railEurope.init();

});

var railEurope = {
	init : function (){
		if($('body').hasClass('fullWidth')){
			this.addBanners();
			this.moveOffers();
			this.normalize();
		};
	},
	addBanners  : function (){
		$.each(REBanners,function (){
			var ele = $('<div />');
			var uri = this.img + new String (Math.random()).substring (2, 11);
			ele.append('<a href="' + this.link + '" target="_BLANK"><img src="' + uri + '" border=0></a>')
			ele.appendTo('#bannersWrapper');
		});
	},
	moveOffers : function (){
		
		var clonedOffers = $('div#topOffersWrapper').clone();
		$('div#topOffersWrapper').remove();
		$('div.leftPanel').append(clonedOffers);
		$('div#topOffersWrapper').show();
		
	},
	normalize : function (){
		$('div.related .panel .content').ceNormaliser();
	}
};

/*function checkTableCols(){
	
	var nightsCount = $('table.roomType tbody tr:nth-child(3) td').length;
	
	if ($('table.roomType tbody tr:nth-child(3) td').length > 7) { //more than 5 DAY cols	
		
		$('table.roomType tbody td:not(.firstCol) span').css({'fontSize':'12px','fontWeight':'normal'});
		//$('table.roomType tbody td.lastCol > span').css('marginTop','2px');//empty span by PHP

	}
}*/

function lightboxesSetUp(){
	$('a.lightbox').click(function(e) {
		$('body').addClass('lightboxActive'); 
		$('div#pageContainerOuter').css('marginLeft','-512px');//compensate for no scrollbar
		$('<div id="overlay"></div>').css({'top': $(document).scrollTop(),'opacity':'0'}).animate({'opacity': '0.5'}, 'slow').appendTo('body');
		$('<div id="lightbox"></div>').hide().appendTo('body');

			if($(this).hasClass('help')){//popup help box/text

				var strHelp = $('div#help').html();
				getWinDims();
				
				$('<div />').append(strHelp).attr('id','helpPopup').css({'width':'275px','height':'200px'}).load().appendTo('#lightbox');
				
				$('<span />').attr({'id':'closeBtn', 'title':'Close'}).text('Close').click(function(){
					removeLightbox();
				  }).prependTo('#helpPopup');
				 
			}else if($(this).is('#updateSearch')){//popup hotel search form
				
				$('body').removeClass('lightboxActive');
				$('#overlay').css({'top':'0','backgroundImage':'none','position':'fixed'}); 
				var strHTMLForm = $('div#tabSearch').html();
				getWinDims();
				$('<div />').append(strHTMLForm).attr({'id':'tabSearch','class':'popup'}).load().appendTo('#lightbox');
			
				$('#lightbox #arrT').attr('id','arr');//T = temp
				$('#lightbox #depT').attr('id','dep');
				$('#lightbox #arrCalT').attr('id','arrCal');
				$('#lightbox #depCalT').attr('id','depCal');
				$('#lightbox #tabSearch form').attr({'id':'hotelSearchForm','name':'hotelSearchForm'});
				
				$('<span></span>').attr({'id':'closeBtn', 'title':'Close'}).text('Close').click(function(){
					removeLightbox();
				  }).prependTo('#tabSearch.popup');
				 
			}else if($(this).hasClass('map')){//popup maps - http://gmaps-samples.googlecode.com/svn/trunk/simplewizard/makestaticmap.html
				
				var hotelName = $(this).parents('.block').children('h2').text();
				
				$('<img />').attr('src', $(this).attr('href')).load(function(){
					positionLightbox();
				  }).appendTo('#lightbox').parent().addClass('mapBox').prepend('<h3>'+ hotelName +'</h3><span id="closeBtn" title="Close">Close</span>');
				  
				  $('#lightbox #closeBtn').click(function(){removeLightbox(); })

			}else if($(this).hasClass('starBox')){//popup msg about star ratings
				
				var starURL = $(this).attr('href');
				getIFrameDims();
				
				$('<div id="starsWrap"><iframe scrolling="auto" frameborder="0" allowtransparency="true" marginheight="0" marginwidth="0" src="'+ starURL +'" style="height:450px;margin:0;padding:0;width:650px;scroll:auto;"><\/iframe><\/div>').load().appendTo('#lightbox');

				$('<span />').attr({'id':'closeBtn', 'title':'Close'}).text('Close').click(function(){
					removeLightbox();
				}).prependTo('#starsWrap');

			}else{//an image

				$('<img />').attr('src', $(this).attr('href')).attr('title','Close').css('cursor','pointer').load(function(){
					positionLightbox();
				  }).click(function(){
					removeLightbox();
				  }).appendTo('#lightbox');
			}
		
		return false;;
	  });	
}

function getIFrameDims(){
  var topH = ($(window).height() - 450) / 2;
  var leftH = ($(window).width() - 650) / 2;	
  $('#lightbox').css({'top': topH + $(document).scrollTop(), 'left': leftH}).fadeIn('fast');
}

function getWinDims(){
  var topH = ($(window).height() - 200) / 2;
  var leftH = ($(window).width() - 275) / 2;	
  $('#lightbox').css({'top': topH + $(document).scrollTop(), 'left': leftH}).fadeIn('fast');
}

function positionLightbox() {
  var top = ($(window).height() - $('#lightbox').height()) / 2;
  var left = ($(window).width() - $('#lightbox').width()) / 2;
  $('#lightbox').css({'top': top + $(document).scrollTop(), 'left': left}).fadeIn('fast');
}

function removeLightbox() {
  $('#overlay, #lightbox').fadeOut('slow', function(){
      $(this).remove();
      $('body').removeClass('lightboxActive'); // show scrollbars
	  $('div#pageContainerOuter').css('marginLeft','-503px');
    });
}

var hotelDates = function (){
	
	$('#tabSearch li').click(function(){
	
		$('#tabSearch li.on').add('#tabSearch div.on').removeClass('on');
		var strTab = $(this).attr('class');
		$(this).add('#tabSearch div#'+ strTab).addClass('on');
		
		if ($('#tabSearch #trainTab').is('.on')) {
			$('#tabSearch').addClass('withOutImg');	
		} else {
			$('#tabSearch').removeClass('withOutImg');	
		}
		
	});
	
	$('#tabSearch #addAnother').click(function(){
		
		var rowCount = $('#tabSearch .content .row').length -4;
		
		if(rowCount < 4){
		
			var theRow = rowCount +2;
			$('#tabSearch .content .row:last-child').prev().clone().insertAfter('#tabSearch .content .row:eq('+ theRow +')');
			
			var stri = rowCount;
			var xi = rowCount +1;
				
			$('#tabSearch legend:last').text('Room '+ xi);
			$('#tabSearch fieldset:last label.qty1').attr('for','adultsHotel_'+ xi);
			$('#tabSearch fieldset:last select.qty1').attr('id','adultsHotel_'+ xi).attr('name','adultsHotel_'+ xi);
			$('#tabSearch fieldset:last label.qty2').attr('for','childrenHotel_'+ xi);
			$('#tabSearch fieldset:last select.qty2').attr('id','childrenHotel_'+ xi).attr('name','childrenHotel_'+ xi);
			$('#tabSearch fieldset:last .kids').remove();
			
		}else{
			if($('#tabSearch p#addAnother + label.error').length == 0){
				$('#tabSearch p#addAnother').after('<label class="error" style="width: auto;">There is a maximum of 4 rooms per booking</label>');
				$('#tabSearch p#addAnother').remove();
			}
		}
	});	
	

};

var normaliseHome = function (){
	if($('.homeTwoColWrapper').length > 0){
		$('.homeTwoColWrapper .twoColWrapper').each(function (){
			var maxHeight = 0;
			$(this).children().each(function (){
				var height = $(this).find('.modContent').height();
				if(height > maxHeight){
					maxHeight = height
				};
			});
			$(this).children().each(function (){
				$(this).find('.modContent').height(maxHeight);
			});
			$()
		});
	};
};


var homeCarousel = {
	
	img: {
		small:70,
		large:100,
		smallOffset:15,
		largeOffset:0
	},
	spacing: 9,
	numItems:0,
	
	elem:null,
	
	scrolling:false,
	sizeDown:true,
	sizeUp:true,

	init:function() {
		if($('div.homeCarousel').length < 1 || $('div.homeCarousel img').length < 3) { return; }
		var obj = this;
		
		obj.elem = $('div.homeCarousel').eq(0);
		
		obj.elem.append('<a href="#" class="prev">Previous</a><a href="#" class="next">Next</a>');

		obj.elem.find('a.prev').click(function(ev) {
			ev.preventDefault();
			obj.scrollPrev();
		});

		obj.elem.find('a.next').click(function(ev) {
			ev.preventDefault();
			obj.scrollNext();
		});
		
		obj.numItems = obj.elem.find('div.hider li').length;
		
		
		obj.elem.find('div.hider li').removeClass('selected');
		
		// set initial sizing
		obj.elem.find('div.hider li img').css({'margin-top' : obj.img.smallOffset});
		obj.elem.find('div.hider li').css({ 'margin-right' : obj.spacing });
	
		// set the middle visible element to display large
		obj.elem.find('div.hider li img').eq(1).css({
			'height' : obj.img.large,
			'width' : obj.img.large,
			'margin-top' : obj.img.largeOffset
		});
	
		// set the width of the slider element
		obj.elem.find('div.slider').css({
			'width' : ( ( ( ( obj.numItems-1 ) * obj.img.small ) + obj.img.large ) ) + ( obj.numItems*obj.spacing ) + obj.img.large
		});
		
		obj.elem.find('div.slider').eq(0).css({'left':0});

	},
	
	scrollPrev:function() {
	
		var obj = this;
		if(obj.scrolling != false) { return; }
		
		// prevent further clicks until we're done
		obj.scrolling = true;
		obj.sizeDown = true;
		obj.sizeUp = true;	
		
		// first clone the first item and add it to the end of the list...
		obj.elem.find('div.hider li').eq(parseInt(obj.elem.find('div.hider li a').length, 10)-1).clone().prependTo(obj.elem.find('div.hider ul').eq(0));

		// reset left offset
		var s = obj.elem.find('div.slider').eq(0);
		s.css({ 'left':parseInt(s.css('left'), 10)-obj.img.small-obj.spacing });	

		var newPos = parseInt(s.css('left'), 10)+obj.img.small+obj.spacing;

		var currImg, prevImg;
		obj.elem.find('div.hider img').each(function(s, e) {
			if($(e).width() == obj.img.large) {
				currImg = $(e);
				prevImg = obj.elem.find('div.hider img').eq(s-1);
			}
		});

		s.animate({ 'left':newPos }, obj.speed, "swing", function() {  
			obj.scrolling = false;
			obj.scrollPrevEnd();
		});
		
		currImg.animate({ 'width':obj.img.small,'height':obj.img.small,'marginTop':obj.img.smallOffset }, obj.speed, "swing", function() {  
			obj.sizeDown = false;
			obj.scrollPrevEnd();
		});
		
		prevImg.animate({ 'width':obj.img.large, 'height':obj.img.large, 'marginTop':obj.img.largeOffset }, obj.speed, "swing", function() {  
			obj.sizeUp = false;
			obj.scrollPrevEnd();
		});	

	},
	
	scrollPrevEnd:function() {
		var obj = this;
		if(obj.scrolling != false || obj.sizeDown != false || obj.sizeUp != false) { return; }
		
		obj.elem.find('div.hider li:last').remove(); // remove item that was cloned
		obj.clickDisabled = false; // allow further clicks
	},
	
	scrollNext:function() {
		
		var obj = this;

		obj.scrolling = true;
		obj.sizeDown = true;
		obj.sizeUp = true;
		
		// first clone the first item and add it to the end of the list...
		obj.elem.find('div.hider li').eq(0).clone().appendTo(obj.elem.find('div.hider ul').eq(0));
		
		var s = obj.elem.find('div.slider').eq(0);
		var newPos = parseInt(s.css('left'), 10)-obj.img.small-obj.spacing;

		var currImg, nextImg;
		obj.elem.find('div.hider img').each(function(s, e) {
			if($(e).width() == obj.img.large) {
				currImg = $(e);
				nextImg = obj.elem.find('div.hider img').eq(s+1);
			}
		});
		
		s.animate({ 'left':newPos }, obj.speed, "swing", function() {  
			obj.scrolling = false;
			obj.scrollNextEnd(); 
		});
		
		currImg.animate({ 'width':obj.img.small,'height':obj.img.small,'marginTop':obj.img.smallOffset }, obj.speed, "swing", function() {  
			obj.sizeDown = false;
			obj.scrollNextEnd(); 
		});
		
		nextImg.animate({ 'width':obj.img.large, 'height':obj.img.large, 'marginTop':obj.img.largeOffset }, obj.speed, "swing", function() {  
			obj.sizeUp = false;
			obj.scrollNextEnd(); 
		});		
		
	},
	
	scrollNextEnd:function() {

		var obj = this;
		if(obj.scrolling != false || obj.sizeDown != false || obj.sizeUp != false) { return; }

		obj.elem.find('div.hider li').eq(0).remove(); // remove item that was cloned
		
		// reset left offset
		var s = obj.elem.find('div.slider').eq(0);
		s.css({'left':parseInt(s.css('left'), 10)+obj.img.small+obj.spacing});
		
		obj.clickDisabled = false; // allow further clicks

	}
}



// sets the height of the two items side by side to be equal on a per row basis
var doModuleHeightNormalisation = {
	init:function() {
	
		if($('div.homeTwoColWrapper div.moduleTwoCol').length > 1) {
			$('div.homeTwoColWrapper').each(function() {
				var h = 0;
				$(this).find('div.modContent').each(function() {
					if(parseInt($(this).height(), 10) > h) { h = parseInt($(this).height(), 10); }
				});
				$(this).find('div.modContent').css({'height':h});
			});
		}
		
		if($('div.linkPanel div.linkPanelContentMid').length > 1) {
			var h = 0;
			$('div.linkPanel div.linkPanelContentMid').each(function() {
				if(parseInt($(this).height(), 10) > h) { h = parseInt($(this).height(), 10); }
			});
			$('div.linkPanel div.linkPanelContentMid').css({'height':h});
		}		
	}
}




// add functionality for jump menu
var jumpMenu = {
	init:function() {
		if($('div.jumpMenu').length < 1) { return; }
		$('select').change(function() {
			if($(this).attr('value') != '') { location.href = $(this).attr('value'); }
		});
	}
}


// reposition ads on the destination landing page
var repositionLPAds = {
	init:function() {
		if($('#destinationLandingPage').length < 1) { return; }
		if($('div.linkPanelRow4').length > 0) {
			if($('div.contentColumnRightCol').length > 0) {
				$('div.contentColumnRightCol').html('<div class="linkPanelSquare marginAfter">' + $('div.linkPanelRow4').html() + '</div>');
				$('div.linkPanelRow4').css({'display':'none'});
				$('div.contentColumnRightCol div.linkPanel').removeClass('first');
				$('div.contentColumnRightCol div.linkPanel').each(function(s, e) {
					if(s%2 == 0) {
						$(e).addClass('first');
					}
				});
			} else { $('div.contentColumnSplit').eq(0).html($('div.linkPanelRow4')); }
		}
	}
}

// reposition the jump menu for landing pages
var repositionLPJump = {
	init:function() {
		if($('#destinationListMenu').length < 1) { return; }
		$('div.mainHeadingFullSpan').eq(0).append($('#destinationListMenu div.jumpMenu').eq(0));
	}
}


// reposition ads on the train operator page
var repositionTOAds = {
	init:function() {
		if($('#trainOperatorPage').length < 1) { return; }
		if($('div.linkPanelRow4').length > 0) {
			if($('div.contentColumnRightCol').length > 0) {
				$('div.contentColumnRightCol').html('<div class="linkPanelSquare marginAfter">' + $('div.linkPanelRow4').html() + '</div>');
				$('div.linkPanelRow4').css({'display':'none'});
				$('div.contentColumnRightCol div.linkPanel').removeClass('first');
				$('div.contentColumnRightCol div.linkPanel').each(function(s, e) {
					if(s%2 == 0) {
						$(e).addClass('first');
					}
				});
			} else { $('div.contentColumnSplit').eq(0).html($('div.linkPanelRow4')); }
		}
	}
}

// reposition the jump menu for train operator pages
var repositionTOJump = {
	init:function() {
		if($('#trainOperatorListMenu').length < 1) { return; }
		$('div.mainHeadingFullSpan').eq(0).append($('#trainOperatorListMenu div.jumpMenu').eq(0));
	}
}


var destinationHeroImage = {

	init:function() {
		if($('div.destinationHero').length < 1) { return; }
		var obj = this;
		
		$('div.destinationHero div.subImages li a').each(function(s, e) {
			$(e).click(function(ev) { ev.preventDefault(); });
			
			$(e).mouseenter(function() { obj.switchHero(s); });
			$(e).mouseleave(function() { obj.switchHero(); });
			
		});
	},
	
	switchHero:function(num) {
		if(!num && num != 0) {
			$('div.destinationHero img.subImage').css({'left':-9999});
			$('div.destinationHero img.mainImage').css({'left':0});
			$('div.destinationHero p.caption').eq(0).html($('div.destinationHero img.mainImage').eq(0).attr('title'));
		} else {
			var srcElem = $('div.destinationHero div.subImages li a').eq(num);

			if($('div.destinationHero img.subImage.pos'+num).length < 1) {
				$('div.destinationHero p').eq(0).before('<img src="' + srcElem.attr('href') + '" title="' + srcElem.find('img').eq(0).attr('title') + '" alt="' + srcElem.find('img').eq(0).attr('alt') + '" width="706" class="subImage pos' + num + '"/>');
			}
			
			$('div.destinationHero img.mainImage').css({'left':-9999});
			$('div.destinationHero img.subImage.pos'+num).css({'left':0});
			$('div.destinationHero p.caption').eq(0).html(srcElem.find('img').eq(0).attr('title'));

			
		
		}
		
	
	}

}


var doCollationNav = {
	
	numItems: 0,
	selected: 1,

	init:function() {
		if($('#collationGroupPage').length < 1) { return; }
		this.numItems = parseInt($('div.collationGroup').length, 10);
		if(this.numItems > 1) { this.createNav(); }
	},
	
	createNav:function() {
		var obj = this;
		$('div.mainHeadingFullSpan').eq(0).addClass('noMargin');

		$('div.collationGroup').css({'display':'none'});
		$('div.collationGroup').eq(0).css({'display':'block'});
		
		var htmlOut = '';
		
		htmlOut += '<div class="paginationNav">';
		htmlOut += '	<ul class="paginationNav">';
		htmlOut += '		<li class="prev"><a href="#">&laquo; Previous</a></li>';

		for(var i=0; i < parseInt($('div.collationGroup').length, 10); i++) {
			htmlOut += '<li><a href="' + (parseInt(i, 10)+1) + '"' + (i==0 ? ' class="selected"' : '') + '>' + (parseInt(i, 10) +1) + '</a></li>';
		}

		htmlOut += '		<li class="next"><a href="#">Next &raquo;</a></li>';
		htmlOut += '	</ul>';
		htmlOut += '</div>';
		
		// append the control html
		$('#collationGroupPage div.mainHeadingFullSpan').after('<div class="fullWidthPlain roundedEnd"><div class="fullWidthPlainContent">' + htmlOut + '</div></div>');
		$('#collationGroupPage').append('<div class="collationPaginationControl">' + htmlOut + '</div>');
		
		// add the click events
		$('div.paginationNav li a').click(function(ev) {
			ev.preventDefault();
			if($(this).parent().hasClass('prev')) { obj.doPrev(); }
			else if($(this).parent().hasClass('next')) { obj.doNext(); }
			else { obj.displayItem($(this).attr('href')); }
		});
		
	},
	
	displayItem:function(num) {
		var obj = this;

		if(num < 1 || num > obj.numItems) { return; }
		obj.selected = parseInt(num, 10);
		
		$('div.collationGroup').css({'display':'none'});
		$('div.collationGroup').eq(num-1).css({'display':'block'});
		$('div.paginationNav').each(function(s, e) {
			$(e).find('li a').removeClass('selected');
			$(e).find('li a').eq(obj.selected).addClass('selected');	
		});
	},
	
	doPrev:function() { this.displayItem(this.selected-1); },
	doNext:function() { this.displayItem(this.selected+1); }
}


function miniCarousel(elem, smallImgSize, largeImgSize, topOffsetSmall, topOffsetLarge) {
	if(elem.length < 1) { return; }
	var obj = this;
	
	obj.img = {
		small:(!smallImgSize && smallImgSize != 0 ? 70 : smallImgSize),
		large:(!largeImgSize && largeImgSize != 0 ? 100 : largeImgSize),
		smallOffset:(!topOffsetSmall && topOffsetSmall != 0 ? 15 : topOffsetSmall),
		largeOffset:(!topOffsetLarge && topOffsetLarge != 0 ? 0 : topOffsetLarge)
	};
	
	obj.elem = elem;
	obj.current = 0;
	
	// these events are used with animate callbacks to determine what stage of the process we're at...
	obj.scrolling = false;
	obj.sizeDown = false;
	obj.sizeUp = false;
	obj.transitioning = false;
	obj.clickDisabled = false;

	obj.speed = 700;
	obj.spacing = 9; // to account for any spacing to the left/right of the images
	
	obj.numItems = parseInt(obj.elem.find('li').length, 10);
	
	// different logic required if there are less than five images
	if(obj.numItems > 4) {
		obj.current = 2;
		obj.createImageContainer(); // create containers for images
	}
	
}

// determine how to handle scroll (previous or next)
miniCarousel.prototype.scrollTo = function(num, dir) {
	
	var obj = this;
	
	if(!num && dir) { // if no num then 
		if(dir == 'prev') { var num = obj.elem.find('div.hider li a.carouselPos'+obj.current).eq(0).parent().prev().find('a').eq(0).attr('title'); }
		if(dir == 'next') { var num = obj.elem.find('div.hider li a.carouselPos'+obj.current).eq(0).parent().next().find('a').eq(0).attr('title'); }
	}
	
	// this check is required for IE7 which insists on adding the domain name to the href for the 0 element, and it has to be cast as a string first so FF doesn't complain...
	var checkNum = ''+num;
	checkNum = checkNum.split('/');
	if(checkNum.length > 1) { num = checkNum[checkNum.length-1]; }
	
	if(num == obj.current || obj.clickDisabled != false) { return; }
	obj.clickDisabled = true;
	
	var curr = 0, trg = 0;
	obj.elem.find('div.hider a').each(function(s, e) {
		if($(e).hasClass('carouselPos'+obj.current)) { curr = s; }
		if($(e).hasClass('carouselPos'+num)) { trg = s; }
	});
	
	if(trg > curr) { obj.scrollNext((trg-curr), num); } 
	else { obj.scrollPrev((curr-trg), num); }
}

// scroll to next item - if rep is passed then it will scroll more than once
miniCarousel.prototype.scrollNext  = function(rep, num) {
	var obj = this;

	obj.scrolling = true;
	obj.sizeDown = true;
	obj.sizeUp = true;
	
	// first clone the first item and add it to the end of the list...
	obj.elem.find('div.hider li').eq(0).clone(true).appendTo(obj.elem.find('div.hider ul').eq(0));
		
	var s = obj.elem.find('div.slider').eq(0);
	var newPos = parseInt(s.css('left'), 10)-obj.img.small-obj.spacing;

	var currImg, nextImg;
	obj.elem.find('div.hider img').each(function(s, e) {
		if($(e).width() == obj.img.large) {
			currImg = $(e);
			nextImg = obj.elem.find('div.hider img').eq(s+1);
		}
	});
	
	s.animate({ 'left':newPos }, obj.speed, "swing", function() {  
		obj.scrolling = false;
		obj.scrollNextEnd(rep, num); 
	});
	
	currImg.animate({ 'width':obj.img.small,'height':obj.img.small,'marginTop':obj.img.smallOffset }, obj.speed, "swing", function() {  
		obj.sizeDown = false;
		obj.scrollNextEnd(rep, num); 
	});
	
	nextImg.animate({ 'width':obj.img.large, 'height':obj.img.large, 'marginTop':obj.img.largeOffset }, obj.speed, "swing", function() {  
		obj.sizeUp = false;
		obj.scrollNextEnd(rep, num); 
	});	
	
	if(rep == 1) {
		obj.elem.find('ul.data div.textAndThumb').each(function(s, e) { 
			if($(this).css('display') == 'block') {  $(this).fadeOut(obj.speed);  } 
		});
		obj.elem.find('ul.data div.textAndThumb').eq(num).fadeIn(obj.speed);
	}

	obj.current = nextImg.parents('a').attr('class').split('carouselPos')[1];
	

}

// deals with the end state of the scrolling carousel
miniCarousel.prototype.scrollNextEnd  = function(rep, num) {
	
	var obj = this;
	if(obj.scrolling != false || obj.sizeDown != false || obj.sizeUp != false) { return; }

	obj.elem.find('div.hider li').eq(0).remove(); // remove item that was cloned
	
	// reset left offset
	var s = obj.elem.find('div.slider').eq(0);
	s.css({'left':parseInt(s.css('left'), 10)+obj.img.small+obj.spacing});
	
	if(rep > 1) { obj.scrollNext((rep-1), num); } 
	else { obj.clickDisabled = false; } // allow further clicks

}

// scroll to previous items
miniCarousel.prototype.scrollPrev  = function(rep, num) {
	
	var obj = this;

	obj.scrolling = true;
	obj.sizeDown = true;
	obj.sizeUp = true;
	
	// first clone the first item and add it to the end of the list...
	obj.elem.find('div.hider li').eq(parseInt(obj.elem.find('div.hider li a').length, 10)-1).clone(true).prependTo(obj.elem.find('div.hider ul').eq(0));

	// reset left offset
	var s = obj.elem.find('div.slider').eq(0);
	s.css({'left':parseInt(s.css('left'), 10)-obj.img.small-obj.spacing});
	
	var newPos = parseInt(s.css('left'), 10)+obj.img.small+obj.spacing;

	var currImg, prevImg;
	obj.elem.find('div.hider img').each(function(s, e) {
		if($(e).width() == obj.img.large) {
			currImg = $(e);
			prevImg = obj.elem.find('div.hider img').eq(s-1);
		}
	});

	s.animate({ 'left':newPos }, obj.speed, "swing", function() {  
		obj.scrolling = false;
		obj.scrollNextEnd(rep, num); 
	});
	
	currImg.animate({ 'width':obj.img.small,'height':obj.img.small,'marginTop':obj.img.smallOffset }, obj.speed, "swing", function() {  
		obj.sizeDown = false;
		obj.scrollPrevEnd(rep, num); 
	});
	
	prevImg.animate({ 'width':obj.img.large, 'height':obj.img.large, 'marginTop':obj.img.largeOffset }, obj.speed, "swing", function() {  
		obj.sizeUp = false;
		obj.scrollPrevEnd(rep, num); 
	});	

	if(rep == 1) {
		obj.elem.find('ul.data div.textAndThumb').each(function(s, e) { 
			if($(this).css('display') == 'block') {  $(this).fadeOut(obj.speed);  } 
		});
		obj.elem.find('ul.data div.textAndThumb').eq(num).fadeIn(obj.speed);
	}
	
	obj.current = prevImg.parents('a').attr('class').split('carouselPos')[1];
}


// deals with the end state of the scrolling carousel
miniCarousel.prototype.scrollPrevEnd  = function(rep, num) {
	
	var obj = this;
	if(obj.scrolling != false || obj.sizeDown != false || obj.sizeUp != false) { return; }
	
	obj.elem.find('div.hider li:last').remove(); // remove item that was cloned
	
	if(rep > 1) { obj.scrollPrev(rep-1, num); } 
	else { obj.clickDisabled = false; } // allow further clicks

}

// create containers for images
miniCarousel.prototype.createImageContainer = function() {
	
	var obj = this, htmlOut = '';
	
	
	obj.elem.find('ul').addClass('data');
	
	htmlOut += '<div class="hider">'
	htmlOut += '	<div class="slider"><ul>';
	
	obj.elem.find('li').each(function(s, e) {
		var cImg = $(e).find('img').eq(0);
		htmlOut += '<li><a href="' + ($(this).find('a').hasClass('useLink') ? $(this).find('a').attr('href') : '#') + '" class="' + ($(this).find('a').hasClass('useLink') ? 'useLink' : '') + '"><img src="' + cImg.attr('src') + '" alt="' + cImg.attr('alt') + '" title="' + cImg.attr('alt') + '" height="' + obj.img.small + '" width="' + obj.img.small + '"/></a></li>';
	});
	
	htmlOut += '	</ul></div>'

	htmlOut += '<div class="maskLeft">&nbsp;</div><div class="maskRight">&nbsp;</div>';
	htmlOut += '<a href="#" class="prev">Previous</a><a href="#" class="next">Next</a>';
	
	htmlOut += '</div>';
	obj.elem.prepend(htmlOut);
	
	// add click events to images
	obj.elem.find('div.hider li a').each(function(s, e) {
		$(e).attr({'title':''+s});
		$(e).addClass('carouselPos' + s);
		$(e).click(function(ev) {
			ev.preventDefault();
			if($(this).hasClass('useLink') && $(this).find('img').height() === 100){
				window.location = $(this).attr('href');
			}
			obj.scrollTo(s);
		});
	});
	
	obj.elem.find('div.hider a.prev').click(function(ev) {
		ev.preventDefault();
		obj.scrollTo(false,'prev');
	});

	obj.elem.find('div.hider a.next').click(function(ev) {
		ev.preventDefault();
		obj.scrollTo(false,'next');
	});
	
	// set initial sizing
	obj.elem.find('div.hider li img').css({'margin-top' : obj.img.smallOffset});
	obj.elem.find('div.hider li').css({ 'margin-right' : obj.spacing });
	
	// set the middle visible element to display large
	obj.elem.find('div.hider li img').eq(2).css({
		'height' : obj.img.large,
		'width' : obj.img.large,
		'margin-top' : obj.img.largeOffset
	});
	
	// set the width of the slider element
	obj.elem.find('div.slider').css({
		'width' : ( ( ( ( obj.numItems-1 ) * obj.img.small ) + obj.img.large ) ) + ( obj.numItems*obj.spacing ) + obj.img.large
	});
	
	// set all the data heights to be the same so transitions look neater
	var h = 0;
	obj.elem.find('ul.data div.textAndThumb').each(function(s, e) {
		if(parseInt($(e).height(), 10) > h) {
			h = parseInt($(e).height(), 10);
		}
	});
	
	obj.elem.find('ul.data').css({ 'height':h });
	obj.elem.find('ul.data div.textAndThumb').css({ 'height':h, 'display':'none' });
	obj.elem.find('ul.data div.textAndThumb').eq(obj.current).css({ 'display':'block' });
	
}

var attachFloatingBG = {

	// this function will handle all the logic around which flash file to show in the footer bg area
	// or whether to show a static image replacement - the non-js version is attached in the styles
	// and disabled in the js-enabled stylesheet where appropriate

	offset: 389,
	defaultBG: '/static-images/bg-flash-placeholder.jpg',

	init:function() {
		
		if($('#pageContainerOuter').length < 1) { return; }

		var obj = this;
		var flOffset = parseInt($('#pageContainerOuter').height(), 10) - obj.offset;

		$('#pageFlash').html('<img src="' + obj.defaultBG + '" alt=""/>');
		$('#pageFlash').css({'top':flOffset});
		
		/* requires switch statement to determine offset and image/flash to use */
		$('#pageContainerOuter').resize(function(e) {
			
			var flOffset = parseInt($('#pageContainerOuter').height(), 10) - obj.offset;
			$('#pageFlash').css({'top':flOffset});
			
		});
	}
}

var doOfferTable = {
	elem:null,

	init:function() {
		if($('div.homeTableArea').length < 1) { return; }
		
		var obj = this;
		obj.elem = $('div.homeTableArea').eq(0);
		
		/*
		var htmlOut = '<ul class="homeTableNav">';
		$('div.homeTableArea').find('div.homeTableData').each(function(s, e) {
			var selected = (s == 0 ? ' selected' : '');
			var tabID = $(e).find('h2').attr('id');
			var tabName = $(e).find('h2').html();
			
			htmlOut += '<li class="' + tabID + selected + '">';
			htmlOut += '<a href="#' + tabID + '"><span><strong>' + tabName + '</strong></span></a>';
			htmlOut += '</li>';
		
		});
		htmlOut += '</ul>';
		*/
		
		obj.elem.find('div.homeTableData').removeClass('first');
		obj.elem.find('div.homeTableData').eq(0).show();
		
		obj.elem.find('ul.homeTableNav a').each(function(s, e) {
			$(e).click(function(ev) {
				ev.preventDefault();
				$('ul.homeTableNav li').removeClass('selected');
				$(e).parent().addClass('selected');
				obj.elem.find('div.homeTableData').hide();
				obj.elem.find('div.homeTableData').eq(s).show();
			
			});
		});
		
	}

}

function mobileUserCheck(){

	if(isMobile()){
		
		$('.bookingColumn iframe').before('<a href="http://app.redspottedhanky.com/"><img src="/static-images/download-app-banner.jpg" width="238" height="363" alt="" /></a>');		
		
		$(window).load(function(){
			$('#bookingColumnAd').hide();
		});	
		
	}

}

function isMobile(){// Return boolean TRUE/FALSE
	var userAgentList = "_h797|160x|471x|480x|848b|a615|ahong|android|avantgo|b832|blackberry|blazer|c7100|c-810|compal|d615|d763|e860|el370|elaine|fennec|g83|hiptop|i250|iemobile|ipad|iphone|ipod|jasmine|lg210|lg370|lg380|lg47|lg840|lg920|lge |lgku|lgu900|m5252|m800|m881|mg50|mini 9.5|minimo|mk99|mob-x|mot-|mowser|mp500|mt12|n210|opera mini|operamobi|palm|palm|palm os|plucker|r800|rim8|s55|s580|s700|s920|sam-r|samu3|samu4|samu5|samu6|samu7|samu9|sk16d|sl900|sprint|symbian|t503|t66|u940|ux840|v120|vx10|vx1000|vx400|w398samr810|w839|webos|windows ce|windows phone|wireless|x160|x640|xda_|xiino|googlebot-mobile";
	var uAgent = navigator.userAgent.toLowerCase();
	var returnValue = false;
	var temp = $.each(userAgentList.split('|'),function (index,agent){
		if(uAgent.search(agent) > -1){
			returnValue = true;
			return;
		};
	});
	return returnValue;
};
