/***ä
* Stanilux Gallery
* 
***/

jQuery.noConflict();

jQuery(document).ready(function(){
    // Only add slider-functionality if > thumbs
    if ( jQuery("#thumbsBox .galleryThumb").length > 15 ) {
        // Height of visible box and total slider height
        var thumbs_box_height = jQuery("#thumbsBox").height();
        var thumbs_slider_height = jQuery("#thumbsSlider").height();
        
        // Position of #thumbsBox from document top
        var box_offset_y = Math.round( jQuery("#thumbsBox").offset().top );
        
        // Minimal and maximal negative margin of #thumbsSlider
        // (for moving it inside of #thumbsBox)
        var min_slider_offset = 0;
        var max_slider_offset = thumbs_box_height - thumbs_slider_height;
        
        // Height of one gallery thumb
        var thumb_height = jQuery("#thumbsBox .galleryThumb").height() + 1;  // 1px margin
        
        // Bind event mousemove to thumbs box
        jQuery("#thumbsBox").mousemove(function(event){
            // Relative vertical mouse-position inside #thumbsBox
            var vertical_pos = event.pageY - box_offset_y;
            
            // Relative vertical mouse-position inside tolerance area in box
            var tolerance = 35;
            var thumbs_box_height_tolerance = thumbs_box_height - 2 * tolerance;
            var vertical_pos_tolerance = vertical_pos - tolerance;
            // Correct values outside of tolerance area
            if ( vertical_pos_tolerance < 0 ) {
                vertical_pos_tolerance = 0;
            } else if ( vertical_pos_tolerance > thumbs_box_height_tolerance ) {
                vertical_pos_tolerance = thumbs_box_height_tolerance;
            }
            
            // Part of mouse-position from total box-height (between 0 and 1)
            var vertical_part = vertical_pos_tolerance / thumbs_box_height_tolerance;
            
            // Negative margin of #thumbsSlider
            var offset = Math.round( min_slider_offset + max_slider_offset * vertical_part );
            /**
            if ( offset % thumb_height != 0 ) {
                offset -= offset % thumb_height;
            }
            */
            
            jQuery("#thumbsSlider").css("margin-top", offset+"px");
        });
    }
    
    // Show arrows
    jQuery("#galleryBig .arrows").show();
    
    // Add load-handler to big image
    jQuery("#galleryBig a img").load(function(){
        jQuery(this).parents("#galleryBig").first().removeClass("waiting");
    });
});

// Load new big image
function showBig(object, big_img_path, alt, description, year, img_path) {
    var image_object = new Image();
    
    jQuery("#galleryBig").addClass("waiting");
    
    image_object.src = big_img_path;
    image_object.onLoad = setBig(big_img_path, alt, description, year, img_path);
    
    jQuery("#thumbsBox .galleryThumb.shownBig").removeClass("shownBig");
    jQuery(object).parent().addClass("shownBig");
    
    return false;
}

// Set big image to new image
function setBig(big_img_path, alt, description, year, img_path) {
    jQuery("#galleryBig a").attr("href", img_path);
    jQuery("#galleryBig a img").attr("src", big_img_path);
    jQuery("#galleryBig a img").attr("alt", alt);
    jQuery("#galleryBig a img").attr("title", alt);
    jQuery("#galleryBig .description").html(description+" ("+year+")");
}

// Show next image
function showNext() {
    if ( jQuery("#thumbsBox .galleryThumb.shownBig").next().find("a").length == 0 ) {  // no next image found
        jQuery("#thumbsBox .galleryThumb a").first().click();  // back to first image
    } else {
        jQuery("#thumbsBox .galleryThumb.shownBig").next().find("a").click();  // to next image
    }
    
    return false;
}
// Show previous image
function showPrev() {
    if ( jQuery("#thumbsBox .galleryThumb.shownBig").prev().find("a").length == 0 ) {  // no prev image found
        jQuery("#thumbsBox .galleryThumb a").last().click();  // to last image
    } else {
        jQuery("#thumbsBox .galleryThumb.shownBig").prev().find("a").click();  // to previous image
    }
    
    return false;
}

