var ul=$("#ul_gallery");
var li=$("#ul_gallery li");
var img=$("#ul_gallery img");
li.eq(0).css({left:"0px",display:"block"});
li.eq(1).css({top:"14px",left:"176px",width:"215px",height:"132px",display:"block"});
img.eq(1).css({width:"215px",height:"132px"});
li.eq(2).css({top:"0",left:"391px",width:"237px",height:"146px",display:"block"});
img.eq(2).css({width:"237px",height:"146px"});
li.eq(3).css({top:"14px",left:"628px",width:"215px",height:"132px",display:"block"});
img.eq(3).css({width:"215px",height:"132px"});
li.eq(4).css({left:"843px",display:"block"});
var x=img.length;
var y=2;


$("#span_next").click(function() {

	//if(y==(x-2)){$("#span_next").fadeOut();}
	//$("#span_prev").show();
    y++;
    


    var y_pos0=y-3;
    var y_pos1=y-2;
    var y_pos2=y-1;
    var y_pos4=y+1;
    var y_pos5=y+2;
    var y_pos6=y+3;

    if((y-3)<0){
        y_pos0=x-1;
    }
    if((y-2)<0){
        y_pos0=x-2;
        y_pos1=x-1;
    }
    if((y-1)<0){
        y_pos0=x-3;
        y_pos1=x-2;
        y_pos2=x-1;
    }
    
    if((y+3)>=x){
        y_pos6=0;
    }
    
    if((y+2)>=x){
        y_pos5=0;
        y_pos6=1;
    }

    if((y+1)>=x){
        y_pos4=0;
        y_pos5=1;
        y_pos6=2;
    }

    if(y==x){
        y=0;
        y_pos4=1;
        y_pos5=2;
        y_pos6=3;
    }




 
    li.eq(y_pos0).fadeOut("slow");

    li.eq(y_pos1).animate({top:"38px",left:"0",width:"176px",height:"108px"});
    img.eq(y_pos1).animate({width:"176px",height:"108px"});

    li.eq(y_pos2).animate({top:"14px",left:"176px",width:"215px",height:"132px"});
    img.eq(y_pos2).animate({width:"215px",height:"132px"});
    
    li.eq(y).animate({top:"0",left:"391px",width:"237px",height:"146px"});
    img.eq(y).animate({width:"237px",height:"146px"});
    
    li.eq(y_pos4).animate({top:"14px",left:"628px",width:"215px",height:"132px"});
    img.eq(y_pos4).animate({width:"215px",height:"132px"});

    li.eq(y_pos5).css({left:"843px",display:"none"});
    li.eq(y_pos5).fadeIn("slow");

});



$("#span_prev").click(function() {

	y--;

    if(y<0){
        y=x-1;
    }
	//if(y==2){$("#span_prev").fadeOut();}
	//$("#span_next").show();

    var y_pos0=y-3;
    var y_pos1=y-2;
    var y_pos2=y-1;
    var y_pos4=y+1;
    var y_pos5=y+2;
    var y_pos6=y+3;

//alert('0:'+y_pos0+'\n'+'1:'+y_pos1+'\n'+'2:'+y_pos2+'\n'+'y:'+y+'\n'+'4:'+y_pos4+'\n'+'5:'+y_pos5+'\n'+'6:'+y_pos6+'\n');

    if((y-3)<0){
        y_pos0=x-1;
    }
    if((y-2)<0){
        y_pos0=x-2;
        y_pos1=x-1;
    }
    if((y-1)<0){
        y_pos0=x-3;
        y_pos1=x-2;
        y_pos2=x-1;
    }
    
    if((y+3)>=x){
        y_pos6=0;
    }
    
    if((y+2)>=x){
        y_pos5=0;
        y_pos6=1;
    }

    if((y+1)>=x){
        y_pos4=0;
        y_pos5=1;
        y_pos6=2;
    }

    if(y==0){
        y_pos4=1;
        y_pos5=2;
        y_pos6=3;
    }
    //alert('0:'+y_pos0+'\n'+'1:'+y_pos1+'\n'+'2:'+y_pos2+'\n'+'y:'+y+'\n'+'4:'+y_pos4+'\n'+'5:'+y_pos5+'\n'+'6:'+y_pos6+'\n');

    li.eq(y_pos1).css({left:"0px",display:"none"});
    li.eq(y_pos1).fadeIn("slow");

    li.eq(y_pos2).animate({top:"14px",left:"176px",width:"215px",height:"132px"});
    img.eq(y_pos2).animate({width:"215px",height:"132px"});

    li.eq(y).animate({top:"0",left:"391px",width:"237px",height:"146px"});
    img.eq(y).animate({width:"237px",height:"146px"});

    li.eq(y_pos4).animate({top:"14px",left:"628px",width:"215px",height:"132px"});
    img.eq(y_pos4).animate({width:"215px",height:"132px"});

    li.eq(y_pos5).animate({top:"38px",left:"843px",width:"176px",height:"108px"});
    img.eq(y_pos5).animate({width:"176px",height:"108px"});

    li.eq(y_pos6).fadeOut("slow");

});