/** ADDED BY Dave Ringoen, September 2007 */
/** Initialize all of our objects now. */

var subnav_url_base = '';

/** Create init methods for the Homes Sub Nav */
function initialize_dthml_history_tabs() {
  // initialize our DHTML history
  dhtmlHistory.initialize();
  // subscribe to DHTML history change
  // events
  dhtmlHistory.addListener(handleHistoryChange);
}

function initialize_basic_tabs() {
  var tab = location.hash.replace('#', '');
  if (tab !== '') {
    if (tab == 'arriving') { // not so dry, but call the loadGM() Google Maps function if we're on that tab.    
      new Ajax.Updater('content-inner', subnav_url_base.replace('xyzzy_action',tab), {asynchronous:true, evalScripts:true, 
        onLoading:function(request){$('loading').show();},
        onComplete:function(request){$('loading').hide(); activate_sub_nav_links(tab); loadGM(); ('loading').hide();}}); // seem to need to call .hide twice. DRR
    }
    else {
      new Ajax.Updater('content-inner', subnav_url_base.replace('xyzzy_action',tab), {asynchronous:true, evalScripts:true, 
        onLoading:function(request){$('loading').show();},
        onComplete:function(request){$('loading').hide(); activate_sub_nav_links(tab); $('loading').hide(); }});  // maybe do it twice here, this case doesn't seem reliable either.
    }
  }
}

function set_subnav_url_base(url_base) {
  //alert('set_subnav_url_base ' + url_base);
  subnav_url_base = url_base;
}

/** Our callback to receive history
    change events. */
function handleHistoryChange(pageId, pageData) {
  if (!pageData) return;
  var info = pageId.split(':');
  var id = info[0];
  //pageData += '';
  if (id == 'arriving') { // not so dry, but call the loadGM() Google Maps function if we're on that tab.
  new Ajax.Updater('content-inner', pageData, {asynchronous:true, evalScripts:true, 
    onLoading:function(request){$('loading').show();},
    onComplete:function(request){$('loading').hide(); activate_sub_nav_links(id); loadGM();}});
  }
  else {
    new Ajax.Updater('content-inner', pageData, {asynchronous:true, evalScripts:true, 
      onLoading:function(request){$('loading').show();},
      onComplete:function(request){$('loading').hide(); activate_sub_nav_links(id);}});    
  }
}

function addQuintessPageToHistory(url, subPage) {
  // var array = url.split('?');
  // var qs = new Querystring(array[1]);
  // var sort = qs.get('sort')
  // var dir = qs.get('sort_direction')
  //   var page = qs.get('page')
  // if (sort || dir || page) dhtmlHistory.add(active_scaffold_id+":"+page+":"+sort+":"+dir, url);

  // following line needed to drive dhtml history
  // dhtmlHistory.add(subPage, url);
  
  // needed for basic tabs
  location.hash = subPage;
  
  activate_sub_nav_links(subPage);
	return true;
}

function activate_sub_nav_links(id)
{
	$$('.content-link').each(function(element) {
	  Element.removeClassName(element, 'selected');
  })
	Element.addClassName(id + '-link', 'selected');
}


/** set onload handler */
//Event.observe(window, 'load', initialize_dthml_history_tabs, false);
Event.observe(window, 'load', initialize_basic_tabs, false);
