var animationSrc = '';
var animationTitle = '';
var animationTop = '';

function nextBaner(id) {
    var i = 0;
    if(typeof id == 'undefined'){
        
        id = 0;
    }
    var tmpId = 0;
    var found = 0;
    for(i in ids) {
        if(id == 0){
            found = 1;
            id = ids[i].id_web_contents;
            break;
        }else if(tmpId == id){
            found = 1;
            id = ids[i].id_web_contents;
            break;
        }else{
            tmpId = ids[i].id_web_contents;
            continue;
        }
    }
    if(!found) {
        nextBaner(0);
        return null;
    }
    var imgSrc = '/upload/thumb_'+id+'.jpg';
    if($('#slide').attr('src') == imgSrc){
        nextBaner(id);
    }else{
        $('#banerText').fadeOut(1000);
        $('#slide').fadeOut( 1000, function() {
            $('.banerKolko').each(function(){
                $(this).removeClass('banerKolkoActive')
            })
            var nrKolka = i*1 + 1;
            $('#'+nrKolka).addClass('banerKolkoActive');
            $('#banerText').html(ids[i].title+ids[i].content);
            $('#slide').attr("src", imgSrc);
            $('#banerText').fadeIn(1000);
            $('#slide').fadeIn( 1000, function() {
                setTimeout('nextBaner('+id+')',5500);
            });
        });
    }
}


function banner() {
    var tmpSrc = animationSrc;
    var tmpTitle = animationTitle;
    var tmpTop = animationTop;

    getBaner();

    $('#slide').fadeOut( 1000, function() {
        $('#slide').css({
            "top":"0px"
        });

        $('#slide').attr("src", tmpSrc);
        $('#slide').attr("title", tmpTitle);
        $('#slide').attr("alt", tmpTitle);

        $('#slide').fadeIn( 1000, function() {

            //$('h1').html('a' + tmpTop);

            //            $('#slide').animate({top:tmpTop},{queue:true,duration:15000});

            setTimeout("banner()",5500);
        });
    });
}

function getBaner() {
    $.getJSON("/ajax/banner",function(result) {

        animationSrc = '/upload/' + result.name;
        image1 = new Image(result.width, result.height);
        image1.src = animationSrc;

        animationTitle = result.title;

        var height = result.height - 247;
        animationTop = '-' + height + 'px';

    //$('h1').html('a' + animationTop);

    });
}

