﻿var fbAccountName = 'tv8vail';
var twAccountName = 'tv8vail';
var tag;
var firstScriptTag;
var done = false;

$(function () {
    $('.resortCamLink').fancybox({ type: 'image' });
    $('.camIcon').fancybox({ type: 'image' });

    $.tools.tooltip.addEffect("slidedownup",
    // opening animation 
	    function (done) {
	        this.getTip().slideDown().show();
	    },
    // closing animation 
	    function (done) {
	        this.getTip().slideUp(function () {
	            $(this).hide();
	            done.call();
	        });
	    }
	);

	// weather
	$('.forecastDayContainer').mouseover(function () {
	    $('.forecastDetailContainer').hide();
	    $(this).find('.forecastPlus').hide();
	    $(this).find('.forecastDetailContainer').show();
	});
	$('.forecastDayContainer').mouseout(function () {
	    $(this).find('.forecastDetailContainer').hide();
	    $(this).find('.forecastPlus').show();
	});

    $('div.navButton').each(function (index, element) {
        var pos = 'bottom right';

        if (index == ($('div.navButton').length - 1)) {
            $(this).addClass("last");
        }

        $(this).tooltip({
            relative: true,
            position: pos,
            effect: 'slidedownup',
            onBeforeShow: function () {
                // recalculate our offset since the widths aren't exactly correct until after the
                // tooltip might be created
                var trigger = this.getTrigger();
                $(trigger).addClass('active');

                var w = $(trigger).width();
                var pl = parseInt($(trigger).css('padding-left').replace('px', ''));
                var pr = parseInt($(trigger).css('padding-right').replace('px', ''));
                var offset = -1 * eval(w + pl + pr);

                var tip = this.getTip();
                var tipWidth = $(tip).width();
                var topNavWidth = $('#topNavContainer').width();

                if (index == ($('div.navButton').length - 1)) {
                    // this is the last one.  So set the offset to be right-aligned instead.
                    var tip = this.getTip();
                    var tipWidth = $(tip).width();

                    // if the tooltip itself is wider than the triggering element, move it over
                    // an additional amount of the difference between widths
                    if (tipWidth > Math.abs(offset)) {
                        offset -= eval(tipWidth - Math.abs(offset));
                    }
                }

                var conf = this.getConf();

                conf.offset = [0, offset];

            },
            onHide: function () {
                var trigger = this.getTrigger();
                $(trigger).removeClass('active');
            }
        });
    });

    $('#btnSearch').click(function () {
        var q = $('#q');
        if ($(q).val() != '' && $(q).val() != $(q)[0].defaultValue) {
            document.location = '/search-results.aspx?q=' + escape($(q).val());
        }
        else {
            alert('Please enter some search criteria');
        }
    });
    $('#q').focus(function () {
        if (this.defaultValue == $(this).val()) {
            $(this).val('');
        }
    });
    $('#q').blur(function () {
        if ($(this).val() == '') {
            $(this).val(this.defaultValue);
        }
    });
    $('#q').keypress(function (e) {
        if (checkForEnter(e)) {
            $('#btnSearch').click();
        }
    });

    $('a.searchButton').click(function () {
        var q = $('input.searchQuery');
        if ($(q).val() != '' && $(q).val() != 'Search') {
            document.location = '/search-results.aspx?q=' + escape($(q).val());
        }
        else {
            alert('Please enter some search criteria');
            return false;
        }
    });
    $('input.searchQuery').focus(function () {
        if (this.defaultValue == $(this).val()) {
            $(this).val('');
        }
    });
    $('input.searchQuery').blur(function () {
        if ($(this).val() == '') {
            $(this).val(this.defaultValue);
        }
    });
    $('input.searchQuery').keypress(function (e) {
        if (checkForEnter(e)) {
            $('a.searchButton').click();
        }
    });

    $('div.promoArrow').mouseover(function () {
        $(this).addClass('active');
    });
    $('div.promoArrow').mouseout(function () {
        $(this).removeClass('active');
    });

    // custom carousel code
    $('div.carouselContainer div.carouselArrow').click(function () {
        var $clickedArrow = $(this);
        //if ($(this).hasClass('active')) {
        var container = $clickedArrow.parents('div.carouselContainer');
        if ($(container).data('executing')) {
            return;
        }
        $(container).data('executing', true);
        //var visibleItem = $(container).find('div.carouselItem:visible');
        var visibleItem = $(container).find('div.carouselItem.active');
        var visibleItemHeight = $(visibleItem).height();
        var heightContainer = $(visibleItem).parents('div.carouselItems');
        var prevArrow = $(container).find('div.carouselArrow.prev');
        var nextArrow = $(container).find('div.carouselArrow.next');
        var itemCount = $(container).find('div.carouselItem').not('div.carouselItem.template').length;
        var itemIndex = $(container).find('span.carouselIndex');

        // which is the next visible item to show?
        var index = $(visibleItem).index();
        var nextIndex = index;
        if ($clickedArrow.hasClass('next')) {
            nextIndex++;
        }
        else {
            nextIndex--;
        }
        if (nextIndex > (itemCount - 1)) {
            nextIndex = 0;
        }
        else if (nextIndex < 0) {
            nextIndex = itemCount - 1;
        }

        var nextItem = $(container).find('div.carouselItem')[nextIndex];

        var nextHeight = $(nextItem).actual('height');

        $(heightContainer).css('height', visibleItemHeight + 'px');
        $(visibleItem).fadeOut('fast', function () {
            $(this).removeClass('active');
            $(heightContainer).animate({
                height: nextHeight
            }, 250, function () {

                $(nextItem).fadeIn('fast', function () {
                    $(this).addClass('active');

                    $(itemIndex).html(nextIndex + 1);

                    var thisHeight = $(this).height();
                    if (thisHeight != $(heightContainer).height()) {
                        $(heightContainer).animate({
                            height: thisHeight
                        }, 100);
                    }
                    $(container).removeData('executing');
                });
            });
        });
        //}
    });


    // add like box to the facebook post template
    var fbIframe = $('<iframe></iframe>');
    $(fbIframe).attr('scrolling', 'no');
    $(fbIframe).attr('frameborder', '0');
    $(fbIframe).attr('style', 'border:none; overflow:hidden; width:48px; height:21px;');
    $(fbIframe).attr('allowTransparency', 'true');
    $(fbIframe).attr('src', '//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2F' + fbAccountName + '&layout=button_count&show_faces=false&width=48&action=like&font&colorscheme=light&height=21');
    $('.likeBoxFbPage').children().remove();
    $('.likeBoxFbPage').append($(fbIframe));

    //promoLinkFbLike
    $.each(
        $('div.promoLinkFbLike'),
        function (idx, item) {
            var urlToLike = $(item).siblings('div.promoLinkFbHidden').text();
            var fbPromoIframe = $('<iframe></iframe>');
            $(fbPromoIframe).attr('scrolling', 'no');
            $(fbPromoIframe).attr('frameborder', '0');
            $(fbPromoIframe).attr('style', 'border:none; overflow:hidden; width:48px; height:21px;');
            $(fbPromoIframe).attr('allowTransparency', 'true');
            $(fbPromoIframe).attr('src', '//www.facebook.com/plugins/like.php?href=' + escape(urlToLike) + '&layout=button_count&show_faces=false&width=48&action=like&font&colorscheme=light&height=21');
            $(item).children().remove();
            $(item).append($(fbPromoIframe));
        }
    );

    // add follow button to twitter post template
    // http://platform.twitter.com/widgets/follow_button.html?screen_name=tv8vail&show_screen_name=false&show_count=false
    var twIframe = $('<iframe></iframe>');
    $(twIframe).attr('scrolling', 'no');
    $(twIframe).attr('frameborder', '0');
    $(twIframe).attr('style', 'border:none; overflow:hidden; width:61px; height:20px;');
    $(twIframe).attr('allowTransparency', 'true');
    $(twIframe).attr('src', '//platform.twitter.com/widgets/follow_button.html?screen_name=' + twAccountName + '&show_screen_name=false&show_count=false');
    $('.twFollowButton').children().remove();
    $('.twFollowButton').append($(twIframe));

    // asynch load the sharing scripts
    // <script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
    var twitterScript = $('<script></script>').attr('type', 'text/javascript').attr('src', '//platform.twitter.com/widgets.js');
    $('div.sharingTweetContainer').append(twitterScript);
    // facebook like button in bottom of left content area
    var fbLikeIframe = $('<iframe></iframe>').attr('src', '//www.facebook.com/plugins/like.php?href=' + thisUrl + '&send=false&layout=button_count&width=100&show_faces=false&action=like&colorscheme=light&font&height=21')
        .attr('scrolling', 'no')
        .attr('frameborder', '0')
        .attr('style', 'border:none; overflow:hidden; width:100px;')
        .attr('height', '21px')
        .attr('width', '100px')
        .attr('allowtransparency', true);
    $('div.sharingLikeBoxContainer').append(fbLikeIframe);

    // google plus one
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);

    $('#footerTwitterIcon').click(function () {
        window.open($(this).parent().find('a').attr('href'), 'twitter');
    });
    $('#footerFacebookIcon').click(function () {
        window.open($(this).parent().find('a').attr('href'), 'twitter');
    });

    // livestream
    $('a.livestreamLink').fancybox({
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'href': this.href,
        'type': 'iframe',
        'width': 560,
        'height': 340,
        'autoScale': false
    });

    $('#tabWeather').click(function () {
        if (!$(this).hasClass('active')) {
            $('#tabSnow').removeClass('active');
            $(this).addClass('active');
            $('#snowPane').fadeOut('fast', function () {
                $('#weatherPane').fadeIn('fast');
            });
        }
    });
    $('#tabSnow').click(function () {
        if (!$(this).hasClass('active')) {
            $('#tabWeather').removeClass('active');
            $(this).addClass('active');
            $('#weatherPane').fadeOut('fast', function () {
                $('#snowPane').fadeIn('fast');
            });
        }
    });
});

function checkForEnter(e) {
    if (window.event) {
        key = window.event.keyCode;
    }
    else {
        key = e.which;
    }
    if (key == 13) {
        e.preventDefault();
        return true;
    }
    else {
        return false;
    }
}

$(document).ready(function() {
    if ($('#calendar').length > 0) {
        $('#calendar').fullCalendar({
            header: {
                left: '',
                center: '',
                right: ''
            },
            defaultView: 'agendaWeek',
            editable: false,
            droppable: false, 
            allDaySlot: false,
            slotMinutes: 15,
            columnFormat: 'ddd',
            year: g_tvScheduleYear,
            month: g_tvScheduleMonth,
            date: g_tvScheduleDate,
            firstHour: g_tvScheduleHourNow,
            events: g_tvScheduleEventJson
        });
    }
});
