

	// Force IE6 to cache background images
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}


	// standalone onDomReady function
	window.onDomReady = function(fn){
		window.__ondom_functionArray.push(fn);
	};
	(function(){
		window.__ondom_functionArray = [];
		function _runFunctions(){
			for (var i in window.__ondom_functionArray){
				(window.__ondom_functionArray[i])();
			}
		};
		var _khtml = /(WebKit|khtml)/i.test(navigator.userAgent);
		if(document.addEventListener && !_khtml){
			document.addEventListener('DOMContentLoaded', _runFunctions, false);
		}else if(_khtml){
			var _timer = setInterval(function(){
				if(/loaded|complete/.test(document.readyState)){
					clearInterval(_timer);
					_runFunctions();
				}
			}, 10);
		}else{
			document.write('<script id=__ie_ondom defer src=javascript:void(0)><\/script>');
			var script = document.getElementById('__ie_ondom');
			script.onreadystatechange = function(){
				if(this.readyState == 'complete'){
					_runFunctions();
				}
			};
		}
	})();


	var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);


	// on dom ready
	onDomReady(function(){

		// intialize the option icons
		intializeOptionIcons();

		// intialize the list items
		intializeListItems();

		// initialize the reserve form
		intializeReserveForm();

		// initialize the lightboxes
		intializeLightboxes();

	});


	// set the option icons events, create hidden post input and set the reset button event
	function intializeOptionIcons()
	{
		var
			optionIcon, optionIcons,
			optionIconsContainer, optionIconsContainers = getElementsByClassName('optionIcons', 'div'),
			type, value, input;

		if (!optionIconsContainers.length) return;

		for (var i = 0; optionIconsContainer = optionIconsContainers[i]; i++)
		{
			optionIcons = optionIconsContainer.getElementsByTagName('img');
			if (!optionIcons.length) return;
//			type = optionIcons[0].src.replace(/.*([A-Z][^/]*?)[A-Z][^/]*/, '$1');
			type = optionIconsContainer.className.replace(/.*type=([^\s"]*).*/, '$1').replace(/_/, ' ');

			// create hidden input to be able to post the selection
			input = document.createElement('input');
			input.type = 'hidden';
			input.name = 'search[item][' + type + ']';
			optionIconsContainer.appendChild(input);

			for (var j = 0; optionIcon = optionIcons[j]; j++)
			{
				optionIcon.style.cursor = (isIE6 ? 'hand' : 'pointer');
//				value = optionIcon.src.replace(/.*[A-Z][^/]*?([A-Z][^/\.]*)\.[^/\.]*/, '$1');
//				value = optionIcon.src.replace(/.*\/iko?([^\/\.]*)\.[^\/\.]*/, '$1');
				value = optionIcon.className.replace(/.*iw_id=([0-9]*).*/, '$1');

				if (hasClassName(optionIcon, 'active')) input.value += value + '|';

				addEvent(optionIcon, 'click', (function (scope, optionIcon, input, value) {
					return function(e)
					{
						(function(){
							if (!hasClassName(optionIcon, 'active'))
							{
								addClassName(optionIcon, 'active');
								addInputValue(input, value);
							}
							else
							{
								removeClassName(optionIcon, 'active');
								removeInputValue(input, value);
							}
						}).call(scope);
					};
				})(this, optionIcon, input, value));
			}

			input.value = input.value.replace(/\|$/, '');
		}

		// make the reset button clear the hidden inputs and unselect the images
		addEvent(getResetButton(), 'click', (function (scope, optionIconsContainers) {
			return function(e)
			{
				(function(){
					var optionIcon, optionIcons, optionIconsContainer;
					for (var i = 0; optionIconsContainer = optionIconsContainers[i]; i++)
					{
						optionIcons = optionIconsContainer.getElementsByTagName('img');
						input = optionIconsContainer.getElementsByTagName('input')[0];

						for (var j = 0; optionIcon = optionIcons[j]; j++)
						{
							removeClassName(optionIcon, 'active');
							input.value = '';
						}
					}
				}).call(scope);
			};
		})(this, optionIconsContainers));
	}


	// set the list items events
	function intializeListItems()
	{
		var
			listItem, listItems,
			list, lists = getElementsByClassName('list'),
			link;

		if (!lists.length) return;

		for (var i = 0; list = lists[i]; i++)
		{
			listItems = getElementsByClassName('listItem', '*', list);

			for (var j = 0; listItem = listItems[j]; j++)
			{
				addEvent(listItem, 'mouseover', function(e) {
					addClassName(this, 'active');
				});
				addEvent(listItem, 'mouseout', function(e) {
					removeClassName(this, 'active');
				});
				if (link = listItem.getElementsByTagName('a')[0])
				{
					listItem.style.cursor = (isIE6 ? 'hand' : 'pointer');

					addEvent(listItem, 'click', (function (scope, link) {
						return function(e)
						{
							(function(){
								document.location = link.href;
							}).call(scope);
						};
					})(this, link));
				}
			}
		}
	}


	// make the reserve form appear on an overlay onclick of the reserve button; also make the submit pass through the form validator
	// display the form immediately on input error
	function intializeReserveForm()
	{
		var linkReserve, formReserve;

		if (linkReserve = $('linkReserve'))
		{
			linkReserve.style.display = 'inline';
			formReserve = linkReserve.parentNode.parentNode.getElementsByTagName('form')[0];
			formReserve.style.display = 'none';

			if (hasClassName(formReserve, 'submit')) showReserveForm(formReserve);

			addEvent(linkReserve, 'click', (function (scope, formReserve) {
				return function(e)
				{
					(function(){

						showReserveForm(formReserve);

						// preventing default dispatch
						if (!e) e = window.event;
						if(e.preventDefault) e.preventDefault();
						else e.returnValue = false;

					}).call(scope);
				};
			})(this, formReserve));

			// initialize the form validator
			FormValidator.initialize();
		}
	}


	function showReserveForm(formReserve)
	{
		showOverlay(formReserve);
	}


	// make the large image appear on an overlay onclick of the smaller image
	function intializeLightboxes()
	{
		var lightboxImageUrl, lightboxImageLink, lightboxImageLinks = getElementsByClassName('lightbox', 'a');

		for (var i = 0; lightboxImageLink = lightboxImageLinks[i]; i++)
		{
			lightboxImageUrl = lightboxImageLink.href;

			addEvent(lightboxImageLink, 'click', (function (scope, lightboxImageUrl) {
				return function(e)
				{
					(function(){

						showLightboxImage(lightboxImageUrl);

						// preventing default dispatch
						if (!e) e = window.event;
						if(e.preventDefault) e.preventDefault();
						else e.returnValue = false;

					}).call(scope);
				};
			})(this, lightboxImageUrl));

			// preload the large image; after window load
			addEvent(window, 'load', (function (scope, lightboxImageUrl) {
				return function(e)
				{
					(function(){

						var image = new Image();
						image.src = lightboxImageUrl;

					}).call(scope);
				};
			})(this, lightboxImageUrl));
		}
	}


	function showLightboxImage(imageUrl)
	{
		showOverlay(imageUrl);
	}


	// show overlay
	function showOverlay(element)
	{
		// create the overlay if it not exists
		if (!window.overlay)
		{
			// create the content container
			overlayContent = document.createElement('div');
			overlayContent.id = 'overlayContent';
			overlayContent.style.position = 'absolute';

			// create the close button
			overlayIconClose = document.createElement('div');
			overlayIconClose.id = 'overlayClose';
			overlayIconClose.style.cursor = (isIE6 ? 'hand' : 'pointer');
			addEvent(overlayIconClose, 'click', (function (scope, overlayContent) {
				return function(e)
				{
					(function(){
						overlay.style.display = 'none';
						overlayContent.style.display = 'none';
						var element = overlayContent.children[0];
						if (element.tagName == 'IMG')
						{
							element.parentNode.removeChild(element);
						}
						else
						{
							document.getElementsByTagName('body')[0].appendChild(element);
							element.style.display = 'none';
						}
					}).call(scope);
				};
			})(this, overlayContent));

			// create the overlay
			overlay = document.createElement('div');
			overlay.id = 'overlay';
			overlay.style.position = 'absolute';
			overlay.style.top = overlay.style.left = '0px';
//			setOpacity(overlay, .9);
			addEvent(overlay, 'click', (function (scope, overlayContent) {
				return function(e)
				{
					(function(){
						overlay.style.display = 'none';
						overlayContent.style.display = 'none';
						var element = overlayContent.children[0];
						if (element.tagName == 'IMG')
						{
							element.parentNode.removeChild(element);
						}
						else
						{
							document.getElementsByTagName('body')[0].appendChild(element);
							element.style.display = 'none';
						}
					}).call(scope);
				};
			})(this, overlayContent));

			// inject all into the DOM
			document.getElementsByTagName('body')[0].appendChild(overlay);
			overlayContent.appendChild(overlayIconClose);
			document.getElementsByTagName('body')[0].appendChild(overlayContent);

			// make the overlay resize and the content move on window resize
			addEvent(window, 'resize', (function (scope, overlayContent) {
				return function(e)
				{
					(function(){
						if (overlay.style.display == 'block')
						{
							overlay.style.display = 'none';
							documentScrollSize = getDocumentScrollSize();
							overlay.style.width = documentScrollSize.x + 'px';
							overlay.style.height = documentScrollSize.y + 'px';
							overlay.style.display = 'block';

							positionOverlay();
						}
					}).call(scope);
				};
			})(this, overlayContent));

			// make the content position adapt on scroll
			addEvent(window, 'scroll', (function (scope, overlayContent) {
				return function(e)
				{
					(function(){
						if (overlay.style.display == 'block')
						{
							positionOverlay();
						}
					}).call(scope);
				};
			})(this, overlayContent));
		}

		// if the passed argument is a string, it is an image path
		if (typeof(element) == 'string')
		{
			var image = document.createElement('img');
			image.onload = positionOverlay;
			image.src = element;
			element = image;
		}
		// make sure the content element is positioned static
		element.style.position = 'static';
		// move the element to into the content container (before the close icon)
		overlayContent.insertBefore(element, overlayIconClose);

		// show the overlay
		documentScrollSize = getDocumentScrollSize();
		overlay.style.width = documentScrollSize.x + 'px';
		overlay.style.height = documentScrollSize.y + 'px';
		overlay.style.display = 'block';

		// show the content
		overlayContent.style.display = element.style.display = 'block';
		positionOverlay();
	}


	// position overlay
	function positionOverlay()
	{
		var contentSize = getElementSize(overlayContent);
		var viewportSize = getViewportSize();
		var scrollPosition = getScrollPosition();
		overlayContent.style.left = (((viewportSize.x - contentSize.x) / 2) + scrollPosition.x) + 'px';
		overlayContent.style.top = (((viewportSize.y - contentSize.y) / 2) + scrollPosition.y) + 'px';
	}


	// very simple $ function
	function $(id)
	{
		return document.getElementById(id);
	}


	// adds a className to an element
	function addClassName(element, className)
	{
		removeClassName (element, className);
		element.className = (element.className + ' ' + className).replace(/^\s+|\s+$/g, '');
	}


	// removes a className from an element
	function removeClassName(element, className)
	{
		element.className = element.className.replace(className, '').replace(/^\s+|\s+$/g, '');
	}


	// check whether an element has a specific class
	function hasClassName(element, name)
	{
		var i, list = element.className.split(" ");

		for(i = 0; i < list.length; i++)
			if(list[i] == name)
				return true;

		return false;
	}


	// standalone getElementsByClassName function from http://www.robertnyman.com/index.php?p=256
	function getElementsByClassName(className, tagName, parent)
	{
		var
			testClass = new RegExp("(^|\\s)" + className + "(\\s|$)"),
			tagName = tagName || '*', parent = parent || document,
			elements = (tagName == '*' && parent.all)? parent.all : parent.getElementsByTagName(tagName),
			returnElements = [], current, length = elements.length;

		for(var i=0; i<length; i++){
			current = elements[i];
			if(testClass.test(current.className)){
				returnElements.push(current);
			}
		}

		return returnElements;
	}


	// set opacity of an element
	function setOpacity(element, opacity)
	{
		element.style.opacity = opacity;
		element.style.MozOpacity = opacity;
		element.style.filter = "alpha(opacity=" + (opacity * 100) + ")";
	}


	// better addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	function addEvent(element, type, fn)
	{
		if (element.addEventListener)
		{
			element.addEventListener(type, fn, false);
		}
		else if (element.attachEvent)
		{
			element['e' + type + fn] = fn;
			element[type + fn] = function() {
				element['e' + type + fn](window.event);
			};
			element.attachEvent('on' + type, element[type + fn]);
		}
	}


	// better removeEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	function removeEvent(element, type, fn)
	{
		if (element.removeEventListener)
		{
			element.removeEventListener(type, fn, false);
		}
		else if (element.detachEvent)
		{
			element.detachEvent('on' + type, element[type + fn]);
			element[type + fn] = null;
			element['e' + type + fn] = null;
		}
	}


	// add an input value (in a bar ('|') separated list)
	function addInputValue(input, value)
	{
		removeInputValue(input, value);
		input.value = (input.value + '|' + value).replace(/^\|+|\|+$/g, '');
	}


	// remove an input value (in a bar ('|') separated list)
	function removeInputValue(input, value)
	{
		input.value = input.value.replace(new RegExp('(^|\\|)' + escapeRegexChars(value) + '(\\||$)'), '|').replace(/(^\|+|\|+$)/g, '');
	}


	// get reset button, optionally within container
	function getResetButton(container)
	{
		var input, inputs = (container || document).getElementsByTagName('input');

		for (var i = 0; input = inputs[i]; i++)
		{
			if (input.type == 'reset')
			{
				return input;
			}
		}

		return false;
	}


	// get an element's size
	function getElementSize(element)
	{
		return {x: element.offsetWidth, y: element.offsetHeight};
	}


	compatMode = document.compatMode;
	userAgent = navigator.userAgent.toLowerCase();
	isOpera = (userAgent.indexOf('opera') > -1);
	isIE = (!isOpera && userAgent.indexOf('msie') > -1);

	// set of functions to get the document's scroll width and height or both
	function getDocumentScrollSize()
	{
		return {x: getDocumentScrollWidth(), y: getDocumentScrollHeight()};
	}
	function getDocumentScrollWidth()
	{
		var scrollWidth = (compatMode != 'CSS1Compat' ? document.body.scrollWidth : document.documentElement.scrollWidth);

		return Math.max(scrollWidth, getViewportWidth());
	}
	function getDocumentScrollHeight()
	{
		var scrollHeight = (compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight);

		return Math.max(scrollHeight, getViewportHeight());
	}
	// set of functions to get the viewport's width or height or both
	function getViewportSize()
	{
		return {x: getViewportWidth(), y: getViewportHeight()};
	}
	function getViewportWidth()
	{
		var width = window.innerWidth;

		if(compatMode || isIE)
		{
			width = (compatMode == 'CSS1Compat') ? document.documentElement.clientWidth : document.body.clientWidth;
		}

		return width;
	}
	function getViewportHeight()
	{
		var height = window.innerHeight;

		if ((compatMode || isIE) && !isOpera)
		{
			height = (compatMode == 'CSS1Compat' ? document.documentElement.clientHeight : document.body.clientHeight);
		}

		return height;
	}


	// get the document's scroll position
	function getScrollPosition()
	{
		if (typeof window.pageYOffset != 'undefined')
			return {x: window.pageXOffset, y: window.pageYOffset};
		if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
			return {x: document.documentElement.scrollLeft, y: document.documentElement.scrollTop};
		if (typeof document.body != 'undefined')
			return {x: document.body.scrollLeft, y: document.body.scrollTop};
		return {x: 0, y: 0};
	}


	// create cookie function 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=/";
	}


	// read cookie function from http://www.quirksmode.org/js/cookies.html
	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;
	}


	// erase cookie function from http://www.quirksmode.org/js/cookies.html
	function eraseCookie(name) {
		createCookie(name,"",-1);
	}


	// excape regex special characters in a string to be used as a regex itself
	function escapeRegexChars(text)
	{
		if (!arguments.callee.sRE)
		{
			var specials = ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'];
			arguments.callee.sRE = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
		}

		return text.replace(arguments.callee.sRE, '\\$1').replace(/\s/g, '\\\s');
	}


