/**
 * Include script for javascript application layer.
 * 
 * @author R.J.T. de Vries <rdevries@thirdwave.nl>
 * @version 1.00, 01/18/2008
 * @access public
 */

//------------------------------------------------------------------------------
// PHASE I: INCLUDE ALL NECESSARY JAVASCRIPT FILES.
//------------------------------------------------------------------------------
document.write("<script src='/cms/jscripts/cms.event.js'></script>");
document.write("<script src='/cms/jscripts/cms.functions.js'></script>");
document.write("<script src='/cms/jscripts/cms.Timeout.js'></script>");
document.write("<script src='/cms/jscripts/cms.cookie.js'></script>");
document.write("<script src='/inc/jscripts/ddMenus.js'></script>");

//------------------------------------------------------------------------------
// PHASE II: INITIALIZATION FUNCTION, CALLED ON DOCUMENT LOAD EVENT
//------------------------------------------------------------------------------

/**
 * Instance of ddMenus object.
 * @var object ddmenus
 * @access global
 */
var ddmenus;

/**
 * Instance of TabManager object.
 * @var object tabManager
 * @access global
 */
var tabManager = null;

/**
 * Initialization function.
 *
 * @param		boolean		refresh		should the nav-list be refreshed? default to true.
 * @return 	void
 * @access	public
 */
function init() {
	var h1;

	ddmenus = new ddMenus;
	if ( !ddmenus.init() ) {
		alert('Dropdown menus kunnen niet worden geactiveerd');
	}

	if ( typeof(TabManager) != 'undefined' ) {
		tabManager = new TabManager;
		tabManager.init();
	}
	
	// attach an onclick event to the first h1 on the page.
	h1 = document.getElementsByTagName('h1');
	if ( h1.length ) {
		h1 = h1[0];
		addEvent(h1, 'click', function(e) { document.location = '/'; });
		h1.style.cursor = 'pointer';
	}

	// add events to the username and password input elements.
	inps = document.getElementsByTagName('input');
	for ( i = 0; i < inps.length; i++ ) {
		if ( inps[i].className.indexOf('deftxt') != -1 ) {
			inps[i].setAttribute('defaultText', inps[i].value);
			addEvent(inps[i], 'focus', removeDefTxt);
			addEvent(inps[i], 'blur', restoreDefTxt);
		}
	}

	selects = document.getElementsByTagName('select');
	for ( i = 0; i < selects.length; i++ ) {
		if ( selects[i].getAttribute('val') ) {
			selects[i].value = selects[i].getAttribute('val');
		}
	}
	
	setHeights();
} // init()

//------------------------------------------------------------------------------
// PHASE III: Some site specific functions and setting of the onload handler.
//------------------------------------------------------------------------------

window.onload = init;

/**
 * Website specific function to set heights of several containers.
 * 
 * @return	void
 */
function setHeights() {
	var height, topbar, imgbar, footer, rgt, cnt;
	
	height = getHeight(document.body);
	if ( (topbar = document.getElementByAttribute('className', 'topbar', 'div')) ) {
		height -= getHeight(topbar);
		height -= parseInt(topbar.style.marginTop) || 0;
		height -= parseInt(topbar.style.marginBottom) || 0;
		height -= parseInt(topbar.style.borderTop) || 0;
		height -= parseInt(topbar.style.borderBottom) || 0;
	}
	if ( (imgbar = document.getElementByAttribute('className', 'imgbar', 'div')) ) {
		height -= getHeight(imgbar);
		height -= parseInt(imgbar.style.marginTop) || 0;
		height -= parseInt(imgbar.style.marginBottom) || 0;
		height -= parseInt(imgbar.style.borderTop) || 0;
		height -= parseInt(imgbar.style.borderBottom) || 0;
	}
	if ( (footer = document.getElementByAttribute('className', 'footer', 'div')) ) {
		height -= getHeight(footer);
		height -= parseInt(footer.style.marginTop) || 0;
		height -= parseInt(footer.style.marginBottom) || 0;
		height -= parseInt(footer.style.borderTop) || 0;
		height -= parseInt(footer.style.borderBottom) || 0;
	}
	
	if ( (rgt = document.getElementById('rgt')) ) {
		rgt.style.minHeight = height + 'px';
	}
	if ( (cnt = document.getElementById('cnt')) ) {
		cnt.style.minHeight = height + 'px';
	}
} // setHeights()

/**
 * Remove the default text from inputs to which this event was attached.
 * 
 * @param		object	[e]	event object for Mozilla based browsers.
 * @return	void
 */
function removeDefTxt(e) {
	if ( !e ) e = window.event;
	var eventSrc = getEventSrc(e);
	if ( eventSrc.getAttribute('defaultText') == eventSrc.value ) {
		eventSrc.value = '';
		if ( eventSrc.getAttribute('ispass') == 'yes' ) {
			eventSrc = changeInputType(eventSrc, 'password');
		}
		eventSrc.className = eventSrc.className.replace(' deftxt', '');
	}
	setTimeout( function() { eventSrc.focus(); }, 100);
} // removeDefTxt()

/**
 * Restore the default value for inputs to which this event was attached.
 * 
 * @param		object	[e]	event object for Mozilla based browsers.
 * @return	void
 */
function restoreDefTxt(e) {
	if ( !e ) e = window.event;
	var eventSrc = getEventSrc(e);
	if ( eventSrc.value == '' ) {
		eventSrc.value = eventSrc.getAttribute('defaultText');
		if ( eventSrc.getAttribute('ispass') == 'yes' ) {
			changeInputType(eventSrc, 'text');
		}
		eventSrc.className += ' deftxt';
	}
} // restoreDefTxt()

/**
 * Change the type of given input in a way that even Internet Explorer can
 * understand.
 * 
 * @param		object		inp				input element.
 * @param		string		newtype		new type to set.
 * @return	void
 */
function changeInputType(inp, newtype) {
	var str, input;
	try {
		inp.setAttribute('type', newtype);
	} catch(e) {
		str = "<input type='" + newtype + "' name='" + inp.name + "' value='" + inp.value + "' class='" + inp.className + "'>";
		input = document.createElement(str);
		input.setAttribute('ispass', 'yes');
		input.setAttribute('defaultText', inp.getAttribute('defaultText'));
		inp.parentNode.replaceChild(input, inp);
		addEvent(input, 'focus', removeDefTxt);
		addEvent(input, 'blur', restoreDefTxt);
		inp = input;
	}
	return inp;
} // changeInputType()
