var timeout_id = [0,0,0]; var timeout_id2; var timeout_id3;
//var i = 0; var j = 0; var k = 0;
counter = [0,0,0]
funcs = ['animateScr()','animateScr2()','animateScr3()']
var speed = 5000;
var len = $('#scroller_1').find('.scroller_content img').length;
var len2 = $('#scroller_2').find('.scroller_content img').length;
var len3 = $('#scroller_3').find('.scroller_content img').length;
var wrap = $('#scroller_1');
var wrap2 = $('#scroller_2');
var wrap3 = $('#scroller_3');

var arr = [1,2,3];

function animateScr(){
    clearTimeout(timeout_id[0]);
    function inner(){
        if(counter[0] == len) {
            counter[0] = 0;
        }

        wrap.find('.scroller_content img').fadeTo(200, 0);
        wrap.find('.scroller_content a').eq(counter[0]).removeClass('hidden2').siblings('a').addClass('hidden2');
        wrap.find('.switchers').find('a').removeClass('active');
        wrap.find('.scroller_content img').eq(counter[0]).fadeTo(200, 1, function(){
            wrap.find('.switchers').find('a').eq(counter[0]).addClass('active');
            counter[0]++;
            timeout_id[0] = setTimeout('animateScr()', speed);
        });
    }
    return inner();
}

function animateScr3(){
    clearTimeout(timeout_id[2]);
    function inner(){
        if(counter[2] == len3) {
            counter[2] = 0;
        }

        wrap3.find('.scroller_content img').fadeTo(200, 0);
        wrap3.find('.scroller_content a').eq(counter[2]).removeClass('hidden2').siblings('a').addClass('hidden2');
        wrap3.find('.switchers').find('a').removeClass('active');
        wrap3.find('.scroller_content img').eq(counter[2]).fadeTo(200, 1, function(){
            wrap3.find('.switchers').find('a').eq(counter[2]).addClass('active');
            counter[2]++;
            timeout_id[2] = setTimeout('animateScr3()', speed);
        });
    }
    return inner();
}


function animateScr2(){
    clearTimeout(timeout_id[1]);
    function inner(){
        if(counter[1] == len2) {
            counter[1] = 0;
        }

        wrap2.find('.scroller_content img').fadeTo(200, 0);
        wrap2.find('.scroller_content a').eq(counter[1]).removeClass('hidden2').siblings('a').addClass('hidden2');
        wrap2.find('.switchers').find('a').removeClass('active');
        wrap2.find('.scroller_content img').eq(counter[1]).fadeTo(200, 1, function(){
            wrap2.find('.switchers').find('a').eq(counter[1]).addClass('active');
            counter[1]++;
            timeout_id[1] = setTimeout('animateScr2()', speed);
        });
    }
    return inner();
}

$(document).ready(function(){

    animateScr();
    animateScr2();
    animateScr3();

    $('.switchers a').click(function(){

        hs_this = $(this).parents('.header_scroller');
        hs = $('.scroller_container .header_scroller')
        ind = hs.index(hs_this);
//        console.log(arr[ind]);
//        return;

        clearTimeout(timeout_id[ind]);
        var scroller = $('.scroller_container .header_scroller').eq(ind);
        ind_ctrl = $('.switchers a', scroller).index($(this));
        
        scroller.find('.scroller_content img').fadeTo(200, 0);
        scroller.find('.scroller_content a').eq(ind_ctrl).removeClass('hidden2').siblings('a').addClass('hidden2');
        scroller.find('.switchers a').removeClass('active').eq(ind_ctrl).addClass('active');
                
        scroller.find('.scroller_content img').eq(ind_ctrl).fadeTo(100,1);
        scroller.find('.scroller_content a').eq(ind_ctrl).fadeTo(100,1);
        //$('#controls').find('.button').eq(ind).addClass('active');
        counter[ind]=0;
        timeout_id[ind] = setTimeout(funcs[ind], speed);
        return false;
    });

});


