(function() {

	document.resbooker = { 
	
		options: {
			hostName:		'secure',
			folderName:		'resbooker',
			scriptName:		'resbooker.js',
			queryString:	'',
			iframeHeight:	500,
			contentId:		'OWSContent'
		},

		id : null,
	
		loaded: false,
		
		path: null,
	
		lightbox: {
			
			top: 0,
			
			visible: false,
			
			attachEvent: function(id, event, func) {

				var obj = document.getElementById(id);				

				if (obj.addEventListener) {
					obj.addEventListener(event, func, false);
					return true;
				} else return obj.attachEvent("on" + event, func);

			},

			content: function() {
	
				var el = document.getElementById(document.resbooker.id + '-content');
				
				if (el == null) {

					el = document.createElement("link");
					el.rel = "stylesheet";
					el.type = "text/css";
					el.href = document.resbooker.path + '/Resources/Stylesheets/lightbox.css';
					el.media = "all";
			
					document.lastChild.firstChild.appendChild(el);

					el = document.createElement('div');
					el.id = document.resbooker.id + '-content'; 
					el.style.display = 'none';

					document.body.appendChild(el);

				}

				return el;
	
			},
	
			overlay: function() {
				
				var el = document.getElementById(document.resbooker.id + '-overlay');
				
				if (el == null) {

					el = document.createElement('div');
					el.id = document.resbooker.id + '-overlay'; 
					el.style.display = 'none';

					document.body.appendChild(el);

				}

				return el;

			},

			hide: function () {
				
				var content = this.content(),
					overlay = this.overlay(),
					bod = document.getElementsByTagName('body')[0],
					htm = document.getElementsByTagName('html')[0],
					sel = document.getElementsByTagName('select'),
					i = null,
					isIE = !(/opera|webtv/i.test(navigator.userAgent.toLowerCase())) && /msie\s(\d)/.test(navigator.userAgent.toLowerCase());

				if (isIE) {

					bod.style.height = 'auto';
					bod.style.overflow = 'auto';
					htm.style.height = 'auto';
					htm.style.overflow = 'auto';

					for(i = 0; i < sel.length; i++)
						sel[i].style.visibility = 'visible';

					window.scrollTo(0, this.top);
					
				}
				
				content.style.display = 'none';
				overlay.style.display = 'none';

				content.innerHTML = '';

				this.visible = false;

			},
	
			show: function (dialog) {
				
				var content = this.content(),
					overlay = this.overlay(),
					bod = document.getElementsByTagName('body')[0],
					htm = document.getElementsByTagName('html')[0],
					sel = document.getElementsByTagName('select'),
					i = null
					isIE = !(/opera|webtv/i.test(navigator.userAgent.toLowerCase())) && /msie\s(\d)/.test(navigator.userAgent.toLowerCase());

				if (isIE) {

					this.top = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;

					bod.style.height = '100%';
					bod.style.overflow = 'hidden';
					htm.style.height = '100%';
					htm.style.overflow = 'hidden';

					window.scrollTo(0, 0);

					for(i = 0; i < sel.length; i++)
						sel[i].style.visibility = 'hidden';

				}

				overlay.style.display = 'block';

				if (typeof dialog != 'undefined') {
					
					content.innerHTML = dialog;
					content.style.display = 'block';
					
				}

				this.visible = true;
		
			}
		
		},

		querystring: function(param) {
	
			var i = 0, pair, qs = [];
			
			for (i = 0; i < document.getElementsByTagName("script").length; i++) 
				if (document.getElementsByTagName("script")[i].src.toLowerCase().indexOf(document.resbooker.options.scriptName) >= 0) 
					qs = document.getElementsByTagName("script")[i].src.substring(document.getElementsByTagName("script")[i].src.indexOf(document.resbooker.options.scriptName) + document.resbooker.options.scriptName.length + 1).split('&');
			
			for (i = 0; i < qs.length; i++) {
				pair = qs[i].split('=');
				if (pair[0].toLowerCase() == param) 
					return (pair.length == 2) ? unescape(pair[1]) : '';
			}
			
			return '';

		},

		iframe: {
			
			get: function () {
				
				var el = document.getElementById(document.resbooker.id + '-iframe'),
					targetframe = document.frames ? document.frames[el.name] : document.getElementById(el.id);

				return targetframe.contentWindow || targetframe;
				
			},
			
			load: function(target) {
				
				var iFrame = '',
					suffix = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) { 
			    		var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); 
					    return v.toString(16); 
					}).toLowerCase(); 
		
				document.resbooker.id = target; // + suffix;
	
				iFrame = '<iframe ';
				iFrame += 'name="' + document.resbooker.id + '-iframe" ';
				iFrame += 'id="' + document.resbooker.id + '-iframe" ';
				iFrame += 'scrolling="auto" ';
				iFrame += 'marginwidth="0" ';
				iFrame += 'marginheight="0" '; 
				iFrame += 'frameborder="0" ';
				iFrame += 'vspace="0" ';
				iFrame += 'hspace="0" ';
				iFrame += 'width="100%" ';
				iFrame += 'style="width: 100%; display: none;" ';
				iFrame += 'allowtransparency="true" ';
				iFrame += 'onload="document.resbooker.stretch(this);" ';
				iFrame += '></iframe>';

				document.write(iFrame);
	
			},
	
			resize: function(el, scrollup) {
				
				var scrollup = (typeof scrollup == 'undefined' ? true : scrollup),
					targetframe = null,
					minheight = document.resbooker.options.iframeHeight,
					newheight = minheight;

				try {
			
					if (el.style.display != '') 
						el.style.display = '';
				
					if (el.style.height != '') 
						el.style.height = '';
				
					targetframe = document.frames ? document.frames[el.name] : document.getElementById(el.id);
					targetframe = targetframe.contentWindow || targetframe;

					newheight = parseInt(targetframe.document.getElementById(document.resbooker.options.contentId).offsetHeight);

					document.getElementById(el.id).height = (newheight > minheight) ? newheight + 'px' : minheight + 'px';
					document.getElementById(el.id).width = el.parentNode.offsetWidth + 'px';
		
				} catch (err) { }
				
				if (scrollup) window.scrollTo(0, document.getElementById(document.resbooker.id + '-iframe').offsetTop);
		
			}
			
		},
	
		stretch: function(el, scrollup) {

			if (!this.loaded) {
				
				this.path = (this.querystring('host') != '' ? this.querystring('host').toLowerCase() : document.resbooker.options.hostName) + '.' + document.domain;
				this.path = document.location.protocol + '//' + this.path.replace(new RegExp('^\\.', 'gi'), '') + '/' + document.resbooker.options.folderName;

				el.src = this.path;

				// If parameters were specified on the page load then include them here
				if (location.search != '')
					el.src = el.src + (el.src.indexOf('?') >= 0 ? '&' : '?') + location.search.substr(1) + '&_rbv=true';

				// If parameters were specified in the options then include them here
				if (document.resbooker.options.queryString != '')
					el.src = el.src + (el.src.indexOf('?') >= 0 ? '&' : '?') + document.resbooker.options.queryString;

				// If an alternative startup folder was specified then include it here
				if (this.querystring('start') != '')
					el.src = el.src + (el.src.indexOf('?') >= 0 ? '&' : '?') + '_rbs=' + this.querystring('start').toLowerCase();

				this.loaded = true;
			}
			
			window.setTimeout(function() {
				document.resbooker.iframe.resize(el, scrollup);
			}, 50);
		
		}
		
	}

	if (document.resbooker) {
		// Drop the host from the document.domain
		document.domain = location.host.replace(new RegExp('^' + document.resbooker.options.hostName + '\\.', 'gi'), '');
		// Load the i-frame
		document.resbooker.iframe.load('resbooker');
	}

})();

