/* set of routines used by most pages */

// function to show the spinner and follow a link
// NOTE THIS IS OVERRIDEN IN ie6_support.js TO DEAL WITH ENCODING OF HASH CHARACTERS
function spinAndFollowLink(urlToFollow) {
	try {
		Fade.showWaiting();
		window.location = urlToFollow;
		return false;
	} catch (e) {
		return true
	}
}

//function to set the focus to a specific element
//delay is to overcome a problem in IE with innerHTML in the Ajax routines
function setFirstFocus(elementId) {
	var el = document.getElementById(elementId);
	if (el) window.setTimeout(function() { 
		try { el.focus(); } catch (e) { } 
	}, 500);
}

/* routine to support toggling areas to show/hide from a link */
function toggleObjectDisplay(actionLink,objID,open_text,close_text) {
	toggledObj = document.getElementById(objID);
	try {
		if (toggledObj.style.display != 'block') {
			toggledObj.style.display = 'block';
			actionLink.firstChild.nodeValue = close_text;
		} else {
			toggledObj.style.display = 'none';
			actionLink.firstChild.nodeValue = open_text;
		}
	} catch (e) {
		CliDebug( "UI script toggleObjectDisplay error: " + e.message );
	}
	return false;
}


/* routines that support the menu bar search box */
function inputBoxManageBkg(e) {

	if (!e) {
		var e = window.event;
	}
	var event_type = (e)?e.type:'scripted';

	inputObj = this;

	switch (event_type) {
		case 'focus':
		case 'keypress':
			inputObj.hasFocus = true;
			inputObj.style.backgroundPosition = "left bottom";	// hide bkg
			break;
		case 'load':
		case 'scripted':
			if (inputObj.hasFocus) {
				inputObj.style.backgroundPosition = "left bottom";	// hide bkg
				break;
			}
		default:	// blur
			if (!inputObj.value) {
				inputObj.style.backgroundPosition = "left top";		// show bkg
			} else {
				inputObj.style.backgroundPosition = "left bottom";	// hide bkg
			}
			inputObj.hasFocus = false;
	}
}

//JS way to include extra JS files, used for the HitBox JS
function includeJs(include) {
	var script = document.createElement('script');
	script.setAttribute('type', 'text/javascript');
	script.setAttribute('src', include);
	document.getElementsByTagName('head')[0].appendChild(script);
}

/* make sure that any search boxes with backgrounds reflects any contents in them by firing off their onblur events */
function initInputBoxes () {
	//find all the INPUT tags and then loop for those which have belong to the  'inputBoxWithBkg' class
	var allInputTags = document.getElementsByTagName('INPUT');
	//loop through all the tags and find the ones with class='inputBoxWithBkg'
	for (var i = 0; i <  allInputTags.length  ; i++)  {
		if ((allInputTags[i].className) && (allInputTags[i].className.indexOf('inputBoxWithBkg') >= 0)){
			allInputTags[i].onfocus = inputBoxManageBkg;
			allInputTags[i].onblur = inputBoxManageBkg;
			allInputTags[i].onkeypress = inputBoxManageBkg;
			allInputTags[i].onblur();	// fire their blur event to set the background appropriately
		}
	}
}

/* detect Firefox 2 and older on the mac... used to address specific bugs with Flash and opacity */
function detectFF2OnMac(){
	try {
		var userAgent = navigator.userAgent.toLowerCase();
		if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
			var ffversion = new Number(RegExp.$1);
			if (ffversion < 3 && userAgent.indexOf('mac') != -1) {
				return true;
			}
		}
	} 
	catch (e) {
		// do nothing... assume false
	}
	return false;
}

window.ff2onmac = detectFF2OnMac();

function onloadEventAppend(func){
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		var oldOnload = window.onload;
		window.onload = function(){
			if (oldOnload) {
				try {
					oldOnload();
				} catch(e) {
					CliDebug('onloadEventAppend script failed: ' + e.message);
				}
			}
			func();
		}
	}
}

//add init to post ajax load and page load
PageCallback.addCallback(initInputBoxes);

/**
 * BlockToggler class
 * 
 * creates a toggle block which can change the text and show / hide a block
 * 
 * @param boolean open - initial open state
 * @param string toggler - id of the element which will cause the toggle
 * @param string container - id of the block to toggle
 * @param string openText - text to display when the block is open
 * @param string closeText - text to display when the block is closed
 */
var BlockToggler = function(open, toggler, container, openText, closeText) {
	this.initialize(open, toggler, container, openText, closeText);
}

BlockToggler.prototype = {
	
	initialize: function(open, toggler, container, openText, closeText) {
		
		this.changeText = (closeText && openText);
		this.openText = openText;
		this.closeText = closeText;
		
		var self = this;
		this.toggler = document.getElementById(toggler);
		this.toggler.onclick = function() { self.toggle(); };
		
		this.container = document.getElementById(container);
		
		this.open = open;
		if (!this.open) this.container.style.display = 'none';
	},
	
	toggle: function() {
		if (this.open) {
			this.container.style.display = 'none';
			if (this.changeText) this.toggler.firstChild.nodeValue = this.closeText;
			this.open = false;
		} else {
			this.container.style.display = 'block';
			if (this.changeText) this.toggler.firstChild.nodeValue = this.openText;
			this.open = true;
		}
	}
	
}

// and declare empty functions that will be overwritten if needed by conditionally included comments
function fixInlineLinks() {}
function showImageOverlay() {}
function handleModals() {}
function initSubmitButtons() {}
function expandSelect() {}
function contractSelect() {}
function blurThisObj() {}
