if(!$chk(MVP)) {
	var MVP = {}; 
}

var MVPVarsGeneral = {
	selectors: {
		mainMenu: {
			container: 'mainnav',
			home: 'MainMenuItemHome',
			container: 'MainMenuItem'
		},
		submenu : {
			container: 'subnavigation',
			menu: 'submenu',
			item: 'submenuItem'
		}
	},
	accordion: 'accordionHome',
	accordionToggler: 'li.toggler',
	accordionElement: 'li.element',

	accordion2: 'onderwijsAccordion',
	accordion2Toggler: 'div.onderwijsVorm',
	accordion2Element: 'div.onderwijsVormOpleidingen',

	subnavigation: 'subnavigation',
	selectedMenu: null,
	menuToggle: Class.empty
};

MVP = {
	setActiveMenu: function(menuId, submenuId){
	
		if(menuId != 1){
			$(MVPVarsGeneral.selectors.submenu.container).setStyle('height', '40px');
			this.menuId = menuId;
			var mainItem = $('menu' + menuId);
			var items = $$('.MainMenuItemSel');
			if ($chk(mainItem)){
				items.each(function(el){el.className="MainMenuItem";});
				mainItem.className = 'MainMenuItemSel';
			}
			if($chk($('submenu'+menuId))){
				$(MVPVarsGeneral.selectors.submenu.menu + menuId).setStyle('display','inline');
				MVPVarsGeneral.selectedMenu = $('submenu'+menuId);
				MVPVarsGeneral.selectedMenu.removeClass('hidden');
				MVPVarsGeneral.selectedMenu.setStyle('display','inline');

				if ($chk(submenuId)){
					this.submenuId = submenuId;
					var subitem = $(MVPVarsGeneral.selectors.submenu.item + submenuId);
					if ($chk(subitem)){
						var els = $$('.subMenuItemSel');
						els.each(function(el){
							el.className = 'subMenuItem';
						});
						subitem.className = 'subMenuItemSel';
					}
				}
			}
		} 
	},

	activateMenu: function(){
		var menuItems = $$('#mainnav .MainMenuItem');
		
		if($chk($('MainMenuItem'))){
			$('MainMenuItemHome').addEvent('mouseover',function(item,index){
				$('MainMenuItemHome').id='MainMenuItemSelHome';
			});
			
			$('MainMenuItemHome').addEvent('mouseout',function(item,index){
				$('MainMenuItemSelHome').id='MainMenuItemHome';
			});
		}
		
		MVPVarsGeneral.menuToggle = new Fx.Tween('subnavigation',{duration: 250});
		
		for(var i=0;i<menuItems.length;i++){ 
			
			var menuItem = menuItems[i];
			menuItem.addEvent('mouseover',MVP.MenuMouseOver.bindWithEvent(this,[menuItem]));
			menuItem.addEvent('mouseout',MVP.MenuMouseOut.bindWithEvent(this,[menuItem]));
//			menuItem.addEvent('click',MVP.MenuMouseClick.bindWithEvent(window,[menuItem]));
		}
		
		var menuItems = $$('#mainnav .MainMenuItemSel');
		
		for(var i=0;i<menuItems.length;i++){ 
			var menuItem = menuItems[i];
			menuItem.addEvent('mouseover',MVP.MenuMouseOver.bindWithEvent(this,[menuItem]));
			menuItem.addEvent('mouseout',MVP.MenuMouseOut.bindWithEvent(this,[menuItem]));
//			menuItem.addEvent('click',MVP.MenuMouseClick.bindWithEvent(window,[menuItem]));
		}
		$(MVPVarsGeneral.selectors.submenu.container).addEvent('mouseenter', MVP.showSubmenu.bind(this));
		$(MVPVarsGeneral.selectors.submenu.container).addEvent('mouseleave', MVP.hideSubmenu.bind(this));
	},

	MenuMouseOver: function(e, menuItem){
		var effect = new Fx.Morph($(MVPVarsGeneral.selectors.submenu.container), {
			duration: 'normal', 
			transition: Fx.Transitions.Sine.easeOut
		});
		effect.start({height: 40});
		if($chk(MVPVarsGeneral.selectedMenu)){
			MVPVarsGeneral.selectedMenu.setStyle('display','none');
		}
		MVPVarsGeneral.selectedMenu = $('sub'+menuItem.id);
		
		if($chk(MVPVarsGeneral.selectedMenu)){
			if(MVPVarsGeneral.selectedMenu.getChildren() == ''){
				MVPVarsGeneral.selectedMenu.setStyle('display','none');
				
			}else{
				MVPVarsGeneral.selectedMenu.setStyle('display','inline');
				MVPVarsGeneral.selectedMenu.removeClass('hidden');
			}
		}
		
		var menuImage = menuItem.getChildren();
		var className = menuImage[0].className;
		menuImage[0].className = className+'Hover';
	},
	
	MenuMouseOut: function(e, menuItem){
		var menuImage = menuItem.getChildren();
		var className = menuImage[0].className.replace(/Hover/,'');
		menuImage[0].className = className;
	},
	
	showSubmenu: function(){
		if ($chk(MVPVarsGeneral.selectedMenu)) {
			MVPVarsGeneral.selectedMenu.setStyle('display','inline');
		}
	},
	
	hideSubmenu: function(){
		if ($chk(MVPVarsGeneral.selectedMenu)) {
			MVPVarsGeneral.selectedMenu.setStyle('display','none');
		}
		this.setActiveMenu(this.menuId, this.submenuId);
	},
	
	MenuMouseClick: function (e, menuItem, toggleEvent){
		if($chk(MVPVarsGeneral.selectedMenu)){
			MVPVarsGeneral.selectedMenu.setStyle('display','none');
		}
		MVPVarsGeneral.selectedMenu = $('sub'+menuItem.id);
		MVPVarsGeneral.menuToggle.start($(MVPVarsGeneral.selectedMenu).getStyle('height'), 40);
		MVPVarsGeneral.selectedMenu.setStyle('display','inline');
		
	},
	
	initAccordion: function(){
		var homeContentToggle = new Fx.Tween('homeElement', 'height',{duration: 500});
		var homeBackToggle = new Fx.Tween('homeBackgroundImage', 'height',{duration: 500});
		
		var lastClicked = null;
		
		$$('.toggler').each(function(item,index){
			item.addEvent('click', function() {
				if (this.id == lastClicked) {
					homeContentToggle.start($('homeElement').getStyle('height'),origHeight);
					homeBackToggle.start($('homeBackgroundImage').getStyle('height'),origHeight);
					lastClicked = null;
				} else {
					lastClicked = this.id;
				}
			});
		});
		
		var origHeight = $('homeElement').getCoordinates().height;
		this.accordion = new Accordion(MVPVarsGeneral.accordionToggler, MVPVarsGeneral.accordionElement, {
			opacity: true,
			alwaysHide: true,
			start: 'all-closed',
			onActive: function(toggler, element){
				var toggler = $(toggler);
				toggler.toggleClass(toggler.id);
				
				var arrow = toggler.getElement('img');
				arrow.src = '/images/'+toggler.id+'/arwlrg_down_bg.gif';
				
				var rows = element.getChildren();
				rows.each(function(item,index){
					//item.toggleClass(toggler.id);
					item.addEvent('mouseover',function(){
						item.addClass(toggler.id+'_over');
						item.removeClass(toggler.id);
						item.getElement('a').addClass(toggler.id+'_link');
					});
					item.addEvent('mouseout',function(){
						item.addClass(toggler.id);
						item.removeClass(toggler.id+'_over');
						item.getElement('a').removeClass(toggler.id+'_link');
					});
				});
				if(rows.length > 3){
					height = (rows.length - 3) * 25;
					var newHeight = origHeight + height;
					
					homeContentToggle.start($('homeElement').getStyle('height'),newHeight);
					homeBackToggle.start($('homeBackgroundImage').getStyle('height'),newHeight);
					
				}else{
					homeContentToggle.start($('homeElement').getStyle('height'),origHeight);
					homeBackToggle.start($('homeBackgroundImage').getStyle('height'),origHeight);
				}
			},
		 
			onBackground: function(toggler, element){
				var toggler = $(toggler);
				toggler.removeClass(toggler.id);
				var arrow = toggler.getElement('img');
//				arrow.src = '/images/'+toggler.id+'/blue-arrow.png';
				arrow.src = '/images/blue-arrow.png';
			}

		}, $(MVPVarsGeneral.accordion));
		$(MVPVarsGeneral.accordion).setStyle('display','inline');
	},

	loadDropDownsHome: function(){
		var dropDowns = $$('.'+MVPVarsGeneral.dropDownClass);
		for(i=0;i<dropDowns.length;i++){
			var selectedItems = dropDowns[i].getElements('.selected');
			var selected = selectedItems[0];
			selected.addEvent('click',MVP.showDropDownOptionsHome.bindWithEvent(window,[dropDowns[i]]));
			selected.addEvent('mouseover',MVP.changeMouseOver.bindWithEvent(window,[dropDowns[i],'border-color','#ffffff']));
			selected.addEvent('mouseout',MVP.changeMouseOver.bindWithEvent(window,[dropDowns[i],'border-color','#8CB1D5']));
		}
	},

	changeMouseOver: function(e,element,property,color){
		element.setStyle(property,color);
	},
	showDropDownOptionsHome: function(e, downDownElement){
		var optionsPanel = null;
		var selectedItems = null;
		var options = null;
	
		var optionsPanel = downDownElement.getElements('.options');
		var selectedItems = downDownElement.getElements('.selected');
		var hiddenItems = downDownElement.getElements('.hidden');
		var selected = selectedItems[0];
		var hidden = hiddenItems[0];
		
		
		var options = optionsPanel.getElements('.option');
		
		for(i=0;i<options[0].length;i++){
			var option = options[0][i];
			option.removeEvents();
			option.addEvent('mouseover', MVP.changeMouseOver.bindWithEvent(window,[option,'color','#003366']));
			option.addEvent('mouseover', function(){downDownElement.setStyle('border-color','#ffffff')});
			option.addEvent('mouseout', MVP.changeMouseOver.bindWithEvent(window,[option,'color','#ffffff']));
			option.addEvent('click',MVP.setSelectedItemHome.bindWithEvent(window,[option,selected,hidden,optionsPanel]));
			
		}
		
		$$('.backgroundTypical').each(function(item,index){
			item.addEvent('click',function(){selected.innerHTML = 'Alle opties'});
			item.addEvent('mouseover',MVP.changeMouseOver.bindWithEvent(window,[item,'background-color','#ffffff']));
			item.addEvent('mouseout',MVP.changeMouseOver.bindWithEvent(window,[item,'background-color','#8cb1d5']));
		});
		
		optionsPanel[0].setStyle('display','inline');
		optionsPanel[0].setStyle('border-color','#ffffff');
		downDownElement.setStyle('border-color','#ffffff');
		downDownElement.addEvent('mouseleave',function(){
				optionsPanel[0].setStyle('display','none');
				optionsPanel[0].setStyle('border-color','#8CB1D5');
				downDownElement.setStyle('border-color','#8CB1D5');
		});
	}, 
	
	setSelectedItemHome: function(e, option, selected, hidden, optionsPanel){
		selected.innerHTML = option.innerHTML;
		hidden.value = option.id;
		optionsPanel[0].setStyle('display','none');
	},

	initAccordion2: function(){
		var accordion2 = new Accordion(MVPVarsGeneral.accordion2Toggler, MVPVarsGeneral.accordion2Element, {
			opacity: true,
			alwaysHide: false,
			start: '1',
			onActive: function(toggler, element){
				var arrowElement = toggler.getElement('div.arrowIconRight');
				toggler.setStyle('background-color','#003366');
				arrowElement.className = 'arrowIconDown';
				
				// set new icon for the different background
				var iconElement = toggler.getElement('div.acc_cpdvorm');
				iconElement.className = 'mbva_cpdvorm';
				
				
			},
			onBackground: function(toggler, element){
				var arrowElement2 = toggler.getElement('div.arrowIconDown');
				if(arrowElement2 != null){
					arrowElement2.className = 'arrowIconRight';
					toggler.setStyle('background-color','#6699CC');
				}
				
				// set new icon for the different background
				// set new icon for the different background
				var iconElement = toggler.getElement('div.mbva_cpdvorm');
				if (iconElement) {
					iconElement.className = 'acc_cpdvorm';
				}
			}	 
			
		}, $(MVPVarsGeneral.accordion2));
		$(MVPVarsGeneral.accordion2).setStyle('display','inline');
	
	},

	addFormButtonHover: function() {
		$$('.formButton').each(function(item) {
			item.addEvents({
				'mouseout': function() {
					MVP.hoverFormbutton(item);
				},
				'mouseover': function() {
					MVP.hoverFormbutton(item);
				}
			})
		});
	},
	
	hoverFormbutton: function(button) {
		if (button.hasClass('formButtonOver')) {
			button.removeClass('formButtonOver');
		} else {
			button.addClass('formButtonOver');
		}
	}, 
	
	addSearchHoverSearchButton: function() {
		$$('input.searchButton').each(function(item) {
			item.addEvents({
				'mouseover': function() {
					MVP.searchHoverSearchButton(item);
				},
				'mouseout': function() {
					MVP.searchHoverSearchButton(item);
				}
			});
		});
	},
	
	searchHoverSearchButton: function(button) {
		if (button.getProperty('src') == '/images/button/btn_search.jpg') {
			button.setProperty('src', '/images/button/btn_search_over.gif');
		} else {
			button.setProperty('src', '/images/button/btn_search.jpg');
		}
	}
};

