﻿
function loadGallery_scroll() {

    ScrollGallery($('#galleryscroll'), $('#galleryscroll_cont'), 15);

    /* when resizing the window resize the picture */
    $(window).bind('resize', function () {
        //thumbsDim();
        ScrollGallery($('#galleryscroll'), $('#galleryscroll_cont'), 15);
    });

    function ScrollGallery($wrapper, $container, contPadding) {
        //var count = 0;
        var finalW = 0;
        var $elem = $("#galleryscroll_cont");
        $elem.find('img').each(function (i) {
            var $img = $(this);
            finalW += $img.width() + 5;
            //count++;
            //plus 5 -> 4 margins + 1 to avoid rounded calculations
        });
        $elem.css('width', finalW + 'px').css('visibility', 'visible');
        //alert(count);

        var divWidth = $wrapper.width();

        //Remove scrollbars
        $wrapper.css({
            overflow: 'hidden'
        });

        //Find last image container
        var lastLi = $container.find('img:last-child');
        $wrapper.scrollLeft(0);
        //When user move mouse over menu
        $wrapper.unbind('mousemove').bind('mousemove', function (e) {

            //As images are loaded ul width increases,
            //so we recalculate it each time
            var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + contPadding;

            var left = (e.pageX - $wrapper.offset().left) * (ulWidth - divWidth) / divWidth;
            $wrapper.scrollLeft(left);
        });
    }
}
