if(jquery_enabled()) {
  
  $(document).ready(function(){
    if(tabs_history_enabled()) {
  		// Initialize history plugin.
  		// The callback is called at once by present location.hash. 
  		$.history.init(pageload);
  		
  		// set onlick event for buttons
  		$("a[@rel='history']").click(tab_click);
		} else {
		  init_tabs();
		}
	});
} else if(window.addEventListener) {
	window.addEventListener('load', init_tabs, false); 
} else{
	window.attachEvent('onload', init_tabs);
}

function tab_click() {
  var hash = this.href;
  hash = hash.replace(/^.*#/, '');
  // moves to a new page. 
  // pageload is called at once. 
  $.history.load(hash);
  return false;
}

function init_tabs() {
  var match = window.location.hash.match(/^#tab_(.*)/);
  var default_selected = null;
  if(match) {
    default_selected = match[1];
  }
  display_tabs(default_selected);
}

var hideable_areas = new Array();

function display_tabs(selected_area)
{
  var tab_menu_text = '';
  var last_section = ''; 
  for (var i in hideable_areas) {
    last_section = i;
  }
  
  var j = 1;
  for(var i in hideable_areas)
  {
    var currently_selected = false;
    if(typeof(selected_area) != 'string')
    {
      selected_area = i;
    }
    if(selected_area == i)
    {
      currently_selected = true;
    }
    if (j == 1 ) {
      tclass = 'first';
      if (last_section == i) {
        tclass = 'both';
      }
    } else if (last_section == i) {
      tclass = 'last';
    } else {
      tclass = '';
    }
    
    var link_onclick = "onclick=\"display_tabs('"+i+"');return false;\"";
    
    if(tabs_history_enabled()) {
      var link_onclick = '';
    }
    
    if(currently_selected)
    {
      tab_menu_text+= '<li class="' + tclass + ' here"><a href="#tab_'+i+'" rel="history" '+link_onclick+'>'+hideable_areas[i]['title']+'</a></li>';
    }
    else
    {
      tab_menu_text+= '<li class="'+ tclass +'"><a href="#tab_'+i+'" rel="history" '+link_onclick+'>'+hideable_areas[i]['title']+'</a></li>';
      hide_area(i);
    }
    j++;
  }
  show_area(selected_area);
  var menu = document.getElementById('tabs');
  if(menu)
  {
    menu.innerHTML = tab_menu_text;
  }
  else
  {
    alert('Error - ul "tabs" not found.  You must include a ul with id of "tabs" in the page where you would like the tab menu to appaear');
  }
}

function show_area(area)
{
  // window.location.hash = 'tab_'+area;
  var div = document.getElementById(area);
  if(div) {
    div.style.display = '';
  }
}


function hide_area(area)
{
  document.getElementById(area).style.display = 'none';
}

function jquery_enabled() {
  return (typeof($) != 'undefined');
}

function tabs_history_enabled() {
  return (jquery_enabled && typeof($.history) != 'undefined');
}

function pageload(hash) {
  var selected_area = null;
  if(hash) {
    var match = hash.match(/^tab_(.*)/);
    if(match) {
      selected_area = match[1];
    }
  }
  display_tabs(selected_area);
}
