/**
 * file default.js
 *
 * contains javascript for NIMA website
 *
 * @require MooTools version 1.2.1
 */


window.addEvents({
	'domready': function() {
		closeIE6Warning();
		initLightbox();
		initMenu();
		initSearchForm();
		initShowForm();
		initShowNNG();
		initTableZebra();
		initYoutubeParam();
		initLightboxCloseButton();
		initCalendars();
		initTargetBlank();
	},
	'load': function() {
		initNewsTicker();
		initHomeTabs();
		makeEqualHeight($$('ul.intro_thema_quicklinks'));
		initCarrousel();
		initFaq();
		closeIE6Warning();
		postFormOnChange();		
	}
});



/**
 * Toggles the loading screen after some time. This is for operations
 * taking some more time in case the external database (SuperOffice) is
 * not responding in time for example.
 * 
 * @author Stephan van de Haar <stephan.vande.haar@efocus.nl>
 * @since 1.0, 8-12-2009
 */
/*
function showLoader() {
	var t = setTimeout("document.getElementById('loader').style.display = 'block'", 3000);
}
*/

/*
 * initLightbox
 * Initializes the Shadowbox lightbox (shadowbox.js and shadowbox-mootools.js adapter needed!).
 * 
 * @author CSD (clientsidedevelopers[AT]efocus.nl)
 * @uses <a href="/siteimg/temp/visual.jpg" rel="shadowbox">View image in lightbox</a>
 */
function initLightbox() {
	var options = {
		overlayColor: '#000000',
		overlayOpacity: 0.8
//		,modal: 'true'
	};
	
	Shadowbox.init(options);

}




/**
 * makeEqualHeight
 *
 * makes multiple different elements all the same height,
 * assuming the height of the highest element
 *
 * @param arElements, Array of DOM elements to compare fix height
 *
 * @author Klaas Dieleman <klaas{AT}efocus.nl>
 *
 * @return h, height of highest element in px
 */
function makeEqualHeight(arElements) {
	var h = 0;
	
	for(var i = 0 ; i < arElements.length ; i++) {
		if(arElements[i].getSize().y > h) h = arElements[i].getSize().y;
	}
	
	arElements.each(function(item) {
		var ch = 0;
		var ah = item.getStyle('padding-top').toInt();
		ah += item.getStyle('padding-bottom').toInt();
		ah += item.getStyle('border-top-width').toInt();
		ah += item.getStyle('border-bottom-width').toInt();
		ch = h - ah;
		item.setStyle('height', ch);
	});
	
	return h;
}


/**
 * initNewsTicker()
 *
 * scrolls newsticker in Home Visual
 *
 * @author Klaas Dieleman <klaas{AT}efocus.nl>
 * @return void
 */
function initNewsTicker() {
	if(!$('homevisual')) return false;
	
	var tickerspeed = 30;
	var newsList = new Array;
	var tweens = new Array;
	
	newsList[0] = $('homevisual').getElement('ul.ticker');
	var tickerWidth = newsList[0].getSize().x;
	if(tickerWidth <= $('homevisual').getSize().x) return false;
	
	newsList[1] = newsList[0].clone();
	var tweenDuration = tickerWidth * tickerspeed;
	
	tweens[0] = new Fx.Tween(newsList[0], {
		'duration':		tweenDuration,
		'transition':	'linear'
	});
	
	tweens[1] = new Fx.Tween(newsList[1], {
		'duration':		tweenDuration,
		'transition':	'linear'
	});
	
	var animate = function(index) {
		var index2;
		index == 0 ? index2 = 1 : index2 = 0

		newsList.each(function(item) { item.setStyle('margin-left', 0) });
		newsList[index2].inject(newsList[index], 'after');
		
		tweens[index].start('margin-left', -tickerWidth).chain(function() {
			animate(index2)
		});
	};
	
	animate(0);
	
}


/**
 * initHomeTabs()
 *
 * enables tab functionality in Themes introduction on homepage
 *
 * @author Klaas Dieleman <klaas{AT}efocus.nl>
 * @return void
 */
