var timer = 0;
var interval = 10;
var hnDisplayed = 0;

function count(){
    if (timer == interval){
        interval = 10;
        timer = 0;
        
            
        $("#hn-loader").show();
        
        var nextCl = (hnSelected)%5 + 1;
        $("#hn-"+nextCl).css('color', 'red');
        $.post('/controller/ajax/Domov-slike.php',{ pic_id: nextCl }, function(data){

            $("#hn-loader").hide();
            
            $("#hn-"+hnSelected).parent().css('background-image', 'none');
            $("#hn-"+hnSelected).css('color', '#00763C');
            hnSelected = nextCl;
            $("#hn-"+hnSelected).parent().css('background-image', 'url(material/ozadje_stevilke.png)');
            $("#hn-display-"+hnDisplayed).fadeOut();
            hnDisplayed = Math.abs(hnDisplayed-1);
            $("#hn-display-"+hnDisplayed).html(data);
            $("#hn-display-"+hnDisplayed).fadeIn();
        }); 
        
        setTimeout(function(){ count(); }, 1000);         
                
    } else {
        timer++;
        setTimeout(function(){ count(); }, 1000);
    }
}

$(document).ready(function(){
    
    $(".hna").click(function(){
        timer = 0;
        interval = 30;
        if ($(this).attr('id')!="hn-"+hnSelected){
            
            $("#hn-loader").show();
            $(this).css('color', 'red');
            var nextCl = $(this).html();
            $.post('/controller/ajax/Domov-slike.php',{ pic_id: nextCl }, function(data){

                $("#hn-loader").hide();
                
                $("#hn-"+hnSelected).parent().css('background-image', 'none');
                $("#hn-"+hnSelected).css('color', '#00763C');
                hnSelected = nextCl;
                $("#hn-"+hnSelected).parent().css('background-image', 'url(material/ozadje_stevilke.png)');
                $("#hn-display-"+hnDisplayed).fadeOut();
                hnDisplayed = Math.abs(hnDisplayed-1);
                $("#hn-display-"+hnDisplayed).html(data);
                $("#hn-display-"+hnDisplayed).fadeIn();
            });
        }
    }); 
    
    $("#hn-loader").show();
    $("#hn-1").css('color', 'red');
    var nextCl = 1;
    $.post('/controller/ajax/Domov-slike.php',{ pic_id: nextCl }, function(data){

        $("#hn-loader").hide();
        
        $("#hn-"+hnSelected).parent().css('background-image', 'none');
        $("#hn-"+hnSelected).css('color', '#00763C');
        hnSelected = nextCl;
        $("#hn-"+hnSelected).parent().css('background-image', 'url(material/ozadje_stevilke.png)');
        $("#hn-display-"+hnDisplayed).fadeOut();
        hnDisplayed = Math.abs(hnDisplayed-1);
        $("#hn-display-"+hnDisplayed).html(data);
        $("#hn-display-"+hnDisplayed).fadeIn();
    });   
});
