﻿/*
* Front page specific JS
* Controls the Fade in/out scroller
*/
$(document).ready(function () {
    var url = document.location.href;

    if ($('#front-images').length) {//&& (document.location.href.indexOf('localhost') < 0)) {
        var currentDiv = 1; // Always start fromthe beginning
        var numItems = $('#front-images div').size(); //num items = number of images
        var go = true; // Controls if paused or playing


        function generatePager(num) {
            for (count = 1; count <= num; count++) {
                $('#front-pager ul').append('<li><a href="#pic' + count + '" class="' + count + '">&nbsp;</a></li>');
            }
            $('#front-pager ul li:last').css('marginRight', '3px');
            var pagerWidth = 45 + 20 * numItems;
            var pagerLeft = 830 - ((numItems - 3) * 20);
            $('#front-pager').css('width', pagerWidth);
            $('#front-pager').css('left', pagerLeft);
        }
        /* Shows the current div */
        function showDiv() {
            $("#front-images div").hide();
            $("#front-images #pic" + currentDiv).fadeIn("fast");
        }

        /*
        * Controls which div gets shown & places appropriate classes
        * on the pager <a>
        *
        * If the current div is the last one we set it = 1
        * Else, advance the current div by 1
        */
        function next(event) {
            if (currentDiv == numItems) {
                $('.current').removeClass('current');
                $("#front-pager li:first").addClass('current');
                currentDiv = 1; //set the new currentDiv to the beginning
                showDiv(currentDiv);
            }
            else {
                var x = $('.current'); // set temporary variable to hold old currentDiv
                x.next().addClass('current');
                //$('.current').next().addClass('current');
                x.removeClass('current');
                currentDiv++;
                showDiv(currentDiv);
            }
        }

        /* 
        * Advances if go is true 
        */
        function advance() {
            if (go) {
                next();
            }
        };

        /* Call advance() every 5 seconds */
        var t = setInterval(function () {
            advance()
        }, 5000);

        $('#pause a').click(function () {
            if (go) {
                go = false;
                $(this).addClass('pause');

            }
            else {
                go = true;
                $(this).removeClass('pause');
            }
        });
        generatePager(numItems);
        $('#front-images div:not(:first)').hide();

        $('#front-pager li').click(function (e) {
            $('#front-images div').hide();
            $('#front-pager .current').removeClass("current");
            $(this).addClass('current');

            var clicked = $(this).find('a:first').attr('href');
            // IE7 is giving the full url so  I'm just grabbing the ID at the end
            var clicked = clicked.substring(clicked.indexOf('#'), clicked.length);
            currentDiv = $(this).find('a:first').attr('class');
            $('#front-images ' + clicked).fadeIn('fast');

            e.preventDefault();
            if (!$('#pause a').hasClass('pause')) {
                $('#pause a').addClass('pause');
            }
            go = false;

        }).eq(0).addClass('current');

    }
    else if ($('#front-images')) {
        $('#front-images div:not(:first)').hide();
    }
});
