$(function(){

	$('h1').click(function(){ window.location=$(this).children('a:first').attr('href'); });
	
	$('h2').click(function(){ 
		if($(this).attr('title').toLowerCase() != 'selected')
			window.location=$(this).children('a:first').attr('href'); 
	});
	
	$('.showForm').click(function(){
		var formId = $(this).attr('formId');
		if (formId.length > 0){
			$('#Center').css('height', 'auto');
			$('#'+formId).slideToggle(400);
			setTimeout(function(){
				$('.ResizeToMax').css('height', 'auto');			
		    	setHeigth()
			}, 401);
		}
	});
	
	$('.archiveMonth').click(function(){
		var contentId = 'contentOf' + $(this).attr('id').replace('btnFor', '');
		$('#Center').css('height', 'auto');
		$('#'+contentId).slideToggle(400);
		setTimeout(function(){
			$('.ResizeToMax').css('height', 'auto');			
			setHeigth();
		}, 401);
	});

	
	$('.SmallBlockEvent, .SmallBlockUserEvent, .ArchiveBlockEvent').hover(
		function () {
			var strId =	getNumbersFromString($(this).attr('id'));
			var strColor = switchBgColor($(this).attr('title'));
			$('#Date'+strId).css('background-color', strColor);
			$('#Content'+strId).css('background-color', strColor);
		},
		function () {
			var strId =	getNumbersFromString(jQuery.trim($(this).attr('id')));	
			$('#Date'+strId).css('background-color', '#FFFFFF');
			$('#Content'+strId).css('background-color', '#FFFFFF');
		}
  	);
	
	$('.SmallBlockEvent, .ArchiveBlockEvent').click(function(){
		var strId =	getNumbersFromString($(this).attr('id'));
		window.location=$('#Content'+strId).children('h3:first').children('a:first').attr('href');
	});
	
	$('.SmallBlockUserEvent').click(function(){
		var strId =	getNumbersFromString($(this).attr('id'));
		window.open($('#Content'+strId).children('h3:first').children('a:first').attr('href'));
	});
	
	$('#Right .Images li img').hover(
		function () {
			var strColor = switchBgColor($(this).attr('name'));
			$(this).css('border-color',strColor);
		},
		function () {
			$(this).css('border-color', '#FFFFFF');
		}
	);
	
	$('#Menu li').hover(
		function () {
			var strColor = switchBgColor(strSelectedCat);
			$(this).css('background-color',strColor);
			$(this).children('a').css('text-decoration','underline');
		},
		function () {
			$(this).css('background-color', 'transparent');
			$(this).children('a').css('text-decoration','none');
		}
	);
	
	$('a.Lightbox').lightBox();
	
	$('a.MovieBtn').click(function(){
		$('#BigMovie').load($(this).attr('href')+ ' #BigMovie object');
		return false;
	});
	
	$('form').submit(function(){
		switch($(this).attr('id')){
			case'PostEvent':
			case'loginForm':
			case'PostReaction':
			case'contactForm':
			case'emailFeedForm':
				var returnVal = true;
				var LastRadioButton = '';
				var RadioButtons = new Array();
				var CheckedRadioButtons = new Array();
				var IRB = 0;
				$('#'+$(this).attr('id')+' input.req, #'+$(this).attr('id')+' textarea.req').each(function(){	
					if (returnVal){												 
						switch ($(this).attr('type')){
							default:
							case 'text':
								if ($(this).val().length == 0){
									alert(jQuery.trim($(this).parent().children('label:first').html()) + ' is verplicht');
									$(this).focus();
									returnVal = false;
								}
								
								if($(this).hasClass('email') && returnVal){	
									var goodEmailA = $(this).val().match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
									if (goodEmailA){
									}else{
										window.alert(jQuery.trim($(this).parent().children('label:first').html()) + ' is een ongeldig emailadres.');
										$(this).focus();
										returnVal = false;
									}
								}
							break;
							
							case'radio':
								if (LastRadioButton != $(this).attr('name')){
									LastRadioButton = $(this).attr('name');
									RadioButtons[IRB] = $(this);
									IRB++;
								}
								
								if (CheckedRadioButtons[LastRadioButton] != true)
									CheckedRadioButtons[LastRadioButton] = $(this).attr('checked');
						
							break;
							
							case'checkbox':
								if ($(this).attr('checked') == false){
									alert(jQuery.trim($(this).parent().children('label:first').html()) + ' is verplicht');
									$(this).focus();
									returnVal = false;
								}
							break;
						}
					}
				});
				
				for(i=0;i<IRB;i++){
					var tmpStr = RadioButtons[i].attr('name');
					if (CheckedRadioButtons[tmpStr] == false && returnVal){
						alert(RadioButtons[i].parent().children('label:first').html() + ' is verplicht');
						$(this).focus();
						returnVal = false;
					}
				}
				
				return returnVal;
			break;
			default: return true; break;
			
		}
	});

	$('#Menu li').click(function(){ 
		var URL = $(this).children('a:first').attr('href'); 					 
		window.location=URL; 
		
	});
	
	$(".pngFix").pngfix();
});

