var timeout = 2000;
var speed = 500;
var move = 154;
var actualPhoto = 0;
var count = 5;

var maxCount;

var progress = false;

var sliding = true;

var timeout_id;


function clear()
{
    progress = false;
}

function timeouter()
{
    timeout_id = setTimeout("timeouter()", timeout);
    moveLeft();
}

$(document).ready(function(){
    maxCount = $('#slider ul li').size();
    
    $('#slider ul li').each(function(i){
        if (i<count){
            $(this).css('left', (i*move)+'px');
            if (i<=2){
                $(this).css('padding-top', ((i*20)+20)+'px');
                $(this).css('z-index', (i+3)*100);
                //$('this > span').css('font-size', '21');
            }else{
                $(this).css('padding-top', (((4-i)*20)+20)+'px');
                $(this).css('z-index', (4-i)*100);
                $(this).children('span').css('font-size', (25-(i*2))+'px');
            }
        }else{
            $(this).hide();
        }
        i++;
    });

    $('#btn_left').click(function(){
        moveLeft(this);
        clearTimeout(timeout_id);
    });

    $('#btn_right').click(function(){
        moveRight(this);
        clearTimeout(timeout_id);
    });
    
    timeouter();
});

$('#slider a').mouseover(function(){
    alert('ss');
    sliding = false;
});

function moveLeft(btn)
{
    if (progress || !sliding)
        return;
    else
        progress = true;
    setTimeout('clear()', speed);
    $('#btn_left').fadeOut('fast');
    var zindex = 0;
    var decreseZindex = false;

    for (var x=actualPhoto; x<=(count+actualPhoto); x++){
        var obj = $('#slider ul li:eq('+(x%maxCount)+')');

        if (x==actualPhoto){ // first
            obj.fadeOut('slow');
            obj.css('z-index', 1);
        }else{
            if (x>=(count+actualPhoto)){ // last
                obj.css('left', ((count-1)*move)+'px');
                obj.css('z-index', 1);
                obj.fadeIn('slow');
                obj.children('span').css('font-size', '16px');
            }else{
                if (zindex>=Math.round(count/2) || decreseZindex==true){
                    zindex--;
                    decreseZindex = true;
                    obj.children('span').css('font-size', (20+zindex)+'px');
                    //plusminus = '-';
                }else{
                    zindex++;
                    obj.children('span').css('font-size', '25px');
                }
                obj.css('z-index', zindex);
                obj.animate({
                        left: '-='+move,
                        paddingTop: (((zindex-1)*20)+20)+'px'
                        //zIndex: /*plusminus+'=100'*/zindex*100
                    }, speed);
            }
        }
    }
    actualPhoto++;
    $('#btn_left').fadeIn('fast');
}

Number.prototype.mod = function(n) {
    return ((this%n)+n)%n;
}

function moveRight()
{
    var zindex = 0;
    var decreseZindex = false;

    for (var x=actualPhoto-1; x<=(count+actualPhoto); x++){
        var obj = $('#slider ul li:eq('+(x.mod(maxCount))+')');

        if (x==actualPhoto-1){ // first
            obj.css('left', '0px');
            obj.css('z-index', 1);
            obj.fadeIn('slow');
            obj.children('span').css('font-size', '16px');
        }else{
            if (x>=(count+actualPhoto-1)){ // last
                obj.fadeOut('slow');
                obj.css('z-index', 1);
            }else{
                if (zindex>=Math.floor(count/2) || decreseZindex==true){
                    zindex--;
                    decreseZindex = true;
                    obj.children('span').css('font-size', (20+zindex)+'px');
                }else{
                    zindex++;
                    obj.children('span').css('font-size', '25px');
                }
                obj.css('z-index', zindex);
                obj.animate({
                        left: '+='+move,
                        paddingTop: (((zindex+1)*20)+20)+'px'
                        //zIndex: /*plusminus+'=100'*/zindex*100
                    }, speed);
            }
        }
    }
    actualPhoto--;
}

