/*
Supersized - Fullscreen Slideshow jQuery Plugin
By Sam Dunn (www.buildinternet.com // www.onemightyroar.com)
Version: supersized.2.0.js // Relase Date: 5/7/09
Website: www.buildinternet.com/project/supersized
Thanks to Aen for preloading, fade effect, & vertical centering
*/

(function($) {

    //Resize image on ready or resize
    $.fn.supersized = function() {
        $.inAnimation = false;
        var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);

        $(window).bind("load", function() {
            $('#loading').hide();
            $('#supersize').fadeIn('fast');
            $('#content').show();
            if ($('#slideshow .activeslide').length == 0) $('#supersize a:first').addClass('activeslide');
            

            if (options.navigation == 0) $('#navigation').hide();
            //Slideshow
            if (options.slideshow == 1) {

                slideshow_interval = setInterval("nextslide()", options.slide_interval);
                if (options.navigation == 1) { //Skip if no navigation
                    $('#navigation a').click(function() {
                        $(this).blur();
                        return false;
                    });

                    //Slide Navigation
                    $('#nextslide').click(function() {
                        if ($.inAnimation) return false;
                        clearInterval(slideshow_interval);
                        nextslide();
                        //                        slideshow_interval = setInterval(nextslide, options.slide_interval);
                        return false;
                    });

                    // User clicked on image
                    $('.superImageNext').click(function() {
                        if ($.inAnimation) return false;
                        clearInterval(slideshow_interval);
                        nextslide();
                        //                        slideshow_interval = setInterval(nextslide, options.slide_interval);
                        return false;
                    });



                    $('#prevslide').click(function() {
                        if ($.inAnimation) return false;
                        clearInterval(slideshow_interval);
                        prevslide();
                        //                        slideshow_interval = setInterval(nextslide, options.slide_interval);
                        return false;
                    });

                  


                    $('.showThisImage').click(function() {

                        clearInterval(slideshow_interval);
                        //                        slideshow_interval = setInterval(nextslide, options.slide_interval);




                        if (!$('#supersize .activeslide').hasClass($(this).attr("id"))) {



                            
							var currentslide = $('#supersize .activeslide');
                            currentslide.removeClass('activeslide');

                            var imageSelected = "." + $(this).attr("id");
                            $(imageSelected).hide().addClass('activeslide');


                            $('.showThisImage').removeClass('showThisImageActive');
                            $('.showDescImg').removeClass('showDescImgActive');

                            $(imageSelected).addClass('showDescImgActive');

                            $(this).addClass('showThisImageActive');







                            if (options.transition == 0) {
                                $(imageSelected).show(); $.inAnimation = false;
                            }
                            if (options.transition == 1) {
                                $(imageSelected).fadeIn(750, function() { $.inAnimation = false; });
                            }
                            if (options.transition == 2) {
                                $(imageSelected).show("slide", { direction: "up" }, 'slow', function() { $.inAnimation = false; });
                            }
                            if (options.transition == 3) {
                                $(imageSelected).show("slide", { direction: "right" }, 'slow', function() { $.inAnimation = false; });
                            }
                            if (options.transition == 4) {
                                $(imageSelected).show("slide", { direction: "down" }, 'slow', function() { $.inAnimation = false; });
                            }
                            if (options.transition == 5) {
                                $(imageSelected).show("slide", { direction: "left" }, 'slow', function() { $.inAnimation = false; });
                            }
                        }


                    });
                }
            }
        });


        //Pause when hover on image
        $('#supersize > *').hover(function() {
            original_title = $(this).find('img').attr("title");
            if ($.inAnimation) return false; else $(this).find('img').attr("title", "");
        }, function() {
            $(this).find('img').attr("title", original_title);
        });

        $('#supersize').hide();
        $('#content').hide();
    };



    $.fn.supersized.defaults = {
        startwidth: 4,
        startheight: 3,
        vertical_center: 1,
        slideshow: 1,
        navigation: 1,
        transition: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
        pause_hover: 0,
        slide_counter: 1,
        slide_captions: 1,
        slide_interval: 5000
    };

})(jQuery);


//Slideshow Next Slide

