/* Generalized tab navigation, for static, all within-page switches and AJAX.
   For use with the _tabs view.
 
Requires several variables to be predefined:

navigationItems, structured as follows: 
  var navigationItems = {
    tabId: title,
    tabId: title,
    ...etc...
  }

defaultNavigationItem     Should equal the base of uri of one of the navigation items
includePath               Include path of tab content files to load via AJAX. Requires
                          trailing slash.
titleId

Typically these will be automatically generated by the _tabs view; 
see /exhibitions/mummy_chamber/interactive/ for an example.

*/

function bindTabEvents(navigationItems, tabListId) {
  $each(navigationItems, function(title, tabId) {
    $(tabListId + '-' + tabId + '-tab').addEvent('click', function(event) {
      event.stop();
      selectTab(navigationItems, tabListId, tabId);
    });
  });
}

function bindTabEventsAjax(navigationItems, tabListId, includePath, tabContainerId, extraGet) {
  if(!$chk(extraGet)) {
    var extraGet = '';
  }
  $each(navigationItems, function(title, tabId) {
    // Previously includePath was assumed to be a URL prefix. This is presumptive. Now includePath can be an arbitrary callback func:
    if(typeOf(includePath) == 'function') {
      var tabPath = includePath(tabId);
    } else
      var tabPath = includePath + tabId + '.php';
    $(tabListId + '-' + tabId + '-tab').addEvent('click', function(event) {
      event.stop();
      selectTab(navigationItems, tabListId, tabId);
      ajaxLoad(tabPath, tabContainerId, extraGet);
    });
  });
}

function selectTab(navigationItems, tabListId, tabId) {
  deselectTabs(navigationItems, tabListId);
  $(tabListId + '-' + tabId + '-tab').getParents('li')[0].addClass('tab-selected');
  if($chk($(tabListId + '-' + tabId))) {
    $(tabListId + '-' + tabId).setStyle('display', 'block');
  }
  if(typeof(titleId) !== 'undefined') {
    if($chk($(titleId))) {
      $(titleId).set('text', navigationItems[tabId]); 
    }
  }
  toggleHomeIcon();
}

function deselectTabs(navigationItems, tabListId) {
  $each(navigationItems, function(title, tabId) {
    $(tabListId + '-' + tabId + '-tab').getParents('li')[0].removeClass('tab-selected');
    if($chk($(tabListId + '-' + tabId))) {
      $(tabListId + '-' + tabId).setStyle('display', 'none');
    }
  });
}

function toggleHomeIcon() {
  if ( $('home-icon') ) {
    if( $('home-icon').getParent('li').hasClass('tab-selected') )
      $('home-icon').set( 'src', '/images/icons/home_grey.png');
    else  
      $('home-icon').set( 'src', '/images/icons/home_blue.png');
  }
}

function ajaxLoad(path, tabContainerId, extraGet) {
  if(!$chk(extraGet)) {
    var extraGet = '';
  }
  var tabContainer = $(tabContainerId);
  var req = new Request.HTML({
    method: 'get',
    evalScripts: true,
    url: path, 
    update: tabContainer,
    onFailure: function() {
      tabContainer.set('text', 'The request failed.');
    },
    onComplete: function() {
      // Look for array of named func.s to invoke (declared in _tabs.php templ)
      if(typeof(on_load_functions)!='undefined') {
        for(var i=0;i<on_load_functions.length;i++) {
          var f = on_load_functions[i];
          eval('setTimeout(' + f + ', 1500)');
        }
      }
    }
  });
  tabContainer.set('html', '<p style="text-align:center;padding-top:3em;"><img src="/images/moodalbox/loading.gif" /></p>');
  if(kiosk) {
    req.send('ajax=true&kiosk=true' + extraGet);    
  } else {
    req.send('ajax=true' + extraGet);
  }
}

