// Create namespace
if ( ! window.nl.hq ) {
	window.nl.hq = {} ;
} ;


/* HQ Class */
nl.hq.HQ = function() {
	this.initialize() ;
} ;
nl.hq.HQ.prototype = {
	initialize: function(){
		new nl.hq.MainMenu() ;
		
		if( nl.xd.dom.DOM.get( 'vacatureresults' ) ){
			new nl.hq.VacatureResults() ;
		}
		
		if( nl.xd.dom.DOM.get( 'vacatureform' ) ){
			new nl.hq.VacatureForm() ;
		}
		
		if( nl.xd.dom.DOM.get( 'snelzoeken' ) ){
			new nl.hq.SnelzoekForm() ;
		}
		
		if( nl.xd.dom.DOM.get( 'vacaturereageerform' ) ){
			new nl.hq.VacatureReageerForm() ;
		}
		
		if( nl.xd.dom.DOM.get( 'vacatureemailform' ) ){
			new nl.hq.VacatureEmailForm() ;
		}
		
		if( nl.xd.dom.DOM.get( 'belmijterug' ) ){
			new nl.hq.BelMijTerugForm() ;
		}
		
		this.cleanEmptyImageHolders();
		
	} ,
	cleanEmptyImageHolders: function(){
		// clean up empty imageholders for ie6
		var items = nl.xd.dom.DOM.getByClassName( 'imageholder' , 'div' , document );
		for( var i=0;i<items.length;i++ ){
			if( nl.xd.util.StringFunctions.Trim( items[i].innerHTML )=="" ){
				nl.xd.dom.DOM.addClass( items[i] , 'displaynone' ) ;
			} else {
				// fix IE a href problem
				if( items[i].innerHTML.indexOf( 'href' ) > -1 ){
					new nl.hq.RollOverItem( items[i] ) ;
				}
			}
		}
		
	}
} ;

/* MainMenu Class */
nl.hq.MainMenu = function(){
	this.initialize() ;	
} ;
nl.hq.MainMenu.prototype = {
	initialize: function(){
		this.items = new Array() ;
		this.submenus = new Array() ;
		this.buildItems() ;
		this.currentRollOverSubMenu = null ;
	} ,
	buildItems: function(){
		var menu = nl.xd.dom.DOM.get( 'topmenu' ) ;
		var items = nl.xd.dom.DOM.getByClassName( 'menuitem' , 'div' , menu ) ;
		var self = this ;
		for( var i=0; i<items.length; i++ ){
			var e = items[ i ] ;
			var newitem = new nl.hq.MenuItem( e, self, i ) ;
			this.items.push( newitem ) ;
		}
		var submenu = nl.xd.dom.DOM.get( 'submenu' ) ;
		var submenus = nl.xd.dom.DOM.getByClassName( 'submenuholder', 'div', submenu ) ;
		this.submenus = new Array() ;
		for( var i=0; i<submenus.length; i++ ){
			this.submenus[ i ] = new nl.hq.SubMenu( submenus[ i ], self, i ) ;
			if( nl.xd.dom.DOM.hasClass( submenus[ i ], 'selected' ) ){
				this.submenus[ i ].show() ;
				this.currentSelectedSubMenu = this.submenus[ i ] ;
			} else {
				this.submenus[ i ].hide() ;	
			}
		}
	} ,
	onRollOverItem: function( index ){
		var self = this ;
		this.stopDelayedHide() ;
		if( this.currentRollOverSubMenu != null ){
			this.currentRollOverSubMenu.hide() ;
		}
		if( this.currentRollOverItem != null ){
			this.currentRollOverItem.rollOverDeselect() ;
		}
		if( this.items[ index ] == this.currentSelectedItem ){
			this.currentRollOverItem = null ;
		} else {
			this.currentRollOverItem = this.items[ index ] ;
			this.currentRollOverItem.rollOverSelect() ;
		}
		this.currentRollOverSubMenu = this.submenus[ index ] ;
		if( this.currentSelectedSubMenu ){
			this.currentSelectedSubMenu.hide() ;	
		}
		if( this.currentRollOverSubMenu ){
			this.currentRollOverSubMenu.show() ;
		}
	} ,
	onRollOutItem: function( index ){
		this.startDelayedHide() ;
	} ,
	startDelayedHide: function(){
		var self = this ;
		this.timer = setTimeout( function(){ self.hideSubMenu(); } , 350 ) ;
	} ,
	stopDelayedHide: function(){
		clearTimeout( this.timer ) ;
	} ,
	onClickItem: function( index ){

	} ,
	hideSubMenu: function(){
		if( this.currentRollOverItem!= null ){
			this.currentRollOverItem.rollOverDeselect() ;
			this.currentRollOverSubMenu.hide() ;
		}
		if( this.currentSelectedSubMenu ){
			this.currentSelectedSubMenu.show() ;
		}
	}
	
} ;