function gotoUrlSelectBox(goto){
	var SelectBox = new Array();
	
	$('#FilterForm select').each(function(){
		 SelectBox[$(this).attr('name')] = $(this).val();
	});
	
	var Url = strBaseURL+'/Podia/';
	
	Url += (SelectBox['Podia'].length==0)? 'All' : SelectBox['Podia'];
	Url += (SelectBox['Month'].length==0)? '' : '/Month_'+ SelectBox['Month'];
	Url += (SelectBox['Locatie'].length==0)? '' : '/Locatie_'+ SelectBox['Locatie'];
	Url += '/index.html';
	
	if(goto) window.location=Url;
}

function switchLeftBanner(newNum){
	setTimeout(function(){

		if(LeftBanner.length <= newNum)
			newNum = 0;
		
		if(LeftBanner.length > 1){
			$('#LeftBanners').animate({ opacity: 0}, 600 );
			setTimeout(function(){
				$('#LeftBanners').html(LeftBanner[newNum]);
			}, 601);
	
			$('#LeftBanners').animate({ opacity: 1}, 600 );	
			switchLeftBanner(newNum+1);
		}
		
	}, 3400);
	
}

function switchBgColor(str){
	var strColor = '';
	switch(jQuery.trim(str.toLowerCase())){
		case 'music':		strColor = '#e9915f';	break;
		case 'art':			strColor = '#f5c15e';	break;
		case 'theatre':		strColor = '#94a0bb';	break;
		case 'crossover':	strColor = '#a45053';	break;
		default:			strColor = '#8a8383';	break;
	}
	return strColor;
}

function getNumbersFromString(str){
	var rgxNums = new RegExp('[0-9].*');
	var strMatch = rgxNums.exec(jQuery.trim(str));
	var strReturnVal = '';
	if (strMatch != null)
		for (i = 0; i < strMatch.length; i++)
		  strReturnVal = strReturnVal + strMatch[i];
	return strReturnVal;
}

function setHeigth(){	
	var MenuHeight = parseFloat($('#Menu').height()) + parseFloat($('#Menu').css('top'));
	var HeaderHeight = parseFloat($('#Header').height());
	var ContainerHeight = parseFloat($('#Container').height());
	$('.ResizeToMax').each(function(){
		var defSize = (ContainerHeight - HeaderHeight - parseFloat($(this).css('padding-top')) - parseFloat($(this).css('padding-bottom')));
		if(defSize < MenuHeight)
			defSize = MenuHeight - parseFloat($(this).css('padding-top')) - parseFloat($(this).css('padding-bottom'));
		$(this).css('height', defSize+'px');
	});
}
