var count_now = 0;
var move_back = 0;
var all_width = 0;
var speed = 300;
var stop_move = 0;
var unixtime = 0;
function move_gallery(){
    var pics_width =  $(".pics").width();
    //alert((pics_width)+" : "+gallery_width);
    var margin_left =  $(".pics").css("margin-left").replace("px","")-0;
    var gallery_width =  $(".gallery").width();
    var next_width = $(".pics a:eq("+count_now+")").width()+2;
    if(!stop_move){
        if(!move_back){
            if((all_width+(margin_left-next_width))<gallery_width){
                next_width = all_width - (gallery_width + Math.abs(margin_left));
                move_back = 1;
            }
            $(".pics").animate(
                { marginLeft: "-="+next_width},
                speed, "swing", function(){
                    count_now++;
                }
            );
        }else{
            $(".pics").animate(
                { marginLeft: "0px" },
                speed, "swing", function(){
                    move_back = 0;
                    count_now = 0;
                }
            );
        }
    }
    setTimeout(function() { move_gallery() }, 3000);
}
function stop_move_func(){
    stop_move = 1;
    var foo = new Date;
    var unixtime_ms = foo.getTime();
    unixtime = parseInt(unixtime_ms / 1000);
    setTimeout(function() {
        var foo_now = new Date;
        var unixtime_ms_now = foo_now.getTime();
        var unixtime_now = parseInt(unixtime_ms_now / 1000);
        if((unixtime_now-unixtime)>=5){
            stop_move = 0;
        }
    }, 6000);
}

$(document).ready(function(){
    setTimeout(function() { move_gallery(); }, 3000);
    for(i=0;i<$(".pics a").size();i++){
        all_width+=$(".pics a:eq("+i+")").width();
    }
    $(".pics").css("width",all_width+20);
    $(".gallery").css("visibility","visible");
	$(".left_list").click(function(){
        stop_move_func();
        move_back = 0;
        if((count_now-1)>=0){
            var count_w = 0;
            for(i=0;i<(count_now-1);i++){
                count_w+=$(".pics a:eq("+i+")").width()+2;
            }
            $(".pics").animate(
                { marginLeft: "-"+count_w+"px" },
                speed, "swing", function(){
                    count_now--;
                }
            );
        }
        return false;
	});
    $(".right_list").click(function(){
        stop_move_func();
        var next_width = $(".pics a:eq("+count_now+")").width()+2;
        if(!move_back){
            var margin_left =  $(".pics").css("margin-left").replace("px","")-0;
            var gallery_width =  $(".gallery").width();
            if((all_width+(margin_left-next_width))<gallery_width){
                next_width = all_width - (gallery_width + Math.abs(margin_left));
                move_back = 1;
            }
            $(".pics").animate(
                { marginLeft: "-="+next_width},
                speed, "swing", function(){
                    count_now++;
                }
            );
        }
        return false;
	});
});

function new_photos(){
    var rand = Math.floor(Math.random()*($(".new1 a").size()));
    while (now_random == rand){
        rand = Math.floor(Math.random()*($(".new1 a").size()));
    }
    now_random = rand;
    $(".new1 a:eq("+rand+")").animate( { backgroundColor: '#6186B3' }, 3000, function (){
        new_photos();
        $(".new1 a:eq("+rand+")").animate( { backgroundColor: '#FFFFFF' }, 3000);
    });

    //setTimeout(function() { new_photos(); }, 3000);
}
