/**
 * Include script for ThirdWave Website.
 * 
 * @author R.J.T. de Vries <rdevries@thirdwave.nl>
 * @version 1.00, 05/03/2007
 * @access public
 * @package THIRDWAVE
 */

//------------------------------------------------------------------------------
// PHASE II: INITIALIZATION FUNCTION, CALLED ON DOCUMENT LOAD EVENT
//------------------------------------------------------------------------------

/**
 * ServiceMenuPD object, which takes care of the pulldown and collaps effects for the servicemenu.
 * @var object serviceMenuPD
 * @access global
 */
var serviceMenuPD = null;

/**
 * PortfolioBanners object, which takes care of the sliding banner effects and the info pop-up at \
 * the top of the website.
 * @var object portfolioBanners
 * @access global
 */
var portfolioBanners = null;

/**
 * PortfolioOvv object, which takes care of showing a popover with a possibility
 * to browse through the screenshots of the various projects in the portfolio.
 * @var object portfolioOvv
 * @access global
 */
var portfolioOvv = null;

/**
 * Slider for the random items block on the homepage.
 * @var object itemslide
 * @access global
 */
var bannerslide = null;

/**
 * Slider for the random items block on the homepage.
 * @var object messageslide
 * @access global
 */
var messageslide = null;

/**
 * Instance of PlanRoute object.
 * @var object planRoute
 * @access global
 */
var planRoute = false;

/**
 * Initialize website.
 *
 * Initializes the dropdown menu on the website (ddmenus.init()).
 *
 * After that, the current document is checked for forms in which special
 * events need to be attached to form elements.
 * 
 * @return 	void
 * @access	global
 */
function init() {
	var lnks,		// anchors with rel='newwin'
			i;			// iterator.
	
	serviceMenuPD = new ServiceMenuPD;
	serviceMenuPD.init();
	
	portfolioBanners = new PortfolioBanners;
	portfolioBanners.init();
	
	messageslide = new itemSlide;
	messageslide.init('headlines');

	bannerslide = new itemSlide;
	bannerslide.init('banner');
	
	// if map div exists than call load function to show map
	if ( document.getElementById('map') ) {
		load();
	}
	
	// Add a 'target=' to all <a> elements that have 'rel="newwin"' as attribute.
	var lnks = document.getElementsByAttribute('rel', 'newwin', 'a');
	for ( i = 0; i < lnks.length; i++ ) {
		lnks[i].setAttribute('target', '_blank');
		lnks[i].removeAttribute('rel');
	}
	
	// Turn off background image caching to get IE6 to stop blinking on hovers.
	if ( isIE() ) {
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {}
	}
	

	// add events to the username and password input elements.
	var inps = document.getElementsByTagName('input'),
			addedTo = [];
			
	for ( i = 0; i < inps.length; i++ ) {
		// if ( inps.getAttribute('defaulttext') ) {
		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 ( !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/, '');
			}
		}
	}
	
	planRoute = new PlanRoute;
	planRoute.init();
	
	new TwitterAjax('inc/getNewTweets.php', 2).getNew();
	
	document.getElementById('footerbg').className += ' loaded';
} // 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()

sfHover = function() {
	var sfEls = document.getElementById('ddmenu').getElementsByTagName('LI');
	for ( var i = 0; i < sfEls.length; i++ ) {
		sfEls[i].onmouseover=function() {
			this.className+=" hover";
		}
		sfEls[i].onmouseout=function() {
			this.className = this.className.replace(new RegExp(" hover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent('onload', sfHover);

window.onload = init;


/* end of include script */

