$(document).ready(function(){
	$.get('/php/siteConfig.php');
	
	// INTERVIEW || STARTING UP
	if( $('.interview').size()>0 || $('.starting-up').size()>0 ) {
		$(".filter-box .active").parent().addClass("minus");
		$(".filterhead.industrie-rol, .filterhead.specialisme").parent().addClass("minus");
		
		$(".filter-box:not(.minus) a").hide();
		$(".filter-box .filterhead").click(function(){
			if ( $(this).parent().hasClass("minus") ) {
				$(this).parent().removeClass("minus").find("a").slideUp();
			} else {
				$(this).parent().addClass("minus").find("a").slideDown();
			}
		});
	}

	// INTERVIEW DETAIL
	if( $('.detail.interview').size()>0 ) {

		$("#vote").click(function(){
			var obj = $(this);			
			if( !obj.hasClass("finished") ) {
				$.post("/ajax.php", { type: "vote", toVote: $(this).attr("href") },
				function(data){
					switch(data){
						case "succes":
							obj.addClass("voted").addClass("finished").html("Dank je wel!");
							var votes = parseInt($("#vote-count").html())+1;
							$("#vote-count").html( votes );
							if ( votes == 1 ) {
								$("#vote-txt").html("persoon vond");
							} else if ( votes == 2 ) {
								$("#vote-txt").html("personen vonden");
							}
							
							break;
							
						case "time-lock":
							obj.addClass("finished").html("Je hebt al gestemd").after("<strong>Je mag maximaal 1x in 24u op een interview stemmen.</strong><br />").slideUp();
							break;
							
						default:
							obj.after("<strong>Er is een fout opgetreden. Probeer later aub nog een keer te stemmen.</strong>");
							break;
					}
				});
			}
			return false;
		});
	} 
	
	
	// GLOBAL
	$(".wide .box:last:not(.excl)").css({paddingBottom:"30px"});
	var height = 0;
	$(".h-fix").each(function(){
		if ( $(this).height() > height ) {
			height = $(this).height();
		}
	}).css({minHeight: height+"px"});
	
	$(".item:even").addClass("odd");
	
	$.datepicker.setDefaults($.datepicker.regional['nl']);
	if( $("#form_date").size() > 0 ){
		$("#form_date, #form_end_date").datepicker({ dateFormat: 'dd/mm/yy' });
	}
	
	$(".numeric").keyup(function(){
		$(this).val( $(this).val().replace(/[a-z]/gi,'').replace(",",".") );
	});
	
	$("#location-picker span").toggle(function(){
		$(this).next().slideDown();
	},function(){
		$(this).next().slideUp();
	});
	
	if ( $(".excl").size()>0 ) {
		var h = -920;
		h = h + $(".excl").height() + 30;
		$("body").css({backgroundPosition: "0 "+h+"px"});
	}
	
	$("textarea[maxlength]").keyup(function(){
		var max = parseInt($(this).attr("maxlength"));
		
		if ( $(this).val().length > max ) {
			$(this).val($(this).val().substr(0, $(this).attr("maxlength")));
		}
		
		$(this).parent().find('.charsRemaining').html((max - $(this).val().length) + ' tekens resterend. (max. ' + max + ' tekens)');
	});
	
	$(".dialog").each(function(){
		
		var item = $("#dialog-" + $(this).attr("id"));
		
		var dHeight = ( $(this).hasClass("d-agenda") ) ? 600 : 350;
		
		var d = $(this).dialog({
			autoOpen: false,
			bgiframe: true,
			resizable: true,
			width:570,
			height: dHeight,
			modal: true
		});
		
		item.click(function(){
			d.dialog('open');
			return false;
		});
		
		$('#submit',this).click(function(){
			var form = $(this).parent().parent();
			if( checkForm( form, "string" ) == "true" ) {
				
				if ( form.hasClass("mail") ) {
					
					var fName = $("#form_name").val();
					var fEmail = $("#form_email").val();
					var fMsg = $("#form_msg").val();
					var fX = $("#x").val();
					var fY = $("#y").val();
					var fZ = $("#z").val();
					
					$.post("/ajax.php", { type: "mail", name: fName, email: fEmail, msg:fMsg, x: fX, y:fY, z:fZ },
					function(data){
						switch(data){
							case "succes":
								$("#remind").removeClass("focus");
								form.parent().html("<h2>Dank je wel! Je bericht is verzonden!</h2>");
								setTimeout(function(){ window.location=form.attr("action"); },2000);
								break;
								
							default:
								$("#remind").addClass("focus").html("<strong>Er is een fout opgetreden. Probeer het later aub nog een keer.</strong>");
								break;
						}
					});
				// end mail
				}				
			}
			return false;		
		});
		
	});
	
	$("form").submit(function(){
		return checkForm( $(this), "boolean" );
	});
	
	function checkForm(obj,type){
		var errors = 0;
		$(".obliged",obj).each(function(){
			if( $(this).val() == "" || $(this).val()==$(this).attr("title") ) {
				$(this).addClass("focus").focus();
				errors++;
			}
		});
		
		if ( errors > 0 ) { 
			$("#remind").addClass("focus").animate({fontSize:"14px"}).fadeTo(2000,1).animate({fontSize:"11px"});
			return (type=="string") ? "false" : false;
		} else {
			return (type=="string") ? "true" : true;
		}
	}
	
	function fbCount(id){
		alert(id);
	}
	
	$('a[href^="http://"]').attr({target: "_blank"});
	
});
 