﻿window['slideshowTimer'] = null;
window['slideshowInterval'] = null;

var slideshow = function () {
    var cur = $(".photo-wrapper .slide:visible");
    var nxt = $(".photo-wrapper .slide:visible").next(".slide");

    nxt.css({ "display": "block", "top": (cur.height() * -1) + "px" });

    //nxt.show('fast', function () {
    cur.fadeOut(900, function () {
        $(this).css({ "z-index": "0" });
        nxt.css({ "z-index": "1", "top": "0px" });
        if (nxt.get(0) == $(".photo-wrapper .slide:last").get(0)) {
            $(".photo-wrapper .slide:first").css({ "z-index": "1", "top": "0px", "display":"block" });
            nxt.css({ "z-index": "0", "top": "0px", "display":"none" });
        }

        // window['slideshowTimer'] = window.setTimeout(slideshow, 6000);
    });
}

window['bouceWelcomeTimer'] = null;
window['bounceWelcomeInterval'] = null;

var bounceWelcome = function () {
    $(".info-bar ul.bar li.headline ul").addClass('active').animate({
        'margin-top': '-20px',
        'height': '55px'
    }, 350, 'swing', function () {
        $(".info-bar ul.bar li.headline ul").animate({
            'margin-top': '0px',
            'height': '35px'
        }, 350, 'easeOutBounce', function () {
            $(this).removeClass('active'); 
        });
    });
}

