var jq = jQuery;

var jlib = {
	ekhr_version: 2,
	ie6: false,
	ie7: false,
	safari2: false,
	firefox2: false,
	animationSpeed: 'normal',
	canAnimate: true,
	minFlashVer: '9.0.0',
	haveFlash: false,
	haveFlashHeader: false,
	today: new Date(),

	openNewWindow: function () {
		$('a[rel="external"]').click(function (e) {
			window.open($(this).attr('href'), '', '');
			e.preventDefault();
		});
	},

	openflashFloorPlan: function () {
		$('a[rel="floorPlan"]').click(function (e) {
			jlib.swf.flashShowFloorplan();
			e.preventDefault();
		});
	},
	
	//this is for the virtual tour popup
	openVirtualTourPopup: function () {
		var w = '576',
			h = '324',
			winl = (screen.width-w)/2,
			wint = (screen.height-h)/2,
			settings = 'width='+w+',height='+h+',top='+wint+',left='+winl+',toolbar=no,menubar=no,location=no';
		$('a[rel="vt"]').click(function (e) {
			window.open($(this).attr('href'), 'vt', settings);
			e.preventDefault();
		});
	},
	// returns true if any element in els is target or a descendant of target
	isDescendant: function (target, els) {
		var ancestors = $(target).parents().andSelf();
		var isDescendant = false;
		$.each(els, function () {
				if (ancestors.index(this) > -1) {
					isDescendant = true;
					return false;
				}
				return true;
			});
		return isDescendant;
	},

	// registers a document click event listener to do something if the users clicks outside of one or more elements
	clickOutside: function (fn) {
		var els = $.makeArray(arguments);
		els.shift();
		$(document).click(function (e) {
				if (!jlib.isDescendant(e.target, els)) {
					fn();
				}
			});
	},

	// focus event delegation
	// see http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
	delegateFocus: function (el, fn) {
		if (el.addEventListener) {
			el.addEventListener('focus', fn, true);
		} else if (el.attachEvent) {
			el.attachEvent('onfocusin', function () { fn({ type: 'focus', target: window.event.srcElement }); });
		}
	},

	// registers a document focus event listener to do something if the users focuses outside of one or more elements
	focusOutside: function (fn) {
		var els = $.makeArray(arguments);
		els.shift();
		jlib.delegateFocus(document, function (e) {
				// firefox fires a focus event (with e.target == document) when the user clicks on an unfocusable element
				if (!jlib.isDescendant(e.target, els) && e.target != document) {
					fn();
				}
			});
	},

	// ie6 max-height (almost)
	ie6MaxHeight: function (el) {
		var maxH, h;
		el = $(el);
		maxH = parseInt(el.css('max-height'), 10);

		if (!jlib.ie6 || !maxH) {
			return;
		}

		h = jlib.height(el.get(0))[0];

		el.css((h > maxH) ? {
				height: maxH + 'px',
				overflowY: 'scroll'
			} : {
				height: 'auto',
				overflowY: 'visible'
			});
	},

	// slide down or regular show depending on browser (or canAnimate)
	show: function (el, options) {
		var opts = $.extend({}, {
					speed: jlib.animationSpeed,
					pre: function () {},
					post: function () {},
					classPrefix: '',
					classTarget: el,
					canAnimate: jlib.canAnimate,
					queueTarget: el
				}, options),
			cls = (opts.classPrefix) ? {
				open: opts.classPrefix + 'Open',
				closed: opts.classPrefix + 'Closed',
				opening: opts.classPrefix + 'Opening',
				closing: opts.classPrefix + 'Closing'
			} : {
				open: '',
				closed: '',
				opening: '',
				closing: ''
			},
			isSameQueue = (opts.queueTarget == el),
			q = $(opts.queueTarget);
		el = $(el);
		opts.classTarget = $(opts.classTarget);

		if (opts.canAnimate) {
			q.queue(function () {
					opts.pre();
					opts.classTarget.removeClass(cls.closed).addClass(cls.open).addClass(cls.opening);
					q.dequeue();
				});
			if (isSameQueue) {
				q.slideDown(opts.speed);
			} else {
				q.queue(function () {
						el.slideDown(opts.speed)
							.queue(function () {
									el.dequeue();
									q.dequeue();
								});
					});
			}
			q.queue(function () {
					opts.classTarget.removeClass(cls.opening);
					opts.post();
					q.dequeue();
				});
		} else {
			q.queue(function () {
					opts.pre();
					opts.classTarget.removeClass(cls.closed).addClass(cls.open);
					el.show();
					opts.post();
					q.dequeue();
				});
		}
	},

	// slide up or regular hide depending on browser (or canAnimate)
	hide: function (el, options) {
		var opts = $.extend({}, {
					speed: jlib.animationSpeed,
					pre: function () {},
					post: function () {},
					classPrefix: '',
					classTarget: el,
					canAnimate: jlib.canAnimate,
					queueTarget: el
				}, options),
			cls = (opts.classPrefix) ? {
				open: opts.classPrefix + 'Open',
				closed: opts.classPrefix + 'Closed',
				opening: opts.classPrefix + 'Opening',
				closing: opts.classPrefix + 'Closing'
			} : {
				open: '',
				closed: '',
				opening: '',
				closing: ''
			},
			isSameQueue = (opts.queueTarget == el),
			q = $(opts.queueTarget);
		el = $(el);
		opts.classTarget = $(opts.classTarget);

		if (opts.canAnimate) {
			q.queue(function () {
					opts.pre();
					opts.classTarget.addClass(cls.closing);
					q.dequeue();
				});
			if (isSameQueue) {
				q.slideUp(opts.speed);
			} else {
				q.queue(function () {
						el.slideUp(opts.speed)
							.queue(function () {
									el.dequeue();
									q.dequeue();
								});
					});
			}
			q.queue(function () {
					opts.classTarget.removeClass(cls.closing).removeClass(cls.open).addClass(cls.closed);
					opts.post();
					q.dequeue();
				});
		} else {
			q.queue(function () {
					opts.pre();
					el.hide()
					opts.classTarget.removeClass(cls.open).addClass(cls.closed);
					opts.post();
					q.dequeue();
				});
		}
	},

	// queues one or more functions to be run on load / document ready
	// if first argument is false, functions are added to the front of the queue
	queue: function () {
		var args = jq.makeArray(arguments), isJump = !args[0];
		if (!jlib._queue) {
			jlib._queue = [];
		}
		if (isJump) {
			args.shift();
			Array.prototype.unshift.apply(jlib._queue, args);
		} else {
			Array.prototype.push.apply(jlib._queue, args);
		}
		if (jlib._queueStarted && jlib._queueStopped) {
			jlib._queueStopped = false;
			jlib.runQueue();
		}
	},

	// starts calling functions passed to jlib.queue()
	runQueue: function (context) {
		context = (context || window);
		jlib._queueStarted = true;
		setTimeout(function () {
				var q = jlib._queue;
				if (!q || q.length == 0) {
					jlib._queueStopped = true;
					return;
				}
				(q.shift()).call(context);
				setTimeout(arguments.callee, 10);
			}, 10);
	},

	// detaches the given element from the dom, returns a function that can be called to reattach the element
	detach: function (el) {
		var par, pos, hold;
		el = $(el);
		par = el.parent();
		pos = par.children().index(el);
		hold = $('<div></div>').append(el);
		return function () {
				par.children().eq(pos).before(el);
				el = par = hold = null;
			};
	}

};