window.addEvent('domready',function(){
	var locaties = $$('#locatiesLeft span');
	var total = locaties.length;
	locaties.each(function(item,index) {
		if (index >= Math.ceil(total/2)) {
			$('locatiesRight').adopt(item);
		}
	});
});
window.addEvent('domready',function(){
	MVP.addFormButtonHover();
	MVP.addSearchHoverSearchButton();
	
	
});
window.addEvent('domready',function(){
	var saf_button = $$('.safButton');
	var print_button = $$('.printButton');
	
	MVP.activateMenu();
	if($chk($(MVPVarsGeneral.accordion))){
		MVP.initAccordion();
	}
	if($chk($(MVPVarsGeneral.accordion2))){
		MVP.initAccordion2();
	}
	if ($chk(saf_button)) {
		saf_button.store('tip:title', $('tooltip_mail'));
		var Tips1 = new Tips(saf_button, {
			offsets : {
				x : -100,
				y : -10
			}
		});
	}

	if ($chk(print_button)) {
		print_button.store('tip:title', $('tooltip_print'));
		var Tips1 = new Tips(print_button, {
			offsets : {
				x : -100,
				y : -10
			}
		});
	}
});

setBackground = function(src_image, target_element) {
	var image = $(src_image);
	var target_element = $(target_element);
	
	src = image.getProperty('src');
	
	target_element.setStyle('background-image', "url('" + src + "')");
}

