
// variables, die in de template op true worden gezet als de auio/videoplayer direct in beeld moet.
var showvideo = false;
var showaudio = false;

$(function(){
	
	var currentMenuItem, currentSubMenuItem;
	var dropMenuHideTimeout, dropSubMenuHideTimeout, dropSubMenuShowTimeout, holdSubMenuTimeout;
	var hoveredMenuId, hoveredSubMenuId; // the items that are just hovered
	var holdSubMenu = false;
	var item;
	
	/*
	$("#submenu > li").hover(function(){
		$(this).children("a").children("span").toggleClass('menuItemOpen');
	}, function(){
		$(this).children("a").children("span").toggleClass('menuItemOpen');
	});
	*/
	
	// dropmenu
		$("#submenu .dropMenu").hide();
		$("#submenu .dropMenuContainer").hover(function(){
			clearTimeout(dropMenuHideTimeout);
			if (hoveredMenuId && $(this).attr("id") != hoveredMenuId) {
				$('#'+hoveredMenuId).children(".dropMenu").hide();
				$(this).children("a").removeClass('active');
			}
			$(this).children(".dropMenu").show();
			$("#submenu .dropMenuContainer a").removeClass('active');
			$(this).children("a").addClass('active');
							
		}, function(){
			//$("#submenu .dropSubMenu").hide();
			currentMenuItem = $(this);
			hoveredMenuId = currentMenuItem.attr("id");
			dropMenuHideTimeout = setTimeout(function(){
				currentMenuItem.children(".dropMenu").hide();
				//currentMenuItem.children("a").children("span").removeClass('menuItemOpen');
				$("#submenu .dropMenuContainer a").removeClass('active');
				}, 500);
			
		});
	
	// dropsubmenu
		$("#submenu .dropSubMenu").hide();
		$("#submenu .dropSubMenuContainer").hover(function(){
			if (holdSubMenu) {
				// don't show this hovered submenu when another is just opened..
				itemId = $(this).attr("id");
				holdSubMenuTimeout = setTimeout(function(){clearHold(itemId)}, 400);
				return;
			}
	
			clearTimeout(dropSubMenuHideTimeout);
			clearTimeout(dropSubMenuShowTimeout);
			//$("#submenu .dropSubMenu").hide();
			if (hoveredSubMenuId && $(this).attr("id") != hoveredSubMenuId) {
				$('#'+hoveredSubMenuId).children(".dropSubMenu").hide();
				$('#'+hoveredSubMenuId).children("span").children("a").removeClass('active');
			}
			//$(this).children(".dropSubMenu").show();
			currentSubMenuItem = $(this);
			dropSubMenuShowTimeout = setTimeout(function(){showSubMenu(currentSubMenuItem)}, 180);
			//showSubMenu(currentSubMenuItem);
			//alert($(this).attr("id"));
			
		}, function(){
			currentSubMenuItem = $(this);
			hoveredSubMenuId = currentSubMenuItem.attr("id");
			//$(this).children(".dropSubMenu").slideUp();
			dropSubMenuHideTimeout = setTimeout(function(){
				currentSubMenuItem.children(".dropSubMenu").hide();
				currentSubMenuItem.children("span").children("a").removeClass('active');			
				}, 500);
		});
	 
		function showSubMenu(theSubMenu) {
			// show subMenu, don't hide it when user hovers over mainmenu item
			clearTimeout(dropSubMenuShowTimeout);
			$("#submenu .dropSubMenu").hide(); // force all to hide
			$("#submenu .dropSubMenuContainer span a").removeClass('active');	
			theSubMenu.children(".dropSubMenu").show();
			holdSubMenu = true;
			theSubMenu.children("span").children("a").addClass('active');
		}
		function clearHold(hoveredSubMenuId) {
			holdSubMenu=false;
			showSubMenu($('#'+hoveredSubMenuId));
			return;
			// if other than holded submenu...
			//if (hoveredSubMenu != currentSubMenuItem) {
			if (hoveredSubMenuId && hoveredSubMenuId != currentSubMenuItem.attr("id")) {
				currentSubMenuItem.children(".dropSubMenu").hide();
				$('#'+hoveredSubMenuId).children(".dropSubMenu").show();
			}
		}
	
		//togglebutton
			$(".toggleItem").hide();
			$(".toggleButton").click(function(){
				$(".toggleItem").toggle();
				$(this).toggleClass('toggleButtonHide');
			});
		
		// calendar columns
			$("table.calendar tr.weekdays th a").mouseover( function() {
				var iIndex = $(this).parent().parent().children('th').index($(this).parent()[0]);
				$("table.calendar tr td:nth-child(" + (iIndex+1) + ") a").addClass('active');
			 });
			$("table.calendar tr.weekdays th a").mouseout( function() {
				var iIndex = $(this).parent().parent().children('th').index($(this).parent()[0]);
				$("table.calendar tr td:nth-child(" + (iIndex+1) + ") a").removeClass('active');
			 });
			 
		// calendar rows
			$("table.calendar th.week a").mouseover( function() {
				$(this).parents('tr').find('a').addClass('active') });
			$("table.calendar th.week a").mouseout( function() {
				$(this).parents('tr').find('a').removeClass('active') });
		
				
		//styled select
			$('.selectorContainer').hover(
				function(){
					$(this).children('.pullDown').show();
				},
				function(){
					$(this).children('.pullDown').hide();
				});
			
			$(".pullDown span").hover(
				function(){
					$(this).addClass('dropMenuHover');
				},
				function(){
					$(this).removeClass('dropMenuHover');
				}
			)
			
			$('.fotoSelectButton').click(function(){
				fotoId = $(this).attr("id").substr(16);
				// zet ze allemaal uit, behalve de geselecteerde, die moet aan
				for(i=1;i<=$(".fotoContainer").length;i++) {
					if (i==fotoId)
						$("#fotoContainer"+i).show();
					else
						$("#fotoContainer"+i).hide();
				}				
				return false;
			});
			
			// initially hide form, except when getvar showReactieForm is in url
			$("#reactieColomn").hide();			
			// show reactieform on click
			$(".buttonPlaatsReactie").click(function(){ 
				$("#reactieColomn").show();	
				return false;			
			});
			
			/**
			 * animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'),
			 * speed: Fadingspeed in milliseconds or keywords (slow, normal or fast)(Default: 'normal'),
			 * timeout: Time between the fades in milliseconds (Default: '2000'),
			 * type: Type of slideshow: 'sequence' or 'random' (Default: 'sequence'),
			 * containerheight: Height of the containing element in any css-height-value (Default: 'auto'),
			 * runningclass: CSS-Class which the container get�s applied (Default: 'innerfade')
			 */
			
			/**
			 * Nieuws lichtkrant
			 */
			$('#lichtkrant').innerfade({
				animationtype: 'slide',
				speed: 1000,
				timeout: 8000,
				type: 'sequence',
				containerheight: '18px;'
			});			
			
			/**
			 * Binnenkort slideshow
			 */
			$('#binnenkortVoorstellingen').innerfade({
				speed: 2000,
				timeout: 5000,
				type: 'sequence',
				containerheight: '215px'
			});

			// default teksten voor zoeken
			sDefaultZoekTxt = 'trefwoord...';
			$("#formZoekenInput").focus(function(){
				if (this.value == sDefaultZoekTxt)
					this.value = '';
			});
			$("#formZoekenInput").blur(function(){
				if (this.value == '')
					this.value = sDefaultZoekTxt;
			});
			
			// default teksten voor nieuwsbriefaanmelden.
			sDefaultEmailTxt = 'Vul hier uw e-mailadres in...';
			$("#formNieuwsbriefAanmeldenEmail").focus(function(){
				if (this.value == sDefaultEmailTxt)
					this.value = '';
			});
			$("#formNieuwsbriefAanmeldenEmail").blur(function(){
				if (this.value == '')
					this.value = sDefaultEmailTxt;
			});

			// print button mag alleen verschijnen als er wel javascript is.
			$(".buttonPrintDezePagina").removeClass("hidden");

			
			function show_video() {
				$(".imageContainer").hide();	
				$(".audioContainer").hide();	
				$("#audioPlayer").html('&nbsp;');	// empty audio container to be sure to stop that clip
				$(".videoContainer").removeClass("hidden");	
				$(".videoContainer").show();
				
				$("#videoPlayer").html('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8" width="225" height="225">'
				 + '<param name="movie" value="/flash/Video_player_basic_4x3.swf?myFile=' + video_file + '&amp;playMe=true" />'
				 + '<param name=quality value=high />'
				 + '<param name="wmode" value="transparent" />'
				 + '<param name="bgcolor" value="#ffffff" />'
				 + '<embed src="/flash/Video_player_basic_4x3.swf?myFile=' + video_file + '&amp;playMe=true" background="#FFFFFF" wmode="transparent" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="225" height="225"></embed>'
				 + '</object>');
			}
			if (showvideo == true) {
				show_video();
			}

			function show_audio() {
				$(".imageContainer").hide();	
				$(".videoContainer").hide();	
				$("#videoPlayer").html('&nbsp;');	// empty video container to be sure to stop that movie
				$(".audioContainer").removeClass("hidden");	
				$(".audioContainer").show();	
				$("#audioPlayer").html('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8" width="225" height="40">'
				 + '<param name="movie" value="/flash/audioplayer2.swf?myFile=' + audio_file + '&amp;playMe=true" />'
				 + '<param name=quality value=high />'
				 + '<param name="wmode" value="transparent" />'
				 + '<param name="bgcolor" value="#ffffff" />'
				 + '<embed src="/flash/audioplayer2.swf?myFile=' + audio_file + '&amp;playMe=true" background="#FFFFFF" wmode="transparent" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="225" height="40"></embed>'
				 + '</object>');
			}
			if (showaudio == true) {
				show_audio();
			}

			// bekijk video button
			$(".buttonBekijkFilmpje").click(function(){ 
				show_video();
				// b sure that video is in focus
				scrollTo(0,0);
				return false;			
			});

			// beluister audio button
			$(".buttonBeluisterAudio").click(function(){ 
				show_audio();
				// b sure that audio is in focus
				scrollTo(0,0);
				return false;			
			});

			// Sluit audio/video button
			$(".buttonSluitAudioVideo").click(function(){ 
				$(".imageContainer").show();	
				$(".videoContainer").hide();	
				$("#videoPlayer").html('&nbsp;');	// empty video container to be sure to stop that movie
				$(".audioContainer").hide();	
				$("#audioPlayer").html('&nbsp;');	// empty audio container to be sure to stop that clip
				return false;			
			});
			// amateur voorstellingen  onclick
			$("#amateurvoorstellingen").click(function(){ 
				$("#formToonAmateurvoorstellingen").submit();	
				return false;			
			});
			
			/**
			 * autoSelect, jQuery plugin
			 * 
			 * Auto-submit forms when select options have changed
			 * Smoothens out (almost) behavioural differences between browsers.
			 * Inpsired on: http://themaninblue.com/writing/perspective/2004/10/19/
			 * 
			 * usage: $selection.autoSelect({options})
			 * 
			 * @author AK | Peppered
			 * @version 1.0.1
			 */
			(function(a){a.fn.autoSelect=function(){return this.each(function(){var b=a(this);b.validKeyChange=true;b.initValue=a(this).val();b.change(function(){if(b.validKeyChange&&(this.value!=b.initValue)){b.initValue=this.value;this.form.submit()}});b.keydown(function(c){if((c.keyCode==13||c.keyCode==9)&&this.value!=b.initValue){b.validKeyChange=true;b.change()}else{if(c.keyCode==27||((c.keyCode==13||c.keyCode==9)&&this.value==b.initValue)){this.value=b.initValue}else{b.validKeyChange=false}}})})}})(jQuery);
			
			// auto-submit genre/maand select
			
			if ($.fn.autoSelect) {		
				var $form = $('#genreForm');
				//console.log($form);
				var $enabled = $form.find('select').autoSelect({debug: false});		
				if ($enabled.length) {
					$form.addClass('autoSelect');			
				}
				$form = $('#maandForm');
				$enabled = $form.find('select').autoSelect();
				if ($enabled.length) {
					$form.addClass('autoSelect');
				}
				$form = $('#seizoenForm');
				$enabled = $form.find('select').autoSelect();
				if ($enabled.length) {
					$form.addClass('autoSelect');
				}
			}				
});