var sliderImages=new Array();

$(document).ready(function(){
    var startStep = 50;
    var step = startStep;
    var leftDirection=true;
    var discret = 700;
    var tmpImage;
    var width=0;
    var firstList;
    var listImages = $('#randomWallpTapeList ul');
    

    $('#randomWallpTapeList ul li').each(function() {
        sliderImages.push({href:$('#randomWallpTapeList ul li span.href').html(), imgsrc:$('#randomWallpTapeList ul li span.icon').html()});
        $(this).remove();
    });

    listImages.css('display', 'block')
    
    loadImages();
    function loadImages() {
        if (image = sliderImages.shift()) {
            tmpImage = new Image();
            $(tmpImage).load(function(){
                link=$('<a href="'+image.href+'"></a>');
                link.append(tmpImage);
                $('<li></li>').css('display','none').append(link).appendTo(listImages).fadeIn('fast');
                width += parseInt($(tmpImage).attr('width'))+7;
                listImages.css('width',width);
                loadImages();
            }).attr({
                src: image.imgsrc
            });
        }
       else {
            firstList = $('#randomWallpTapeList ul');
            moveStep();
            $('#randomWallpTapeList').mousemove(chengeSpeed);

            $('#randomWallpTapeList').hover(function () {
            }, function () {
               step = startStep;
            });

        }
    }

    function chengeSpeed(e) {
        step=0;
    }

    function moveStep(){
        var leftPosFirst = parseInt(firstList.css('left'));
        if (leftDirection) {
            if ((step>0)&&(leftPosFirst < -300)) {
                firstel = $('#randomWallpTapeList ul li:first');
                firstImg = $('#randomWallpTapeList ul li img:first');
                firstImg = firstImg[0];
                var imageWidth = parseInt($(firstImg).attr('width'))+4;
                firstel.remove().appendTo("#randomWallpTapeList ul");
                $('#randomWallpTapeList ul').css('left',leftPosFirst+imageWidth);
                leftPosFirst = parseInt(firstList.css('left'));
                if( leftPosFirst < -300) {
                    moveStep();
                    return;
                }

            }
            //alert(leftPosFirst);
            firstList.animate({
                left: leftPosFirst - step
                }, discret,'linear',moveStep);
        }
        else {
            if ((step>0)&&(-leftPosFirst < step*2)) {
                firstel = $('#randomWallpTapeList ul li:last');
                firstImg = $('#randomWallpTapeList ul li img:last');
                firstImg = firstImg[0];
                imageWidth = parseInt($(firstImg).attr('width'))+4;
                firstel.remove().prependTo("#randomWallpTapeList ul");
                $('#randomWallpTapeList ul').css('left',leftPosFirst-imageWidth);
                leftPosFirst = parseInt(firstList.css('left'));
                if( -leftPosFirst < width/2) {
                    moveStep();
                    return;
                }
            }

            firstList.animate({
                left: leftPosFirst + step
                }, discret,'linear',moveStep);
        }
    }

});

