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();
		});
	},

	// 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;
			};
	}

};

/************************************************************
 * hoverImages
 */
jlib.hoverImages = function () {
	$('img.over2').hover(
		function () {
			this.src = this.onSrc || this.src.replace(/_off\./, '_on.');
			this.onSrc = this.src;
		},
		function () {
			if ($(this).hasClass('current')) { return; }
			this.src = this.offSrc || this.src.replace(/_on\./, '_off.');
			this.offSrc = this.src;
		}
	)
	.each(function () {
		// preload
		$('<img/>').get(0).src = this.src.replace(/_off\./, '_on.');
	});
	
}

/************************************************************
 * Main nav menus
 */
jlib.mainNav = function () {
	var me = arguments.callee,
		href = location.href,
		sep = (jQuery.browser.msie && parseFloat(jQuery.browser.version) < 8) ? ' ' : ', ',
		speed = 'fast',
		isGroup = window.location.pathname.indexOf('/group/')==0;  //is group site?

	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'),
			currentSrc = offImg.attr('src').replace(/_off/, '_current'),
			div = $('<div></div>'),
			mTop = $('<div class="navTop"><img class="navArrow" src="/images_v2/group/main_header/dropmenu_arrow.gif"/></div>'),
			nav = $('<ul class="plain"></ul>'),
			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
		overImg = $('<img src="' + overSrc + '" alt="" class="over" />');
		if (jlib.canAnimate) {
			overImg.css('opacity', 0);
		} else {
			overImg.css('display', 'none');
		}
		overImg.appendTo(a);

		// replace off with current image for current section
		if (
			href.indexOf('/reserve/')>-1 && a[0].href.indexOf('/reserve/')>-1 ||
			href.indexOf('/conservation/')>-1 && a[0].href.indexOf('/conservation/')>-1 ||
			href.indexOf('/florafauna/')>-1 && a[0].href.indexOf('/florafauna/')>-1 ||
			href.indexOf('/experience/')>-1 && a[0].href.indexOf('/experience/')>-1 ||
			href.indexOf('/downloads/full/')>-1 && a[0].href.indexOf('/conservation/')>-1
		) {
			offImg.attr('src', currentSrc);
		}

		if (isGroup) div.append(mTop);
		
		switch(li.attr('id')) {
			case 'reserveNav':
				nav.append([
					'<li><a href="/en/reserve/location/"><img class="over2" src="/images/en/nav/sub/png/location_and_zones_off.png" alt="Location &amp; Zones"/></a></li>',
					'<li><a href="/en/reserve/history/"><img class="over2" src="/images/en/nav/sub/png/reserve_history_off.png" alt="Reserve History"/></a></li>',
					'<li><a href="/en/reserve/vision/"><img class="over2" src="/images/en/nav/sub/png/our_vision_and_values_off.png" alt="Our Vision &amp; Values"/></a></li>',
					'<li><a href="/en/reserve/board-sponsors/"><img class="over2" src="/images/en/nav/sub/png/board_and_sponsors_off.png" alt="Board &amp; Ruler\'s Decree"/></a></li>',
					'<li><a href="/en/reserve/sponsors-asso/"><img class="over2" src="/images/en/nav/sub/png/sponsors-asso_off.png" alt="Sponsors &amp; Associations"/></a></li>'
				].join(''));
				break;
			case 'conservationNav':
				nav.append([
					'<li><a href="/en/conservation/research/"><img class="over2" src="/images/en/nav/sub/png/current_research_off.png" alt="Current Research"/></a></li>',
					'<li><a href="/en/conservation/reports/"><img class="over2" src="/images/en/nav/sub/png/research_reports_off.png" alt="Research Reports"/></a></li>',
					'<li><a href="/en/conservation/sustainability/"><img class="over2" src="/images/en/nav/sub/png/sustainability_off.png" alt="Sustainability"/></a></li>'
				].join(''));
				break;
			case 'floraNav':
				break;
			case 'experienceNav':
				nav.append([
					'<li><a href="/en/experience/activities/"><img class="over2" src="/images/en/nav/sub/png/activities_off.png" alt="Activities"/></a></li>',
					'<li><a href="/en/experience/visits/"><img class="over2" src="/images/en/nav/sub/png/day_visits_and_tours_off.png" alt="Day Visits &amp; Tours"/></a></li>',
					'<li><a href="/en/experience/resort/"><img class="over2" src="/images/en/nav/sub/png/resort_off.png" alt="Resort"/></a></li>'
				].join(''));
				break;
		}
		nav.find('li:last').addClass('last');
		if (jlib.ie6) {
			nav.find('img').each(function () {
				this.src = this.src
					.replace('/png/', '/gif/')
					.replace(/\.png$/i, '.gif');
			});
		}

		
		//var nav = $('#' + li.attr('id').replace(/Nav/, 'Links')).clone().removeAttr('id');
		if (nav.find('li').length > 0) {
			div
				.append(nav)
				.css('left', '-9999px')
				.appendTo(li);
		}

		// 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 ||
				href.indexOf('/downloads/full/')>-1 && a[0].href.indexOf('/conservation/reports/')>-1
			) {
				a.parent().addClass('on');
				a.find('img')
					.addClass('current')
					.attr(
						'src',
						a.find('img').attr('src').replace(/_off\./, '_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();
			if (divW < 150) divW = 150;  //minimum width, to avoid clipping by pre-mature calculation before images in div are loaded
			div.css({
				top: (liH - divH) + 'px',
				clip: getRect(divH)
			});

			over = function () {
				if(isGroup) {
					var arrow = mTop.find('.navArrow'),
						w = Math.round((li.width()-13)/2),  //13 = width of menu arrow image
						d = 10;  //right padding of menu image
					if(li.hasClass('last')) {
						arrow.css('right', w+'px');
					} else {
						arrow.css('left', w-d+'px');
					}
				}
				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;
};

/************************************************************
 * Side nav menu
 */
jlib.sideNav = function () {
	var me = arguments.callee,
		href = location.href;

	if (me.hasInit) {
		return;
	}

	$('ul.sideNav li').each(function () {
		if (href.indexOf($(this).find('a')[0].href) == 0) {
			$(this).addClass('current');
		}
	});

	me.hasInit = true;
};

/************************************************************
 * Did you know
 */
jlib.didYouKnow = function () {
	var me = arguments.callee,
		dyk = $('div.dyk'),
		dykSet = dyk.find('.dykSet'),
		w=171,
		scrollSpeed = 800;

	if (me.hasInit) {
		return;
	}

	if (dykSet.find('li').length <= 1) {
		extendLimits();
		me.hasInit = true;
		return;
	}
	
	dyk.find('.dykTitle').append(
		[
			'<div class="dykCtrl">',
				'<a class="dykCtrlL" href="#"><img class="over2" src="/images/arrow_left_off.gif" alt=""/></a>',
				'<a class="dykCtrlR" href="#"><img class="over2" src="/images/arrow_right_off.gif" alt=""/></a>',
			'</div>'
		].join('')
	);

	extendLimits();

	dyk.find('.dykCtrlL').click(function (e) {
		e.preventDefault();
		if (!me.animating) {
			scrollLeft();
		}
	});

	dyk.find('.dykCtrlR').click(function (e) {
		e.preventDefault();
		if (!me.animating) {
			scrollRight();
		}
	});

	me.hasInit = true;
	
	function scrollLeft () {
		me.animating = true;
		dykSet.animate(
			{ left: '+='+w }, scrollSpeed, 
			function() {  // Animation complete.
				// rotate
				$(this).find('li:last').prependTo($(this));
				$(this).css('left',$(this).position().left-w+'px');
				me.animating = false;
			}
		);
	}

	function scrollRight () {
		me.animating = true;
		dykSet.animate(
			{ left: '-='+w }, scrollSpeed, 
			function() {  // Animation complete.
				// rotate
				$(this).find('li:first').appendTo($(this));
				$(this).css('left',$(this).position().left+w+'px');
				me.animating = false;
			}
		);
	}
	
	function extendLimits () {
		var lis = dykSet.find('li'),
			width = w*lis.length;
		dykSet
			.width(2*width+'px')
			.css('left', 0-width+'px')
			.prepend(lis.clone());
		dyk.find('.dykViewport').height(dykSet.height()+'px');
	}

};

/************************************************************
 * Pic Panel
 */
jlib.picPanelThreadId = 0;
jlib.picPanelHomeThreadId = 0;
jlib.picPanelHomeThread2Id = 0;

jlib.picPanelRotate = function () {
	var as = $('.picPanel .picSwitch a[rel=image]'),
		cur = as.filter('.on'),
		nx = cur.parent().next();

	if (nx.length==0) {
		nx = as.filter(':first').parent();
	}
	cur.trigger('mouseout');
	nx.find('a').trigger('mouseover');
}

jlib.picPanel = function () {
	var me = arguments.callee,
		pp = $('.picPanel'),
		ppH = pp.height(),
		sw = pp.find('.picSwitch'),
		h = 139, //height of sw
		as = sw.find('a[rel=image]'),
		picSeed = pp.find('.picPanelPic'),
		speed = 200,
		rotateSpeed = 5000,
		pics;

	if (me.hasInit) {
		return;
	}

	if (jlib.ie6) {
		as.hover(
			function () {
				$(this).addClass('over');
				as.not(this).removeClass('over').removeClass('on');
			},
			function () { $(this).removeClass('over'); }
		);
		as.find('img.border').removeClass('hide');
		as.find('img.picPanelTN').each(function () {
			$(this).attr(
				'src',
				$(this).attr('src').replace(/\.png$/i, '.gif')
			);
		});
	}
	if ($.browser.chrome) {
		$('.picPanel .picSwitch li a img.border').css('border-color', '#DDD'); //no transparency
	}
	
	if ($('body.homepage').length>0) {
		// Elements having PNG as background image does not register mouse events on IE's.
		// Create an <img> tag rather than using background image.
		if ($.browser.msie) {
			sw = $('<div class="picSwitch"></div>')
				.append('<img class="picSwitchBkgd png" src="/images/home/picSwitch_bkgd.png" alt=""/>')
				.append(sw.removeClass('picSwitch'))
				.appendTo(pp);
		}
		
		pp.hover(
			function () { // show
				if (sw.position().top <= ppH-h) { return; }
				sw.stop().animate(
					{ top: ppH-h+'px' },
					2*speed
				);
			},
			function () { // hide
				if (sw.position().top >= ppH) { return; }
				sw.attr('animating', 'true');
				sw.stop().animate(
					{ top: ppH+'px' },
					2*speed
				);
			}
		);
	}

	as.click(function (e) {
		e.preventDefault();
	})
	.mouseover(function (e) {
		var src = this.href,
			p, p2;
		
		if ($(e.originalTarget).hasClass('border')) { return; }  //avoid duplicated events
		e.stopPropagation();
	
		as.not(this).removeClass('on');
		$(this).addClass('on');

		p = pp.find('.picPanelPic').filter(':last');
		if (src.indexOf(p.attr('src'))!=-1) { // same photo
			p.stop().fadeTo(speed, 1);
		} else {
			p2 = pics.filter('[src$='+src+']')
				.stop()
				.css('opacity', 0.2)  //restore default
				.insertBefore(p);  //put under p
			p.stop().fadeTo(
				speed, 0.2,
				function () {
					p
						.fadeTo(speed, 0)  //continue fading
						.insertBefore(p2)  //put under p2
						.css('opacity', 0.2);  //restore default
					p2.fadeTo(speed, 1);
				}
			);
		}
	})
	.filter(':first').addClass('on').end()
	.not(':first').each(function () {
		picSeed.clone()
			.removeAttr('id')
			.attr('src', this.href)
			.attr('useMap', '#'+$(this).attr('id').replace('picSwitch', 'homepicPanelMap'))
			.css('opacity', 0)
			.prependTo(pp);
	});

	
	pp.mouseover(function (e) {
		window.clearInterval(jlib.picPanelThreadId);
		window.clearInterval(jlib.picPanelHomeThreadId);
		window.clearInterval(jlib.picPanelHomeThread2Id);
	})
	.mouseout(function (e) {
		window.clearInterval(jlib.picPanelThreadId);
		window.clearInterval(jlib.picPanelHomeThreadId);
		window.clearInterval(jlib.picPanelHomeThread2Id);
		jlib.picPanelThreadId = window.setInterval('jlib.picPanelRotate()', rotateSpeed);
	})
	.trigger('mouseout');
	
	pics = pp.find('.picPanelPic');
	pp.trigger('mouseover');
	jlib.picPanelHomeThreadId = window.setTimeout('$(".picPanel").trigger("mouseout")', rotateSpeed * 1.5);
	jlib.picPanelHomeThread2Id = window.setTimeout('$(".picPanel .picSwitch a[rel=image]").eq(1).trigger("mouseover");', rotateSpeed);
	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,
			matched = 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
			}
			if (pathname.indexOf(a.attr('href').toLowerCase()) == 0) {
				matched = true;
			}
		});
		
		if (!matched) {
			// add class to the second last one
			$('.breadcrumbs > li:last').prev().addClass('last');
			return;
		}

		// create menu
		parent.append([
			'<span>', cur.text(), '</span>'
		].join(''));
		menu = parent.find('.menu');
		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)
		};
		jlib.clickOutside(function () {
			jlib.hide(ul, hideOpts);
			isVisible = false;
		}, parent);
	});
};

