window.addEvents({
	'domready': function() {
		f4_pushBox();
		initMainNavHover();	
		setMenuHeight();
		initMediaLinks();
		f16_signuprecipeform();
	},
	'load': function() {
		setBoxHeight();
	}
});

/**
 * EfxBaseSlideShow Class
 * Reusable slideshow class.
 *
 * @author Ralph Meeuws (ralph-meeuws[AT]efocus.nl)
 * @param arrSlides, intStartSlide, blnLoop, intInterval, blnAutoPlay, blnPlaying
 * @param strOutEffectProperty, intOutEffectStartValue, intOutEffectEndValue, intOutEffectDuration
 * @param strInEffectProperty, intInEffectStartValue, intInEffectEndValue, intInEffectDuration
 * @return EfxBaseSlideShow
 */
var EfxBaseSlideShow = new Class({
	Implements: [Options, Events, Chain],
	options: {
		arrSlides: [],
		intStartSlide: 0,
		blnLoop: true,
		intInterval: 2500,
		blnAutoPlay: true,
		blnPlaying: false,
		
		strOutEffectProperty: 'opacity',
		intOutEffectStartValue: 1,
		intOutEffectEndValue: 0,
		intOutEffectDuration: 'long',
		
		strInEffectProperty: 'opacity',
		intInEffectStartValue: 0,
		intInEffectEndValue: 1,
		intInEffectDuration: 'long'
	},
	initialize: function(options) {
		var that = this;
		this.setOptions(options);
		this.addSlides(this.options.arrSlides);
		this.options.arrSlides.each(function(slide){
			slide.setStyle('opacity', 0);
		});
		this.showSlide(this.options.intStartSlide);
		if (this.options.blnAutoPlay == true && this.options.blnPlaying == false) {
			this.playSlideShow();
		}
		this.intSlideCount = 0;
	},
	arrSlideCollection: [],
	addSlides: function(arrSlides){
		arrSlides.each(function(slide){
			this.arrSlideCollection.include(slide);
		}, this);
	},
	addSlide: function(slide){
		this.addSlides($splat(slide));
	},
	cycleForwards: function(){
		if ($chk(this.intCurrentSlideNumber) && this.intCurrentSlideNumber < this.arrSlideCollection.length-1) {
			this.showSlide(this.intCurrentSlideNumber+1);
		} else if ((this.intCurrentSlideNumber) && this.options.blnLoop) {
			this.showSlide(0);
		} else if (!defined(this.intCurrentSlideNumber)) {
			this.showSlide(this.options.intStartSlide);
		}
	},
	cycleBackwards: function(){
		if (this.intCurrentSlideNumber > 0) {
			this.showSlide(this.intCurrentSlideNumber-1);
		} else if (this.options.blnLoop) {
			this.showSlide(this.arrSlideCollection.length-1);
		}
	},
	showSlide: function(intSlideNumberToShow){
		if (this.arrSlideCollection[intSlideNumberToShow]) {
			var that = this;
			if (this.arrSlideCollection[this.intCurrentSlideNumber]) {
				var intPreviousSlideNumber = this.intCurrentSlideNumber;
		//		this.arrSlideCollection[intSlideNumberToShow].setStyle(that.options.strInEffectProperty, that.options.intInEffectEndValue);
				this.arrSlideCollection[intSlideNumberToShow].setStyle(that.options.strOutEffectProperty, that.options.intOutEffectStartValue); // DEBUG THIS LINE!!!
				this.arrSlideCollection[this.intCurrentSlideNumber].get('tween', {property: that.options.strOutEffectProperty, duration: that.options.intOutEffectDuration}).start(that.options.intOutEffectStartValue, that.options.intOutEffectEndValue).chain(function(){
					that.arrSlideCollection[intPreviousSlideNumber].setStyle(that.options.strInEffectProperty, that.options.intInEffectStartValue);
				});
			}
			this.arrSlideCollection[intSlideNumberToShow].get('tween', {property: that.options.strInEffectProperty, duration: that.options.intInEffectDuration}).start(that.options.intInEffectStartValue, that.options.intInEffectEndValue);
			this.intCurrentSlideNumber = intSlideNumberToShow;
		}
		this.fireEvent('slideShown', this);
	},
	runSlideShow: function(){
		var that = this;
		
		if (that.intCurrentSlideNumber != this.intSlideCount) {
			this.intSlideCount = that.intCurrentSlideNumber;
		}
		this.intSlideCount++;
		if (this.intSlideCount >= that.arrSlideCollection.length) {
			this.intSlideCount = 0;
		}			
		this.showSlide(this.intSlideCount);
	},
	playSlideShow: function(){
		this.options.blnPlaying = true;
		this.slideShowTimer = this.runSlideShow.periodical(this.options.intInterval, this);
	},
	resetTimer: function(){
		if ($chk(this.slideShowTimer)) {
			this.slideShowTimer = $clear(this.slideShowTimer);
			this.playSlideShow();
		}
	},
	name: 'EfxBaseSlideShow'
});