function initHomeTabs() {
	if(!document.getElement('div.intro_themas')) return false
	
	$$('div.intro_themas div.tabcontent').addClass('active');
	makeEqualHeight($$('div.intro_themas div.tabcontent'));
	$$('div.intro_themas div.tabcontent').removeClass('active');
	
	$$('div.intro_themas div.thema').each(function(thema) {
		var tablinks = thema.getElements('ul.tabs li');
		var tabcontents = thema.getElements('div.tabcontent');
		
		tablinks[0].addClass('active');
		tabcontents[0].addClass('active');
		
		tablinks.each(function(tablink, index) {
			tablink.getElement('a').set('href', 'javascript: void(0)');
			tablink.addEvent('click', function(event) {
				event.stop();
				tablink.getElement('a').blur();
				tablinks.removeClass('active');
				tablink.addClass('active');
				tabcontents.removeClass('active');
				tabcontents[index].addClass('active');
			});
		});
	});
}




function initYoutubeParam(){
      if (!$$('embed')) return false;
            var blnFirstEmbed = false;
            var arrUserContents = $$("div.usercontent");
            var elPlaceHolder = null;
            var elEmbed = null;
            
            arrUserContents.each(function(elDiv, index){
                  if(elDiv.getElement('embed') && blnFirstEmbed == false){
                        elPlaceHolder = elDiv.getElement('p');
                        elEmbed = elDiv.getElement('embed');
                        blnFirstEmbed = true

                        var elPClone = $(elPlaceHolder).clone();
                        var elEmbed = elPClone.getElement('embed');
                        elEmbed.setAttribute("wmode", "opaque");
                        $(elPlaceHolder).dispose();
                        elDiv.adopt(elPClone);
                  }
       })
}



/**
 * initMenu()
 *
 * initializes menu
 *
 * @author Mirjam <mirjam{AT}efocus.nl>
 * @return void
 */
function initMenu() {
	
	if($$('ul.mainnav ul').length == 0) return false;
	
	var mainNav = $$('div.nav')[0];
	var allNavItems = $$('ul.mainnav li');
	var orgH = mainNav.getSize().y;
	var newH = mainNav.getScrollSize().y + 20;
	var navTimeOut;
		
	for (var i = 0; i < allNavItems.length; i++) {
		
		allNavItems[i].getElements('ul').setStyle('display', 'none');
		
		allNavItems[i].addEvent('mouseenter', function(){
			this.getParent().getChildren('li').each(function(item) {
				if (item.getElements('ul')) {
					item.getElements('ul').setStyle('display', 'none');
					item.getElements('li').removeClass('hover');
				}
			});
			
			if (this.getElement('ul')) {
				this.getElement('ul').setStyle('display', 'block');
			}
			
			if (this.getParent('ul').hasClass('mainnav') && this.getElements('ul').length > 0) {
				navTimeOut = $clear(navTimeOut); 
				mainNav.tween('height', newH);
			}
			
			this.getParent().getChildren('li').each(function(item) {
				item.removeClass('hover');
			});
			this.addClass('hover');
			
		});	
		
	}
	
	mainNav.addEvent('mouseleave', function(){
			navTimeOut = hideMenu.delay(500); 
	});
	
	function hideMenu() {
		navTimeOut = $clear(navTimeOut); 
		allNavItems.removeClass('hover');
		mainNav.tween('height', orgH);
	}

}


/**
 * initCarrousel()
 *
 * enables horizontal scrolling on Carrousel
 *
 * @author Klaas Dieleman <klaas{AT}efocus.nl>
 * @return void
 */