/* MainMenuItem Class */
nl.hq.MenuItem = function( element, listener, index ){
	this.element = element ;
	this.listener = listener ;
	this.index = index ;
	this.initialize() ;
} ;
nl.hq.MenuItem.prototype = {
	initialize: function(){
		this.setEvents() ;
	} ,
	setEvents: function(){
		var self = this ;
		nl.xd.event.Event.addListener( this.element , 'click' , function() { self.onClick();  } ) ;
		nl.xd.event.Event.addListener( this.element , 'mouseover' , function() { self.onRollOver(); } ) ;
		nl.xd.event.Event.addListener( this.element , 'mouseout' , function() { self.onRollOut();  } ) ;
	} ,
	onRollOver: function(){
		// nl.xd.dom.DOM.addClass( this.element, 'rollover' ) ;
		this.listener.onRollOverItem( this.index ) ;
	} ,
	onRollOut: function(){
		// nl.xd.dom.DOM.removeClass( this.element, 'rollover' ) ;
		this.listener.onRollOutItem( this.index ) ;
	} ,
	onClick: function(){
		this.listener.onClickItem( this.index ) ;
	} ,
	rollOverSelect: function(){
		nl.xd.dom.DOM.addClass( this.element, 'rollover' ) ;
	} ,
	rollOverDeselect: function(){
		nl.xd.dom.DOM.removeClass( this.element, 'rollover' ) ;
	}
} ;

/* SubMenu Class */
nl.hq.SubMenu = function( element, listener, index ){
	this.element = element ;
	this.listener = listener ;
	this.active = false ;
	this.index = index ;
	this.items = new Array() ;
	this.initialize() ;
} ;
nl.hq.SubMenu.prototype = {
	initialize: function(){
		this.buildItems() ;	
	} ,
	buildItems: function(){
		var menu = nl.xd.dom.DOM.get( 'submenu' ) ;
		var items = nl.xd.dom.DOM.getByClassName( 'menuitem' , 'td' , menu ) ;
		var self = this ;
		for( var i=0; i<items.length; i++ ){
			var e = items[ i ] ;
			var newitem = new nl.hq.MenuItem( e, self, i ) ;
			this.items.push( newitem ) ;
		}
	} ,
	hide: function(){
		this.active = false ;
		nl.xd.dom.DOM.addClass( this.element, 'displaynone' ) ;
	} ,
	show: function(){
		this.active = true ;
		nl.xd.dom.DOM.removeClass( this.element, 'displaynone' ) ;
	} ,
	onRollOverItem: function( index ){
		if( this.active ){
			this.listener.stopDelayedHide() ;
		}
	} ,
	onRollOutItem: function( index ){
		if( this.active ){
			this.listener.startDelayedHide() ;
		}
	} ,
	onClickItem: function( index ){
	}
} ;

/* VacatureForm */
nl.hq.VacatureForm = function(){
	this.initialize() ;	
}
nl.hq.VacatureForm.prototype = {
	initialize: function(){
		this.setEvents() ;	
		this.initDropDownBoxes() ;
	} ,
	setEvents: function(){
		var self = this ;
		var submitbutton = nl.xd.dom.DOM.get( 'vacatureform_submit' ) ;
		nl.xd.event.Event.addListener( submitbutton, 'click', function(){ self.onClickSubmit() } ) ;
		var resetbutton = nl.xd.dom.DOM.get( 'vacatureform_reset' ) ;
		nl.xd.event.Event.addListener( resetbutton, 'click', function(){ self.onClickReset() } ) ;
	} ,
	onClickSubmit: function(){
		var vacatureform = nl.xd.dom.DOM.get( 'vacatureform' ) ;
		this.cleanUp() ;
		vacatureform.submit() ;
	} ,
	cleanUp: function(){
		var vacatureform = nl.xd.dom.DOM.get( 'vacatureform' ) ;
		var unwanted = nl.xd.dom.DOM.getByClassName( 'remove' , 'div' , vacatureform ) ;
		for( var i=0; i<unwanted.length; i++ ){
			unwanted[i].parentNode.removeChild( unwanted[ i ] ) ;	
		}
	} ,
	onClickReset: function(){
		this.resetForm() ;
	} ,
	resetForm: function(){
		for( var i=0; i<this.dropDownBox_arr.length; i++ ){
			this.dropDownBox_arr[ i ].deselectAll() ;
		}
	} ,
	initDropDownBoxes: function(){
		this.dropDownBox_arr = new Array() ;
		var self = this ;
		var boxes = nl.xd.dom.DOM.getByClassName( 'dropdownbox', 'div', document ) ;
		for( var i=0; i<boxes.length; i++ ){
			this.dropDownBox_arr.push( new nl.hq.DropDownBox( boxes[ i ] , i , self ) );
		}
	}
}

