/**
 * Include script for javascript application layer.
 * 
 * @author R.J.T. de Vries <rdevries@thirdwave.nl>
 * @version 1.00, 09/04/2006
 * @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='/inc/jscripts/checkFields.js'></script>");
document.write("<script src='/inc/jscripts/googlemaps.js'></script>");
document.write("<script src='/inc/jscripts/Tabs.js'></script>");
document.write("<script src='/inc/jscripts/ChangeImage.js'></script>");

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

/**
 * Instance of the Tabs object, used for switching tabs.
 * @var object tabs
 * @access global
 */
var tabs = null;

/**
 * Instance of ChangeImage object.
 * @var object changeimage
 * @access global
 */
var changeimage;

/**
 * Initialize application.
 *
 * Called from <body> tag.
 * 
 * @author R.J.T. de Vries <rdevries@thirdwave.nl>
 * @param		boolean		refresh		should the nav-list be refreshed? default to true.
 * @return 	void
 * @access	public
 */
function init() {
	// if map div exists than call load function to show map
	if ( document.getElementById('map') ) {
		load();
	}
	
	if ( isIE() ) {
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {}
	}
		
	// Initialize tabs.
	tabs = new Tabs;
	tabs.init();
	
	changeimage = new ChangeImage;
	changeimage.init();
		
	// 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');
			}
		}
	}
} // init()

/**
 * Function to use for bookmarking website.
 * 
 * @param 	string	url
 * @param		string	title
 * @param		string	lang		language to show error in ('nl' or 'en').
 * @return 	void
 */
function bookmark(url, title, lang) {
	if ( !url ) url = document.location.href;
	if ( !title ) title = document.title;
	if ( !lang ) lang = 'nl';
	if ( document.all ) {
		window.external.AddFavorite(url, title);
	} else {
		if ( lang == 'nl' ) {
			msg = "Gebruik CTRL-D om deze pagina aan je favorieten toe te voegen.";
		} else {
			msg = "Please use CTRL-D keyboard shortcut to bookmark this page.";
		}
		alert(msg);
	}
} // bookmark()

window.onload = init;
	
// if map div exists than call unload function to unload? map
if ( document.getElementById('map') ) {
	window.onunload = 'GUnload';
}


/**
 * 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 */

