/***************************************
 Gallery popup
****************************************
 This script is dependent on:
	prototype.js
***************************************/

// Global variables to store current scroll position
var originalScrollX = 0;
var originalScrollY = 0;

function showOverlay()
{
	var body = document.getElementsByTagName('body')[0];

	// Get window dimensions
	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// Store current scroll position
	if (document.documentElement && document.documentElement.scrollTop)
		originalScrollY = document.documentElement.scrollTop;
	else if (document.body)
		originalScrollY = document.body.scrollTop;

	var overlay = Element.extend(document.createElement('div'));
	overlay.setAttribute('id', 'gallery_overlay');
	body.appendChild(overlay);
	
	overlay.setStyle({
		display: 'none',
		visibility: 'hidden',
		position: 'absolute',
		top: 0,
		left: 0,
		width: '100%',
		zIndex: '1500'
	});
	
	if (Prototype.Browser.Gecko) {
		overlay.setStyle({
			backgroundImage: 'url(/img/galleryOverlay70.png)',
			backgroundRepeat: 'repeat',
			height: windowHeight+'px'
		});			
	} else {
		overlay.setStyle({
			opacity: 0.7,
			backgroundImage: 'url(/img/galleryOverlay.png)',
			backgroundRepeat: 'repeat',
			height: windowHeight+'px'
		});
	}

	$('gallery_overlay').style.display = 'block'
	$('gallery_overlay').style.visibility = 'visible';
	
	window.scrollTo(0,0); // Reset the windows scroll position (back to the top)
	hideScrollBars(); // Hide the scroll bars
}

function hideOverlay()
{
	$('gallery_overlay').remove();
	showScrollBars(); // Show the scroll bars
	window.scrollTo(originalScrollX, originalScrollY); // and scroll the window back to its original position
}

// Functions to show/hide main scrollbars...
// For some reason setting overflow:hidden on body tag in IE doesn't work, so set it on the html tag aswell
function hideScrollBars() { document.body.style.overflow='hidden'; document.getElementsByTagName('html')[0].style.overflow='hidden'; }
function showScrollBars() { document.body.style.overflow='auto'; document.getElementsByTagName('html')[0].style.overflow='auto'; }

function showImage(imagepath, imageWidth, imageHeight)
{
	showOverlay();
	var body = document.getElementsByTagName('body')[0];	

	var popup = Element.extend(document.createElement('div'));
	popup.setAttribute('id', 'gallery_popup');

	var image = Element.extend(document.createElement('img'));
	image.setAttribute('id', 'gallery_image');
	image.setAttribute('src', imagepath);
	image.setAttribute('width', imageWidth);
	image.setAttribute('height', imageHeight);

	popup.appendChild(image);
	popup.appendChild(Element.extend(document.createElement('br')));
	popup.appendChild(document.createTextNode("Click to close"));
	body.appendChild(popup);
	popup.setStyle({
		cursor: 'pointer',
		display: 'none',
		visibility: 'hidden',
		position: 'absolute',
		top: '50%',
		left: '50%',
		width: (imageWidth)+'px',
		height: (imageHeight+13)+'px',		
		marginLeft: '-'+((imageWidth)/2)+'px',
		marginTop: '-'+((imageHeight)/2)+'px',
		padding: '5px',
		zIndex: '1501',
		textAlign: 'center',
		backgroundColor: '#FFF'
	});
	image.setStyle({ marginBottom: '3px' });
		
	$('gallery_popup').style.display = 'block'
	$('gallery_popup').style.visibility = 'visible';

	Event.observe($('gallery_popup'), "click", function(e) {
		hideImage();return false;
	});

	return false;
}

if ($('lightwindow_title_bar_close_link')) {
	Event.observe('lightwindow_title_bar_close_link', 'click', this.deactivate.bindAsEventListener(this));
	$('lightwindow_title_bar_close_link').onclick = function() {return false;};
}

function hideImage()
{
	$('gallery_popup').remove();
	hideOverlay();
}

function newaddressCheck() {
	if ($('newaddress').checked) {
		$('newAddressBlock').style.display = "block";
	} else {
		$('newAddressBlock').style.display = "none";
	}
	
}

function checkcode () {	   
    
  var re = new RegExp("^RFS", "i");
  if ($('ResourcePromoCode').value.match(re)) {
    alert("Sorry, it is not possible to book reduced ferry travel for this promotion from this page. Please go to the web address printed in the Red Funnel voucher leaflet that you should have received from your Holiday Park.");
    return false
  }
  
  return true;
}