function initCarrousel() {
	$$('div.carrousel').each(function(carrousel) {
		var h = makeEqualHeight(carrousel.getElements('div.container')); // make all items equal height
		
		carrousel.getElement('a.prevbutton').setStyle('margin-top', h / 2 - 8);
		carrousel.getElement('a.nextbutton').setStyle('margin-top', h / 2 - 8);
		
		var w = carrousel.getElement('div.innercarrousel>ul>li').getSize().x;
		w += carrousel.getElement('div.innercarrousel>ul>li').getStyle('margin-right').toInt();
		var pos = 0;
		
		if(pos == 0){carrousel.getElement('a.prevbutton').addClass('inactive')}
		
		carrousel.getElement('a.prevbutton').addEvent('click', function(event) {
			carrousel.getElement('a.nextbutton').removeClass('inactive')
			//event.stop();
			carrousel.getElement('a.prevbutton').blur();
			if(pos >= 0) return;
			pos++;
			carrousel.getElement('div.innercarrousel>ul').tween('margin-left', pos*w);
			if(pos == 0){carrousel.getElement('a.prevbutton').addClass('inactive')}
		});
		
		carrousel.getElement('a.nextbutton').addEvent('click', function(event) {
			carrousel.getElement('a.prevbutton').removeClass('inactive');
			event.stop();
			carrousel.getElement('a.nextbutton').blur();
			if(pos < 4 - carrousel.getElements('div.innercarrousel>ul>li').length) return;
			pos--;
			carrousel.getElement('div.innercarrousel>ul').tween('margin-left', pos*w);
			
			if(pos == 3 - carrousel.getElements('div.innercarrousel>ul>li').length){
				carrousel.getElement('a.nextbutton').addClass('inactive')
			}
		});
	});
}



/**
 * initFaq
 *
 * puts Specifications list in an accordion
 *
 * @author Klaas Dieleman <klaas{AT}efocus.nl>
 * @editor Phi Son Do <phison.do{AT}efocus.nl>
 * @return void
 */

function initFaq() {
	if(!document.getElement('ul.faq')) return false;
	
	var specsAccordion = new Accordion('ul.faq>li>h3', 'ul.faq div.faq_answer', {
		onActive: function(toggler){
			toggler.getParent().addClass('open');
		},
		onBackground: function(toggler){
			toggler.getParent().removeClass('open');
		},
		alwaysHide: true,
		show: -1
	});	
}



/**
 * initSearchForm
 *
 * Submit search, show/hide search hint, add/remove search hint class
 *
 * @author Gijs Oliemans <gijs.oliemans{AT}efocus.nl>
 * @editor Phi Son Do <phison.do{AT}efocus.nl>
 * @return void
 */
 
function initSearchForm() {
	if (!document.getElement('.zoeken')) return;
	
	var arrFormInput = $$('.searchkeyword');	
	var arrSearchHint = $$('.search_hint');
	
	arrFormInput.each(function (elFormInput){
		arrSearchHint.each(function (searchHint){
			searchHint = searchHint.innerHTML;

			elFormInput.addEvent('blur', function (){
				if(elFormInput.value.trim() == '') {
					elFormInput.value = searchHint;
					elFormInput.removeClass('focus');
				}
			});
		
			elFormInput.addEvent('focus', function (){
				if(elFormInput.value.trim() == searchHint.trim()) {
					elFormInput.value = '';
					elFormInput.addClass('focus');
				}
			});
			
			if(elFormInput.value.trim() == ''){
				elFormInput.value = searchHint;
			}else if(elFormInput.value != searchHint){
				elFormInput.addClass('focus');
			}	
		});
	});
	
}


/**
 * initShowForm
 *
 * show form
 *
 * @author Bas van der Togt <bas.vander.togt{AT}efocus.nl>
 * @return void
 */
 
function initShowForm() {
	
	if (!document.getElement('a.reageer')) return;

	var button = $$('a.reageer');
	var form = $('reageerform');
	
	if(window.location.hash=='#form') {
		button[0].setStyles({display:'none'});
		form.setStyles({display:'block'});
	}
			
	button[0].addEvent('click', 
		function(event){
			event.stop();
			button[0].setStyles({display:'none'});
			form.setStyles({display:'block'});
		}
	)
	
}


/**
 * initShowNNG
 *
 * show Nog niet geregistreerd links
 *
 * @author Bas van der Togt <bas.vander.togt{AT}efocus.nl>
 * @return void
 */
 
function initShowNNG() {
	
	if (!document.getElement('a.nng1')) return;

	var button1 = $$('a.nng1');
	var ul1 = $('nng1');
	
	button1[0].addEvent('click', 
		function(event){
			event.stop();
			ul1.setStyles({display:'block'});
		}
	)
	
	var button2 = $$('a.nng2');
	var ul2 = $('nng2');
			
	button2[0].addEvent('click', 
		function(event){
			event.stop();
			ul2.setStyles({display:'block'});
		}
	)
	
}



/*
 * initTableZebraEffect
 * 
 * 
 * @author CSD (phison.do[AT]efocus.nl)
 */
