﻿/****************************************************************************

function gallery expects a div with 'gItems' class.
in it thumbnails wrapped with link
the <a> href points to the big version image of <img>

in other words....

<a href="../images/bigVersion.jpg">                                      
    <img src="../images/thmbnail.jpg"/>
</a>
 .
 .
 .
<a href="../images/bigVersion.jpg">                                      
    <img src="../images/thmbnail.jpg"/>
</a>

*****************************************************************************/

function gallery(galleryWidth, withPresentation) {
    this.picturesInGallery = $(".gItems a").length;
    if (this.picturesInGallery < 1) { return false; }
    

    var spacing = "7px";                // the space between thumbnails
    var sliderHeight = "96px";          // sliders height
    var defaultWidth = "96px"; 
    var sliderSideMargin = "30px"       // distance from prev and next controls  
    var thumbsTotalWidths = 0;          // not for tweaking
    var slidingStepValue = 2;           // slider speed 1-slow 10-very fast
    var galleryTotalWidth = galleryWidth + (parseInt(sliderSideMargin));
    var timer1 = 0;
    var timer2 = 0;
    galleryWidth -= parseInt(sliderSideMargin);   // adjustment for prev and next
    
    

    recreate_gallery();
    
    
    
    
    $(".gallery").css("width", galleryTotalWidth);
    
    // get the total width of all pictures
 
    
    
    $(".gItems img").each(function() {

        if (parseInt($(this).css("width"))) {
            thumbsTotalWidths = thumbsTotalWidths + parseInt($(this).css("width"));
            thumbsTotalWidths = thumbsTotalWidths + parseInt(spacing);
        }
        else {
            // hack for ie - ie doesn't calculate heights. it is always auto.
            
            thumbsTotalWidths = thumbsTotalWidths + parseInt(this.width);
            thumbsTotalWidths = thumbsTotalWidths + parseInt(spacing);
        }

    });

    
    $(".gItems").css("width", thumbsTotalWidths + "px");
    $(".gItems a").css("margin-right", spacing);
    
    $(".prev").css("height", sliderHeight);
    $(".next").css("height", sliderHeight);
    
    $(".itemsWrapper").css("margin-right", sliderSideMargin);
    $(".itemsWrapper").css("margin-left", sliderSideMargin);
    $(".itemsWrapper").css("width", galleryWidth);
    $(".itemsWrapper").css("height", sliderHeight);


    var container = parseInt($(".itemsWrapper").css("height"));
    var margin = 0;
    
    // centerize thumbnails
    $(".gItems img").each(function() {
        if (parseInt(this.height)) {
            margin = (container - parseInt(this.height)) / 2;
        }
        else {
            // hack for ie - ie doesn't calculate heights. it is always auto.
            margin = 0;
        }
        $(this).css("margin-top", margin + "px");
    });

    // disable default behaviour
    $(".prev").click(function() { return false; });
    $(".next").click(function() { return false; });
    $(".gItems a").click(function() { return false; });
    
    
    $(".prev").mouseover(function() {
        $(this).addClass("prevOver");
        timer1 = setInterval(animatePrev, 20);
    });

    $(".prev").mouseout(function() {
        clearTimeout(timer1);
        $(this).removeClass("prevOver");
    });

    $(".next").mouseover(function() {
        $(this).addClass("nextOver");
        timer2 = setInterval(animateNext, 20);     
    });

    $(".next").mouseout(function() {
        $(this).removeClass("nextOver");
        clearTimeout(timer2);
    });

    
    
    function animatePrev() {
        if (parseInt($(".gItems").css("left")) <= 0) {
            var newleft = (parseInt($(".gItems").css("left")) + slidingStepValue) + "px";
            $(".gItems").css("left", newleft);

        } else {

            if (timer1) {
                clearTimeout(timer1);
            }
        }
    }

    function animateNext() {

        // hack for ie - ie doesn't calculate right. it is always auto.
        if (parseInt($(".gItems").css("left"))>= (galleryWidth - thumbsTotalWidths)) {
            var newleft = (parseInt($(".gItems").css("left")) - slidingStepValue) + "px";
            $(".gItems").css("left", newleft);
        } else {

            if (timer2) {
                clearTimeout(timer2);
            }
        }    
       
       /*if (parseInt($(".gItems").css("right")) <= 0 || !parseInt($(".gItems").css("right"))) {
            var newleft = (parseInt($(".gItems").css("left")) - slidingStepValue) + "px";
            $(".gItems").css("left", newleft);
       } else {

            if (timer2) {
                clearTimeout(timer2);
            }
        }*/

    }

    if (withPresentation == 1 || withPresentation == null) {

        $(".slider").before('<div class="presentation"><img src="" alt="" /></div>');

        $(".presentation img:first").hide();
        $(".presentation").css("width", galleryTotalWidth);
        $(".gItems a").mouseover(function() {
            var href = $(this).attr("href");
            $(".presentation img:first").attr("src", href);
            $(".presentation img:first").fadeIn();
        });

        $(".gItems a").mouseout(function() {
            $(".presentation img:first").hide();
        });
    }

    function recreate_gallery() {
        $(".gItems").wrap('<div class="slider"></div>');
        $(".gItems").before('<a href="#" class="prev"></a>');
        $(".gItems").after('<a href="#" class="next"></a>');
        $(".gItems").wrap('<div class="itemsWrapper"></div>');
        $(".slider").wrap('<div class="gallery"></div>');


    }
    $(".gItems").hide();
    $(".gItems").css("visibility", "visible");
    $(".gallery").css("visibility", "visible");
    $(".gItems").fadeIn(2000);
}
function preloadImagesFromLinks(selector) {
    var linksArr = $(selector);
    for (var i = 0; i < linksArr.length; i++) {
        var currImage = new Image();
        currImage.src = linksArr[i].href;

    }
    

}

      