/************************************************************
 * Main nav menus
 */
jlib.mainNav = function () {
	var me = arguments.callee,
		href = location.href,
		sep = (jQuery.browser.msie && parseFloat(jQuery.browser.version) < 8) ? ' ' : ', ',
		speed = 'fast';

	if (me.hasInit) {
		return;
	}

	$('ul.mainNav li').each(function () {
		var li = $(this),
			a = li.find('a'),
			offImg = a.find('img'),
			overSrc = offImg.attr('src').replace(/_off/, '_on'),
			div = $('<div></div>'),
			isOn = false,
			overImg, over, out, liH, divH, divW, blurId, as, a, i, l;

		function getRect(h) {
			return 'rect(' + h + 'px' + sep + divW + 'px' + sep + divH + 'px' + sep + '0px)';
		}

		// add over image for fade in/out, replace off with on image for current section
		if (href.indexOf(a[0].href) == 0) {
			offImg.attr('src', overSrc);
			isOn = true;
		} else {
			overImg = $('<img src="' + overSrc + '" alt="" class="over" />');
			if (jlib.canAnimate) {
				overImg.css('opacity', 0);
			} else {
				overImg.css('display', 'none');
			}
			overImg.appendTo(a);
		}

		// copy links from site directory
		div
			.append($('#' + li.attr('id').replace(/Nav/, 'Links')).clone().removeAttr('id'))
			.css('left', '-9999px')
			.appendTo(li);

		if (isOn) {
			// need to search the links in reverse
			as = div.find('a');
			l = as.length;
			for (i = l - 1; i >= 0; i--) {
				a = as.eq(i);
				if (href.indexOf(a[0].href) == 0) {
					a.parent().addClass('on');
					break;
				}
			}
		}

		// normally we would add an iframe for ie6
		// but since the menus are not long enough to overlap any selects
		// we'll skip it this time

		if (jlib.canAnimate) {
			liH = li.outerHeight();
			divH = div.outerHeight();
			divW = div.outerWidth();
			div.css({
				top: (liH - divH) + 'px',
				clip: getRect(divH)
			});

			over = function () {
				liH = li.outerHeight();
				divH = div.outerHeight();
				if (overImg) {
					overImg.stop().fadeTo(speed, 1);
				}
				div.stop().animate({
					top: liH + 'px',
					clip: getRect(0)
				}, speed);
			};
			out = function () {
				liH = li.outerHeight();
				divH = div.outerHeight();
				if (overImg) {
					overImg.stop().fadeTo(speed, 0);
				}
				div.stop().animate({
					top: (liH - divH) + 'px',
					clip: getRect(divH)
				}, speed);
			};

		} else {
			div.hide();

			over = function () {
				if (overImg) {
					overImg.show();
				}
				div.show();
			};
			out = function () {
				if (overImg) {
					overImg.hide();
				}
				div.hide();
			};
		}

		div.css('left', '');
		li
			.hover(over, out)
			.find('a')
				.focus(function () {
					if (blurId != null) {
						clearTimeout(blurId);
						blurId = null;
					}
					over();
				})
				.blur(function () {
					if (blurId != null) {
						clearTimeout(blurId);
					}
					blurId = setTimeout(function () {
							out();
							blurId = null;
						}, 200);
				});
	});

	me.hasInit = true;
};

