/* common.js */
function searchController() {
	var searchForm = $('search');
	var searchField = $('query');
	
	if (searchForm) {
		searchField.addEvent('focus', function(){ if (this.value == this.title) this.value = ''; });
		searchField.addEvent('blur', function(){ if (this.value == '') this.value = this.title; });
			searchForm.addEvent('submit', function(){ 
			if (this.query.value == '' || this.query.value == this.query.title) { 
				alert("Please enter some search terms");
				return false;
			} else return true; 
		});
	}
}

function menuController(subMenuArray) {
  $$('#menu ul li ul').each(function(item){
    var parent = item.getParent();    
    var expand = new Element('span', {'text':'expand', 'class':'hide_text expander' });
    
    if (parent.hasClass('static')) expand.addClass('open');
    else expand.addClass('closed');
    
    expand.addEvent('click', function(){
    	this.toggleClass('open'); this.toggleClass('closed');    
   		this.getParent('li').getElement('ul').slide('toggle');
		return false;
	});
    parent.getFirst('a').setStyle('padding-right', '1.6em');
    expand.inject( parent.getFirst('a'), 'after');    
    item.set('class','unstatic');
    item.set('slide', {'duration':'short'});
    if (!parent.hasClass('static')) item.slide('hide');    
  });
  
}


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

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


function contentController() {
	var p = new Element('p');
	var expandLink = new Element('a', { 'text' : 'View full size', 'style' : 'cursor: pointer; font-weight: bold; display: block; text-align: right;'});
	var bigObj = false;
	var contentWidth = $('w4').getSize().x;
	$$('#content table.tabledata_blue, #content img').each(function(obj, num) {
		if (obj.getSize().x > contentWidth) {
			myExpandLink = expandLink.clone().addEvent('click', function() { myOverlay.show(obj); return false; });
			myExpandLink.inject(p.clone().inject(obj, 'before'));
			bigObj = true;
		}
	});
	if (bigObj) var myOverlay = new Overlay({overlayFixedWidth: true, centerOverlay: true});
}

window.addEvent('domready', function() {
 	//headController();
	menuController();
	contentController();
	searchController();

	try {
		if ($chk(JsHook)) $each(JsHook, function(hook, index){eval(hook);});
	} catch (e) {}
	
 }); 
 