/**
 * Util function to show a div based on a location
 * @param HTMLOption locatie
 */
function showRow(locatie, type){
	$$('.locatie_span').each(function(item, index){
		item.style.display = 'none';
	});
	
	if(locatie != ''){
		var locatie_naam = $(locatie).getAttribute('value');
		
		var locatie_span = $(type + '_' + locatie_naam);
		var locatie_span_2 = $(type + '_' + locatie_naam + '_2');
		
		if ($chk(locatie_span)){
			locatie_span.style.display = 'inline';
		}
		if ($chk(locatie_span_2)){
			locatie_span_2.style.display = 'inline';
		}

		$$('.startdata_row').each(function(item, index){
			item.addClass('startdata_noshow');
		});

		$$('.startdata_header').each(function(item, index){
			item.removeClass('startdata_noshow');
		});				
		
		
		$$('.' + locatie_naam).each(function(item, index){
			item.removeClass('startdata_noshow');
		});
		
	}else{
		$$('.startdata_row').each(function(item, index){
			item.addClass('startdata_noshow');
		});				

		$$('.startdata_header').each(function(item, index){

			item.addClass('startdata_noshow');
	
		});	
		
	}
}


/**
 * Util function to show a div based on a location
 */
window.addEvent('domready', function() {
	var updateStudieschema = (function(locatie, type, opleiding, order_type, filter_leslocaties) {
		$$('.locatie_span').each(function(item, index){
			item.style.display = 'none';
		});
		if (order_type.length === 0) {
			order_type = 'modulenaam';
			$('order').options[1].selected = 'selected';
		}
		
		if(locatie) {
			var locatie_naam = $(locatie).getAttribute('value');
			var locatie_id = $(locatie).getAttribute('id');
			var locatie_span = $(type + '_' + locatie_naam);
			var locatie_span_2 = $(type + '_' + locatie_naam + '_2');
			if (locatie_id) {
				
				if ($chk(locatie_span)){
					locatie_span.style.display = 'inline';
				}
				if ($chk(locatie_span_2)){
					locatie_span_2.style.display = 'inline';
				}
				$$('.filter').each(function(el) {
					el.setAttribute('disabled', 'disabled');
				});
				new Request.JSON({url:"/studieschema.php", 
					onSuccess: function(jsondata) {
						var result = jsondata.resulthtml;
						
						$('startdatatablearea').innerHTML = result;
						$('order').removeProperty('disabled');
					},
					onComplete: function(e) {
						$('startdatatablearea').removeClass('loading');
						$$('.filter').each(function(el) {
							el.removeAttribute('disabled');
						});
					},
					onFailure: function(e) {
						$('startdatatablearea').addClass('failure');
					},
					onRequest: function(e) {
						$('startdatatablearea').removeClass('failure');
						$('startdatatablearea').addClass('loading');
					}
				}).get({opleiding: opleiding, category:type, lokatie:locatie_id, order:order_type, filter_leslocaties:filter_leslocaties });
				
			}else{
				$('startdatatablearea').empty();
			}
		}
	});
	
	if (!!$('lloc'))
	$('lloc').addEvent('change', function() {
		updateStudieschema(this.options[this.selectedIndex], this.getProperty('data-category'), this.getProperty('data-opleiding'), $('order').options[$('order').selectedIndex].value, $('filterOfLeslocaties').options[$('filterOfLeslocaties').selectedIndex].value);
	});
	
	if (!!$('filterOfLeslocaties'))
	$('filterOfLeslocaties').addEvent('change', function() {
		updateStudieschema($('lloc').options[$('lloc').selectedIndex], this.getProperty('data-category'), this.getProperty('data-opleiding'), $('order').options[$('order').selectedIndex].value, this.options[this.selectedIndex].value);
	});
	
	if (!!$('order'))
	$('order').addEvent('change', function() {
		updateStudieschema($('lloc').options[$('lloc').selectedIndex], this.getProperty('data-category'), this.getProperty('data-opleiding'), this.options[this.selectedIndex].value, $('filterOfLeslocaties').options[$('filterOfLeslocaties').selectedIndex].value);
	});
});
