function myToggleClass(fieldId, fieldClass) {
	document.getElementById(fieldId).className = fieldClass;
}

function submitForm(id) {
	if (id == 'searchForm') {
		if (document.getElementById('query').value == 'Uw zoekopdracht') {
			document.getElementById('query').value = '';
		}
	}
	document.forms[id].submit();
}

function printwindow(mypage, myname, w, h) {
	LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100;
	TopPosition = (screen.height) ? (screen.height - h) / 2 : 100;
	settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
	win = window.open(mypage, myname, settings);
}

function updateDatePullDownMenu(objForm, fieldName) {
	var pdmDays = fieldName + "_days";
	var pdmMonths = fieldName + "_months";
	var pdmYears = fieldName + "_years";

	time = new Date(objForm[pdmYears].options[objForm[pdmYears].selectedIndex].text, objForm[pdmMonths].options[objForm[pdmMonths].selectedIndex].value, 1);

	time = new Date(time - 86400000);

	var selectedDay = objForm[pdmDays].options[objForm[pdmDays].selectedIndex].text;
	var daysInMonth = time.getDate();

	for ( var i = 0; i < objForm[pdmDays].length; i++) {
		objForm[pdmDays].options[0] = null;
	}

	for ( var i = 0; i < daysInMonth; i++) {
		objForm[pdmDays].options[i] = new Option(i + 1);
	}

	if (selectedDay <= daysInMonth) {
		objForm[pdmDays].options[selectedDay - 1].selected = true;
	} else {
		objForm[pdmDays].options[daysInMonth - 1].selected = true;
	}
}

// BOOKMARK FUNCTION
function addToFavorites(obj, url, title) {
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url, "");
	} else if (window.external) { // IE Favorite
		window.external.AddFavorite(url, title);
	} else if (window.opera && window.print) {
		// Opera Hotlist
		obj.setAttribute('href', url);
		obj.setAttribute('title', title);
		obj.setAttribute('rel', 'sidebar');
		obj.click();
	}
	return false;
}

function showDialog(p_sTitle, p_sHtml, p_iW, p_iH) {
	if (empty(p_sTitle)) {
		p_sTitle = '';
	}
	if (empty(p_sHtml)) {
		p_sHtml = '';
	}
	if (empty(p_iW)) {
		p_iW = 400;
	}
	if (empty(p_iH)) {
		p_iH = 240;
	}

	$("#dialog").attr('title', p_sTitle).html(p_sHtml).dialog({
		height: p_iH,
		width: p_iW,
		modal: true
	});
}

var oPostData = {
	firstname: null,
	lastname: null,
	email: null,
	room: null
};

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
	return pattern.test(emailAddress);
};
	
function formSubmitted(){
	$('div.errorBox').remove();
	var sHtmlSuccess = '<h1>Het formulier is succesvol verzonden</h1><br />Wij hebben alle gegevens en bestanden ontvangen. We doen ons best om deze zo spoedig te boordelen en ze daarna te plaatsen op onze website.<br /><br />Met vriendelijke groet,<br /><br />Het College';
	$('#mediaForm').before(sHtmlSuccess);	
	$('#mediaForm').remove();		
}

var oUploadify = null;
function saveFiles(){
	oUploadify.uploadifySettings('postData', oPostData);	
	oUploadify.uploadifyUpload();						
}