$(function () {
    $(window).bind('resize load', function (event) {
        /*
        if ($(window).height() < $(document).height()) $('.homepage').css({ 'overflow-y': 'hidden' }); // Fix for IE7, html has to be assigned overflow-y style... 
        else $(".homepage").css({ 'overflow-y': 'auto' });
        if ($(window).width() < 960) {
        $('.homepage').css({ 'overflow-x': 'auto' });
        //$('.homepage .info-bar').css({ 'margin-bottom': '18px' });
        }
        else {
        $('.homepage').css({ 'overflow-x': 'hidden' })
        //$('.homepage .info-bar').css({ 'margin-bottom': '0px' });

        }
        */
    });

    // Welcome SEO UI
    var showWelcome = function (event) {
        window.clearInterval(window['bounceWelcomeInterval'])
        window['bounceWelcomeInterval'] = null;

        $(this).addClass('hover'); // Fix for IE7, can't use :hover selector... 
        // over
        // Calculate the height of the menu...
        if (!$(this).data('menuHeight')) {
            var menuHeight = 0;
            $(".info-bar ul.bar li.headline ul li").each(function (i) {
                if (!$(this).hasClass('head')) menuHeight += $(this).outerHeight();
            });
            $(this).data('menuHeight', menuHeight);
        }
        // Class swap
        $(".info-bar ul.bar li.headline ul").addClass("active");

        $(".info-bar ul.bar li.headline ul").animate({
            'margin-top': $(this).data('menuHeight') * -1,
            'height': $(this).data('menuHeight') + 35
        }, 350, 'swing', function () {
        });
    }

    var hideWelcome = function (event) {
        // out
        $(this).removeClass('hover');
        window.setTimeout(function () {
            if ($(".info-bar ul.bar li.headline ul.hover").length == 0) {
                $(".info-bar ul.bar li.headline ul").animate({
                    'margin-top': 0,
                    'height': 35
                }, 350, 'swing', function () {
                    // Class swap
                    $(".info-bar ul.bar li.headline ul").removeClass("active");
                });
            }

            window['bounceWelcomeInterval'] = window.setInterval(bounceWelcome, 10000);

        }, 600);
    }

    $(".info-bar ul.bar li.headline ul").hoverIntent(showWelcome, hideWelcome);

    // Show the Welcome stuff for 3.5 secs, then hide away!
    // Changed, only reveal 20px/bounce ever 20 secs.
    /*
    $(".info-bar ul.bar li.headline ul").addClass('hover');
    showWelcome();
    window.setTimeout(function () {
    if ($(".info-bar ul.bar li.headline ul").hasClass('hover')) {
    $(".info-bar ul.bar li.headline ul").removeClass('hover');
    hideWelcome();
    }
    }, 3500);
    */


    // News Menu UI
    $(".news-menu .news-items li.head a").click(function (event) { event.preventDefault(); });

    // Activate Image overlay.. 
    $(".news-menu .news-items li a").not(".news-menu .news-items li.head a, .news-menu .news-items li.foot a").hoverIntent(function (event) {
        if ($(".news-photo-wrapper .slide:eq(" + ($(this).parent("li").index() - 1) + ")")) {
            // Pause Welcome bounce...
            window.clearInterval(window['bounceWelcomeInterval'])
            window['bounceWelcomeInterval'] = null;

            // Pause the slideshow... 
            window.clearInterval(window['slideshowInterval']);
            window['slideshowInterval'] = null;

            var parentIndex = $(this).parent("li").index();
            var newsItem = $(this);

            // Set the sub headline
            if ($.trim(newsItem.next('.subheadline').text()) != '') $(".info-bar .headline .head").html(newsItem.next('.subheadline').html());
            else $(".info-bar .headline .head").html($(".info-bar .headline .head").data('default'));

            // Fade in the associated photo and pause the slideshow... 
            $(".news-photo-wrapper .slide:eq(" + (parentIndex - 1) + ")").fadeIn('slow');
        }
    }, function (event) {
        if ($(".news-photo-wrapper .slide:eq(" + ($(this).parent("li").index() - 1) + ")")) {
            var parentIndex = $(this).parent("li").index();

            if ($(".news-menu .news-items li a.hover").not(".news-menu .news-items li.head a, .news-menu .news-items li.foot a").length == 0) {
                // Start the slideshow... 
                if (window['slideshowInterval'] == null) window['slideshowInterval'] = window.setInterval(slideshow, 6000);
                // Resume Welcome bounce...
                window['bounceWelcomeInterval'] = window.setInterval(bounceWelcome, 10000);
            }
            // Fade out the associated photo
            $(".news-photo-wrapper .slide:eq(" + (parentIndex - 1) + ")").delay(500).fadeOut('slow');
        }
    });

    $(".news-menu ul").hoverIntent(function (event) {
        // over
        $(this).addClass('hover'); // Fix for IE7, can't use :hover selector... 
        // Calculate the height of the menu... 
        if (!$(this).data('menuHeight')) {
            var menuHeight = 0;
            $(".news-menu .news-items li").each(function (i) {
                if (!$(this).hasClass('head')) menuHeight += $(this).outerHeight();
            });
            $(this).data('menuHeight', menuHeight);
        }

        $(".news-menu .news-items li.head a").css({ "cursor": "default" });
        $(".news-menu .news-items").animate({
            'margin-top': ($(this).data('menuHeight') * -1),
            'height': $(this).data('menuHeight') + 35
        }, 350, 'swing', function () {
        });
    }, function (event) {
        $(this).removeClass('hover');
        // out
        window.setTimeout(function () {
            if ($(".news-menu ul.hover").length == 0) {
                $(".news-menu .news-items").animate({
                    'margin-top': 0,
                    'height': 35
                }, 350, 'swing', function () {
                    $(".news-menu .news-items li.head a").css({ "cursor": "pointer" });
                    // Reset the headline (Welcome text)...
                    $(".info-bar .headline .head").html($(".info-bar .headline .head").data('default'));
                });
            }
        }, 500);
    });

    // Slideshow
    $(".photo-wrapper").append($(".photo-wrapper .slide:first").clone());

    // Randomly select 1st photo.. 
    if ($(".photo-wrapper .slide:visible").length == 0) $(".photo-wrapper .slide:eq(" + Math.floor(Math.random() * ($(".photo-wrapper .slide").length - 1)) + ")").show().css({ "z-index": "1" });

    window['slideshowInterval'] = window.setInterval(slideshow, 6000);


    window['bounceWelcomeInterval'] = window.setInterval(bounceWelcome, 10000);
    // Record the heading default markup...
    $(".info-bar .headline .head").data('default', $(".info-bar .headline .head").html());
});

