function prevNext(count,koniec,stan){
    $j("#header img").removeClass("active").removeClass("prev").removeClass("next");
        var next = count + 1;
        if(next > koniec){
            next = 0;
        }
        var prev = count - 1;
        if(prev < 0){
            prev = koniec;
        }  

        $j("#header img").css({"top":"0","left":"-464px"})
        $j("#header img:eq("+prev+")").addClass("prev").css({"left":"-464px"})
        $j("#header img:eq("+next+")").addClass("next").css({"left":"464px"});
        $j("#header img:eq("+count+")").addClass("active").css({"left":"0px"});
        $j("#arrow-change span").addClass("free").removeClass("busy");
        
        
}


function przesunWprawo(count,koniec){
    $j("#header img.active").animate({left: '-=464'}, 1500);
    $j("#header img.next").animate({left: '-=464'}, 1500);
    $j("#header img.prev").animate({left: '+=0'}, 1500,
    function(){
        prevNext(count,koniec,0)
    });

}


function change(stan){
    var koniec = $j("#header img").size() - 1;
    var count = $j("#header img.active").index();
    if(stan == "next"){
        count = count + 1;
        if(count > koniec){
            count = 0;
        }
        przesunWprawo(count,koniec);
    }else{
        count = count - 1;
        if(count < 0){
            count = koniec;
        }
       przesunWlewo(count,koniec)
   }
}


$j(document).ready(function(){ 
    $j("#header img").show();
	$j("#header").css('overflow: hidden');
    var koniec = $j("#header img").size() - 1;
    prevNext(0,koniec,2);
    var refreshIntervalId = setInterval(function(){change("next")}, 3500);
    
    $j("#arrow-change span").click(function(){
           if($j(this).hasClass("free")){
               $j(this).addClass("busy").removeClass("free");
               change($j(this).attr("id"))
               }
               clearInterval(refreshIntervalId);
               refreshIntervalId = setInterval(function(){change("next")}, 3500);
           });
});
