//** Preload Images **//
var myimg = new Array();
function preloadimages() {
  for (i=0; i<preloadimages.arguments.length; i++) {
    myimg[i] = new Image();
    myimg[i].src = preloadimages.arguments[i];
  }
}

/*preloadimages("interface/minimap_over.gif","interface/button_findhotels_over.gif", "interface/button_subscribe_over.gif", "interface/menu/01_hotels_over.gif", "interface/menu/02_luxurybreaks_over.gif", "interface/menu/03_golf_over.gif", "interface/menu/04_healthbeauty_over.gif", "interface/menu/05_restaurants_over.gif", "interface/menu/06_weddings_over.gif", "interface/menu/07_giftvouchers_over.gif", "interface/menu/08_latestnews_over.gif");*/


//** emailThisPage **//
function emailThisPage() {
	var sThisPage = location.pathname;
	var newsite = "http://www.macdonaldhotels.co.uk"
	location.href = newsite + "/email.asp?ID=" + sThisPage;
	// document.getElementById("basehref").getAttribute("href") + "
}


//** setDefaultDates **//
if (document.forms['search']) {
	setDefaultDates(document.forms['search']);
}


//** Popup Window **//
function popUp(URL, popWidth, popHeight, popLeft, popTop, autoCenter) {
	day = new Date();
	id = day.getTime();
	if (autoCenter == 1) {
		var popLeft = (screen.width - popWidth) / 2;
		var popTop = (screen.height - popHeight) / 2;
	}
	eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+popWidth+",height="+popHeight+",left="+popLeft+",top="+popTop+"');");
}


function ForwardEmailShow() {
	var form = document.getElementById('ForwardEmailForm');
	var lbl = document.getElementById('ForwardEmailLink');
	form.style.display = '';
	lbl.style.display = 'none';
}


function iotbs() {
	switcher = new switchManager();
};

var switcher;

//setup initialisation function
//.. gecko, safari, konqueror and generic
if(typeof window.addEventListener != 'undefined') {
	window.addEventListener('load', iotbs, false);
}
//.. opera 7
else if(typeof document.addEventListener != 'undefined') {
	document.addEventListener('load', iotbs, false);
}
//.. win/ie
else if(typeof window.attachEvent != 'undefined') {
	window.attachEvent('onload', iotbs);
}


//preferences manager
function switchManager() {
	//string for storing the overall custom classname
	//I was originally storing it in the body class name directly
	//but 1.7+ mozilla builds were not honouring the trailing whitespace we need
	this.string  = '';

	//store reference to body element
	this.body = document.getElementsByTagName('body')[0];

	//store the initial classname
	this.initial = this.body.className;

	//if the default classname is empty, add "iotbs"
	//because we need there to be at least one classname already -
	//the leading and trailing space in each custom classname is required,
	//but you can't set the body classname as " something" (beginning with a leading space)
	//because that may not work in Opera 7
	if(this.initial == '') {
		this.initial = 'itobs';
	}

	//look for a stored cookie
	this.cookie = this.read();

	//if it exists
	if(this.cookie != null) {
		//store cookie value to string
		this.string = this.cookie;

		//set new body class name
		this.body.className = this.initial + ' ' + this.string;
	}

	//*** dev
	//document.title = '<' + this.body.className.replace(/ /g,'+') + '>   [' + this.string.replace(/ /g,'+') + ']';
};

//set a cookie method
switchManager.prototype.set = function(days) {
	//format expiry date
	this.date = new Date();
	this.date.setTime(this.date.getTime() + ( days *24*60*60*1000));

	//store the string, replacing spaces with '#' so that leading spaces are preserved
	this.info = this.string.replace(/ /g,'#');

	//if the value is empty, set its expiry in the past to delete the cookie
	if(this.info == '') { this.date.setTime(0); }

	//create the cookie
	document.cookie = 'bodySwitcher=' + this.info
		+ '; expires=' + this.date.toGMTString()
		+ '; path=/';
};


