//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();
        });
    }


    //init client side blog sidebar
    if ($('#tickerRight').length > 0) {
        var headlineList = $('#tickerRight');
        $.ajax({
            url: '/HeadlineGeneratorJson.pxml',
            success: function (data) {
                headlineList.empty();
                $.each(data.headlines.headline, function (i, headline) {
                    var p = $('<p></p>').text(headline.title);
                    if (i > 0) {
                        p.addClass('inactive');
                    }
                    var a = $('<a target="_blank">read more &raquo;</a>').attr('href', headline.link);
                    p.append(a);
                    headlineList.append(p);
                });

                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 () {
                    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);
                }, 0);
            }
        });
    }

    $('.tab').click(function () {
        $('#comboWidget a.active').removeClass('active');
        //alert($('#comboWidget a.active').html());
        //alert($(this).html());
        $(this).addClass('active');
        $('#comboWidget > .content').removeClass('active');
        $(this.rel).addClass('active');
    });

    var camWrapper = $('#comboWidget > #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;
}