/*
 * f15_captureAreaToolTips
 * Shows and hides a custom tooltip for each item in the capture area list within F15.
 * 
 * @author Ralph Meeuws (ralph-meeuws[AT]efocus.nl)
 * @return
 */
function f15_captureAreaToolTips(){
	
	var arrF15 = $$('.f15_fish_information_extended');	
	if (arrF15.length == 0) return;
	
	if (Browser.Engine.trident4) { // IE6
		var intToolTipOffsetY = -45;
	} else if (Browser.Engine.trident5) { // IE7
		var intToolTipOffsetY = -50;
	} else {
		var intToolTipOffsetY = -45;
	}
	
	var arrCaptureAreaToolTips = arrF15.getElement('.capture_area_worldmap').getElements('a');
	
	var objCaptureAreaToolTips = new Tips(arrCaptureAreaToolTips, {offsets: {'x': 2, 'y': intToolTipOffsetY}});
	
	var arrTips = $$('.tip');
	if (arrTips.length == 0) return;
	
	arrTips[0].setStyles({
		'height': 40,
		'padding-top': 10,
		'padding-bottom': 10
	});
	
	objCaptureAreaToolTips.addEvent('show', function(captureAreaToolTip){
		captureAreaToolTip.fade('show');
	});
	
	objCaptureAreaToolTips.addEvent('hide', function(captureAreaToolTip){
		captureAreaToolTip.fade('hide');
	});
}

/**
 * EfxListSlideShow Class
 * Customized slideshow class.
 *
 * @author Ralph Meeuws (ralph-meeuws[AT]efocus.nl)
 * @param elNav, strLabelPrev, strLabelNext, strLabelFirst, strLabelLast, strLabelPlay, strLabelPause
 * @return EfxListSlideShow
 */
var EfxListSlideShow = new Class({
	Extends: EfxBaseSlideShow,
	options: {
		elNav: $('nav')
	},
	
	initialize: function(options){
		
		
		
		this.parent(options);
		if ($defined(this.options.elNav)) {
			//this.arrNavListItems = this.options.elNav.getElements('li');
			
			
			
						
			this.arrNavLinks = this.options.elNav.getElements('a');		
			this.initNav();
			if (this.arrNavLinks.length != 0) {
				this.activateNavLink(this.arrNavLinks[this.options.intStartSlide]);
			}
			this.addEvent('slideShown', function(){
				if (this.arrNavLinks.length != 0) {
					this.activateNavLink(this.arrNavLinks[this.intCurrentSlideNumber]);
				}
			});
		}
	},
	initNav: function(){
		var that = this;
		this.arrNavLinks.each(function(elNavLink, n){
			elNavLink.addEvent('click', function(){
				that.showSlide(n);
				that.resetTimer();
				that.activateNavLink(elNavLink);
			});
		});
	},
	activateNavLink: function(elNavLink){
		this.arrNavListItems.each(function(elNavListItem){
			elNavListItem.removeClass('active');
		});
		elNavLink.getParent().addClass('active');
	},
	name: 'EfxListSlideShow'
});

/**
 * EfxPVisSlideShow Class
 * Extends current slideshow to change navigation links together with the slide
 *
 * @author Rocco Janse, rocco@efocus.nl
 * @return void
 */
 
