﻿jQuery(document).ready(function() {

    var firstType = jQuery('#excerpt li:first p.mainWindowItemType')[0].innerHTML;
    jQuery('#mainWindowTypeNavigation li a#' + firstType.replace(" ", "")).addClass("selected");
    
    //Speed of the slideshow  
    var speed = 9000;

    //You have to specify width and height in #slider CSS properties  
    //After that, the following script will set the width and height accordingly  
    jQuery('#mask-gallery, #gallery li').width(jQuery('#slider').width());
    jQuery('#gallery').width(jQuery('#slider').width() * jQuery('#gallery li').length);
    jQuery('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li').height(jQuery('#slider').height());

    //Assign a timer, so it will run periodically  
    var run = setInterval('newsslider(0)', speed);

    function resetInterval() {
        clearInterval(run);
        var run = setInterval('newsslider(0)', speed);
    }

    jQuery('#gallery li:first, #excerpt li:first, #galleryNavigation li:first').addClass('selected');

    //Pause the slidershow with clearInterval  
    jQuery('#btn-pause').click(function() {
        clearInterval(run);
        return false;
    });

    //Continue the slideshow with setInterval  
    jQuery('#btn-play').click(function() {
        newsslider(0);
        return false;
    });

    //Next Slide by calling the function  
    jQuery('#btn-next').click(function() {
        clearInterval(run);
        newsslider(0);
        return false;
    });

    //Previous slide by passing prev=1  
    jQuery('#btn-prev').click(function() {
        clearInterval(run);
        newsslider(1);
        return false;
    });


    jQuery(function() {
        jQuery("#galleryNavigation").find("li").each(function() {
            jQuery(this).bind("click", function() {
                clearInterval(run);
                newsslidergoto(this.childNodes[0].innerHTML - 1);
                return false;
            });
        });
    });

    //Mouse over, pause it, on mouse out, resume the slider show  
    jQuery('#slider').hover(

        function() {
            clearInterval(run);
        },
        function() {
            run = setInterval('newsslider(0)', speed);
        }
    );

});

function newsslidergoto(slide) {

    //Get the current selected item (with selected class), if none was found, get the first item  
    var current_image = jQuery('#gallery li.selected').length ? jQuery('#gallery li.selected') : jQuery('#gallery li:first');
    var current_excerpt = jQuery('#excerpt li.selected').length ? jQuery('#excerpt li.selected') : jQuery('#excerpt li:first');
    var current_navigation = jQuery('#galleryNavigation li.selected').length ? jQuery('#galleryNavigation li.selected') : jQuery('#galleryNavigation li:first');

    var next_image = jQuery('#gallery li:eq(' + slide + ')');
    var next_excerpt = jQuery('#excerpt li:eq(' + slide + ')');
    var next_navigation = jQuery('#galleryNavigation li:eq(' + slide + ')');

    //clear the selected class
    jQuery('#excerpt li, #gallery li, #galleryNavigation li, #mainWindowTypeNavigation li a').removeClass('selected');

    //reassign the selected class to current items  
    next_image.addClass('selected');
    next_excerpt.addClass('selected');
    next_navigation.addClass('selected');

    //Select Type


    //Scroll the items  
    //jQuery('#mask-gallery').scrollTo(next_image, 800);
    //jQuery('#mask-excerpt').scrollTo(next_excerpt, 800);
    next_image.fadeIn("slow");
    next_excerpt.fadeIn("slow");
    current_image.fadeOut("slow");
    current_excerpt.fadeOut("slow");

    var theType = jQuery('#excerpt li.selected p.mainWindowItemType')[0].innerHTML;
    jQuery('#mainWindowTypeNavigation li a#' + theType.replace(" ", "")).addClass("selected");
}
function newsslider(prev) {

    //Get the current selected item (with selected class), if none was found, get the first item  
    var current_image = jQuery('#gallery li.selected').length ? jQuery('#gallery li.selected') : jQuery('#gallery li:first');
    var current_excerpt = jQuery('#excerpt li.selected').length ? jQuery('#excerpt li.selected') : jQuery('#excerpt li:first');
    var current_navigation = jQuery('#galleryNavigation li.selected').length ? jQuery('#galleryNavigation li.selected') : jQuery('#galleryNavigation li:first');

    //if prev is set to 1 (previous item)  
    if (prev) {

        //Get previous sibling  
        var next_image = (current_image.prev().length) ? current_image.prev() : jQuery('#gallery li:last');
        var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : jQuery('#excerpt li:last');
        var next_navigation = (current_navigation.prev().length) ? current_navigation.prev() : jQuery('#galleryNavigation li:last');
        //if prev is set to 0 (next item)  
    } else {

        //Get next sibling  
        var next_image = (current_image.next().length) ? current_image.next() : jQuery('#gallery li:first');
        var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : jQuery('#excerpt li:first');
        var next_navigation = (current_navigation.next().length) ? current_navigation.next() : jQuery('#galleryNavigation li:first');
    }

    //clear the selected class
    jQuery('#excerpt li, #gallery li, #galleryNavigation li, #mainWindowTypeNavigation li a').removeClass('selected');

    //reassign the selected class to current items  
    next_image.addClass('selected');
    next_excerpt.addClass('selected');
    next_navigation.addClass('selected');


    //Scroll the items  
    //jQuery('#mask-gallery').scrollTo(next_image, 800);
    //jQuery('#mask-excerpt').scrollTo(next_excerpt, 800);
    next_image.fadeIn(1000);
    next_excerpt.fadeIn(1000);
    current_image.fadeOut(1000);
    current_excerpt.fadeOut(1000);

    var theType = jQuery('#excerpt li.selected p.mainWindowItemType')[0].innerHTML;
    jQuery('#mainWindowTypeNavigation li a#' + theType.replace(" ", "")).addClass("selected");


}