// adapted by Jeff from http://plugins.jquery.com/project/clipAnimate
/*
 * jQuery css clip animation support -- Jim Palmer
 * version 0.1.1
 * idea spawned from jquery.color.js by John Resig
 * Released under the MIT license.
 */
(function (jQuery) {
	jQuery.fx.step.clip = function (fx) {
		var re, s, d, a, p;
		if (!fx.diff) {
			re = /rect\(([\d.]+)px ([\d.]+)px ([\d.]+)px ([\d+.])px\)/;
			a = re.exec(fx.elem.style.clip.replace(/, */g, ' '));
			d = re.exec(fx.end.replace(/, */g, ' '));
			fx.start = s = [
				parseFloat(a[1]),
				parseFloat(a[2]),
				parseFloat(a[3]),
				parseFloat(a[4])
			];
			fx.diff = [
				parseFloat(d[1]) - s[0],
				parseFloat(d[2]) - s[1],
				parseFloat(d[3]) - s[2],
				parseFloat(d[4]) - s[3]
			];
			fx.sep = (jQuery.browser.msie && parseFloat(jQuery.browser.version) < 8) ? ' ' : ', ';
		}
		s = fx.start;
		d = fx.diff;
		p = fx.pos;
		a = [
			(s[0] + (d[0] * p)) + 'px',
			(s[1] + (d[1] * p)) + 'px',
			(s[2] + (d[2] * p)) + 'px',
			(s[3] + (d[3] * p)) + 'px'
		];
		fx.elem.style.clip = 'rect(' + a.join(fx.sep) + ')';
	};
})(jQuery);

