/**
 * Include script for Aerodynamics website.
 * 
 * @author R.J.T. de Vries <rdevries@thirdwave.nl>
 * @version 1.00, 03/09/2009
 * @access public
 * @package CMS
 */
 
//------------------------------------------------------------------------------
// 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='/inc/jscripts/ddMenus.js'></script>");
document.write("<script src='/inc/jscripts/ItemSlide.js'></script>");

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

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

/**
 * Instance of SlideInOut object.
 * @var object slideinout
 * @access global
 */
var itemslide;

/**
 * Initialize website. Called from <body> tag.
 *
 * Starts with calling the ddinit() function which initializes the dropdown
 * menus in the website.
 * 
 * @return 	void
 * @access	public
 */
function init() {
	var inps,					// collection of input elements.
			addedTo = [],	// temporary arrry.
			i;						// iterator.
			
	ddmenus = new ddMenus;
	ddmenus.init();
	
	itemslide = new ItemSlide;
	itemslide.init();
	
	// add events to the username and password input elements.
	inps = document.getElementsByTagName('input');
	for ( i = 0; i < inps.length; i++ ) {
		// if ( inps.getAttribute('defaulttext') ) {
		if ( inps[i].className.indexOf('deftxt') != -1 ) {
			addEvent(inps[i], 'focus', removeDefTxt);
			addEvent(inps[i], 'blur', restoreDefTxt);
			if ( !in_array(inps[i].form, addedTo) ) {
				addEvent(inps[i].form, 'submit', removeDefaultTexts);
				addedTo.push(inps[i].form);
			}
			if ( inps[i].getAttribute('defaultText') !== inps[i].value ) {
				inps[i].className = inps[i].className.replace(/ ?deftxt/, '');
			}
			if ( inps[i].getAttribute('value') !== inps[i].value ) {
				inps[i].className = inps[i].className.replace(/ ?deftxt/, '');
			}
		}
	}

	set_select_values();
} // init()

/**
 * 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.className.match(/deftxt/)) {
		eventSrc.value = '';
		eventSrc.className = eventSrc.className.replace(/ ?deftxt/, '');
	}
	setTimeout( function() { eventSrc.focus(); }, 100);
} // removeDefTxt()

/**
 * A function to remove all default texts from the defaultText fields when the
 * form is submitted.
 * 
 * @param e
 * @return
 */
function removeDefaultTexts(e) {
	var it,
			eventSrc,
			inps;
			
	e = e || window.event;
	eventSrc = getEventSrc(e);
	
	inps = document.getElementsByAttribute('defaultText', false, 'input', eventSrc);
	for ( it in inps ) {
		if ( inps[it].className.match(/ ?deftxt/) ) {
			inps[it].value = '';
		}
	}
} // removeDefaultTexts()

/**
 * Restore the default value for inputs to which this event was attached.
 * 
 * @param		object	[e]					event object for Mozilla based browsers.
 * @param		object	[eventSrc]	use as eventSrc if given.
 * @return	void
 */
function restoreDefTxt(e, eventSrc) {
	
	if ( !eventSrc ) {
		if ( !e ) e = window.event;
		eventSrc = getEventSrc(e);
	}
	
	if ( eventSrc.value == '' ) {
		if ( eventSrc.getAttribute('defaultText') !== undefined ) { 
			eventSrc.value = eventSrc.getAttribute('defaultText');
		} else {
			eventSrc.value = '';
		}
		if ( !/( |^)deftxt($| )/.test(eventSrc.className) ) {
			eventSrc.className = trim(eventSrc.className + ' deftxt');
		}
	}
} // restoreDefTxt()

window.onload = init;

/* end of include script */