function nextslide() {

    if ($.inAnimation) return false;
    else $.inAnimation = true;



    var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);


    var currentslide = $('#supersize .activeslide');
    currentslide.removeClass('activeslide');

	

	
    if (currentslide.length == 0) currentslide = $('#supersize a:last');
  


    var nextslide = currentslide.next().length ? currentslide.next() : $('#supersize a:first');
    

    var prevslide = nextslide.prev().length ? nextslide.prev() : $('#supersize a:last');
    

	

    //
    // Display what link is active
    //

    var currentSlideNum = currentslide.attr("id").substring(currentslide.attr("id").length - 1, currentslide.attr("id").length);
    var nextSlideNum = nextslide.attr("id").substring(nextslide.attr("id").length - 1, nextslide.attr("id").length);

    $('.showThisImage').removeClass('showThisImageActive');
    $('.showDescImg').removeClass('showDescImgActive');

    var nextSlideLink = "#imgNum" + nextSlideNum;
    var nextDesc = "#imgDesc" + nextSlideNum;
    var textfield = $("#text" + nextSlideNum);
    

    $(nextSlideLink).addClass('showThisImageActive');
    $(nextDesc).addClass('showDescImgActive');

    $("#image-description").html(textfield.html());


    //
    // End Display what link is active
    //


    $('.prevslide').removeClass('prevslide');
    prevslide.addClass('prevslide');



    //Captions require img in <a>

    if (options.slide_captions == 1) $('#slidecaption').html($(nextslide).find('img').attr('title'));
    nextslide.hide().addClass('activeslide')
 

    if (options.transition == 0) {
        nextslide.show(); $.inAnimation = false;
     
    }
    if (options.transition == 1) {
        nextslide.fadeIn(750, function() { $.inAnimation = false; });
       
    }
    if (options.transition == 2) {
        nextslide.show("slide", { direction: "up" }, 'slow', function() { $.inAnimation = false; });
       
    }
    if (options.transition == 3) {
        nextslide.show("slide", { direction: "right" }, 'slow', function() { $.inAnimation = false; });
        
    }
    if (options.transition == 4) {
        nextslide.show("slide", { direction: "down" }, 'slow', function() { $.inAnimation = false; });
       
    }
    if (options.transition == 5) {
        nextslide.show("slide", { direction: "left" }, 'slow', function() { $.inAnimation = false; });
        
    }


}




//Slideshow Previous Slide

function prevslide() {

    if ($.inAnimation) return false;

    else $.inAnimation = true;


    var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);

    var currentslide = $('#supersize .activeslide');

    currentslide.removeClass('activeslide');
    



    if (currentslide.length == 0) currentslide = $('#supersize a:first');
   


    var nextslide = currentslide.prev().length ? currentslide.prev() : $('#supersize a:last');
   

    var prevslide = nextslide.next().length ? nextslide.next() : $('#supersize a:first');
    




    var currentSlideNum = currentslide.attr("id").substring(currentslide.attr("id").length - 1, currentslide.attr("id").length);
    var nextSlideNum = nextslide.attr("id").substring(nextslide.attr("id").length - 1, nextslide.attr("id").length);

    $('.showThisImage').removeClass('showThisImageActive');
    $('.showDescImg').removeClass('showDescImgActive');

    var nextSlideLink = "#imgNum" + nextSlideNum;
    var nextDesc = "#imgDesc" + nextSlideNum;

    $(nextSlideLink).addClass('showThisImageActive');
    $(nextDesc).addClass('showDescImgActive');









    $('.prevslide').removeClass('prevslide');

    prevslide.addClass('prevslide');


    //Captions require img in <a>
    if (options.slide_captions == 1) $('#slidecaption').html($(nextslide).find('img').attr('title'));

    nextslide.hide().addClass('activeslide')

    
    if (options.transition == 0) {
        nextslide.show(); $.inAnimation = false;
        
    }
    if (options.transition == 1) {
        nextslide.fadeIn(750, function() { $.inAnimation = false; });
    
    }
    if (options.transition == 2) {
        nextslide.show("slide", { direction: "down" }, 'slow', function() { $.inAnimation = false; });
        
    }
    if (options.transition == 3) {
        nextslide.show("slide", { direction: "left" }, 'slow', function() { $.inAnimation = false; });
       
    }
    if (options.transition == 4) {
        nextslide.show("slide", { direction: "up" }, 'slow', function() { $.inAnimation = false; });
        
    }
    if (options.transition == 5) {
        nextslide.show("slide", { direction: "right" }, 'slow', function() { $.inAnimation = false; });
        
    }
}