$(document).ready(function() {
	
	// IMPORTANT: PUT ALLWAYS ANY REFERENCE TO AN ID ATTR. INSIDE AN IF STATEMENT!!
	// BECAUSE OF VIDEO AND IMAGES LIGHTBOX MODULES
	
	if($('#upload_media').length){
		oUploadify = $('#upload_media');
		oUploadify.uploadify({
			fileObjName: 'upload_media',
		    swf: 'ext/jquery/uploadify/uploadify.swf',
		    langFile: 'ext/jquery/uploadify/uploadifyLang_nl.js',
		    uploader: 'template/front_media.php',
		    buttonText: 'Kies bestanden',
		    cancelImage: '/template/images/cancel.gif',
			fileTypeDesc: 'Mediabestanden', 
			fileTypeExts: '*.jpg;*.mov;*.avi;*.3gp',		    
		    auto: false,
			fileSizeLimit: 104857600,
			multi: true,
			queueID: false,
			queueSizeLimit: 20,
			onQueueComplete: function(){
				formSubmitted();
			},
			onSelect: function(event) {
				if(event.size > 1024*1024) {
					alert('Afbeeldingen (.jpg) mogen niet groter zijn dan 1 Mb');
					$('#upload_media').uploadifyCancel(event.id);
				}
		    }
		});
		
		$('#mediaForm').submit(function(){

			$('div.errorBox').remove();			
			var fname = $('#firstname').val();
			var lname = $('#lastname').val();
			var email = $('#email').val();
			var room = $('#room').val();
			var urls = $('#urls').val();
			var upload_media = $('#upload_media_queue').html();
			var aErrors = [];

			if(empty(fname)){
				aErrors.push('Uw voornaam is niet ingevuld of is ongeldig!');
			}
			
			if(empty(lname)){
				aErrors.push('Uw achternaam is niet ingevuld of is ongeldig!');
			}
			
			if(empty(email) || !isValidEmailAddress(email)){
				aErrors.push('Uw e-mailadres is niet ingevuld of is ongeldig!');
			}
			
			if(empty(upload_media) && empty(urls)){
				aErrors.push('Selecteer bestanden of geeft tenminste een Youtube URL op!');
			}
			
			if(aErrors.length == 0){

				oPostData.firstname = fname;
				oPostData.lastname = lname;
				oPostData.email = email;
				oPostData.room = room;
				
				if(!empty(urls)) {
					var oPostUrls = $.extend({urls: urls}, oPostData);
					$.ajax({
						  url: "template/front_media.php",
						  type: 'post',
						  data: oPostUrls,
						  success: function(){
							if(!empty(upload_media)){
								saveFiles();
							} else {
								formSubmitted();
							}
						}
					});					
				} else {
					saveFiles();
				}
								
			} else {
				var sHtmlError = '';
				sHtmlError += '<div class="errorBox">';
				sHtmlError += '<h6>Oeps! Er ging iets niet helemaal goed...</h6>';
				sHtmlError += '<ol>';
				$.each(aErrors, function(i, v) { 
					sHtmlError += '<li>' + v + '</li>';
				});	
				sHtmlError += '</ol></div>';				
				$('#mediaForm').before(sHtmlError);
			}
			return false;
		});
	}
			
	var addthis_config = {
		pubid: "xa-4d302a3672b32ce1",
		ui_508_compliant: true,
		ui_language: "nl"
	};
	
	// start addthis app
	if($('#addThisBox').length){
		addthis.init();
	}	
	
	// bof accident form js
	if($("#victim_type").length){
		$("#victim_type").change(function(){
			if($("#victim_type option:selected").val() == 'Anders nl.'){
				$("#victimTypeOther").slideDown();
			} else {
				$("#victimTypeOther").slideUp();
			}
		});
	}
	
	if($("#accident_type").length){
		$("#accident_type").change(function(){
			if($("#accident_type option:selected").val() == 'Anders nl.'){
				$("#accidentTypeOther").slideDown();
			} else {
				$("#accidentTypeOther").slideUp();
			}
		});
	}
	
	if($("#transport").length){
		$("#transport").change(function(){
			if($("#transport option:selected").val() == 'Per auto, door...'){
				$("#transportOther").slideDown();
			} else {
				$("#transportOther").slideUp();
			}
		});
	}
	// eof accident form js
	
	if($("#date, #member_dob").length){
		$("#date, #member_dob").datepicker({ dateFormat: 'dd-mm-yy' });
	}
		
	// Top main banner
	if ($('#fadeBanner1').length) {
		$('#layoutBanner').cycle({
			fx: 'fade',
			speed: 3000,
			timeout: 7000
		});
	}

	// Side banner
	if ($('#fadeSideBanner1').length) {
		$('#sideBanners').cycle({
			fx: 'fade',
			speed: 1500
		});
	}

	// Fancybox for gallery added in cms sitestructure
	if ($("table.gallerytable").length) {
		$("table.gallerytable a").attr('rel', 'fancybox').fancybox({
			onStart: function(){
				$('div.newsItem iframe').css('visibility','hidden');
			},
			onClosed: function(){
				$('div.newsItem iframe').css('visibility','visible');
			} 					
		});
	}
	
	// Fancybox for: Filmboeken (youtube videos)
	if ($("#showVideos").length) {
		
		$('div.sidebarBox.movieAlbums div.sidebarContent').click(function() {
			$("#showVideos").trigger('click');
		});
		
		$("#showVideos").fancybox({
			width: '100%',
			height: '100%',
			autoScale: true,
			transitionIn: 'none',
			transitionOut: 'none',
			type: 'iframe',
			onStart: function(){
				$('div.newsItem iframe').css('visibility','hidden');
			},
			onClosed: function(){
				$('div.newsItem iframe').css('visibility','visible');
			} 					
		});
	} 
	
	
	// Fancybox for: Fotoboeken 
	if ($("#showImages").length) {
		
		$('div.sidebarBox.photoAlbums div.sidebarContent').click(function() {
			$("#showImages").trigger('click');
		});
		
		$("#showImages").fancybox({
			width: '100%',
			height: '100%',
			autoScale: true,
			transitionIn: 'none',
			transitionOut: 'none',
			type: 'iframe',
			onStart: function(){
				$('div.newsItem iframe').css('visibility','hidden');
			},
			onClosed: function(){
				$('div.newsItem iframe').css('visibility','visible');
			} 								
		});
	}	
	
	// Accordion
	if ($("#accordionContent").length) {
		$("#accordionContent").accordion();
	}

	// Input text field toggle
	// Set default text in attr value and add class to input
	// field with
	// classname: toggle
	$('input:text.toggle').each(function(idx, elem) {
		var oInput = $('#' + elem.id);
		oInput.click(function() {
			if (empty(oInput.attr('rel')) || oInput.attr('rel') == oInput.val()) {
				oInput.attr('rel', oInput.val());
				oInput.val('');
			}
		});
		oInput.blur(function() {
			if (oInput.val() == '') {
				oInput.val(oInput.attr('rel'));
			}
		});
	});

	$('#boxAgenda').closest('li').toggleClass('opened');
	$('ul.siteNavMenu .opened').each(function(idx, elem) {
		$('ul.siteNavMenu .opened ul').css('display', 'block');
	});

	$('span.toggler').click(function() {
		$(this).next('ul').slideToggle(function() {
			$(this).closest('li').toggleClass('opened');
		});
	});

	// AGENDA AND IMAGES GALLERIES SCROLLER
	var iFactor = 8;
	var iCurrentImg = 0;
	var aLargeImages = [];
	
	function stopSlide(obj) {
		obj.stop(true);
	}
		
	//________________________ VIDEO GALLERY ____________________//
	
	if($('#videosContainer').length){

		var oVideoContainerItems = $('#videoContainerItems');
		var oSidebarGalleryUp = $('#videosContainer .up');
		var oSidebarGalleryDn = $('#videosContainer .down');
		
		oSidebarGalleryUp.mouseenter(function() {
			stopSlide(oVideoContainerItems);
			var iDuration = Math.abs(parseInt(oVideoContainerItems.css('top'))) * iFactor;
			var iDivTop = Math.abs(parseInt(oVideoContainerItems.css('top')));
			if (iDivTop > 0) {
				oVideoContainerItems.animate({
					top: 0
				}, {
					duration: iDuration,
					easing: 'linear'
				});
			}
		});
		
		oSidebarGalleryDn.mouseenter(function() {
			oSidebarGalleryDn.trigger('autoSlide', [iFactor, true, 0]);
		});

		oSidebarGalleryDn.bind('autoSlide', function(event, iFac, bStop, iDelay) {
			if (bStop === true) {
				stopSlide(oVideoContainerItems);
			}
			var iDuration = (oVideoContainerItems.height() - 400 - Math.abs(parseInt(oVideoContainerItems.css('top')))) * iFac;
			var iDivTop = oVideoContainerItems.height() - 400;

			if (iDivTop > Math.abs(parseInt(oVideoContainerItems.css('top')))) {
				oVideoContainerItems.delay(iDelay).animate({
					top: '-' + iDivTop
				}, {
					duration: iDuration,
					easing: 'linear'
				});
			}
		});
		
		$('#videoContainerItems .videoGalleryItem').mouseenter(function() {
			stopSlide(oVideoContainerItems);
		});			
		
		// slide gallery onload (if desired uncomment)
		//oSidebarGalleryDn.trigger('autoSlide', [(iFactor * 4), false, 2000]);

		//oSidebarGalleryUp.mouseout(function() {
		//	stopSlide(oVideoContainerItems);
		//});

		//oSidebarGalleryDn.mouseout(function() {
		//	stopSlide(oVideoContainerItems);
		//});
	}
	//________________________ VIDEO GALLERY ____________________//
	
	
	//________________________ IMAGE GALLERY ____________________//
	
	function loadImage(){		
		$('#galleryImgLarge').attr('src', aLargeImages[iCurrentImg]);
	}
	
	// IMAGES GALLERIES HORIZONTAL SCROLLER
	if($('#galleryScroll').length){
		
		$('td.galleryImageScroller img').each(function(index) {
			aLargeImages[index] = $(this).attr('alt');
		});	
		
		$('#scrollerLeft').click(function() {
			if((iCurrentImg-1) >= 0){
				iCurrentImg = (iCurrentImg-1);
				loadImage();
			}			
		});

		$('#scrollerRight').click(function() {
			if((iCurrentImg+1) < aLargeImages.length){
				iCurrentImg = (iCurrentImg+1);
				loadImage();
			}			
		});				
	}
	
	// IMAGES GALLERIES HORIZONTAL SCROLLER CLICKED THUMB LOADS BIG IMG
	if($('img.galleryImg').length){		
		var oGalleryImg = $('img.galleryImg');
		oGalleryImg.click(function() {
			$('#galleryImgLarge').attr('src', $(this).attr('alt'));
		});
	}	
	
	// IMAGE GALLERIES	
	if($('#galleriesContainer').length){

		var oGalleryContainerItems = $('#galleryContainerItems');
		var oSidebarGalleryUp = $('#galleriesContainer .up');
		var oSidebarGalleryDn = $('#galleriesContainer .down');
		
		oSidebarGalleryUp.mouseenter(function() {
			stopSlide(oGalleryContainerItems);
			var iDuration = Math.abs(parseInt(oGalleryContainerItems.css('top'))) * iFactor;
			var iDivTop = Math.abs(parseInt(oGalleryContainerItems.css('top')));
			if (iDivTop > 0) {
				oGalleryContainerItems.animate({
					top: 0
				}, {
					duration: iDuration,
					easing: 'linear'
				});
			}
		});
		
		oSidebarGalleryDn.mouseenter(function() {
			oSidebarGalleryDn.trigger('autoSlide', [iFactor, true, 0]);
		});

		oSidebarGalleryDn.bind('autoSlide', function(event, iFac, bStop, iDelay) {
			if (bStop === true) {
				stopSlide(oGalleryContainerItems);
			}
			var iDuration = (oGalleryContainerItems.height() - 400 - Math.abs(parseInt(oGalleryContainerItems.css('top')))) * iFac;
			var iDivTop = oGalleryContainerItems.height() - 400;

			if (iDivTop > Math.abs(parseInt(oGalleryContainerItems.css('top')))) {
				oGalleryContainerItems.delay(iDelay).animate({
					top: '-' + iDivTop
				}, {
					duration: iDuration,
					easing: 'linear'
				});
			}
		});
		
		$('#galleryContainerItems .imageGalleryItem').mouseenter(function() {
			stopSlide(oGalleryContainerItems);
		});			
		
		// slide gallery onload (if desired uncomment)
		//oSidebarGalleryDn.trigger('autoSlide', [(iFactor * 4), false, 2000]);

		//oSidebarGalleryUp.mouseout(function() {
		//	stopSlide(oGalleryContainerItems);
		//});

		//oSidebarGalleryDn.mouseout(function() {
		//	stopSlide(oGalleryContainerItems);
		//});
	}
	//________________________ IMAGE GALLERY ____________________//
	
	
	
	//________________________ AGENDA GALLERY ___________________//
	var oAgendaContainerItems = $('#agendaContainerItems');	
	var oSidebarAgendaUp = $('#sidebarAgenda .up');
	var oSidebarAgendaDn = $('#sidebarAgenda .down');

	oSidebarAgendaUp.mouseenter(function() {
		stopSlide(oAgendaContainerItems);
		var iDuration = Math.abs(parseInt(oAgendaContainerItems.css('top'))) * iFactor;
		var iDivTop = Math.abs(parseInt(oAgendaContainerItems.css('top')));
		if (iDivTop > 0) {
			oAgendaContainerItems.animate({
				top: 0
			}, {
				duration: iDuration,
				easing: 'linear'
			});
		}
	});
	
	$('#agendaContainerItems .agendaItem').mouseenter(function() {
		stopSlide(oAgendaContainerItems);
	});	
	
	oSidebarAgendaDn.mouseenter(function() {
		oSidebarAgendaDn.trigger('autoSlide', [iFactor, true, 0]);
	});

	oSidebarAgendaDn.bind('autoSlide', function(event, iFac, bStop, iDelay) {
		if (bStop === true) {
			stopSlide(oAgendaContainerItems);
		}
		var iDuration = (oAgendaContainerItems.height() - 172 - Math.abs(parseInt(oAgendaContainerItems.css('top')))) * iFac;
		var iDivTop = oAgendaContainerItems.height() - 172;

		if (iDivTop > Math.abs(parseInt(oAgendaContainerItems.css('top')))) {
			oAgendaContainerItems.delay(iDelay).animate({
				top: '-' + iDivTop
			}, {
				duration: iDuration,
				easing: 'linear'
			});
		}
	});

	// slide agenda onload
	//oSidebarAgendaDn.trigger('autoSlide', [(iFactor * 4), false, 2000]);

	oSidebarAgendaUp.mouseout(function() {
		stopSlide(oAgendaContainerItems);
	});

	oSidebarAgendaDn.mouseout(function() {
		stopSlide(oAgendaContainerItems);
	});
	//________________________ AGENDA GALLERY ___________________//
	
});
