﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
$(document).ready(function() {

    if ($('div#slideshow').length > 0) {
        setInterval('slideSwitch()', 5000);
    }

    if ($('li#number span').html() == '') {
        $('li#number span').html('1/' + $('div#slideshow div.slide').length);
    }

    $(document).keyup(function(e) {

        if (e.keyCode == 80) {
            if ($('li#playpause a').hasClass('active')) {
                playSlideshow();
            }
            else {
                pauseSlideshow();
            }
            return false;
        }

        if (e.keyCode == 37) {
            if (!$('li#prev a').hasClass('disabled')) {
                skipToPrevSlide();
            }
        }

        if (e.keyCode == 39) {
            if (!$(this).hasClass('disabled')) {
                skipToNextSlide();
            }
        }
    });

    $('li#playpause a').click(function() {
        if ($(this).hasClass('active')) {
            playSlideshow();
        }
        else {
            pauseSlideshow();
        }

        return false;
    });

    $('li#prev a').click(function() {
        if (!$(this).hasClass('disabled')) {
            skipToPrevSlide();
        }
        return false;
    });

    $('li#next a').click(function() {
        if (!$(this).hasClass('disabled')) {
            skipToNextSlide();
        }
        return false;
    });

    $('div#slideshow .slide').click(function() {
        if (!$(this).hasClass('disabled')) {
            skipToNextSlide();
        }
    });


});

function slideSwitch(skip, prev) {
    
    var skip = (skip == null) ? false : true;
    var prev = (prev == null) ? false : true;

    if ($('li#playpause a').length == 0 || !$('li#playpause a').hasClass('active') || skip) {

        disableSkipButtons();

        var $active = $('div#slideshow div.active');

        if (prev) {
            if ($active.length == 0) $active = $('div#slideshow div.slide:first');

            var $next = $active.prev().not('.latest').not('.newsletter').not('#controls').length ? $active.prev() : $('div#slideshow div.slide:last');
        } else {

            if ($active.length == 0) $active = $('div#slideshow div.slide:last');

            var $next = $active.next().not('.latest').not('.newsletter').not('#controls').length ? $active.next() : $('div#slideshow div.slide:first');
        }
        
        performTransition($active, $next);
    }
    
}

function performTransition($active, $next) {

    $active.addClass('last-active');

    $next.css({ opacity: 0.0 })
    .addClass('active')
    .animate({ opacity: 1.0 }, 1000, function() {
        $active.removeClass('active last-active');
        activateSkipButtons();
    });

    if ($('li#number span').length) {
        updateNumber($next);
    }
    
}

function updateNumber(el) {

    i = $('div#slideshow div.slide').index(el) + 1;
    count = $('div#slideshow div.slide').length;

    $('li#number span').html(i + '/' + count);
}

function pauseSlideshow() {
    $('li#playpause a').addClass('active').css('background-position', '-30px 0').html('play').attr('title', 'Play');
}

function playSlideshow() {
    $('li#playpause a').removeClass('active').css('background-position', '-19px 0').html('pause').attr('title', 'Pause');
}

function disableSkipButtons() {
    $('li#prev a').addClass('disabled');
    $('li#next a').addClass('disabled');
}

function activateSkipButtons() {
    $('li#prev a').removeClass('disabled');
    $('li#next a').removeClass('disabled');
}

function skipToPrevSlide() {
    pauseSlideshow();
    slideSwitch('skip', 'prev');
}

function skipToNextSlide() {
    pauseSlideshow();
    slideSwitch('skip');
}