function initTableZebra() {
	if(!document.getElement('table')) return false

	var arrTableRow = $$('table tr');	
	var arrTableRowOdd = $$('table tr:odd');
	
	arrTableRowOdd.each(function (elTableRowOdd){
		elTableRowOdd.addClass('odd');		
	})		
}


/*
 * closeIE6Warning
 * 
 * checks for Ie6 browser then injects warning
 *
 * @author Rou-hun Fan (lowen[AT]efocus.nl)
 */
 
function closeIE6Warning() {
	var closeIE6WarningLink = $(document.body).getElement('a.close');
	
	if(!closeIE6WarningLink) {
		return false;
	}
	
	closeIE6WarningLink.addEvent('click', function(event){
		event.stop();
		$(document.body).getElement('div.ie6warning').destroy();
	});	
}


/*
 * initLightboxCloseButton
 * 
 * Makes sure the close-button is shown when opening the lightbox
 *
 * @author Gijs Oliemans (gijs.oliemans[AT]efocus.nl)
 */
function initLightboxCloseButton(){
	if(!document.getElement('div.shadowboxwrapper')) return;
	window.parent.showLightboxCloseButton();
}

/*
 * showLightboxCloseButton
 * 
 * Show the close-button (called by function initLightboxCloseButton())
 *
 * @author Gijs Oliemans (gijs.oliemans[AT]efocus.nl)
 */
function showLightboxCloseButton(){
	if(!$('shadowbox_nav_close')) return;	
	$('shadowbox_nav_close').setStyle('display', 'block');
}

/*
 * hideLightboxCloseButton
 * 
 * Hide the close-button when submitting form (called by function submitLogin())
 *
 * @author Gijs Oliemans (gijs.oliemans[AT]efocus.nl)
 */
function hideLightboxCloseButton() {
	if(!$('shadowbox_nav_close')) return;
	$('shadowbox_nav_close').setStyle('display', 'none');
}

/*
 * submitLogin
 * 
 * Hide the close-button when submitting form (calls hideLightboxCloseButton function)
 *
 * @author Gijs Oliemans (gijs.oliemans[AT]efocus.nl)
 */
function submitLogin() {
	window.parent.hideLightboxCloseButton();
	$('formulier').submit();
	return false;
}

/*
 * initCalendars()
 * 
 * Creates calenders on specific fields
 *
 * @author Sander de Goeij (sander.de.goeij[AT]efocus.nl)
 */
function initCalendars()
{
	new Calendar(
	{
		calendar_0: 'd-m-Y',
		calendar_1: 'd-m-Y',
		form_aanvangsdatumlidmaatschap: 'd-m-Y'
	},
	{
		days: ['zondag','maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
		months: ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'],
		draggable: false,
		tweak:
		{
			x: 6,
			y: 0
		}
	});
}


/*
 * getSelectBoxOnChange()
 * 
 * Get selected option.value from a a selectbox
 *
 * @author Martijn de Valk (martijn.de.valk[AT]efocus.nl)

function getSelectBoxOnChange(){
	var blogForm = $("blogSelect");
	
	var selectBox = blogForm.getElement("select");
	
	blogForm.addEvent('change', function(event){
		window.location.href = (selectBox.get('value'));
	})	
}
*/


/*
 * postFormOnChange()
 * 
 * Get selected option.value from a a selectbox
 *
 * @author Martijn de Valk (martijn.de.valk[AT]efocus.nl)
 */ 
function postFormOnChange(){
	var blogForm = $("blogSelect");
	if(blogForm){
		var selectBox = blogForm.getElement("select");	
		blogForm.addEvent('change', function(event,el){
			blogForm.submit();
		})		
	}
		
}



/*
 * initTargetBlank()
 * 
 * Open external links in new window
 *
 * @author Martijn de Valk (martijn.de.valk[AT]efocus.nl)
 */
function initTargetBlank() {
    var links = $(document.body).getElements('a[href^=http:]');
    links.each(function (el) {
        el.addEvent('click', function (e) {
            e.stop();
            (this.href.test('/' + window.location.host + '/')) ? window.open(this.href) : window.open(this.href, '_blank');
        });
    });
}

