window.addEvent('domready', function() {
    newsmenu_switcher();
    inputfields_setbg();
    submenubar_bg();
    remove_link_lines();
    accordion_init();
    newsaccordion_init();
    list_highlighting();
    sitemap_hover();
    subrubrieken_addevents();
    /*tabmenu_switcher();*/

    center_weeknumbers();
    plaatsreactie_toggle();
    socialbookmarks_toggle();
    searchperiod_toggle();
    reactiesLijst_toggle();
    /*calendarcontrol_toggle();*/
	
	initImageDescription ();
	initMainImageDescription ();
	
	prepareLightboxVideos();
	
});

window.addEvent('load', function() {
	ankeilercaroussel_init();
	toolstabs_switcher();
	//socialbookmarks_hide();
});




/*
 * 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() {
	if (typeof (Shadowbox) == 'undefined') return false;

	Shadowbox.init({
		flvPlayer: '/flash/flvplayer.swf?type=video',
		loadingImage: '/js/skin/classic/loading.gif'
	});

}



function inputfields_setbg() {
	if (!($('loginbox'))) { return; }
	//fields = $ES('input', 'loginbox');
	
	if($('ctl07_ctl11_txtName')) {
		$('ctl07_ctl11_txtName').style.backgroundImage = 'url(/siteimg/bg_login_name.gif)';
		$('ctl07_ctl11_txtName').addEvents({
			'focus': function(){$('ctl07_ctl11_txtName').style.backgroundImage = 'none'},
			'blur': function(){
				if ($('ctl07_ctl11_txtName').value == false) {
					$('ctl07_ctl11_txtName').style.backgroundImage = 'url(/siteimg/bg_login_name.gif)';
				}
			}
		});
	}
	
	if($('ctl07_ctl11_txtAbonneenummer')) {
		$('ctl07_ctl11_txtAbonneenummer').style.backgroundImage = 'url(/siteimg/bg_login_password.gif)';
		$('ctl07_ctl11_txtAbonneenummer').addEvents({
			'focus': function(){$('ctl07_ctl11_txtAbonneenummer').style.backgroundImage = 'none'},
			'blur': function(){
				if ($('ctl07_ctl11_txtAbonneenummer').value == false) {
					$('ctl07_ctl11_txtAbonneenummer').style.backgroundImage = 'url(/siteimg/bg_login_password.gif)';
				}
			}
		});
	}
	
	if($('jobfinder_job')) {
		$('jobfinder_job').addEvents({
			'focus': function(){
				if ($('jobfinder_job').value == 'Zoekterm of referent') {
					$('jobfinder_job').value = '' }
				},
			'blur': function(){
				if ($('jobfinder_job').value == false ) {
					$('jobfinder_job').value = 'Zoekterm of referent';
				}
			}
		});
	}
	
	if($('jobfinder_city')) {
	
		$('jobfinder_city').addEvents({
			'focus': function(){
				if ($('jobfinder_city').value == 'Plaatsnaam') {
					$('jobfinder_city').value = '' }
				},
			'blur': function(){
				if ($('jobfinder_city').value == false ) {
					$('jobfinder_city').value = 'Plaatsnaam';
				}
			}
		});
	}
}


function get_submenu_bg() {
	if (!($('bg_link'))) return;
	
	mainMenuBtns.each(function(mainMenuBtn, index)
	{
		if (mainMenuBtn.className == 'selected') {
			selectedMainMenuBtn = 'main_btn_' + (index+1);
			switch (index) {
			case 0:
				$('bg_link').setStyle('background', '#ae0040');
				break;
			case 1:
				$('bg_link').setStyle('background', '#afbf27');
				break;
			case 2:
				$('bg_link').setStyle('background', '#6e89b2');
				break;
			case 3:
				$('bg_link').setStyle('background', '#db9e23');
				break;
			case 4:
				$('bg_link').setStyle('background', '#6cb4b9');
				break;
			default:
				$('bg_link').setStyle('background', 'none');
				break;
			}
		}
	});	
}

function submenubar_bg() {
	if(!$('mainmenu')) return;
	
	selectedMainMenuBtn = 'main_btn_0';
	
	mainMenuBtns = $$('#mainmenu li').filter(function(item) {
		if (($E('ul', item))) return item;
	});
	
	if (mainMenuBtns[0]) mainMenuBtns[0].bgcolor = '#ae0040';
	if (mainMenuBtns[1]) mainMenuBtns[1].bgcolor = '#afbf27';
	if (mainMenuBtns[2]) mainMenuBtns[2].bgcolor = '#6e89b2';
	if (mainMenuBtns[3]) mainMenuBtns[3].bgcolor = '#db9e23';
	if (mainMenuBtns[4]) mainMenuBtns[4].bgcolor = '#6cb4b9';
	
	get_submenu_bg();
	
	mainMenuBtns.each(function(item) {
		item.addEvents({
			'mouseover': function(){
				$('bg_link').setStyle('background',  item.bgcolor);
				$$('#mainmenu ul').each(function(item) {
					item.setStyle('display', 'none');
				});
				$E('ul', this).setStyle('display', 'block');
			},
			'mouseleave': function(){
				get_submenu_bg();
				if (selectedMainMenuBtn != 'main_btn_0') {
					$E('ul', selectedMainMenuBtn).setStyle('display', 'block');
				} else {
					$('bg_link').setStyle('background', 'none');
				}
				if (this.id != selectedMainMenuBtn) {
					$E('ul', this).setStyle('display', 'none');
				}
			}
		});
	});
	
}


function remove_link_lines() {
	document.getElements('a').addEvent('focus', function(){this.blur()});
}


function ankeilercaroussel_init() {
	if (!($('ankeilercaroussel'))) { return; }
    
	ankeilergroepen = $$('.ankeilergroep');
	var arrAnkeiler = $$('.ankeiler');

	if (ankeilergroepen.length <= 0) return;
	pos_images = $$('#ankeilersposition img');
	aantalgroepen = ankeilergroepen.length;
	laatstegroep = ankeilergroepen.length - 1;
	huidigegroep = 0;
	nieuwegroep = 0;
	ankeilergroepen[huidigegroep].style.left = "0px";
	if (aantalgroepen > 1) 
	{
		$('ankeilers_prev').addEvent('click', function(){
			ankeilercaroussel_back();
			restartTimer();
		});
		$('ankeilers_next').addEvent('click', function(){
			ankeilercaroussel_forward();
			restartTimer();
		});
	}
	
// autoscroll		
	var intCarrouselInterval = 5; 
	var counterOption = { counter: 0 };

	var countdownCarrousel = function(){
		this.counter++;
		if (this.counter >= intCarrouselInterval){
			this.counter = 0;
			ankeilercaroussel_forward();
}
        updateCountdownIndicator(this.counter);
	};
	var simpleTimer = countdownCarrousel.periodical(1000, counterOption);

// countdown		
	updateCountdownIndicator = function(thisCounter){
        intCarrouselTimeLeft = intCarrouselInterval - thisCounter; // Calculate time left
		document.getElement('.countdown_indicator').innerHTML = intCarrouselTimeLeft;
	};	

// timer bar
	var elStatusBar = $('ankeilersposition').getElement('span.statusbar_ankeilier');
//	var elstatusbarPosition = elStatusBar.getPosition().x;
	
	startStatusBar = function(){
		statusbar.start(-140, 0);
	}
	
	var statusbar = new Fx.Style(elStatusBar, 'left', {
		duration:5000,
		transition: Fx.Transitions.linear,
		onComplete: function (){
			startStatusBar();
	}
	});
	startStatusBar();

// stop bij mouseover
	arrAnkeiler.each(function(elAnkeiler){
		
		elAnkeiler.addEvents({
			'mouseenter': function(){
				stopTimer();
			},
			'mouseleave': function(){
				restartTimer();			

}
		});
	});

	stopTimer = function(){
		$clear(simpleTimer);
		statusbar.stop();
	}

	restartTimer = function(){
		stopTimer();
		simpleTimer = countdownCarrousel.periodical(1000, counterOption);
		counterOption = { counter: 0 };
		startStatusBar();
	}
	
	currentPosition = function(){
		
	}
	
	
	
	
}

function ankeilercaroussel_forward() {
	if (!($('ankeilercaroussel'))) { return; }
	if (huidigegroep == laatstegroep) { nieuwegroep = 0; } else { nieuwegroep = huidigegroep + 1; }
	
	ankeilergroepen[huidigegroep].effect('left',{
		duration: 1000,
		transition: Fx.Transitions.Cubic.easeInOut
	}).start(0, -960);
	
	ankeilergroepen[nieuwegroep].effect('left',{
		duration: 1000,
		transition: Fx.Transitions.Cubic.easeInOut,
		onStart: function (){ $('ankeilers_next').removeEvents(); $('ankeilers_prev').removeEvents();},
		onComplete: function (){
			if (huidigegroep == laatstegroep) { huidigegroep = 0; } else { huidigegroep ++; }
			$('ankeilers_prev').addEvent('click', function(){
				ankeilercaroussel_back();
				restartTimer();
			});
			$('ankeilers_next').addEvent('click', function(){
				ankeilercaroussel_forward();
				restartTimer();
			});
		}
	}).start(960, 0);
		
}

function ankeilercaroussel_back() {
	if (!($('ankeilercaroussel'))) { return; }
	if (huidigegroep == 0) { nieuwegroep = laatstegroep; } else { nieuwegroep = huidigegroep - 1; }
	
	ankeilergroepen[huidigegroep].effect('left',{
		duration: 1000,
		transition: Fx.Transitions.Cubic.easeInOut
	}).start(0, 960);
	
	ankeilergroepen[nieuwegroep].effect('left',{
		duration: 1000,
		transition: Fx.Transitions.Cubic.easeInOut,
		onStart: function (){ $('ankeilers_prev').removeEvents(); $('ankeilers_next').removeEvents();},
		onComplete: function (){
			if (huidigegroep == 0) { huidigegroep = laatstegroep; } else { huidigegroep --; }
			$('ankeilers_prev').addEvent('click', function(){
				ankeilercaroussel_back();
				restartTimer();
			});
			$('ankeilers_next').addEvent('click', function(){
				ankeilercaroussel_forward();
				restartTimer();
			});
		}
	}).start(-960, 0);
}


function subrubrieken_addevents() {
	if (!($$('.subrubriek'))) { return; }
	sub_prev = $$('.subrubriek_prev');
	sub_next = $$('.subrubriek_next');
	sub_caroussel = $$('.subrubriekcaroussel');
	subrubriek_pos = new Array;
	subrubriek_aantal = new Array;
	for (p=0;p<sub_prev.length;p++) {
		sub_prev[p].nummer = p;
		sub_prev[p].addEvent('click', function(){subrubriek_back(this.nummer)});
	}
	for (n=0;n<sub_next.length;n++) {
		sub_next[n].nummer = n;
		sub_next[n].addEvent('click', function(){subrubriek_forward(this.nummer)});
		subrubriek_pos[n] = 1;
		subrubriek_aantal[n] = $ES('div', sub_caroussel[n]).length;
	}
}


function subrubriek_back(n) {
	if (!($$('.subrubriek'))) { return; }
	if (subrubriek_pos[n] > 1) {
		var target = sub_caroussel[n].getStyle('margin-left').substring(0, sub_caroussel[n].getStyle('margin-left').length-2).toInt() + 492;
		sub_caroussel[n].effect('margin-left',{
			duration: 700,
			transition: Fx.Transitions.Cubic.easeInOut,
			onStart: function (){ sub_prev[n].removeEvents();},
			onComplete: function (){
				subrubriek_pos[n] --;
				sub_prev[n].addEvent('click', function(){subrubriek_back(n)});
			}
		}).start(target);
	}
}


function subrubriek_forward(n) {
	if (!($$('.subrubriek'))) { return; }
	if (subrubriek_pos[n] < subrubriek_aantal[n] / 2) {
		var target = sub_caroussel[n].getStyle('margin-left').substring(0, sub_caroussel[n].getStyle('margin-left').length-2).toInt() - 492;
		sub_caroussel[n].effect('margin-left',{
			duration: 700,
			transition: Fx.Transitions.Cubic.easeInOut,
			onStart: function (){ sub_next[n].removeEvents();},
			onComplete: function (){
				subrubriek_pos[n] ++;
				sub_next[n].addEvent('click', function(){subrubriek_forward(n)});
			}
		}).start(target);
	}
}


/*** Accordion menu interaction ***/

