$.fn.hasAttr = function(name) {  
   return this.attr(name) !== undefined;
};

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}


jQuery(function($){
	$.datepicker.regional['nl'] = {clearText: 'Wissen', clearStatus: '',
		closeText: 'Fermer', closeStatus: 'Sluiten',
		prevText: 'vorige', prevStatus: 'vorige maand',
		nextText: 'volgende', nextStatus: 'volgende maand',
		currentText: 'huideige', currentStatus: 'deze maand',
		monthNames: ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'],
		monthNamesShort: ['jan','feb','maa','apr','mei','jun','jul','aug','sep','okt','nov','dec'],
		monthStatus: 'andere maand', yearStatus: 'ander jaar',
		weekHeader: 'Wk', weekStatus: '',
		dayNames: ['zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag'],
		dayNamesShort: ['zo','ma','di','wo','do','vr','za'],
		dayNamesMin: ['zo','ma','di','wo','do','vr','za'],
		dayStatus: 'gebruik andere dag als eerste weekdag', dateStatus: 'kies formaat',
		dateFormat: 'dd/mm/yy', firstDay: 0, 
		initStatus: 'selecteer een datum', isRTL: false};
	$.datepicker.setDefaults($.datepicker.regional['nl']);
});

$(document).ready(function(){
						   
	
						   
	$(".cloud, .smallcloud").mouseover(function(){$(this).css( "background-color" , "rgba(255,255,255,1)")}).mouseout(function(){$(this).css("background-color","rgba(255,255,255,.8)")});
	
	$('.depth_0').mouseover(function(){
		idar=$(this).attr('id').split('_');
		$('#sub_' + idar[1]).show();	
	}).mouseout(function(){
		idar=$(this).attr('id').split('_');
		$('#sub_' + idar[1]).hide();		
	});
	$('.sub').mouseover(function(){
		$(this).show();	
	}).mouseout(function(){
		$(this).hide();	
	});
	
	startshow=0;
	$('#slideshow').css({'background':'url(' + FLICKR[1] +  ') ' + FLICKRBGPOS[1] +  ' no-repeat'});
	$('#slideshowtop').css({'background':'url(' + FLICKR[0] +  ') ' + FLICKRBGPOS[0] +  ' no-repeat'});
	
	setTimeout("chpic(" + startshow +  ",'out')",4000);
	
	$('#slideshow, #slideshowtop, #picdesc, #picdesctxt').click(function(){
		window.location.href='http://www.flickr.com/photos/49445426@N07/sets/72157624099455490/';	
	});
	$('#agenda').datepicker({create : setdays(0,0), onChangeMonthYear: function(year, month, inst){
		setdays(month,year);													 
	}});
	
	$('#nbaanmelden').click(function(){
		email=$('#nbemail').val();
		if(email.length){
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			if(!emailReg.test(email)){
				alert('Geen geldig e-mailadres ingevuld');	
			}
			else{
				$.post("ajax/nieuwsbrief.php",{email : email},function(data){
					if(data==0){
						alert('Dit e-mailadrs is al aangemeld!');	
					}
					else if(data==1){
						$('#nbcloud').html('<strong class="ok">Bedankt voor uw aanmelding!</strong>');	
					}
				});
			}
		}
		else{
			alert('Geen e-mailadres ingevuld');	
		}
	});
	
	$('#contactform').submit(function(){
		if(!$('#vraag').val().length || !$('#naam').val().length || !$('#email').val().length){
			alert('U dient alle velden in te vullen!');	
			return false;
		}
		else{
			return true;	
		}
	});
	
	//setdays(0,0);		
	$('#ideesend').click(function(event){
		event.preventDefault();
		if($(this).text().indexOf('Idee')>-1){
			$(this).text('Sluiten (x)');
			$('#idform').slideDown('fast');	
		}
		else{
			$(this).text('Idee inzenden >>');
			$('#idform').slideUp('fast');	
		}
	});
	
	$('#ideeform').submit(function(){
		required=new Array("titel","idee","naam","email");
		proceed=true;
		for(i=0; i<required.length; i++){
			if(!$('#' + required[i]).val().length){
				alert("Niet alle verplichte velden zijn ingevuld!'");
				proceed=false;	
				break;
			}
		}
		return proceed;
	});
	
	$('#reageer').click(function(){
		reacl=$(this).attr('class').split('_');
		$('#content_id').val(reacl[1]);
		cleft=($(document).width() - $('#commentdiv').outerWidth()) / 2;
		ctop=($(window).height() - $('#commentdiv').outerHeight()) / 2;
		$('#commentdiv').css({'left': cleft + 'px','top': ctop + 'px'});
		$('#wrapper').animate({'opacity':'.3'},500,function(){
			$('#commentdiv').show();
		});
	});
	
	$('#sendcom').click(function(event){
		$('#comerror').hide();
		$('.commentinput').css('border-color','#ccc');
		event.preventDefault();
		$(this).attr('disabled','disabled');
		$.post('index.php',{go : 'home.doreactie', naam : $('#naam').val() , email : $('#email').val(), tekst : $('#tekst').val(), captcha : $('#captcha').val(), content_id : $('#content_id').val()},function(data){
			if(data=="OK"){
				location.href="index.php?go=home.idee";	
			}
			else{
				fout=data;
				$('#' + data).css('border-color','#c00');
				$('#comerror').fadeIn('fast').text('U heeft een of meer velden niet of niet correct ingevuld!');
				$('#sendcom').attr('disabled',false);
			}	
		});
	});
	
	$('.closecom').click(function(){
		$('#commentdiv').hide();
		$('#wrapper').animate({'opacity':'1'},500);						  
	});
	
	showarchief($('.archive_year').first().text(), $('.archive_month').first().attr('id'));
	
		
});
function showarchief(y,m){
	if(y && m){
		mnar=m.split('_');
		year=y;
		month=mnar[2];
		$('.archive_month').removeClass('archive_current').hide();
		$('.year_' + year).css({'display' : 'block'});
		$('#month_' + year + '_' + month).css({'display' : 'block'});
		$('#year_' + year).addClass('archive_current');
		$('#month_' + year + '_' + month).addClass('archive_current');
		$('#month_' + year + '_' + month).css({'background-image':'url(art/graphics/loading_small.gif)'});
		$.getJSON("ajax/archief.php",{y:year,m:month},function(data){
			$('#month_' + year + '_' + month).css({'background-image':'url(art/graphics/archive_down.png)'});
			$('.archive_title_ul').remove();
			NUL=$(document.createElement("ul"));
			NUL.addClass('archive_title_ul');
			$.each(data, function(key, val) {
				NLI=$(document.createElement("li"));
				if(val["titel"].length){
					NHREF=$(document.createElement("a"));
					NHREF.addClass('archive_title');
					NHREF.html('<span class="archive_date">' + val["dag"] + '</span> - ' + val["titel"]);
					NHREF.attr('href','index.php?go=home.artikel&artid=' + val["id"]);
					NLI.append(NHREF);
				}
				NUL.append(NLI);
			});
			NUL.insertAfter('#month_' + year + '_' + month);
		});
		$('.archive').click(function(event){
			event.preventDefault();
			idar=$(this).attr('id').split('_');
			yr=idar[1];
			if(idar.length==3){
				mn='month_' + yr + '_' + idar[2];	
			}
			else{
				mn=$('.year_' + yr).eq(1).attr('id');	
			}
			showarchief(yr,mn);
		});
	}
}



