function include(file){  
	if (document.createElement && document.getElementsByTagName) {
		var head = document.getElementsByTagName('head')[0];
		var script = document.createElement('script');
		script.setAttribute('type', 'text/javascript');
		script.setAttribute('src', file);
		head.appendChild(script);
	}else{
	   //browser is te oud!
	   alert('de browser die je gebruikt is te oud');
	}   
}  	

$.fn.search = function(searchElements) {
  $(this).keyup(function(){
    var searchString = $(this).val(searchString);
     if (searchString.length > 0){ 
        $(searchElements).hide();
        $(searchElements+':containsIgnoreCase(' +searchString+ ')').show();
     } else {
        $(searchElements).show();
     }
  }); 
};
jQuery.extend(jQuery.expr[':'], {
	containsIgnoreCase: function(a,i,m) { return(a.textContent||a.innerText||jQuery(a).text()||'').toLowerCase().indexOf((m[3]||'').toLowerCase())>=0}
});

$.easing.easeInOutExpo = function (x, t, b, c, d) { // slow start bij scrollTo
	if (t==0) return b;
	if (t==d) return b+c;
	if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
	return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
}

function lma(modurl,target){
	//load module via ajax
	 $('#'+target).prepend('<div class="loadingdiv"><img src="/gfx/loading3.gif" align="absmiddle">&nbsp;Bezig met laden ...</div>');
	 $.ajax({
	   type: "GET",
	   url: modurl,
	   success: function(data){
			var $targeter = $("#"+target);
			$targeter.html(data);
			if($targeter.offset().top<$(document).scrollTop()){
				$.scrollTo($targeter, 500, {offset:-50} ); 
			}
		}
	 });
}

function checkemail(adres){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(adres))
		return true;
	else{
		return false;
	}
}

function formValidate(formID){
	var clear=false;
	var emailError='';
	$('.required').css("background-color","transparent");
	
	$("#"+formID+" .required").each(function(){
		if($(this).attr('type')=="checkbox"){
			if(!$(this).attr('checked')){
				$(this).parent().css("background-color","#ff0000");
				$(this).click(function(){$(this).parent().css("background-color","#f9f9f9")});	
				clear=true;
			}else{
				$(this).parent().css("background-color","#f9f9f9");
			}
		}else{
			if($(this).hasClass('email')){
				if(!checkemail($(this).val())){
					$(this).css("background-color","red").click(function(){$(this).css("background-color","transparent")});
					$(this).blur(function(){$(this).css("background-color","transparent")});		
					emailError="Er is een ongeldig e-mailadres ingevoerd.<br><br>";		
					clear=true;
				}
			}			
			else if($(this).val()==""){
				$(this).css("background-color","red").click(function(){$(this).css("background-color","transparent")});
				$(this).blur(function(){$(this).css("background-color","transparent")});
				clear=true;
			}else{
				$(this).css("background-color","transparent");
			}
		}
	});

	if(clear){
		$('#formReturn').html('<div class="focus_orange">'+emailError+'Vul alle rood gemarkeerde velden in!</div>').fadeIn();
		setTimeout(function(){$('#formReturn').fadeOut();},1500);
		return false;
	}else{
		return true;
	}
}

function gotoUrl(url){
	location.href = document.getElementsByTagName('base')[0].href + url;
}

function loadCss(url){
	$("head").append('<link href="'+url+'" rel="stylesheet" type="text/css" />');
}

function filterUsersList(page){
	$("#sul-p").val(page);
	var options = $("#sul-form").serialize();
	$('#sul-results').prepend('<div class="loadingdiv"><img src="/gfx/loading3.gif" align="absmiddle">&nbsp;Bezig met laden ...</div>');

	$.post("/ajax/show_users_list.php", options,
	  function(data){
	  	$("#sul-results").html(data);
	  });
}

function fillPlaatsenSelect(landId,targetId){
	if (landId > 0){
		$("#"+targetId).removeOption(/./);
		$("#"+targetId).addOption("none", "- kies een plaats - "); 
		$("#"+targetId).ajaxAddOption("/js/json/plaatsen_json.php",{land:landId},false);
	}
}