var EfxPVislideShow = new Class({
	Extends: EfxBaseSlideShow,
	options: {
		elNav: $('nav')
	},
	initialize: function(options) {
		this.parent(options);
	},
	showSlide: function(intSlideNumberToShow){
		if (this.arrSlideCollection[intSlideNumberToShow]) {
			var that = this;
			if (this.arrSlideCollection[this.intCurrentSlideNumber]) {
				var intPreviousSlideNumber = this.intCurrentSlideNumber;
				this.arrSlideCollection[intSlideNumberToShow].setStyle(that.options.strOutEffectProperty, that.options.intOutEffectStartValue); // DEBUG THIS LINE!!!
				this.arrSlideCollection[this.intCurrentSlideNumber].get('tween', {property: that.options.strOutEffectProperty, duration: that.options.intOutEffectDuration}).start(that.options.intOutEffectStartValue, that.options.intOutEffectEndValue).chain(function(){
					that.arrSlideCollection[intPreviousSlideNumber].setStyle(that.options.strInEffectProperty, that.options.intInEffectStartValue);
				});
			}
			this.arrSlideCollection[intSlideNumberToShow].get('tween', {property: that.options.strInEffectProperty, duration: that.options.intInEffectDuration}).start(that.options.intInEffectStartValue, that.options.intInEffectEndValue);
			this.intCurrentSlideNumber = intSlideNumberToShow;
		}
		
		// fetch corresponding link(s)
		if ($defined(this.options.elNav)) {
			
			var links = $(this.arrSlideCollection[this.intCurrentSlideNumber]).getElement('div.hiddenlinks').getElements('a');
			var items = this.options.elNav.getElements('li');
		
			items.each(function(item) {
				item.set('html', '');
			});
		
			for(i = 0; i < links.length; ++i) {
				if (links[i]) {
					var newlink = new Element('a', {
						'html': links[i].get('html'),
						'href': links[i],
						'title': links[i].get('html'),
						'target': links[i].get('target')						
					});
					
					newlink.inject(items[i]);
				}
			}
		}
		this.fireEvent('slideShown', this);
	},
	name: 'EfxPVislideShow'
});

/**
 * f4_pushBox
 * Animates the pushbox and defines the interaction.
 *
 * @author Ralph Meeuws (ralph-meeuws[AT]efocus.nl)
 * @param arrSlides, intStartSlide, blnLoop, intInterval, blnAutoPlay, blnPlaying
 * @param strOutEffectProperty, intOutEffectStartValue, intOutEffectEndValue, intOutEffectDuration
 * @param strInEffectProperty, intInEffectStartValue, intInEffectEndValue, intInEffectDuration
 * @param elNav, strLabelPrev, strLabelNext, strLabelFirst, strLabelLast, strLabelPlay, strLabelPause
 * @return EfxListSlideShow Class instance
 */
function f4_pushBox(){
	arrPushBoxes = $$('.pushbox');
	if (arrPushBoxes.length == 0) return;
	
	var elMyPb = arrPushBoxes[0].getElement('.pushbox_viewport');
	var arrMySlides = elMyPb.getElements('.pushbox_slide');
	var elMyNav = arrPushBoxes[0].getElement('.pushbox_nav');
	
	//var objPushbox = new EfxListSlideShow({
	var objPushbox = new EfxPVislideShow({
		arrSlides: arrMySlides,
		elNav: elMyNav,
		intInterval: 5000,
		intStartSlide: 1,
		strOutEffectProperty: 'top',
		intOutEffectStartValue: 0,
		intOutEffectEndValue: 246
	});
}

function clearSearchTerm(el){
	if(el.value=='Zoek op trefwoorden'){
		el.value='';
	}else if(el.value=='Zoek informatie'){
		el.value='';
	}else if(el.value=='Zoek recepten'){
		el.value='';
	}
}

function searchInformation(){
	if($('information').value){
		var value = $('information').value;
		
		if(value!='Zoek informatie' && value!=''){
			 location.href='/zoeken/?tx_pvis_pi12[sTerm]='+value;
		}
	}else{
		alert('Search box not defined');
	}
}

/**
 * getHighestNumber
 * get highest number in array
 *
 * @author Mirjam <mirjam{AT}efocus.nl>
 * @return void
*/