/************************************************************
 * Breadcrumb menus
 */
jlib.breadcrumbs = function () {
	var pathname = location.pathname.toLowerCase(),
		offscreen = {
			display: 'block',
			position: 'absolute',
			left: '-5000px',
			top: '-5000px'
		},
		unoffscreen = {
			display: '',
			position: '',
			left: '',
			top: ''
		};

	$('.breadcrumbs ul').each(function () {
		var ul = $(this),
			parent = ul.parent(),
			isVisible = false,
			cur, menu, mid, showPre, hidePost, showOpts, hideOpts;

		// mark current link
		ul.find('a').each(function () {
			var a = $(this);
			if (pathname.indexOf(a.attr('href').toLowerCase()) == 0 || !cur) {
				cur = a;
				// don't break to find the closest match
			}
		});

		// create menu
		parent.append([
			'<span class="spacer">', cur.text(), '</span>',
			'<div class="menu">',
				'<div class="selected">',
					'<div class="tab">',
						'<div>',
							'<a href="', cur.attr('href'), '">', cur.text(), '</a>',
						'</div>',
					'</div>',
				'</div>',
				'<div class="options">',
					'<div class="top"><div><div>&nbsp;</div></div></div>',
					'<div class="mid"><div></div></div>',
					'<div class="btm"><div><div>&nbsp;</div></div></div>',
				'</div>',
			'</div>'
		].join(''));
		menu = parent.find('.menu');
		mid = menu.find('.mid');
		mid.find('div').append(ul);
		parent.addClass('dropdown');

		// event handlers
		if (!jlib.ie7 && !jlib.ie6) {
			showPre = function () {
				parent.addClass('on');
			};
			hidePost = function () {
				parent.removeClass('on');
			};
		} else {
			// need to set a width for ul before showing it
			showPre = function () {
				var w;
				parent.addClass('on');
				ul.css(offscreen);
				w = ul.width() + 27; // extra space for menu arrow
				ul.css(unoffscreen);
				ul.css('width', w + 'px');
			};
			hidePost = function () {
				ul.css('width', '');
				parent.removeClass('on');
			};
		}
		showOpts = {
			classPrefix: 'menu',
			classTarget: menu,
			pre: showPre,
			canAnimate: (jlib.canAnimate && !jlib.firefox2)
		};
		hideOpts = {
			classPrefix: 'menu',
			classTarget: menu,
			post: hidePost,
			canAnimate: (jlib.canAnimate && !jlib.firefox2)
		};
		menu.find('.tab a').click(function (e) {
			if (!isVisible) {
				jlib.show(ul, showOpts);
			} else {
				jlib.hide(ul, hideOpts);
			}
			isVisible = !isVisible;
			e.preventDefault();
		});
		jlib.clickOutside(function () {
			jlib.hide(ul, hideOpts);
			isVisible = false;
		}, parent);

		if (jlib.ie6) {
			// force redraw
			menu.attr('class', menu.attr('class'));
			setTimeout(function () { menu.attr('class', menu.attr('class')); }, 0);
		}
	});
};

/************************************************************
 * Set today's month as default
 */
jlib.contactUsFormInit = function () {
	$('#mr2a').each(function () {
		var thisMonth = jlib.today.getMonth();
		var form = $(this);
		$('#inmm').get(0).selectedIndex = thisMonth;
		$('#outmm').get(0).selectedIndex = thisMonth;

		if (jlib.safari2) {
			// force redraw
			form.find(':submit').click(function () {
				form.css('padding-bottom', '1px');
				setTimeout(function () { form.css('padding-bottom', ''); }, 0);
			});
		}
	});
};

