var $ = jQuery.noConflict();

var deamonPhotos = new Array();
var deamonArticles = new Array();
var deamonArticlesPhotos = new Array();
var current = {'photo':0,'article':0}
var timeoutID;
var timeoutTabs;

$(document).ready(function(){
    $('#front_gallery').children().each(function(){deamonPhotos.push( $(this) )});
    //alert(deamonPhotos);
    startDeamon();
    $('#promoted a').click(function(){
        $(this).stop();
        var index = $('ul#promoted li a').index( this );
        showNext(index);
        return false;
    });
    timeoutID = setTimeout(function(){showNext()}, 7000);
    // Tabs
    $('#newsTabs a[rel^="b_multi_"]').click(function(){
        $(this).stop();
        showNextTab(this);
        return false;
    });
    timeoutTabs = setTimeout(function(){showNextTab()}, 7000);

});

function showNextTab( item )
{
    clearTimeout(timeoutTabs)
    if (item == undefined) {
        // var indexTabs = $('ul#newsTabs li a').index( this )
        var next = $('ul#newsTabs li a').index( $('a[rel^="b_multi_"][class="active"]') ) + 1 ;
        var len = $('ul#newsTabs li a').length;
        if (next >= len) next = 0;
        item = $('ul#newsTabs').children(':eq('+next+')').children(':first');
    } else {
        item = $(item);
    }
    $('a[rel^="b_multi_"]').removeClass('active');
    $('[class^="b_multi_"]').hide();
    $('[class="'+item.attr('rel')+'"]').show();
    item.addClass('active');
    timeoutTabs = setTimeout(function(){showNextTab()}, 7000);
}

function showNext(index)
{
    clearTimeout(timeoutID)
    if( index!=undefined ) {
        current['article']=index;
    } else {
        if ( current['article'] < $('.promoted_h2').children().length - 1 ) {
            current['article']++;
        } else {
            current['article']=0;
        }
    }
    index=current['article'];
    $('.promoted_h2').children().hide()
    $('.promoted_photo').children().hide()
    $('.promoted_txt').children().hide()
    $('.promoted_h2').children().eq(index).show()
    $('.promoted_photo').children().eq(index).show()
    $('.promoted_txt').children().eq(index).show()
    $('#promoted a').removeClass('active');
    $('ul#promoted li').eq(index).children(":first").addClass('active');
    timeoutID = setTimeout(function(){showNext()}, 7000);
}


function startDeamon()
{
    setInterval("runDeamon();", 5000);
}

function runDeamon()
{
    deamonPhotos[current['photo']].hide();
    if ( current['photo'] < deamonPhotos.length - 1 ) {
        current['photo']++
    } else {
        current['photo'] = 0;
    }
    deamonPhotos[current['photo']].fadeIn('very slow');

}
