var searchController = function() {
  this.searchForm = $('search');
  this.searchField = this.searchForm.query;
  
  this.focusSearchField = function () {
    if (this.value == this.title) { this.value = ''; } 
  }
  
  this.blurSearchField = function() {
    if (this.value == '') { this.value = this.title; }
  }
  
  this.checkSearchEntry = function() {
    if (this.query.value == '' || this.query.value == this.query.title) { 
      alert("Please enter some search terms");
      return false;
    } else {
      return true;
    }
  }
  
  if (searchForm) {
    this.searchField.onfocus = this.focusSearchField;
    this.searchField.onblur = this.blurSearchField;
    this.searchForm.onsubmit = this.checkSearchEntry;
  }
}


function menuController(subMenuArray) {
  
  subMenuArray = $$('#menu ul li ul');
  
  subMenuArray.each(function(item){
    var parentMenuItem = item.getParent();
    
    var expanderElement = new Element('span', {'text':'expand', 'class':'hide_text expander' });
    if(parentMenuItem.hasClass('static')) { expanderElement.addClass('open') } else { expanderElement.addClass('closed') }
    expanderElement.addEvent('click', individualHandler);
    parentMenuItem.getFirst('a').setStyle('padding-right', '16px');
    expanderElement.inject( parentMenuItem.getFirst('a'), 'after');
    
    item.set('class','unstatic');
    item.set('slide', {'duration':'short'});
    if(!parentMenuItem.hasClass('static')) {
      item.slide('hide');
    }
    
  });
  
}
  

function individualHandler() {
	var subMenu = this.getParent('li').getElement('ul');
    this.toggleClass('open');
    this.toggleClass('closed');    
    subMenu.slide('toggle');
	return false;
}


function headTrayController() {
	var currentSection = $('current-section');
	
	if (!currentSection) { return; } else { currentSection = currentSection.getFirst('a'); }
	
 	currentSection.addClass('dropdown');
 	$('head').setStyle('height', 'auto');
 	
 	var tray = $('tray');
	var trayFx = new Fx.Slide(tray);

	trayFx.hide();
	tray.setStyle('display', 'block');
	tray.setStyle('visibility', 'hidden');
	
	trayFx.addEvent('complete', function () {
		if (!currentSection.hasClass('open')) {	tray.setStyle('visibility', 'hidden'); } 
	});
	
 	currentSection.addEvent('click', function() {
 		if (!currentSection.hasClass('open')) {	tray.setStyle('visibility', 'visible'); }
 		trayFx.toggle();
		this.toggleClass('open');
 		return false; 
 	});
}

function tableFixController() {
	var p = new Element('p', { 'style' : 'text-align:right' }).setStyle('margin-bottom', 0);
	var expandLink = new Element('a', { 'text' : 'View full table', 'href' : '#', 'style' : 'font-weight:bold;' });
	var bigTables = false;
	var contentWidth = $('w4').getSize().x;
	$$('#content table.tabledata_blue').each(function(table, num) {
		if (table.getSize().x > contentWidth) {
			myExpandLink = expandLink.clone().addEvent('click', function() { myOverlay.show(table); return false; });
			myExpandLink.inject(p.clone().inject(table, 'before'));
			myExpandLink.clone().cloneEvents(myExpandLink).inject(p.clone().inject(table, 'after'));
			bigTables = true;
		}
	});
	if (bigTables) var myOverlay = new Overlay();
}

function SIQFeatureBg() {
	if ($('sidebar')) $('mid').addClass('withfeature');
}
  
window.addEvent('domready', function() {
	SIQFeatureBg();
 	headTrayController();
	menuController();
	tableFixController();
	searchController();
 });