/************************************************************
 * Contact Us - show / hide form 
 */
jlib.contactUsFormSelection = function() {
	var frmCont = $('.formSelectionContainer'),
		rad = frmCont.children(':radio'),
	 	prev = rad.eq(0).val();
	 	$('.contactTable').addClass(prev);
	 	
	 	rad.click(function() {
			 $('.contactTable').removeClass(prev).addClass($(this).val());
		 	 prev = $(this).val();
 		});
};

jlib.expandPanel = function () {
	var gpLink = $('.groupedExpandLink'),
		openedID = null,
		prev = -1;
	
	$('.expandLink').click(function(e) {
		var expandID = $(this).attr('href');
		e.preventDefault();
		if ($(this).hasClass('closed')) {
			$(this).removeClass('closed').addClass('opened');
			
			$(this).blur(); // without this the margin-top cannot be shown in IE8
			jlib.canAnimate ? $(expandID).slideDown(jlib.animationSpeed) : $(expandID).show();
			
		}
		else {
			$(this).removeClass('opened').addClass('closed');
			jlib.canAnimate ? $(expandID).slideUp(jlib.animationSpeed) : $(expandID).hide();
			this.blur(); 
		}
	});
	gpLink.click(function(e) {
		var expandID = $(this).attr('href'),
			s = $(this),
			cur = gpLink.index(this),
			t = this,
			photoIndex = (!(s.attr('rel') == null)) ? s.attr('rel') : -1;
			e.preventDefault();
			
			if ($(expandID).is(':visible')){
				$(expandID).hide();
				s.removeClass('opened').addClass('closed');
				t.blur(); 
			}
			else {
				if (!(openedID == null)) {
						gpLink.eq(prev).removeClass('opened').addClass('closed');
						s.removeClass('closed').addClass('opened');
						$(openedID).hide();
						$(expandID).show();
						t.blur(); 
				}
				else {
					$(expandID).show();
					s.removeClass('closed').addClass('opened');
					t.blur(); 
				}
				
				photoIndex = photoIndex.substring(photoIndex.indexOf('photo_')+6,photoIndex.length) - 1;
				if ((photoIndex > -1)) {
					jlib.swf.changePic(photoIndex);
				}
				
			}
			openedID = expandID;
			prev = cur;
		});
};

/************************************************************
 * Generate two years for the year field
 */
jlib.genYear = function () {
	var thisYear = jlib.today.getFullYear(),
		buf = [
			'<option value="',thisYear,'">',thisYear,'</option>',
			'<option value="',thisYear+1,'">',thisYear+1,'</option>',
			].join('');
		
	$('#inyyyy, #outyyyy')
		.find('option')
		.remove()
		.end()
		.append(buf);
};

