// remove the "$" namespace from jQuery, avoids conflicts with other libraries
// jQuery.noConflict();


// closure, mapping jQuery to $, window, document and undefined - useful for minifing tools
(function ($, window, document, undefined) {

	// document ready method
	$(function () {

		// NAVBAR Fixes for IE 6 and 7...
		// Fix IE 6
		//alert($.browser.version);
		if ($.browser.msie && parseInt($.browser.version, 10) == 6) {

			$('#menu > ul > li')
		.mouseenter(function () {
			$(this)
				.siblings()
				    .removeClass('hover')
				    .end()
				.addClass('hover')
				.parent()
				    .find('ul')
				        .css({ left: '-999em' })
				        .end()
			        .find('li.active > ul')
    			        .css({ 'left': 0 })
    			        .end()
    			    .end()
				.find('ul')
					.css({ 'left': '0' });
		})
		.mouseleave(function () {
			$(this)
				.removeClass('hover')
				.parent()
				    .find('ul')
				        .css({ left: '-999em' })
				        .end()
					.parent()
					    .find('li.active > ul')
					        .css({ 'left': 0 });
		});

			function _isNotActiveSubMenu() {
				alert($(this).parent().hasClass('active'));
				return $(this).parent().hasClass('active');
			}

		}

		// Fix IE 7
		if ($.browser.msie && parseInt($.browser.version, 10) <= 7) {
			var subnavs = $('#menu li ul');
			subnavs.each(function () {
				var lis = $(this).find('li'),
            lisCount = lis.length,
            w = 960,
            lisWidth = 0,
            padd = 0;
				lis.each(function () {
					lisWidth += $(this).width();
				});
				if (w > lisWidth) {
					padd = Math.floor(((w - lisWidth) / lisCount) / 2) - 2;
				}
				lis.each(function () {
					$(this).css({ padding: '0 ' + padd + 'px' });
				});
			});
		}

		// Setup breadcrumbs, ensure they stack properly on each other (z-index)
		if ($('#breadcrumbs a').length) {
			var starter = 100;
			var items = $('#breadcrumbs a');
			items.each(function () {
				$(this).css({ zIndex: --starter });
			});
		}

		//------------------ Sliding Carousel selector for displaying rooms/restaurants etc. ------------------------------
		if ($.fn.jCarouselLite) {
			var slideshow1 = $('#slideshow1');
			if (slideshow1.length) {
				var lis = slideshow1.find('li'),
			open = $('<ul id="clone1"/>').hide().appendTo('body'),
			lisNr = lis.length;

				// Zoom Items on Rollover 
				slideshow1.delegate('.slide', 'mouseenter', function () {
					var item = $(this),
				pos = item.offset();
					if (!$.browser.msie)
						open.empty();
					open.prepend(item.clone().css({ width: '', height: '' }));
					open.show().css({
						top: pos['top'] + item.height() - open.height() + 'px',
						left: pos['left'] - 10 + 'px'
					}).show();
				});

				open.bind('mouseleave', function () {
					open.css({ left: '-999em' });
				});

				var visible = 5;
				var first = 0;
				if (lis.length > visible) {
					slideshow1.append('<p class="nav"><a href="#" class="prev disabled"></a><a href="#" class="next"></a></p>');
					slideshow1.find('.slider').jCarouselLite({
						btnPrev: 'a.prev',
						btnNext: 'a.next',
						visible: visible,
						scroll: 5,
						speed: 400,
						auto: false,
						circular: false,
						afterEnd: function (a) {
							first = $(a[0]).index() + 1;
							last = $(a[a.length - 1]).index() + 1;
							$('.slider').nextAll('.counter').html('Showing ' + first + ' to ' + last + ' of ' + lis.length);
						}
					});
					lis.css({ width: '', height: '' });
				} else {
					slideshow1.find('.slider').width(Math.floor(lis.length * lis.width()));
					visible = lis.length;
				}

				if (lis.length > 0)
					first = 1;

				slideshow1.append('<p class="counter">Showing ' + first + ' to ' + visible + ' of ' + lis.length + '</p>');
			}
		}

		//---------------------- Fading Slideshows -----------------------------------------------------------
		if ($.fn.slideshowFade) {
			if ($('#featuredSlideshow ul li').size() > 1) {
				$('#featuredSlideshow').slideshowFade({
					//speed: 600, // default 600
					//auto: 5000 // default 5000
				})
				.bind('slideChanging', function () {
					// Display caption 2/5
					if (arguments[2].length != 1) {
						$(this).find('.details p').html(arguments[5].index() + 1 + '/' + arguments[2].length);
					}
				})
				.each(function () {
					var t = $(this);
					// Display caption 1/5
					if (t.find('li').length != 1)
						t.find('.details p').html('1/' + t.find('li').length);
				});
			} else {
				$('#featuredSlideshow .details').hide();
				if ($('.slideshow_fade').parents('.homepage').size() == 0 && $('.slideshow_fade').children('.btn_ent_play_video').size() == 0) {
					$('.slideshow_fade').css('height', '286px');
				}

			}
			$('.slideshow_fade li').css('visibility', 'visible');
		}

		//---------------- Cuisine Finder -------------------------------

		$('#cuisineFinder .topLevel li')
	.append('<span class="arrow"></span>')
	.simpleChooser($('#cuisineFinder .secondLevel'));



		//--------------- Other Stuff -----------------------------------
		// adjust navbar item heights
		if ($.fn.setMinHeight) {
			$('.menus > li').setMinHeight(2);
		}

		// setup expandable boxes on detail pages
		if ($.fn.expandable) {
			$('.expandable').expandable();
		}


		if ($('.datepicker').length) {
			//TODO remove this stuff, it does not work the way we need to.
			//		$.getScript('/js/jq-datePicker.min.js', function(){
			//			
			//			$('.datepicker').datepicker({
			//				minDate: 0,		// zero days from today
			//				maxDate: 366,	// days from today
			//				firstDay: 1,	// Monday as first day of week
			//				dateFormat: "d M yy"
			//			});
			//		});
			//$('head').append('<link rel="stylesheet" href="/css/datepicker/overcast/jquery-ui-1.8.14.custom.css"/>');
		}

	});


	// equal heights on promoBoxes
	// done 200ms after window.load to dodge the odd font-face flicker that is causing height miscalculations
	$(window).load(function () {
		setTimeout(function () {
			$('.promoBox').equalHeights();
		}, 200);
	})


	//------------------------------------------------------------------
	// jQuery Plugins
	//------------------------------------------------------------------

	// Simple chooser, by Simon East, igloo digital 2011
	// Use it on a series of links to show the respective element, hiding all others
	$.fn.simpleChooser = function (itemsToShowHide) {
		return this.click(function () {
			$(this)
		.addClass('active')
		.siblings().removeClass('active');
			itemsToShowHide
		.hide()
		.eq($(this).index())
		.show();
			return false;
		});
	}

	// slideshowFade - jQuery plugin - creates a fading slideshow - by Valentin Agachi http://agachi.name
	$.fn.slideshowFade = function (args) {
		if (!this.length) return this;

		var opts = $.extend({
			slides: '> ul > li', // slides selector
			buttons: true, 		// show buttons
			speed: 750, 			// speed of transition
			auto: 5000				// speed of auto transition
		}, args || {});

		return this.each(function () {

			var parent = $(this),
			slides = parent.find(opts.slides),
			buttons = $(),
			timer = null,
			n = 0,
			s = '';

			slides.each(function () {
				this.id = parent[0].id + '-' + (++n);
				s += '<a href="#' + this.id + '">' + n + '</a>';
			});

			if (opts.buttons) {
				buttons = parent.append('<div class="buttons">' + s + '</div>')
				.find('.buttons a').click(function () {
					var hash = this.hash.substr(1);
					parent.triggerHandler('slideChange', [slides.filter('#' + hash)]);
					return false;
				});
			}

			// change slides with fading effect
			function change(next) {
				var active = slides.filter('.active');
				if (!next || !next.jquery)
					next = active.next();
				if (!next.length)
					next = slides.eq(0);

				var stop = parent.triggerHandler('slideChanging', [parent, slides, opts, active, next]); // handler can return true to stop slide advance
				if (!stop) {
					next.addClass('next');
					buttons.removeClass('active').filter('[href$="#' + next[0].id + '"]').addClass('active');
					active.fadeOut(opts.speed, function () {
						next.addClass('active').removeClass('next');
						active.removeClass('active').show();
					});
				} else {
					buttons.removeClass('active').filter('[href$="#' + next[0].id + '"]').addClass('active');
				}

				parent.trigger('slideChanged', [parent, slides, next]);
			}

			// change slides minus fading effect
			function changeNow(next) {
				slides.removeClass('active');
				next.addClass('active').show();
				buttons.removeClass('active').filter('[href$="#' + next[0].id + '"]').addClass('active');
				parent.trigger('slideChanged', [parent, slides, next]);
			}

			function auto() {
				if (opts.auto)
					timer = setInterval(change, opts.speed + opts.auto);
			}

			parent.bind('slideChange', function (ev, slide, now) {
				clearInterval(timer);
				now ? changeNow(slide) : change(slide);
				auto();
			});

			parent.bind('slideAdvance', function (ev, delta) {
				var dir = (delta > 0),
				cnt = Math.abs(delta),
				active = slides.filter('.active'),
				next = active;
				while (cnt--) {
					next = active[(dir ? 'next' : 'prev')]();
					if (!next.length)
						next = slides.eq(dir ? 0 : slides.length - 1);
				}
				parent.trigger('slideChange', [next]);
			});

			parent.bind('slideStop', function (ev) {
				clearInterval(timer);
			});

			parent.bind('changeAuto', function (ev, time) {
				clearInterval(timer);
				opts.auto = time;
				auto();
			});

			if ((a = slides.filter('.active')) && !a.length)
				changeNow(slides.eq(0));
			else
				changeNow(a);

			auto();
		});
	};


	// equalHeightsByRow - jQuery plugin - forces elements on the same row to have the same height (maximum)
	$.fn.setMinHeight = function (setCount) {
		for (var i = 0; i < this.length; i += 2) {
			var curSet = this.slice(i, i + 2),
    height = 0;
			curSet.each(function () { height = Math.max(height, $(this).height()); })
        .css('min-height', height);
		}
		return this;
	};

	// equalHeights - jQuery plugin - forces elements to have the same height (maximum)
	$.fn.equalHeights = function (options) {
		var m = 0;
		this.each(function () {
			m = Math.max(m, $(this).outerHeight());
		});
		return this.each(function () {
			var t = $(this), p = 0;
			$.each(['borderTopWidth', 'paddingTop', 'paddingBottom', 'borderBottomWidth'], function (i, n) {
				var v = parseInt(t.css(n));
				p += (isNaN(v) ? 0 : v);
			});
			var h = m - p;
			if (options && options.padding)
				h += options.padding;
			if ($.browser.msie && $.browser.version <= 6)
				t.css('height', h);
			t.css('min-height', h);
		});
	};


	$.fn.expandable = function (args) {
		if (!this.length) return this;

		var opts = $.extend({
			initial: 'opened', 	// initial status values : opened/closed
			speed: 600, 			// speed of transition
			openText: 'Expand',
			closeText: 'Close'
		}, args || {});

		return this.each(function () {
			var div = $(this), label = ((opts.initial == 'opened') ? opts.closeText : opts.openText), parentClass = ((opts.initial == 'opened') ? 'opened' : '');
			div.addClass(parentClass).append('<a class="btn_expand" href="#">' + label + '</a>').find('a.btn_expand').click(function () {
				div.toggleClass('opened');
				$(this).html((div.hasClass('opened') ? opts.closeText : opts.openText));
				return false;
			});
		});
	};


})(jQuery, window, document);                     		// end enclosure




