//component isolation
(function($) {
	function BookingWidget() {
		this.rangeOpen = true;
		this.arrDate = new Date();
		this.arrDate.setDate(this.arrDate.getDate() + 1);
		this.depDate = new Date();
		this.depDate.setDate(this.arrDate.getDate() + 1);
	};
	BookingWidget.prototype.updateDateFields = function(selectedDate) {
		if (this.rangeOpen) {
			this.arrDate = selectedDate;
			this.depDate = new Date(this.arrDate); //pass by val and set to same day then increment 1 day
			this.depDate.setDate(this.arrDate.getDate() + 1);
			this.rangeOpen = false;
		}
		else {
			if (selectedDate < this.arrDate) {
				this.depDate = this.arrDate;
				this.arrDate = selectedDate;
			}
			else if (selectedDate > this.arrDate) {
				//do nothing if ==
				this.depDate = selectedDate;
			}
			this.rangeOpen = true;
		}
		this.fillDateFields();
	};
	BookingWidget.prototype.fillDateFields = function() {
		$("input#arr").val(this.formatMonth(this.arrDate.getMonth()) + '/' + this.arrDate.getDate() + '/' + this.arrDate.getFullYear());
		$("input#dep").val(this.formatMonth(this.depDate.getMonth()) + '/' + this.depDate.getDate() + '/' + this.depDate.getFullYear());
	};
	BookingWidget.prototype.hilightRange = function(date) {
		if ((date >= this.arrDate) && (date <= this.depDate)) {
			return [true, 'ui-datepicker-range'];
		}
		else return [true, ''];
	};
	BookingWidget.prototype.formatMonth = function(mIndex) {
		return mIndex + 1;
	};
	BookingWidget.prototype.getUrl = function(baseUrl) {
		var arrDate = new Date(Date.parse($("input#arr").val()));
		var depDate = new Date(Date.parse($("input#dep").val()));
		var ad = $('#ad option:selected').val();
		var ch = $('#ch option:selected').val();

		var y = arrDate.getFullYear();
		var m = this.formatMonth(arrDate.getMonth());
		var d = arrDate.getDate();
		var one_day = 86400000;
		var timeDiff = depDate.getTime() - arrDate.getTime();
		var days = Math.ceil(timeDiff / one_day);

		return baseUrl + '#/sun-valley-lodge/' + y + "/" + m + "/" + d + "/" + days + "/" + ad + "/" + ch;
	};

	//bind instance to dom
	window.bookingWidget = new BookingWidget();
	return (window.bookingWidget);
})(jQuery);