jlib.homepagePicPanel = function () {
	var con = $('div.introSlidingContainer'),
		panels = con.find('div.panel'),
		thumbnails = con.find('a.thumbnail'),
		texts = con.find('div.text'),
		photos, arrows, cur, speed,
		showText, hideText, showArrows, hideArrows,
		pathArray, resortPath;


	if ($('body').hasClass('homepage')){
		pathArray = window.location.pathname.split( '/' );
		resortPath = pathArray[1];
		
	// add off images and arrows
	panels.each(function (i) {
		var panel = $(this), onPhoto = panel.find('img.photo');
		onPhoto.after('<img alt="" class="photo off" src="' + onPhoto.attr('src').replace('_on', '_off') + '" />');
		panel.append('<a class="arrow" href="' + thumbnails.eq(i).attr('href') + '"><img alt="" src="/images_v2/'+ resortPath + '/common/arrow_button.gif" /></a>');
		
	});

	photos = con.find('img.off');
	arrows = con.find('a.arrow');
	cur = -1;
	speed = jlib.animationSpeed;

	// define animation functions
	if (jlib.canAnimate) {
		showText = function (i, cb) {
			photos.eq(i).fadeOut(speed);
			if (!jlib.ie6){
				texts.eq(i).fadeIn(speed);
			}
			panels.eq(i).animate({ width: '290px' }, speed, function () {
				if (jlib.ie6){
					texts.eq(i).show();
				}
				if (cb) {
					cb();
				}
			});
		};
		hideText = function (i, cb) {
			photos.eq(i).fadeIn(speed);
			if (!jlib.ie6){
				texts.eq(i).fadeOut(speed);
			}
			panels.eq(i).animate({ width: '130px' }, speed, function () {
				if (jlib.ie6){
					texts.eq(i).hide();
				}
				if (cb) {
					cb();
				}
			});
		};
		showArrows = function (cb) {
			arrows.fadeIn(speed);
			panels.not(':last').animate({ marginRight: '36px', width: '161px' }, speed);
			panels.filter(':last').animate({ width: '161px' }, speed, function () {
				if (cb) {
					cb();
				}
			});
		};
		hideArrows = function (cb) {
			arrows.fadeOut(speed);
			panels.not(':last').animate({ marginRight: '0', width: '130px' }, speed);
			panels.filter(':last').animate({ width: '130px' }, speed, function () {
				if (cb) {
					cb();
				}
			});
		};

	} else {
		showText = function (i, cb) {
			photos.eq(i).hide();
			texts.eq(i).show();
			panels.eq(i).css({ width: '290px' });
			if (cb) {
				cb();
			}
		};
		hideText = function (i, cb) {
			photos.eq(i).show();
			texts.eq(i).hide();
			panels.eq(i).css({ width: '130px' });
			if (cb) {
				cb();
			}
		};
		showArrows = function (cb) {
			arrows.show();
			panels.not(':last').css({ marginRight: '36px', width: '161px' });
			panels.filter(':last').css({ width: '161px' });
			if (cb) {
				cb();
			}
		};
		hideArrows = function (cb) {
			arrows.hide();
			panels.not(':last').css({ marginRight: '0', width: '130px' });
			panels.filter(':last').css({ width: '130px' });
			if (cb) {
				cb();
			}
		};
	}

	// set click handlers
	thumbnails.click(function (e) {
		var index = thumbnails.index(this);
		e.preventDefault();
		con.queue(function () {
			if (cur == -1) {
				// no panels open
				hideArrows(function () {
					showText(index, function () {
						cur = index;
						con.dequeue();
					});
				});
			} else if (index == cur) {
				// clicked on open panel
				hideText(index, function () {
					showArrows(function () {
						cur = -1;
						con.dequeue();
					});
				});
			} else {
				// clicked on closed panel
				hideText(cur);
				showText(index, function () {
					cur = index;
					con.dequeue();
				});
			}
		});
	});
	arrows.click(function (e) {
		e.preventDefault();
		thumbnails.eq(arrows.index(this)).trigger('click');
	});

	// show things
	if (jlib.canAnimate) {
		photos.fadeIn(speed, function () {
			thumbnails.find('img.photo').show();
		});
		thumbnails.find('img.title').fadeIn(speed);
	} else {
		thumbnails.find('img').show();
	}
	showArrows(function () { con.addClass('loaded'); });
	}
};

/************************************************************
 * image button
 */
jlib.imageButtonInit = function () {
	$(':submit[src],:reset[src]').each(function() {	
		var s = $(this);
		$([
			'<a class="imgButton" href="#">',
				'<img class="',s.attr('class'),'" src="',s.attr('src'),'" alt="',s.attr('value'),'" />',
			'</a>'
		].join(''))
		.insertAfter(s)
		.click(function(e){
			e.preventDefault();
			s.trigger('click');
		});
		s.addClass('offscreen').attr('tabIndex','-1');
	});
};

/************************************************************
 * defines swfobject
 */