//read a cookie method
switchManager.prototype.read = function() {
	//set null reference so we always have something to return
	this.cookie = null;

	//if a cookie exists
	if(document.cookie) {
		//if it's our cookie
		if(document.cookie.indexOf('bodySwitcher')!=-1) {
			//extract and store relevant information (turning '#' back into spaces)
			this.cookie = document.cookie.split('bodySwitcher=');
			this.cookie = this.cookie[1].split(';');
			this.cookie = this.cookie[0].replace(/#/g,' ');
		}
	}
	return this.cookie;
};


function changetoDefault() {
	switcher.string = 'default'
	switcher.body.className = " iotbs  ";
	switcher.set(365);
}

function changetoMedium() {
	switcher.string = 'medium';
	switcher.body.className = " iotbs  " + switcher.string +' ';
	switcher.set(365);
}

function changetoLarge() {
	switcher.string = 'large';
	switcher.body.className = " iotbs  " + switcher.string +' ';
	switcher.set(365);
}

//** Popup Window **//
function popUp(URL, popWidth, popHeight, popLeft, popTop, autoCenter) {
	day = new Date();
	id = day.getTime();
	if (autoCenter == 1) {
		var popLeft = (screen.width - popWidth) / 2;
		var popTop = (screen.height - popHeight) / 2;
	}
	eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+popWidth+",height="+popHeight+",left="+popLeft+",top="+popTop+"');");
}

//** Popup NO SCROLLING Window **//
function popUpNS(URL, popWidth, popHeight, popLeft, popTop, autoCenter) {
	day = new Date();
	id = day.getTime();
	if (autoCenter == 1) {
		var popLeft = (screen.width - popWidth) / 2;
		var popTop = (screen.height - popHeight) / 2;
	}
	eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+popWidth+",height="+popHeight+",left="+popLeft+",top="+popTop+"');");
}

function levelDivs() {
	if(document.getElementById("middle") && document.getElementById("left") && document.getElementById("leftBottom") && document.getElementById("middleBottom"))
	{
		document.getElementById("middleBottom").style.height = "auto";
		document.getElementById("leftBottom").style.height = "auto";
		var differance = document.getElementById("left").offsetHeight - document.getElementById("middle").offsetHeight;
		if(differance > 0)
		{
			var newHeight = document.getElementById("middleBottom").offsetHeight + differance;
			document.getElementById("middleBottom").style.height = newHeight + "px";
		}
		else if(differance < 0)
		{
			var newHeight = document.getElementById("leftBottom").offsetHeight - differance;
			document.getElementById("leftBottom").style.height = newHeight + "px";
		}
	}
}

function levelOfferDivs()
{
	//Top Offers
	var topLeft = document.getElementById('topLeftContent');
	var topRight = document.getElementById('topRightContent');
	var targetHeight = 88;
	if (topLeft.offsetHeight > targetHeight) { targetHeight = topLeft.offsetHeight; }
	if (topRight.offsetHeight > targetHeight) { targetHeight = topRight.offsetHeight; }
	var topDiff = targetHeight - topLeft.offsetHeight;
	topLeft.style.paddingBottom = (5 + topDiff) + "px";
	topDiff = targetHeight - topRight.offsetHeight;
	topRight.style.paddingBottom = (5 + topDiff) + "px";
	//Middle Offers
	targetHeight = 73;
	var midLeft = document.getElementById('midLeftContent');
	var midMid = document.getElementById('midMidContent');
	var midRight = document.getElementById('midRightContent');
	if (midLeft.offsetHeight > targetHeight) { targetHeight = midLeft.offsetHeight; }
	if (midMid.offsetHeight > targetHeight) { targetHeight = midMid.offsetHeight; }
	if (midRight.offsetHeight > targetHeight) { targetHeight = midRight.offsetHeight; }
	var midDiff = targetHeight - midLeft.offsetHeight;
	midLeft.style.paddingBottom = (5 + midDiff) + "px";
	midDiff = targetHeight - midMid.offsetHeight;
	midMid.style.paddingBottom = (5 + midDiff) + "px";
	midDiff = targetHeight - midRight.offsetHeight;
	midRight.style.paddingBottom = (5 + midDiff) + "px";
}