function showPaneImage(el) {
	
	var allClickables = document.getElements('.clickable');
	allClickables.each(function(clickable){clickable.getElement('a').className='pane_btn pane_btn_open'});
	allClickables.each(function(clickable){clickable.getElement('a').title='openen'});
	el.getElement('a').className='pane_btn pane_btn_close';
	el.getElement('a').title='sluiten';
}

function hidePaneImage(el) {
	el.getElement('a').className='pane_btn pane_btn_open';
	el.getElement('a').title='openen';
}

function accordion_init() {
	var myClickables;
	var myHideables;
	var myAccordion;

	myClickables = document.getElements('.clickable');
	myHideables = document.getElements('.hideable');

	myAccordion = new Accordion(myClickables, myHideables, 
		{
			opacity : true,
			openClose : true,
			onActive : function(el){showPaneImage(el)},
			onBackground : function(el){hidePaneImage(el)},
			display : 'none',
			alwaysHide : true
		});
};


function showNewsPaneImage(el, allClickables) {
	
/*	var allClickables = document.getElements('.news_clickable');
	allClickables.each(function(clickable){clickable.style.backgroundImage='url(/siteimg/bt_plus.gif)'});
	allClickables.each(function(clickable){clickable.title='openen'});
	el.style.backgroundImage='url(/siteimg/bt_min.gif)';
	el.title='sluiten';
*/

/*	allClickables.each(function(clickable){
		clickable.style.backgroundImage='url(/siteimg/bt_plus.gif)';
		clickable.title='openen';
	});
	el.style.backgroundImage='url(/siteimg/bt_min.gif)';
	el.title='sluiten';
*/

	allClickables.each(function(clickable){
//		clickable.style.backgroundImage='url(/siteimg/bt_plus.gif)';
		clickable.removeClass('weektoggler_opened');
		clickable.title='openen';
	});
//	el.style.backgroundImage='url(/siteimg/bt_min.gif)';
	el.addClass('weektoggler_opened');
	el.title='sluiten';

}

