var CONTROLLER = 'metropictures';

/**
 * Page-specific JavaScript initialization:
 *   - Artist Tabs & thumbnail scroller effects
 */
Controller.prototype.pageInit = function()
{
  // TAB GROUP 1
  this.tabs = {
    anchors: $('a.artists_tab, a.represented_tab, a.works_tab')
  };

  // Set tab click events
  this.tabs.anchors.click({ controller: this }, function(e)
  {
    e.data.controller.handleTabClick.call(e.data.controller, this, '#tabgroup1');
    return false;
  });
}


/**
 * Handle tab click events
 *
 * @param HTMLAnchorElement anchor The clicked tab
 */
Controller.prototype.handleTabClick = function(anchor, parent)
{
  var $anchor = $(anchor);
  // If an inactive tab has been clicked
  if (!$anchor.parent().hasClass('selected'))
  {
    //set new tab variable
    $newTab = '#' + $anchor.attr('id');

    //set active(old) tab variable
    var $oldTab =  '#' + $(parent + ' > li.selected').attr('id');

    // Remove class 'selected' from the active tab's anchor
    $($oldTab).removeClass('selected');

    // Swap the display of items
    toggleItems($oldTab, $newTab);

    // Add class 'selected' to the newly clicked anchor
    $($newTab).addClass('selected');

  } else if ($anchor.parent().hasClass('selected')){
    //alert('this tab is already selected :D');
  }

  function toggleItems(oldId, newId)
  {
    $(oldId + '_tab').hide();
    $(newId + '_tab').show();
  }
}


/**
 * Instantiate and initialize a Controller object
 */
$(document).ready(function()
{
    window[CONTROLLER] = new Controller();
    window[CONTROLLER].init();
});
