﻿/*** 
Simple jQuery Slideshow Script
Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, 
not responsible for anything, etc.  Please link out to me if you like it :)
***/

var interval = null;

function slideSwitch() {
    if ($("#slideshow .slide").length > 1) {
        var $active = $('#slideshow DIV.active');

        if ($active.length == 0) $active = $('#slideshow DIV:last');

        var $next = null;
        if ($('#slideshow DIV.next').length) {
            $next = $('#slideshow DIV.next');
        } else if (interval != null) {
            // use this to pull the divs in the order they appear in the markup
            $next = $active.next().length ? $active.next()
            : $('#slideshow DIV:first');
        }


        // uncomment below to pull the divs randomly
        // var $sibs  = $active.siblings();
        // var rndNum = Math.floor(Math.random() * $sibs.length );
        // var $next  = $( $sibs[ rndNum ] );


        if ($next != null) {
            if ($.support.opacity) {
                $active.animate({ opacity: 0.0 }, 500, function() {
                    $(this).addClass('last-active');

                    $next.css({ opacity: 0.0 })
                    .removeClass('next')
                    .addClass('active')
                    .animate({ opacity: 1.0 }, 500, function() {
                    $active.removeClass('active last-active');
                    });
                });
            } else {
                $active.addClass('last-active')
                    .removeClass('active');

                $next.removeClass('next last-active')
                    .addClass('active');
            }

            // update active page
            $('#paginadorSlide li').removeClass('activo');
            var index = $("#slideshow .slide").index($next);
            $('#paginaSlide-' + index).addClass('activo');
        }
    }
}

// paging
function paging() {
    numPaginas = $("#slideshow .slide").length;
    if (numPaginas > 1) {
        $("#slideshow").before('<ul class="paginadorSlide" id="paginadorSlide"></ul>');

        i = 0; j = 1;
        while (i < numPaginas) {
            $('#paginadorSlide').append('<li id="paginaSlide-' + (j - 1) + '"><a href="#">' + j + '</a></li>');
            i++; j++;
        }
        $('#paginadorSlide li').eq(0).addClass('activo');

        $('#paginadorSlide li a').click(function() {
            if (!$(this).parent().hasClass('activo')) {
                if (interval != null) {
                    clearInterval(interval);
                    interval = null;
                }

                $('#slideshow>DIV').eq($(this).parent().attr('id').split('-')[1]).addClass('next');
                slideSwitch();
                $('#paginadorSlide li').removeClass('activo');
                $(this).parent().addClass('activo');
            }
            return false;
        });
    }
}

$(function() {
    $('#slideshow>DIV:first').addClass("active");
    paging();
    interval = setInterval("slideSwitch()", 5000);
});