var DelayTimeout = undefined;
function inputDelay(func){
	if(DelayTimeout != undefined) {
		clearTimeout(DelayTimeout);
	}
	$functie = func;
	DelayTimeout = setTimeout(function() {
	        DelayTimeout = undefined;
	        eval(""+$functie+"(1)");
	}, 500);
}

/* comments */

function addComment(){
	$("#addCommentMsg").html('<img src="/gfx/loading3.gif" align="absmiddle" />&nbsp;Bezig met opslaan<br />');
	var options = $("#addCommentForm").serialize();
	$("#addCommentForm input").attr('disabled',true);
	$.post("/ajax/comments_util.php", options,
	  function(data){
	  	$("#comments-parent").html(data);
		//$("#commentsDiv").prepend('<div id="parentSlider" style="display:none;">'+data+'</div>');
		//$("#parentSlider").slideDown();
		//$("#addCommentForm")[0].reset();
		//$("#addCommentForm input").attr('disabled',false);
	  });
}

function editComment(cid){
	$('#displayComment'+cid).hide();
	$('#editComment'+cid).show();
}

function editCommentCancel(cid){
	$('#displayComment'+cid).show();
	$('#editComment'+cid).hide();
	$('#editCommentForm'+cid)[0].reset();
}

function deleteComment(cid,cmd,cmd_id,p,oid,secret){
	if (confirm('Weet je zeker dat je dit bericht wilt verwijderen?')){
		$.post("/ajax/comments_util.php",{action:'delete_comment',cid:cid,cmd:cmd,cmd_id:cmd_id,p:p,oid:oid,secret:secret},
		  function(data){
		  	$('#commentHolder'+cid).slideUp(function(){
				$("#comments-parent").html(data);
			});
		  });
	}
}

function quoteComment(cid){
	//var timeStamp = $("#commentTimestamp"+cid).html();
	var quoteMsg = $("#editCommentForm"+cid+" > textarea").text();
	var msgUser = $("#commentUser"+cid).text();
	$("#quoteHolder").html(" "+quoteMsg+"<br />"+msgUser).show();
	$("#commentQuote").val(" "+quoteMsg+"\n"+msgUser);
	$.scrollTo($("#addCommentForm"),500,function(){
		$("#addCommentArea").focus();	
	});
}

function replyComment(cid,toId){
	//var timeStamp = $("#commentTimestamp"+cid).html();
	var quoteMsg = $("#editCommentForm"+cid+" > textarea").text();
	var msgUser = $("#commentUser"+cid).text();
	$("#commentCmdId").val(toId);
	$("#quoteHolder").html(" "+quoteMsg+"<br />"+msgUser).show();
	$("#commentQuote").val(" "+quoteMsg+"\n"+msgUser);
	$.scrollTo($("#addCommentForm"),500,function(){
		$("#addCommentArea").focus();	
	});
}

function saveComment(cid){
	var options = $('#editCommentForm'+cid).serialize();
	$('#displayComment'+cid).show();
	$('#editComment'+cid).hide();
	$('#displayComment'+cid).prepend('<div class="loadingdiv" style="margin-top:10px;"><img src="/gfx/loading3.gif" align="absmiddle" />&nbsp;&nbsp;Bezig met opslaan</div>');
	$.post("/ajax/comments_util.php", options,
	  function(data){
	  	if(data=="ERROR"){
			$("#addCommentMsg").html('Error, bericht niet geplaatst');
		}else{
	  		$("#displayComment"+cid).html(data);
		}
	  });
}

function openInviter(){
	var $inputdata = $("#hyves-login-form").serialize();
	$.post('/beheer/uitnodigen/hyves', $inputdata, function(data) { $.facebox(data) })
}

function inviterTab(welke){
	$(".inviter:visible:not(#inviter-div-"+welke+")").slideUp();
	$("#inviter-div-"+welke).slideToggle();
	
	//$("#inviter-div-"+welke).slideToggle();
}