jlib.swf = {
	conf: {
		resdomain: 'http://res.emirateshotelsresorts.com',
		newId: 'newSwfMain',
		alMaha: {
			menuxml: '/xml_v2/al-maha/en/menu.xml',
			swf: '/media_v2/al-maha/en/amflash.swf',
			hotelid: '7'
		},
		wolganValley: {
			menuxml: '/xml_v2/wolgan-valley/en/menu.xml',
			swf: '/media_v2/wolgan-valley/en/wvflash.swf',
			hotelid: '8'
		}
	},

	flashShowFloorplan: function () {
		var obj = $('#'+jlib.swf.conf.newId);
		if (obj.length==0) {return;}
		obj.get(0).showFloorplan();
	},

	changePic: function (value) {
		var obj = $('#'+jlib.swf.conf.newId);
		if (obj.length==0) {return;}
		obj.get(0).changePic(value);
	},
	
	init: function (o) {
		var hotel = this.conf[o.hotel],
			oldId = o.id ? o.id : 'swfMain',
			flashvars = {
				menuxml: hotel.menuxml,
				xml: o.xml,
				resdomain: this.conf['resdomain'],
				hotelid: hotel.hotelid
			},
			params = {
				menu: 'false',
				scale: 'noScale',
				allowFullscreen: 'true',
				allowScriptAccess: 'always',
				bgcolor: '#FFFFFF'
			},
			attributes = {
				id:jlib.swf.conf.newId
			};
		if(!jlib.haveFlash) {return;}
		if($('#'+oldId).length==0) {return;}
		swfobject.embedSWF(
			hotel.swf, 
			oldId, 
			o.width ? o.width: '960', 
			o.height ? o.height : '537', 
			o.ver ? o.ver : '9.0.0', 
			'/media_v2/expressInstall.swf', flashvars, params, attributes, jlib.afterFlashMain
		);
	}
}

/************************************************************
 * to be called by swfobject
 */
jlib.afterFlashMain = function (e) {
	jlib.haveFlashHeader = e.success;
	if (e.success) {
		//shorter breadcrumbs since the swf contributes some height of breadcrumbs
		$('body').addClass('breadcrumbsBelowSwf');
	} else {
		$('body').addClass('noFlashHeader');
		jlib.mainNav();
	}
};

/************************************************************
 * sidebar on/off, on booking
 */
jlib.sidebarInit = function (){
if(location.href.indexOf("/reservations/booking/online-booking/")!=-1)return;
var sidebarEl=$("#sidebar").get(0);
if(sidebarEl){
var sidebar=new SideBar(sidebarEl);
var divs=sidebarEl.getElementsByTagName("div");
if(divs){
for(var i=0;i<divs.length;i++){
if(divs[i].className.indexOf("sideBar")!=-1){
var panel=new Panel(divs[i],false);
if(panel){
sidebar.addPanel(panel);
panel.setParent(sidebar);
panel.minusButton.src="/images_v2/booking/plus.gif";
}
}
}
var panels=sidebar.getPanels();
if(panels.length>0){
if(panels.length==1){
panels[0].expand();
panels[0].disable();
panels[0].minusButton.src="/images_v2/booking/greyminus.gif";
} 
else if(location.href.indexOf("/reservations/booking/online-booking/")!=-1){
for(var i=0;i<panels.length;i++){
panels[i].expand();
panels[i].disable();
panels[i].minusButton.src="/images_v2/booking/greyminus.gif";
}
}
else{
var lastpanel=panels[panels.length-1];
if(lastpanel){
sidebar.setCurrentPanel(lastpanel);
lastpanel.expand();
}
}
}
}
if(jlib.firefox2)
sidebarEl.style.height=sidebarEl.offsetHeight+"px";
}
}

/************************************************************
 * switch protocol
 */