function hideNewsPaneImage(el) {
	
//	el.style.backgroundImage='url(/siteimg/bt_plus.gif)';
	el.removeClass('weektoggler_opened');
	el.title='openen';
	
}

function newsaccordion_init() {
	
	var myClickables;
	var myHideables;
	var myAccordion;

	myClickables = $$('.news_clickable');
	myHideables = $$('.news_hideable');
	
	myAccordion = new Accordion(myClickables, myHideables, 
		{
			opacity : true,
			onActive : function(el){showNewsPaneImage(el, myClickables)},
			onBackground : function(el){hideNewsPaneImage(el)},
			show : 0,
			alwaysHide : true
		});
		
};


function list_highlighting() {
	$$('.highlightlist li').each(function(listitem) {
		listitem.addEvents({
			'mouseenter': function(){
				listitem.className = 'highlight';
				listitem.effect('padding-right',{
					duration: 100,
					wait: false
				}).start(5,0);
				listitem.effect('padding-left',{
					duration: 100,
					wait: false
				}).start(0,5);
			},
			'mouseleave': function(){
				listitem.className = '';
				listitem.effect('padding-left',{
					duration: 100
				}).start(5,0);
				listitem.effect('padding-right',{
					duration: 100
				}).start(0,5);
			}
    	});
    });
}