function getHighestNumber (newHeight) {
	
	var newH = 0;
	
	for (var i = 0; i < newHeight.length; i++) {
		
		if (newHeight[i].getSize().y > newH) {
			newH = newHeight[i].getSize().y;
		}
		
	}
	
	return newH;
	
}


/**
 * setBoxHeight
 * sets height of boxes homepage
 *
 * @author Mirjam <mirjam{AT}efocus.nl>
 * @return void
*/

function setBoxHeight () {
	
	if ($$('ul.home_newsitems')) {
			
		var items = $$('ul.home_newsitems li');
		var newHeight = new Array();
				
		for (var i = 0; i < items.length; i++) {
			
			newHeight.push(items[i]);
			
			if (i % 3 == 2) {
					
				var newH = getHighestNumber(newHeight);
				for (var j = 0; j < newHeight.length; j++) {
					newHeight[j].setStyle('height', newH);
				}
				
				newHeight = new Array();
				
			}
												
		}
	
	}
	
}

/**
 * setMenuHeight
 * changes menu height
 *
 * @author Mirjam <mirjam{AT}efocus.nl>
 * @return void
*/

function setMenuHeight () {
	
	if (!$('mainmenu')) return;
	
	var startHeight = $('mainmenu').getScrollSize().y;
	$('mainmenu').setStyle('height', $('mainmenu').getScrollSize().y);
	
	// rollovers
	var mainItems = $('mainmenu').getElements('li');
	var topHeight = mainItems[0].getScrollSize().y;
	var prevHeight = startHeight;
	
	mainItems.each(function(mainItem){
		
		if (mainItem.getElement('ul')) {
			
			var newHeight = (topHeight + mainItem.getElement('ul').getScrollSize().y);
			
			if (mainItem.hasClass('active') == false) {
			
				mainItem.addEvents({
					'mouseenter': function() {
						if (newHeight != prevHeight) {
							$('mainmenu').setStyle('height', newHeight);
							prevHeight = newHeight;
						}
					},
					'mouseleave': function() {
						$('mainmenu').setStyle('height', startHeight);
					}
				});
				
			}
			
		} 
		
	});
		
}



/*
 * initMainNavHover
 * Adds hovering functionality to the main navigation while memorizing the active menuitem.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @return void
 */
function initMainNavHover() {
	if (!$('mainmenu')) return;

	var elMainNav = $('mainmenu');
	var arrMainNavListItems = elMainNav.getChildren('li');
	var elMainNavListItemActive = null;

	function getActiveMainNavListItem() {
		arrMainNavListItems.each(function(elMainNavListItem){
			if (elMainNavListItem.hasClass('active')) {
				elMainNavListItemActive = elMainNavListItem;
			}
		});
	}

	arrMainNavListItems.each(function(elMainNavListItem){
		elMainNavListItem.addEvents({
			'mouseenter': function(){
				elMainNavListItem.addClass('hover');
				getActiveMainNavListItem();
				if (elMainNavListItem != elMainNavListItemActive) elMainNavListItemActive.removeClass('active');
			},
			'mouseleave': function(){
				elMainNavListItem.removeClass('hover');
				getActiveMainNavListItem();
				elMainNavListItemActive.addClass('active');
			}
		});
	});
}



/**
 * initMedialinks
 * fetches media links and extracts the title, inserts into mediatitle tag
 *
 * @author Rocco Janse, <rocco[at]efocus.nl>
 * @return void
 */

function initMediaLinks() {
	var heading = $('mediatitel');
	if (!heading) return;
	var links = $$('img.medialink');
	if (links.length == 0) return;
	links.each(function(link) {
		link.addEvents({
			'mouseenter': function() {
				title = link.get('title');
				if (!title) return;
				heading.set('html', '');
				heading.set('html', title);
			}
		});
	});
}


function f16_signuprecipeform() {
	var arrF16 = $$('.signuprecipeform');
	if (arrF16.length == 0) return;
		
	arrF16.each(function(elF16){
		elF16SubmitButton = elF16.getElement('.component_button');
		elF16SubmitButton.addEvent('click', function(event){
			event.stop();
			elF16.submit();
		});
	});
}