$(document).ready(function() {
	var promoWrapper = $('#promoWidget > .slides > .promo');
	var maxIndex = promoWrapper.length - 1;
	var index = 0;
	// only show the first image, hide the rest
	promoWrapper.hide().filter(':first').show();
	
	$('#promoWidget > a.navLeft').click(function () {
		$('#promoWidget').stopTime();
		
		var prevIndex = index;
		if (index == 0) index = maxIndex;
		else index--;
		swapPromo(index, prevIndex, 300);
	});
	
	$('#promoWidget > a.navRight').click(function () {
		$('#promoWidget').stopTime();
		
		var prevIndex = index;
		if (index == maxIndex) index = 0;
		else index++;
		swapPromo(index, prevIndex, 300);
	});
	
	$('#promoWidget').everyTime(10000, function() {
		var prevIndex = index;
		if (index == maxIndex) index = 0;
		else index++;
		swapPromo(index, prevIndex, 600);
	}, 0);
	
	function swapPromo(index, prevIndex, fadeDuration)
	{
		promoWrapper.css('z-index', 4);
		promoWrapper.eq(index).css('z-index', 5);
		promoWrapper.eq(index).fadeIn(fadeDuration, function()
		{
			promoWrapper.eq(prevIndex).hide();
		});
	}
	
	var promoTicker = $('#tickerRight > p');
	var promoTickermaxIndex = promoTicker.length - 1;
	var promoTickerIndex = 0;
	// only show the first image, hide the rest
	promoTicker.hide().filter(':first').show();

	
	$('#tickerRight').everyTime(8000, function() {
		rotateTicker();
	}, 0);
	
	function rotateTicker()
	{
		var prevIndex = promoTickerIndex;
		if (promoTickerIndex == promoTickermaxIndex) promoTickerIndex = 0;
		else promoTickerIndex++;
		
		promoTicker.eq(prevIndex).hide('slide', { direction: "up" }, 1000);
		promoTicker.eq(promoTickerIndex).show('slide', { direction: "down" }, 1000);
	}
	
	//begin booking widget
	$("#calendar").datepicker({
		onSelect: function(dateText, inst) {
			var selectedDate = new Date(Date.parse(dateText));
			bookingWidget.updateDateFields(selectedDate);
		},
		beforeShowDay: hilightRangeLocal
	});

	bookingWidget.fillDateFields();

	function hilightRangeLocal(date) {
		return bookingWidget.hilightRange(date);
	}

	$("#arr").datepicker({
		onSelect: function(dateText, inst) {
			arrDate = new Date(Date.parse(dateText));
			depDate = new Date(arrDate); //pass by val and set to same day then increment 1 day
			depDate.setDate(arrDate.getDate() + 1);
			bookingWidget.arrDate = arrDate;
			bookingWidget.depDate = depDate;
			bookingWidget.fillDateFields();
			//$("#dep").datepicker("minDate", arrDate);
			$("#calendar").datepicker("setDate", depDate)
		}
	});
	$("#dep").datepicker({
		onSelect: function(dateText, inst) {
			depDate = new Date(Date.parse(dateText));
			bookingWidget.depDate = depDate;
			bookingWidget.fillDateFields();
			$("#calendar").datepicker("setDate", depDate)
		}
	});

	$('#roomSearch').click(function() {
		window.location.href = bookingWidget.getUrl("/trip-planner/lodging/");
	});
	//end booking widget
	
	$('.tab').click(function () {
		$('#comboWidgetL1 > .header > a.active').removeClass('active');
		$(this).addClass('active');
		$('#comboWidgetL1 > .content').removeClass('active');
		$(this.rel).addClass('active');
	});
	
	var camWrapper = $('#comboWidgetL1 > #pnl3 .cams > .webcams > .cam');
	var cMaxIndex = camWrapper.length - 1;
	var camIndex = 0;
	// only show the first image, hide the rest
	camWrapper.hide().filter(':first').show();
	
	$('#pnl3 a.navLeft').click(function () {
		var prevIndex = camIndex;
		if (camIndex == 0) camIndex = cMaxIndex;
		else camIndex--;
		swapWebcam(camIndex, prevIndex);
	});
	
	$('#pnl3 a.navRight').click(function () {
		var prevIndex = camIndex;
		if (camIndex == cMaxIndex) camIndex = 0;
		else camIndex++;
		swapWebcam(camIndex, prevIndex);
	});
	
	function swapWebcam(camIndex, prevIndex)
	{
		camWrapper.css('z-index', 4);
		camWrapper.eq(camIndex).css('z-index', 5);
		camWrapper.eq(camIndex).fadeIn(300, function()
		{
			camWrapper.eq(prevIndex).hide();
		});
	}
	
	$('#searchboxSubmit').click(function () {
		var searchString = $("#searchboxInput").val();
		window.location.href = encodeForSearchPage(searchString);
	});
	$("#searchboxInput").live("keypress", function(e) {
		if (e.keyCode == 13) {
			var searchString = $("#searchboxInput").val();
			window.location.href = encodeForSearchPage(searchString);
		}
	});
	$("#searchboxSubmit").live("keypress", function(e) {
		if (e.keyCode == 13) {
			var searchString = $("#searchboxInput").val();
			window.location.href = encodeForSearchPage(searchString);
		}
	});
});

function initPlasmaClient()
{
	$('#plasmaClient').oneTime(1500, function() {
		var pFlash = $('#plasmaFlash');
		pFlash.css('left', '0px');
		$('#plasmaDhtml').hide();
	});
}

function encodeForSearchPage(searchString) {
	var searchData = searchString.split(' ');
	var normalized = "";
	for (var i=0; i<searchData.length; i++)
	{
		normalized += searchData[i];
		if (i < searchData.length - 1) normalized += '+';
	}
	return '/search/#/' + normalized;
}