jQuery(function($) {

  if($("body").hasClass('topic')) makeTabs('#essentials');
  
  /* Homepage */
  if($('body').hasClass('home')) {
    $('.scrollable').scrollable();
  }
  
  /* Toggle the list of resources */
  if($('body').hasClass('resource_list')) {
    $resources = $('ul.resources');
    $controls = $('#view_controls');
    
    $('#view-grid').click(function() {
      $.cookie("view", "grid", { expires: 30 });
      $resources.removeClass('view-list').addClass('view-grid').find('>li:nth-child(3n+1)').attr('style','clear: left');
      selectItem($(this),$controls);
    });
    $('#view-list').click(function() {
      $.cookie("view", "list", { expires: 30 });
      $resources.removeClass('view-grid').addClass('view-list');
      selectItem($(this),$controls);
    });
    
    if ($.cookie("view") == 'grid') { $('#view-grid').click(); }
  }
  
  /* Course Registration */
  if($('body').hasClass('course_registration')) {
  
    /* Add another registrant */
    $('#add_another_registrant_button').click(function() {
      $('.registrant#prototype').clone(true).insertBefore('#add_another_registrant')
                                .show()
                                .removeAttr('id');
    });
    
    /* Delete registrant   -- show all the time
    $('.registrant').hover(
      function() { $(this).find('.remove_registrant').show(); },
      function() { $(this).find('.remove_registrant').hide(); }
    );
   */
    
    /* Allow purchase order as a credit card option */
    $('#credit_card_type').change(function() {
      if($(this).val() == 'po') {
        $('#credit_card_details #purchase_order').show();
        $('#credit_card_details .cc_details').hide();
      } else {
        $('#credit_card_details #purchase_order').hide();
        $('#credit_card_details .cc_details').show();
      }
    });
    
    /* Show Coupon code form */
    $('#add_coupon').click(function() {
      if($('fieldset#coupons ol').is(':hidden')) { 
        $(this).hide();
        $('fieldset#coupons ol').show();
      }
    });
    
    /* Warn user of losing data entered if clicking steps */
    $('#steps .lose-data a').click(function() {
      return confirm('If you leave this page without submitting the form, you may lose data already entered.  Click OK to leave this page, or CANCEL to stay.');
    });
    
  }
  
  /* Course Registration Admin */
  if($('body').hasClass('course_registration_admin')) {
  
    /* Show Past Classes */
    $('#toggle_past_classes').click(function() {
      linkTxt = ($('#past_classes').is(':visible')) ? 'Show Past Classes Taken' : 'Hide Past Classes Taken';
      $(this).text(linkTxt);
      $('#past_classes').toggle();
      return false;
    });
    
    /* Show refund form */
    $('#refund_link').click(function() {
      $(this).parent().hide();
      $('#refund_form').show();
      return false;
    });
    $('#refund_form a').click(function() {
      $(this).parents('form').hide();
      $('#refund_link').parent().show();
      return false;
    });

    /* Show modal edit registrant 
    $('#edit_registrant_button').fancybox({
      'padding': 20,
      'overlayColor': '#000',
      'overlayOpacity': .5
    });
    */
  }
  
});

/* remove a selected class from a sibling and make this selected */
function selectItem($item, $container) {
  $container.find('.selected').removeClass('selected');
  $item.addClass('selected');
}


/* Creates tabs to be used by jQuery Tools
*  Expects the following structure:
*  <div id="foo">
*    <h2>Title</h2>
*    <ul class="panes">
*      <li>
*        <h3 title="Tab Title(optional)">Pane Title</h3>
*        <div>content</div>
*      </li>
*      ...
*    </ul>
*  </div>
*/
function makeTabs(container) {
  $container = $(container);
  $content_items = $container.find('.panes > li');
  panes = container + ' .panes > li';
  
  // Create an empty UL for the tabs
  $tabs = $container.find('h2')
                    .after('<ul class="tabs"></ul>')
                    .next('ul.tabs');
  
  // Clone the headers for each pane and add them to the tabs UL
  $tab_headers = $container.find('ul.panes h3')
                           .clone()
                           .wrapInner('<a href="#"></a>')
                           .appendTo($tabs)
                           .wrap('<li>');

  // If the header has a title, use it as the tab text
  $tab_headers.each(function() {
    var $this = $(this);
    var $a = $this.find('a');
    var tab_title = $this.attr('title');
    //$a.click(function() { return false; });
    if(tab_title != '') $a.text(tab_title);
  });      
  
  // Invoke the jQuery tools tabs
  $tabs.tabs($content_items);
}

