/**
 * Include script for Aerodynamics website.
 * 
 * @author R.J.T. de Vries <rdevries@thirdwave.nl>
 * @version 1.00, 04/26/2007
 * @access public
 * @package Aerodynamics
 */
 
//------------------------------------------------------------------------------
// 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/ChangeImage.js'></script>");
document.write("<script src='/inc/jscripts/ImageSwitcher.js'></script>");
document.write("<script src='/inc/jscripts/ddMenus.js'></script>");
document.write("<script src='/inc/jscripts/googlemaps.js'></script>");
document.write("<script src='/inc/jscripts/planRoute.js'></script>");

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

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

/**
 * Instance of Photoshop object.
 * @var object photoalbum
 * @access global
 */
var photoalbum = false;

/**
 * Instance of Photoalbum object.
 * @var object changeImage
 * @access global
 */
var changeImage = false;

/**
 * Instance of PlanRoute object.
 * @var object planRoute
 * @access global
 */
var planRoute = false;

/**
 * Instance of the ImageSwitcher object, used for the switching images in the
 * header 'animal' menu of the website.
 * @var object imageSwitcher
 * @access global
 */
var imageSwitcher = false;

/**
 * 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() {
	ddmenus = new ddMenus;
	if ( !ddmenus.init() ) {
		alert('Dropdown menus kunnen niet worden geactiveerd');
	}
	
	set_select_values();
	
	// 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);
			if ( inps[i].type == 'password' ) {
				//inps[i].setAttribute('ispass', 'yes');
				//changeInputType(inps[i], 'text');
			}
		}
	}
	
	// if map div exists than call load function to show map
	if ( document.getElementById('map') ) {
		load();
	}

	changeImage = new ChangeImage;
	changeImage.init();
	
	planRoute = new PlanRoute;
	planRoute.init();
	
	imageSwitcher = new ImageSwitcher;
	imageSwitcher.init();	
} // init()

window.onload = 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.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()

/* end of include script */