function newsmenu_switcher() {
	newstabs = $$('.nieuwsoverzicht a.tab');
	newscontents = $$('.nieuwsoverzicht div.content');
	var t=0;
	for (t==0;t<newstabs.length;t++) {
		newstabs[t].nummer = t;
		newstabs[t].addEvent('click', function(){newscontent_swapper(this.nummer)});
	};
};

function newscontent_swapper(id) {
	var n = id.toInt();
	newstabs.each(function(newstab) { newstab.removeClass('active') });
	newscontents.each(function(newscontent) { newscontent.removeClass('active') });
	newstabs[n].addClass('active');
	newscontents[n].addClass('active');
}

function tabmenu_switcher() {
	tabs = $$('.past_editions_nav_container a.tab');
	contents = $$('.past_editions_nav_container div.content');
	var t=0;
	for (t==0;t<tabs.length;t++) {
		tabs[t].nummer = t;
		tabs[t].addEvent('click', function(){content_swapper(this.nummer)});
	};
};

function content_swapper(id) {
	var n = id.toInt();
	tabs.each(function(tab) { tab.className = 'tab' });
	contents.each(function(content) { content.className = 'content' });
	tabs[n].className = 'tab active';
	contents[n].className = 'content active';
}


function toolstabs_switcher() {
	
	toolstabs = $$('div.tools h3');
	toolslists = $$('div.tools ul');
	
	toolsreadmores = $$('div.tools a.readmore_large');
	var t=0;
	for (t==0;t<toolstabs.length;t++) {
		toolstabs[t].nummer = t;
		toolstabs[t].addEvent('click', function(){toolslist_swapper(this.nummer)});
	};
};