jlib.httpsInit = function (){
	var __gLocationRE=new RegExp('^'+location.protocol+'//'+location.host);
	var __gProtocolRE=/^https?:/;
	var __gIBERE=/\/reservations\/booking\/online-booking\/$/;
	var __gIsIBE=isIBEPath(location.pathname);
	function findPath(s){
	if(s==null)return null;
	s=s.toString().replace(__gLocationRE,'');
	return(s.charAt(0)=='/')?s.replace(/(?:#|\?).*$/g,'').replace(/index\.html$/,''):null;
	}
	function isIBEPath(s){
	if(s==null)return false;
	return(decodeURIComponent(s.toString()).search(__gIBERE)!=-1);
	}
	function httpsInit(){
	var b=document.body;
	if(b.addEventListener)b.addEventListener('click',httpsClick,false);
	else if(b.attachEvent)b.attachEvent('onclick',httpsClick);
	}
	function httpsClick(e){
	var t,h,p,isIBE,newP;
	if(!e)e=window.event;
	for(t=e.target||e.srcElement;t;t=t.parentNode){
	if(t.nodeType==1&&(t.nodeName=='A'||t.nodeName=='AREA'))break;
	}
	if(!t)return;
	h=t.href;
	p=findPath(h);
	if(p==null)return;
	isIBE=isIBEPath(p);
	if(__gIsIBE==isIBE)return;
	newP=(isIBE)?'https:':'http:';
	if(h.search(__gProtocolRE)==0)t.href=h.replace(__gProtocolRE,newP);
	else if(h.charAt(0)=='/')t.href=newP+'//'+location.hostname+h;
	}
	(function(){
	var oldP=location.protocol;
	var newP;
	var isHTTPS=(oldP=='https:');
	if(__gIsIBE&&!isHTTPS)newP='https:';
	else if(!__gIsIBE&&isHTTPS)newP='http:';
	if(newP)location.replace(location.href.replace(oldP,newP));
	})();

	httpsInit();
}


/************************************************************
 * onload, onunload
 */

jlib.ie6 = ($.browser.msie && parseFloat($.browser.version) < 7);
jlib.ie7 = ($.browser.msie && parseFloat($.browser.version) < 8 && !jlib.ie6);
jlib.safari2 = ($.browser.safari && parseFloat($.browser.version) < 420);
jlib.firefox2 = ($.browser.mozilla && parseFloat($.browser.version) < 1.9);
jlib.canAnimate = !jlib.safari2;
jlib.haveFlash = (window.swfobject && swfobject.hasFlashPlayerVersion(jlib.minFlashVer));

if (jlib.safari2) {
	// the last version of jQuery that works with Safari 2 is 1.2.6
	jQuery.noConflict(true);
	document.writeln('<script type="text/javascript" src="/common_v2/js/jquery-1.2.6.min.js"></script>');
}

// sifr
if (jlib.safari2) {  //loads sifr scripts for all pages
	document.writeln(
		'<script type="text/javascript" src="/common_v2/js/sifr.js"></script>\n' +
		'<script type="text/javascript" src="/common_v2/js/sifr-config.js"></script>'
	);
} else if ($('.sIFR').length>0) {  //loads sifr scripts only when needed
	document.writeln(
		'<script type="text/javascript" src="/common_v2/js/sifr.js"></script>\n' +
		'<script type="text/javascript" src="/common_v2/js/sifr-config.js"></script>'
	);
}

jlib.httpsInit();

jq(function () {
	var body = $('body');

	jlib.haveFlashHeader = !body.hasClass('noFlashHeader');

	if (jlib.safari2) {
		body.addClass('safari2');
	}
	if (!jlib.haveFlash) {
		body.addClass('noflash');
	}

	if (!jlib.haveFlash || !jlib.haveFlashHeader) {
		jlib.mainNav();
	}

	jlib.openNewWindow();
	jlib.openVirtualTourPopup();
	jlib.openflashFloorPlan();
	jlib.breadcrumbs();
	jlib.runQueue();
});

jlib.queue(function () {
	jlib.homepagePicPanel();
	jlib.expandPanel();
	jlib.contactUsFormSelection();
	jlib.imageButtonInit()
	jlib.contactUsFormInit();
	jlib.sidebarInit();
	jlib.genYear();
	$('.expandLinkContainer').css('display','block');
});