var tabs;

jQuery(document).ready(function(){
    jQuery('#tab_exclusive').addClass('active');
    jQuery('#collection').data('src', imagesUrl+'index_exclusive.jpg');

    tabs = jQuery('#tabs a');

    playList = new Array();
    playOffset = 1;

    tabs.each(function(){
        jQuery(this).prepend('<div id="'+this.id+'_overlay" class="overlay_tab"></div>');
        jQuery('#tab_exclusive').addClass('active_tab');
        jQuery('#'+this.id+'_overlay').hide();
        var img = new Image();
        var filename = this.id.substr(4);
        img.src = imagesUrl+'index_'+filename+'.jpg';
        playList.push(jQuery(this));
    });

    playerID = setTimeout('play()', 10000);

    tabs.hover(function(){
        playOffset = 0;
        clearTimeout(playerID);
        select_tab(this);
        jQuery(this).css('background-position', 'left');
    }, function(){
        playerID = setTimeout('play()', 5000);
    });

});

function play(){
    if (playOffset > tabs.size()-1){
        playOffset = 0;
    }
    playList[playOffset].each(function(){
        select_tab(this);
    });
    playOffset++;
    playerID = setTimeout('play()', 3000);
}

function select_tab(tab){
    if (!jQuery('#'+tab.id).hasClass('active_tab')){
        //jQuery(tab).css('background-position', 'left');
        var container = jQuery('#collection_name div');
        container.stop(false, true);
        container.fadeOut(250, function(){
            container.html('<span>Серия</span> '+tab.name);
            container.fadeIn(250);
        });
        jQuery('#'+tab.id).addClass('active_tab');
        jQuery('#'+tab.id+'_overlay').stop(false, true);
        jQuery('#'+tab.id+'_overlay').fadeIn(300);
        //alert('../images/index_'+tab.id.substr(4)+'.jpg');

        jQuery('#collection').stop(false, true);
        jQuery('#collections').css('background','url('+jQuery('#collection').data('src')+') no-repeat left 1px');
        jQuery('#collection').css('background-image', 'none').fadeOut(0);
        //jQuery('#collection').data('src', 'images/index_'+tab.id.substr(4)+'.jpg');
        jQuery('#collection').css('background-image','url('+imagesUrl+'index_'+tab.id.substr(4)+'.jpg)').data('src', imagesUrl+'index_'+tab.id.substr(4)+'.jpg').fadeIn(400);
        jQuery('#collection_href').attr('href', tab.href);
        unselect_tab(tab);
    }
}

function unselect_tab(tab){
      var active_tab = jQuery('.active_tab');
      if (active_tab.size() > 1) {
          active_tab.each(function(){
              if (this.id != tab.id){
                  jQuery('#'+this.id+'_overlay').stop(false, true);
                  jQuery('#'+this.id+'_overlay').fadeOut(300);
                  jQuery('#'+this.id).removeClass('active_tab');
                  jQuery('#'+this.id).removeClass('active');
              }
          });
      }
}