function toolslist_swapper(id) {
	var n = id.toInt();
	toolstabs.each(function(toolstab) { toolstab.className = '' });
	toolstabs[n].className = 'active';
	toolslists.each(function(toolslist) { toolslist.className = '' });
	toolslists[n].className = 'active';
	
	toolsreadmores.each(function(toolsreadmore) { toolsreadmore.removeClass('active') });
	
	toolsreadmores[n].addClass('active');

}


function sitemap_hover() {
	$$('.sitemap li.top').each(function(listitem) {
		listitem.addEvents({
			'mouseenter': function(){
				listitem.effect('background-color',{
					duration: 150,
					wait: false
				}).start('#ffffff', '#f7f9fb');
			},
			'mouseleave': function(){
				listitem.effect('background-color',{
					duration: 150,
					wait: false
				}).start('#f7f9fb', '#ffffff');
			}
		});
	});
}

function center_weeknumbers() {
	var number_of_weeks = 0;
	$$('.navbar_weeks_this_month li').each(function(){number_of_weeks++});
	if (number_of_weeks == 6) {
		$$('.navbar_weeks_this_month')[0].style.width = '180px';
	} else if (number_of_weeks == 5) {
		$$('.navbar_weeks_this_month')[0].style.width = '150px';
	} else if (number_of_weeks == 4) {
		$$('.navbar_weeks_this_month')[0].style.width = '120px';
	}
}

function socialbookmarks_toggle() {
    if (!($('btnBookmark'))) { return; }
    $('btnBookmark').addEvent('click', function() {
    $('social_bookmarks_collapsible').effect('height', {
            duration: 500
        }).start($('social_bookmarks_collapsible').scrollHeight);
    });
}