/************************************************************
 * expandablePanel
 */
jlib.expandablePanel = function () {
	$('.expandLinkContainer').each(function () {
		$(this).find('.expandLink').click(function (e) {
			e.preventDefault();
			$(this).toggleClass('closed').toggleClass('opened');
			$('#'+$(this).attr('href').substring(1)).slideToggle();
		});
	});
};


/************************************************************
 * 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();
 		});
};


/************************************************************
 * 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);
};

/************************************************************
 * 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: {
		newId: 'newSwfMain'
	},

	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 = {
				xml: o.xml,
				page: o.page,
				image1: o.image1 || '',
				image2: o.image2 || '',
				image3: o.image3 || '',
				image4: o.image4 || '',
				image5: o.image5 || '',
				image6: o.image6 || '',
				image7: o.image7 || '',
				image8: o.image8 || '',
				image9: o.image9 || '',
				image10: o.image10 || ''
			},
			params = {
				menu: 'false',
				scale: 'noScale',
				wmode: 'transparent',
				allowFullscreen: 'true',
				allowScriptAccess: 'always',
				bgcolor: '#FFFFFF'
			},
			attributes = {
				id:jlib.swf.conf.newId
			};
		if(!jlib.haveFlash) {return;}
		if($('#'+oldId).length==0) {return;}
		swfobject.embedSWF(
			'/media/wvflash.swf', 
			oldId, 
			o.width ? o.width: '644', 
			o.height ? o.height : '537', 
			o.ver ? o.ver : '9.0.0', 
			'/media/expressInstall.swf', flashvars, params, attributes
		);
	}
}

/************************************************************
 * onload, onunload
 */

$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());

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>');
}

jq(function () {
	var body = $('body');
	
	if (jlib.safari2) {
		body.addClass('safari2');
	}

	jlib.mainNav();
	jlib.openNewWindow();
	jlib.breadcrumbs();
	jlib.runQueue();
});

jlib.queue(function () {
	jlib.sideNav();
	//jlib.didYouKnow();
	jlib.picPanel();
	jlib.contactUsFormSelection();
	jlib.contactUsFormSelection();
	jlib.imageButtonInit();
	jlib.expandablePanel();
	jlib.contactUsFormInit();
	jlib.genYear();
	jlib.hoverImages();
});