function dropmenu(menuItem) {
	var clickedLI = menuItem.parentNode.parentNode;
	var nextClass = "over"
	if(clickedLI.className == "over") { nextClass="down"; }
	var parentUL = menuItem.parentNode.parentNode.parentNode;
	for(i=0;i<parentUL.childNodes.length;i++)
	{
		if(parentUL.childNodes[i].nodeName == "LI")
		{
			parentUL.childNodes[i].className = "down";
			for(j=0;j<parentUL.childNodes[i].childNodes.length;j++)
			{
				if(parentUL.childNodes[i].childNodes[j].nodeName == "DIV" && parentUL.childNodes[i].childNodes[j].className == "offer_table")
				{
					if(parentUL.childNodes[i].childNodes[j].style.display == "block")
					{
						hiding = true;
						hidingDiv = parentUL.childNodes[i].childNodes[j];
						targetHeight = hidingDiv.offsetHeight;
						parentUL.childNodes[i].childNodes[j].style.height = targetHeight + "px"
						parentUL.childNodes[i].childNodes[j].style.overflow = "hidden"; 
						hideDiv(hidingDiv, targetHeight, 5);
					}
				}
			}
		}
	}
	clickedLI.className = nextClass;
	if(nextClass == "over")
	{
		for(i=0;i<clickedLI.childNodes.length;i++)
		{
			if(clickedLI.childNodes[i].nodeName == "DIV" && clickedLI.childNodes[i].className == "offer_table")
			{
				activeDiv = clickedLI.childNodes[i];
				clickedLI.childNodes[i].style.display = "block";
				targetHeight = clickedLI.childNodes[i].offsetHeight - 14;
				clickedLI.childNodes[i].style.overflow = "hidden";
				clickedLI.childNodes[i].style.height = "0px";
				revealDiv(activeDiv, targetHeight, 5);
			}
		}
	}
}

var activeDiv;
function revealDiv(myDiv, myHeight, timer)
{
	if(!hiding)
	{
		myDiv.style.display = "block";
		if(myDiv.offsetHeight < myHeight)
		{
			var currentHeight = myDiv.style.height;
			currentHeight = currentHeight.substring(currentHeight, (currentHeight.length-2));
			nextHeight = parseInt(currentHeight) + (myHeight / 8);
			if(nextHeight > myHeight)
			{
				myDiv.style.height = "";
				myDiv.style.overflow = "";
			}
			else
			{
				myDiv.style.height = nextHeight + "px";
				setTimeout("revealDiv(activeDiv, "+myHeight+", "+timer+")", timer);
			}
		}
		else
		{
			myDiv.style.height = "";
			myDiv.style.overflow = "";
		}
	}
	else
	{
		myDiv.style.display = "none";
		setTimeout("revealDiv(activeDiv, "+myHeight+", "+timer+")", timer);
	}
}

var hidingDiv;
var hiding = false;
function hideDiv(myDiv, hideHeight, timer)
{
	if(myDiv.offsetHeight > 0)
	{
		var currentHeight = myDiv.style.height;
		currentHeight = currentHeight.substring(currentHeight, (currentHeight.length-2));
		nextHeight = parseInt(currentHeight) - (parseInt(hideHeight) / 8);
		if(nextHeight < 0)
		{
			myDiv.style.display = "none";
			myDiv.style.height = "";
			hiding = false;
		}
		else
		{
			myDiv.style.height = nextHeight + "px";
			setTimeout("hideDiv(hidingDiv, "+hideHeight+", "+timer+")", timer);
		}
	}
	else
	{
		myDiv.style.display = "none";
		myDiv.style.height = "";
		hiding = false;
	}
}