function plaatsreactie_toggle() {
    if ($('btnReageer')) { 
    $('btnReageer').addEvent('click', function() {
        $('reactiecollapsible').effect('height', {
            duration: 500
        }).start($('reactiecollapsible').scrollHeight);
    });
    }
    if ($('btnReageer')) {
    $('btnReageer2').addEvent('click', function() {
        $('reactiecollapsible').effect('height', {
            duration: 500
        }).start($('reactiecollapsible').scrollHeight);
    });
}
    if ($('plaatsreactie')) { 
        $('plaatsreactie').addEvent('click', function() {
            $('reactiecollapsible').effect('height', {
                duration: 500
            }).start($('reactiecollapsible').scrollHeight);
        });
    }
}

function reactiesLijst_toggle() {

    if (!($('toonAlles'))) { return; }
    
    $('more_boven').style.display = 'none';
    $('more_onder').style.display = 'none';
            
	$('toonAlles').addEvent('click', function(){
		$('more_boven').style.display = 'block';
		$('more_onder').style.display = 'block';
	});
}

function toReactieForm() {
    var $height = document.getElementById('contentblock').clientHeight;
    //alert($height);
    window.scroll(0, $height);
}

function searchperiod_toggle() {
	if (!($('ddl_periode'))) { return; }
	$('ddl_periode').addEvent('change', function(){
		box = document.forms[0].dl_periode;
		periode = box.options[box.selectedIndex].value;
		if (periode == 'custom_period') {
			$('custom_period').style.display = 'block';
		} else {
			$('custom_period').style.display = 'none';
		}
	});
}


/*function calendarcontrol_toggle() {
	alert('test');
	if (!($('btnPopUpCalendar'))) { return; }
	$('btnPopUpCalendar').addEvent('click', function(){
		
			$('calendar').style.display = 'block';
	
		
	});
}*/


function initImageDescription() {
	if(!document.getElement('div.image')) return false;

	var elImageBlock = document.getElement('div.image');
	var elImage = elImageBlock.getElement('img');
	var elImageWidth = elImage.getSize().size.x;	
	var arrImageText = elImageBlock.getElements('span');
	
	arrImageText.each(function(elImageText){
		elImageText.setStyle('width', elImageWidth+10 );
	});	
	
}

function initMainImageDescription() {	
	if(!document.getElement('p.artikelintro')) return false;	

	var elMainImage = document.getElement('p.artikelintro').getElement('img');	
    if (!elMainImage) return false;	
	
	var elMainImageWidth = elMainImage.getSize().size.x;
	var ellMainImageText = document.getElement('p.artikelintro span');
	if (!ellMainImageText) return false;	

	ellMainImageText.setStyle('width', elMainImageWidth+10 );
}

/*
 * setListCookie()
 * Set the cookie 'list' $listname
 * Set the cookie 'position' to $position
 *
 * @author Jeroen Datema (jeroen.datema[AT]efocus.nl)
*/
function setListCookie(startID, listName, position) {

    //Verwijder de huidige cookies
    document.cookie = 'startid=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
    document.cookie = 'lis=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
    document.cookie = 'position=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
    
    document.cookie = "startid=" + startID;
    document.cookie = "list=" + listName;
    document.cookie = "position=" + position;
}


/*
 * prepareLightboxVideos()
 * 
 * attaches lightbox functionality to Youtube and FLV links in content
 * make sure this function is loaded BEFORE shadowbox.init()
 *
 * @author Klaas Dieleman (klaas[AT]efocus.nl)
*/
function prepareLightboxVideos() {
	var youtubeLinks = $$('.youtube_lightbox');
	var youtubeBaseUrl = 'http://www.youtube.com/v/';
	var flvLinks = $$('.flv_lightbox');
	
	youtubeLinks.each(function(item) {
		var completeUrl = item.getProperty('href');
		if(completeUrl.split('v=')[1]) {
			var ytCode = completeUrl.split('v=')[1].split('&')[0];
		}
		item.setProperty('href', (youtubeBaseUrl + ytCode + '?autoplay=1'));
		item.setProperty('rel', 'shadowbox;width=448;height=336;type=swf');
	});
	
	flvLinks.each(function(item) {
		item.setProperty('rel', 'shadowbox;width=448;height=336;type=flv');
	});
	
	initLightbox();
}