/* VacatureResults */
nl.hq.VacatureResults = function(){
	this.initialize() ;	
}
nl.hq.VacatureResults.prototype = {
	initialize: function(){
		this.buildItems();		
	} ,
	buildItems: function(){
		var vacatureresults = nl.xd.dom.DOM.get( 'vacatureresults' ) ;
		var items = nl.xd.dom.DOM.getByClassName( 'vacature_result' , 'div' , vacatureresults ) ; 
		for( var i=0; i<items.length; i++ ){
			new nl.hq.RollOverItem( items[ i ] ) ;
		}
	}
}

/* RollOverItem */
nl.hq.RollOverItem = function( element ){
	this.element = element ;
	this.initialize() ;
}
nl.hq.RollOverItem.prototype = {
	initialize: function(){
		var self = this ;
		nl.xd.event.Event.addListener( this.element , 'click' , function() { self.onClick();  } ) ;
		nl.xd.event.Event.addListener( this.element , 'mouseover' , function() { self.onRollOver(); } ) ;
		nl.xd.event.Event.addListener( this.element , 'mouseout' , function() { self.onRollOut();  } ) ;
	} ,
	onClick: function(){
		// find a tags
		var a = nl.xd.dom.DOM.getByTagName( 'a' , this.element ) ; 
		if( a ){
			var href = nl.xd.dom.DOM.getAttribute( a[0] , 'href' ) ;
			var target = ( nl.xd.dom.DOM.getAttribute( a[0] , 'target' ) ).toLowerCase() ;
			if( target==null || target=="" ){
				window.location = href ;
			} else if( target=='_blank' ) {
				// open new window
				 window.open( href );
			}
		}
	} ,
	onRollOver: function(){
		nl.xd.dom.DOM.addClass( this.element , 'rollover' )	;		
	} ,
	onRollOut: function(){
		nl.xd.dom.DOM.removeClass( this.element , 'rollover' )	;
	}
}

/* SnelzoekForm */
nl.hq.SnelzoekForm = function(){
	this.initialize() ;
}
nl.hq.SnelzoekForm.prototype = {
	initialize: function(){
		this.setEvents() ;	
	} ,
	setEvents: function(){
		var self = this ;
		var submitbutton = nl.xd.dom.DOM.get( "snelzoekensubmit" ) ;
		nl.xd.event.Event.addListener( submitbutton , 'click' , function() { self.onClickSubmit();  } ) ;
	} ,
	onClickSubmit: function(){
		var snelzoeken = nl.xd.dom.DOM.get( 'snelzoeken' ) ;
		snelzoeken.submit() ;
	}
}

/* VacatureReageerForm */
nl.hq.VacatureReageerForm = function(){
	this.initialize() ;	
}
nl.hq.VacatureReageerForm.prototype = {
	initialize: function(){
		this.setEvents() ;	
	} ,
	setEvents: function(){
		var submitvacaturereactie = nl.xd.dom.DOM.get( 'submitvacaturereactie' ) ;
		var self = this ;
		nl.xd.event.Event.addListener( submitvacaturereactie , 'click' , function() { self.onClickSubmit();  } ) ;
	} ,
	onClickSubmit: function(){
		var vacaturereageerform = nl.xd.dom.DOM.get( 'vacaturereageerform' ) ;
		var check = nl.xd.form.Form.check( vacaturereageerform ) ;
		if ( check ) {
			vacaturereageerform.submit() ;
		}
	}
}

/* VacatureEmailForm */
nl.hq.VacatureEmailForm = function(){
	this.initialize() ;	
}
nl.hq.VacatureEmailForm.prototype = {
	initialize: function(){
		this.setEvents() ;	
	} ,
	setEvents: function(){
		var submitvacatureemail = nl.xd.dom.DOM.get( 'submitvacatureemail' ) ;
		var self = this ;
		nl.xd.event.Event.addListener( submitvacatureemail , 'click' , function() { self.onClickSubmit();  } ) ;
	} ,
	onClickSubmit: function(){
		var vacatureemailform = nl.xd.dom.DOM.get( 'vacatureemailform' ) ;
		var check = nl.xd.form.Form.check( vacatureemailform ) ;
		if ( check ) {
			vacatureemailform.submit() ;
		} ;
	}
}

/* nl.atros.BelMijTerugForm */
nl.hq.BelMijTerugForm = function(){
	this.initialize() ;	
}
nl.hq.BelMijTerugForm.prototype = {
	initialize: function(){
		this.setEvents() ;
	} ,
	setEvents: function(){
		var belmijterugsubmit = nl.xd.dom.DOM.get( 'belmijterugsubmit' ) ;
		var self = this ;
		nl.xd.event.Event.addListener( belmijterugsubmit , 'click' , function() { self.onClickSubmit();  } ) ;
	} ,
	onClickSubmit: function(){
		var belmijterug = nl.xd.dom.DOM.get( 'belmijterug' ) ;
		var check = nl.xd.form.Form.check( belmijterug ) ;
		if ( check ) {
			belmijterug.submit() ;
		} ;
	}
}


nl.xd.event.Event.addListener( window , 'load' , function() { new nl.hq.HQ() ; } ) ;
