$(function() {

  $('div.tab_welcome div.tab_nav a').click(function(){

    $(this).parents('div.tab_nav').find('li').removeClass('selected');
    $(this).parent().addClass('selected');

    $(this).parents('div.tab_welcome').find('div.tab_body').addClass('hidden');
    $(this).parents('div.tab_welcome').find(
      'a[name=' + $(this).attr("href").replace(/^#/, "") + ']'
    ).parents('div.tab_body').removeClass('hidden');

    return false;

  });
  $('div.tab_welcome div.tab_nav a:first').click();
  
});

// premium quizzes

$(function() {
  
  $('div.premium_quizzes').each(function(){
    var that = $(this),
      ul = that.find( 'ul' ),
      controls,
      html,
      timeout_id,
      fade_speed = 600,
      rotate_speed = 5000;
    
    if ( ul.children( 'li' ).length < 2 ) { return; }
    
    that.addClass( 'premium_quizzes_enabled' );
    
    function prev() {
      ul.children()
        .hide();
      
      ul.children( 'li:last' )
        .prependTo( ul )
        .show()
        .fadeOut( fade_speed );
      
      ul.children( 'li:last' )
        .hide()
        .fadeIn( fade_speed );
    };
    
    function next() {
      ul.children()
        .hide();
      
      ul.children( 'li:last' )
        .show()
        .fadeOut( fade_speed );
          
      ul.children( 'li:first' )
        .appendTo( ul )
        .hide()
        .fadeIn( fade_speed );
    };
    
    function pause() {
      timeout_id && clearTimeout( timeout_id );
      timeout_id = null;
      
      controls.children( '.play' ).show();
      controls.children( '.pause' ).hide();
    };
    
    function play() {
      pause();
      
      (function loopy(){
        timeout_id = setTimeout(function(){
          next();
          loopy();
        }, rotate_speed );
      })();
      
      controls.children( '.play' ).hide();
      controls.children( '.pause' ).show();
    };
    
    that.hover( pause, play );
    
    // initialize controls
    
    html = ''
      + '<div class="controls">'
      + '  <img src="static/images/icons/fff/control_start_blue.png" title="Previous Featured Quiz" class="previous"/>'
      //+ '  <img src="static/images/icons/fff/control_pause_blue.png" title="Pause" class="pause"/>'
      //+ '  <img src="static/images/icons/fff/control_play_blue.png" title="Play" class="play"/>'
      + '  <img src="static/images/icons/fff/control_end_blue.png" title="Next Featured Quiz" class="next"/>'
      + '</div>';
    
    controls = $( html ).appendTo( that.find( '.title' ) )
      
      .children( '.play' )
        .hide()
        .click( play )
        .end()
      
      .children( '.pause, .previous, .next' )
        .click( pause )
        .end()
      
      .children( '.previous' )
        .click( prev )
        .end()
      
      .children( '.next' )
        .click( next )
        .end();
    
    // show an item
    ul.children( 'li:not(:last)' ).hide();
    
    // start playing
    play();
    
  });
  
});


tab_per_page = 15;