function chpic(nr,dir){
	nnr=(nr+1<=FLICKR.length)?nr+2:0;
	if(dir=="out"){
		$('#picdesctxt').fadeOut(250,function(){
			$('#picdesctxt').html(FLICKRTXT[nr+1]);
			$('#picdesctxt').fadeIn(250);
		});
		$('#slideshowtop').animate({'opacity':0},500,function(){
			$('#slideshowtop').css({'background':'url(' + FLICKR[nr + 2] +  ') ' + FLICKRBGPOS[nr + 2] +  ' no-repeat'});
			setTimeout("chpic(" + parseInt(nr+1) +  ",'in')",4000);
		});
	}
	else if(dir=="in"){
		$('#picdesctxt').fadeOut(250,function(){$('#picdesctxt').html(FLICKRTXT[nr+1]);
		$('#picdesctxt').fadeIn(250);});
		$('#slideshowtop').animate({'opacity':1},500,function(){
			$('#slideshow').css({'background':'url(' + FLICKR[nr + 2] +  ') ' + FLICKRBGPOS[nr + 2] +  ' no-repeat'});
			setTimeout("chpic(" + parseInt(nr+1) +  ",'out')",4000);
		});
	}
}
function setdays(m,y){
	$.get("ajax/agenda.php",{m:m,y:y},function(data){
		spans=($('div.ui-datepicker-title').first());
		spans.html('<a href="index.php?go=home.agenda&date=0-' + m + '-' + y + '">' + spans.html() + '</a>');
		$('a.ui-state-default').each(function(){
			dayar=data.split(',');
			if(!inArray($(this).text(),dayar)){
				$(this).parent().append('<span>' + $(this).text() + '</span>');
				$(this).remove();
			}
			else{
				$(this).click(function(event){
					event.preventDefault();
					window.location.href='index.php?go=home.agenda&date=' + $(this).text() + '-' + m + '-' + y;
				});
				//$(this).attr('href','index.php?go=home.agenda&date=' + $(this).text() + '-' + m + '-' + y);	
			}
		});
	});
}