function displayArea(menuItem, hide) {
	var clickedLI = menuItem.parentNode.parentNode;
	var nextClass = "over"
	if(clickedLI.className == "over") { nextClass="down"; }
	var parentUL = menuItem.parentNode.parentNode.parentNode;
	if(hide)
	{
		for(i=0;i<parentUL.childNodes.length;i++)
		{
			if(parentUL.childNodes[i].nodeName == "LI")
			{
				parentUL.childNodes[i].className = "down";
				for(j=0;j<parentUL.childNodes[i].childNodes.length;j++)
				{
					if(parentUL.childNodes[i].childNodes[j].nodeName == "DIV" && parentUL.childNodes[i].childNodes[j].className.indexOf("booking_content") >= 0)
					{
						hiding = true;
						hidingDiv = parentUL.childNodes[i].childNodes[j];
						targetHeight = hidingDiv.offsetHeight;
						parentUL.childNodes[i].childNodes[j].style.height = targetHeight + "px";
						parentUL.childNodes[i].childNodes[j].style.overflow = "hidden";
						hideDiv(hidingDiv, targetHeight, 5);
					}
				}
			}
		}
	}
	clickedLI.className = nextClass;
	if(nextClass == "over")
	{
		for(i=0;i<clickedLI.childNodes.length;i++)
		{
			if(clickedLI.childNodes[i].nodeName == "DIV" && clickedLI.childNodes[i].className.indexOf("booking_content") >= 0)
			{
				activeDiv = clickedLI.childNodes[i];
				clickedLI.childNodes[i].style.display = "block";
				targetHeight = clickedLI.childNodes[i].offsetHeight - 14;
				clickedLI.childNodes[i].style.overflow = "hidden";
				clickedLI.childNodes[i].style.height = "0px";
				revealDiv(activeDiv, targetHeight, 5);
			}
		}
	}
	else
	{
		for(i=0;i<clickedLI.childNodes.length;i++)
		{
			if(clickedLI.childNodes[i].nodeName == "DIV" && clickedLI.childNodes[i].className.indexOf("booking_content") >= 0)
			{
				hiding = true;
				hidingDiv = clickedLI.childNodes[i];
				targetHeight = hidingDiv.offsetHeight;
				clickedLI.childNodes[i].style.height = targetHeight + "px"
				clickedLI.childNodes[i].style.overflow = "hidden";
				hideDiv(hidingDiv, targetHeight, 5);
			}
		}
	}
	
}

function RouteHideImage() {
	if(document.getElementById("bottomImage1") && parent.document.getElementById("bottomImage2"))
	{
		document.getElementById("bottomImage1").style.display = "none";
		document.getElementById("bottomImage2").style.display = "none";
		document.getElementById("plannerIframe").style.width = "534px";
		document.getElementById("plannerIframe").style.height = "579px";
	}
}

function displayRoom(roomID, clickedElId)
{
	tabsDiv = document.getElementById('room_tabs');
	for(i=0;i<tabsDiv.childNodes.length;i++)
	{
		if(tabsDiv.childNodes[i].nodeName == 'UL')
		{
			tabsUL = tabsDiv.childNodes[i];
			for(j=0;j<tabsUL.childNodes.length;j++)
			{
				if(tabsUL.childNodes[j].nodeName == 'LI')
				{
					tabsUL.childNodes[j].className = '';
				}
			}
		}
	}
	
    var clicked = document.getElementById(clickedElId);
	clicked.parentNode.parentNode.className = 'over';

	

	under = roomID.indexOf('_');
	roomsID = roomID.substring(0,under);
	i = 1;
	while(document.getElementById(roomsID + '_' + i))
	{
		roomDiv = document.getElementById(roomsID + '_' + i);
		if((roomsID + '_' + i) == roomID)
		{
			roomDiv.style.display = 'block';
		}
		else
		{
			roomDiv.style.display = 'none';
		}
		i++;
	}
	
}

function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} 
}

function levelBoxes()
{
    highestLogin = 0;
    i = 1;
    while(document.getElementById("loginbox_" + i))
    {
        currentbox = document.getElementById("loginbox_" + i);
        if(currentbox.offsetHeight > highestLogin)
        {
            highestLogin = currentbox.offsetHeight;
        }
        i++;
    }
    i = 1;
    while(document.getElementById("loginbox_" + i))
    {
       currentbox = document.getElementById("loginbox_" + i);
       currentbutton = document.getElementById("button_" + i);
       if(currentbox.offsetHeight < highestLogin)
       {
           var loginDiff = highestLogin - currentbox.offsetHeight;
           if(currentbutton)
           currentbutton.style.marginTop = loginDiff + "px";
           else
           currentbox.style.paddingBottom = (10+loginDiff) + "px";
       }
       i++;
     }
}