$(document).ready(function() {
	$("#content a:has(img)").fancybox();
});

$("#twitter").mouseover(function () {
      $(this).animate({  paddingLeft:"20px" }, 1000);
});
$("#twitter").mouseout(function () {
		$(this).stop();
      $(this).animate({  paddingLeft:"0px" }, 1000);
});

// tooltips
this.vtip = function() {    
    this.xOffset = -10; // x distance from mouse
    this.yOffset = 10; // y distance from mouse       
    
    $(".vtip").unbind().hover(    
        function(e) {
            this.t = this.title;
            this.title = ''; 
            this.top = (e.pageY + yOffset); this.left = (e.pageX + xOffset);
            
            $('body').append( '<p id="vtip">' + this.t + '</p>' );
            $('p#vtip').css("top", this.top+"px").css("left", this.left+"px").fadeIn("slow");
            
        },
        function() {
            this.title = this.t;
            $("p#vtip").fadeOut("slow").remove();
        }
    ).mousemove(
        function(e) {
            this.top = (e.pageY + yOffset);
            this.left = (e.pageX + xOffset);
                         
            $("p#vtip").css("top", this.top+"px").css("left", this.left+"px");
        }
    );            
    
};

$(function($){vtip();}) 

// datepicker dutch
jQuery(function($){
	$.datepicker.regional['nl'] = {
		closeText: 'Sluiten',
		prevText: '<',
		nextText: '>',
		currentText: 'Vandaag',
		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'],
		dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
		dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
		dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
		weekHeader: 'Wk',
		dateFormat: 'dd-mm-yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['nl']);
});

/*
 * TwitStream - A jQuery plugin for the Twitter Search API
*/
String.prototype.linkify=function(){
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&;\?\/.=]+/g,function(m){
		return m.link(m);
	});
};
String.prototype.linkuser=function(){
	return this.replace(/[@]+[A-Za-z0-9-_]+/g,function(u){
		return u.link("http://twitter.com/"+u.replace("@",""));
	});
};
String.prototype.linktag=function(){
	return this.replace(/[]+[A-Za-z0-9-_]+/,function(t){
		return t;
	});
};
var showTweetLinks='none';
function fetch_tweets(elem){
	elem=$(elem);
	keyword=escape(elem.attr('title'));
	num=elem.attr('class').split(' ').slice(-1);
	var url="http://search.twitter.com/search.json?q="+keyword+"&rpp="+num+"&callback=?";
	//alert(url);
	$.getJSON(url,function(json){
		$(json.results).each(function(){
			var tTime=new Date(Date.parse(this.created_at));
			var cTime=new Date();
			var sinceMin=Math.round((cTime-tTime)/60000);
			if(sinceMin==0){
				var sinceSec=Math.round((cTime-tTime)/1000);
				if(sinceSec<10)
					var since='Minder dan 10 seconden geleden.';
				else if(sinceSec<20)
					var since='Minder dan 20 seconden geleden.';
				else
					var since='Een halve minuut geleden.';
			}
			else if(sinceMin==1){
				var sinceSec=Math.round((cTime-tTime)/1000);
				if(sinceSec==30)
					var since='Een halve minuut geleden.';
				else if(sinceSec<60)
					var since='Minder dan een minuut geleden.';
				else
					var since='1 minuut geleden.';
			}
			else if(sinceMin<45)
				var since=sinceMin+' minuten geleden.';
			else if(sinceMin>44&&sinceMin<60)
				var since='Ongeveer een uur geleden.';
			else if(sinceMin<1440){
				var sinceHr=Math.round(sinceMin/60);
				if(sinceHr==1)
					var since='Ongeveer een uur geleden.';
				else
					var since='Ongeveer '+sinceHr+' uur geleden.';
			}
			else if(sinceMin>1439&&sinceMin<2880)
				var since='1 dag geleden.';
			else{
				var sinceDay=Math.round(sinceMin/1440);
				var since=sinceDay+' dagen geleden.';
			}
			var tweetBy='<a class="tweet-user" target="_blank" href="http://twitter.com/'+this.from_user+'">@'+this.from_user+'</a> <span class="tweet-time">'+since+'</span>';
			if(showTweetLinks.indexOf('reply')!=-1)
				tweetBy=tweetBy+' &middot; <a class="tweet-reply" target="_blank" href="http://twitter.com/?status=@'+this.from_user+' &in_reply_to_status_id='+this.id+'&in_reply_to='+this.from_user+'">Reply</a>';
			if(showTweetLinks.indexOf('view')!=-1)
				tweetBy=tweetBy+' &middot; <a class="tweet-view" target="_blank" href="http://twitter.com/'+this.from_user+'/statuses/'+this.id+'">View Tweet</a>';
			if(showTweetLinks.indexOf('rt')!=-1)
				tweetBy=tweetBy+' &middot; <a class="tweet-rt" target="_blank" href="http://twitter.com/?status=RT @'+this.from_user+' '+escape(this.text.replace(/&quot;/g,'"'))+'&in_reply_to_status_id='+this.id+'&in_reply_to='+this.from_user+'">RT</a>';
			var tweet='<div class="tweet"><div class="tweet-left"><a target="_blank" href="http://twitter.com/'+this.from_user+'"><img width="32" height="32" alt="'+this.from_user+' on Twitter" src="'+this.profile_image_url+'" /></a></div><div class="tweet-right"><p class="text">'+this.text.linkify().linkuser().linktag().replace(/<a/g,'<a target="_blank"')+'<br />'+tweetBy+'</p></div><br style="clear: both;" /></div>';
			elem.append(tweet);
		});
	});
	return(false);
}
$(function(){
	showTweetLinks=showTweetLinks.toLowerCase();
	if(showTweetLinks.indexOf('all')!=-1)
		showTweetLinks='reply,view,rt';
	$('.twitStream').each(function(){
		fetch_tweets(this);
	});
});
/* 	Twitter Friends v1.0
*/
(function(d){d.fn.twitterFriends=function(o){var s={debug:0,username:"",friends:0,users:20,users_max:100,loop:0,user_link:0,user_image:36,user_animate:"opacity",user_change:200,user_swap:5E3,user_append:1,header:"",tweet:0,tweet_avatar:1,tweet_author:0,tweet_date:1,tweet_source:1,tweet_image:36,tweet_stay:5E3,tweet_change:200,tweet_animate:"opacity",info:'<div class="tf-info"><a title="get Twitter Friends & Followers Widget!" target="_blank" href="http://www.moretechtips.net/">.i</a></div>'};o=d.extend({},
s,o);return this.each(function(){var t=0,f=[],i=-1,k=-1,l=-1,g=d(this),j=null,m=null,p=null,b=o;if(g.attr("options")){try{b=eval("("+g.attr("options")+")")}catch(B){g.html('<b style="color:red">'+B+"</b>");return}b=d.extend({},s,b)}var q=function(){if((i+1)*b.users>=f.length)if(b.loop)i=-1;else return;j.html("");i++;for(var a=i*b.users;a<(i+1)*b.users;a++){if(a>=f.length)break;var c=f[a],e=b.user_link&&c.url?c.url:"http://twitter.com/"+c.screen_name,h=c.name+(c.status&&b.tweet?": "+c.status.text:
"");h=h.replace(/"/g,"&quot;").replace(/'/g,"&#39;");d('<a target="_blank" style="display:none;height:'+b.user_image+'px" href="'+e+'" title="'+h+'"><img src="'+c.profile_image_url+'" border="0" height="'+b.user_image+'" width="'+b.user_image+'"/></a>').appendTo(j)}k=b.user_append?-1:d("a",j).length;u();l=-1;b.tweet&&v()},u=function(){k=b.user_append?k+1:k-1;var a=d("a:eq("+k+")",j);if(a.length){var c={};c[b.user_animate]="show";a.animate(c,b.user_change,"linear",u)}else b.tweet||j.animate({opacity:1},b.user_swap,
"linear",q)},v=function(){l>-1?d("div",m).fadeOut(b.tweet_change,w):w()},w=function(){for(var a=null,c=null;!c;){l++;if(l>=d("a",j).length){q();return}a=f[i*b.users+l];c=a.status}a=a;c=c;var e=b.user_link&&a.url?a.url:"http://twitter.com/"+a.screen_name,h=a.name;m.html('<div style="display:none;">'+(b.tweet_avatar?'<span class="tf-avatar"><a href="'+e+'" title="'+h+'"><img src="'+a.profile_image_url+'" height="'+b.tweet_image+'" width="'+b.tweet_image+'" border="0"/></a></span>':"")+'<span class="tf-body">'+
(b.tweet_author?'<strong><a href="'+e+'" title="'+h+'">'+a.screen_name+"</a></strong>":"")+'<span class="tf-content">'+C(c.text)+'</span><span class="tf-meta">'+(b.tweet_date?'<a class="tf-date" href="http://twitter.com/'+a.screen_name+"/status/"+c.id+'">'+D(c.created_at)+"</a>":"")+(b.tweet_source?'<span class="tf-source"> from '+c.source.replace(/&lt;/gi,"<").replace(/&gt;/gi,">").replace(/&quot;/gi,'"')+"</span>":"")+"</span></span></div>");a={};a[b.tweet_animate]="show";d("div",m).animate(a,b.tweet_change,
"linear",E)},E=function(){d("div",m).animate({opacity:1},b.tweet_stay,"linear",v)},C=function(a){return a.replace(/\bhttps?\:\/\/\S+/gi,function(c){var e="";c=c.replace(/(\.*|\?*|\!*)$/,function(h,r){e=r;return""});return'<a class="tf-link" href="'+c+'">'+(c.length>25?c.substr(0,24)+"...":c)+"</a>"+e}).replace(/\B\@([A-Z0-9_]{1,15})/gi,'@<a class="tf-at" href="http://twitter.com/$1">$1</a>').replace(/\B\#([A-Z0-9_]+)/gi,'<a class="tf-hashtag" href="http://search.twitter.com/search?q=%23$1">#$1</a>')},
D=function(a){if(/^(\w\w\w) (\w\w\w) (\d\d?) (\d\d?:\d\d?:\d\d?) ([\+\-]\d+) (\d\d\d\d)$/i.test(a))a=a.replace(/^(\w\w\w) (\w\w\w) (\d\d?) (\d\d?:\d\d?:\d\d?) ([\+\-]\d+) (\d\d\d\d)$/i,"$1, $3 $2 $6 $4 $5");var c=new Date,e=new Date;c.setTime(Date.parse(a));var h=e.getDate(),r=e.getMonth()+1,F=e.getFullYear(),G=e.getHours(),n=e.getMinutes();e=e.getSeconds();var x=c.getDate(),y=c.getMonth()+1,z=c.getFullYear(),H=c.getHours(),I=c.getMinutes();a=c.getSeconds();if(z==F&&y==r&&x==h){c=G-H;if(c>0)return c+
" hour"+(c>1?"s":"")+" ago";n=n-I;if(n>0)return n+" minute"+(n>1?"s":"")+" ago";a=e-a;return a+" second"+(a>1?"s":"")+" ago"}else return y+"/"+x+"/"+z},J=function(a){A();if(a.screen_name)p.html(b.header.replace(/_tp_/g,"http://twitter.com/"+a.screen_name).replace(/_fr_/g,a.friends_count).replace(/_fo_/g,a.followers_count).replace(/_ti_/g,a.profile_image_url));else if(b.debug)p.html('<b style="color:red">Error:'+(a.error?a.error:"unkown")+"</b>")},A=function(){if(!t){t=1;g.html("");b.info&&g.append(b.info);
if(b.header)p=d('<div class="tf-header"></div>').appendTo(g);j=d('<div class="tf-users"></div>').appendTo(g);if(b.tweet)m=d('<div class="tf-tweet"></div>').appendTo(g)}};b.header&&d.ajax({url:"http://api.twitter.com/1/users/show.json",data:{screen_name:b.username},success:J,dataType:"jsonp",cache:true});d.ajax({url:b.friends?"http://api.twitter.com/1/statuses/friends.json":"http://api.twitter.com/1/statuses/followers.json",data:{screen_name:b.username,cursor:-1},success:function(a){if(a.users){f=
a.users;if(f.length!=0){if(f.length>b.users_max)f.length=b.users_max;f=f.reverse();A();i=-1;q()}}else if(b.debug)g.html('<b style="color:red">Error:'+(a.error?a.error:"unkown")+"</b>")},dataType:"jsonp",cache:true})})}})(jQuery);jQuery(document).ready(function(){jQuery("div.twitter-friends").twitterFriends()});

// jquery ui 
// style buttons
$(function() {
	$( "input:submit", "#content" ).button();
});
// init datepicker
$(function() {
	$( "#datepicker" ).datepicker( { dateFormat: 'yy-mm-dd', showAnim: 'fold' });
});
// init tabs
$(function() {
	$( "#tabs" ).tabs({
		collapsible: true,
		cookie: {
			// store cookie for a day, without, it would be a session cookie
			expires: 1
		}
	});
});
// init accordion
$(function() {
	$( "#accordion" ).accordion({
		selectedClass: 'current',
		navigation: true,
		autoHeight: false,
		collapsible: true,
		active: false,
		navigationFilter: function() {
			return ($(this).attr('href').toLowerCase() == location.hash.toLowerCase());    
		}

	});
});

// datepicker agenda callender
function getAgenda(date) {
    dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
	var mm = date.getMonth() + 1; 
	var dd = date.getDate();
	var yy = date.getFullYear();
	if (mm < 10){
	  mm = "0" + mm;
	  }
	if (dd < 10){
	  dd = "0" + dd;
	  }

	// format date
	var dmy = yy + "-" + mm + "-" + dd;

    if ($.inArray(dmy, agendaDates) == -1) {
        return [false,"", ""];
    } else {	
        return [true,"", agendaDates[agendaDates.indexOf(dmy)+1]];
    }
}

function gotoAgendaItem(date)
{
	var dd = date.substring(0,2); 
	var mm = date.substring(3,5);
	var yy = date.substring(6,10);
	
	// format date
	var dmy = yy + "-" + mm + "-" + dd;

	if ($.inArray(dmy, agendaDates) == -1) {

    } else {
		window.location = "index.php?pagina=AgendaItem#"+agendaDates[agendaDates.indexOf(dmy)+1];
    }
}

$('#datepicker').datepicker(
{ 
	beforeShowDay: getAgenda,
	onSelect: gotoAgendaItem
});

// datepicker news callender

function getNews(date) {
    dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
	var mm = date.getMonth() + 1; 
	var dd = date.getDate();
	var yy = date.getFullYear();
	if (mm < 10){
	  mm = "0" + mm;
	  }
	if (dd < 10){
	  dd = "0" + dd;
	  }

	// format date
	var dmy = yy + "-" + mm + "-" + dd;

    if ($.inArray(dmy, agendaDates) == -1) {
        
	    return [false,"", ""];

    } else {
		return [true,"", agendaDates[agendaDates.indexOf(dmy)+1]];
			$( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
    }
}

function gotoNewsItem(date)
{
	var dd = date.substring(0,2); 
	var mm = date.substring(3,5);
	var yy = date.substring(6,10);
	
	// format date
	var dmy = yy + "-" + mm + "-" + dd;

	if ($.inArray(dmy, agendaDates) == -1) {

    } else {
	
		window.location = "index.php?action=nieuws_bewerken&meta=websitebeheer&pagedit="+agendaDates[agendaDates.indexOf(dmy)+2]+"#Nieuwsberichten";
		
    }
}

$('#datepickernws').datepicker(
{ 
	beforeShowDay: getNews,
	onSelect: gotoNewsItem
});


// config webvars twitter and facebook dialog
$(function() {
		
		var name = $( "#twittername" ),
			email = $( "#facebook_pagina" ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( "Aantal tekens " + n + " moet " +
					min + " en " + max + " zijn." );
				return false;
			} else {
				return true;
			}
		}
		
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			height: 300,
			width: 350,
			modal: true,
			buttons: {
				"Opslaan": function() {
					var bValid = true;
					bValid = bValid && checkLength( name, "Twitternaam", 3, 160 );
					bValid = bValid && checkLength( email, "Facebookpagina", 6, 180 );

					if ( bValid ) {
						$( this ).dialog( "close" );
						$( "#socialform" ).submit();
					}
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				
			}
		});
		
		$('#contactForm input').focus(function() {
			$(this).val('');
		});
			
		$('#contactForm textarea').focus(function() {
			$(this).val('');
		});
		
		$( "#create-user" )
			.button()
			.click(function() {
				$( "#dialog-form" ).dialog( "open" );
			});
	});
// help
$(function() {
		
		var name = $( "#name" ),
			email = $( "#email" ),
			comment = $( "#comment" ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}


		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( n + " moet uit minimaal " +
					min + " tekens en maximaal " + max + " tekens bestaan." );
				return false;
			} else {
				return true;
			}
		}
		
		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		
		$( "#contact" ).dialog({
			autoOpen: false,
			height: 400,
			width: 360,
			modal: true,
			buttons: {
				"Versturen": function() {
				var bValid = true;
					
					bValid = bValid && checkLength( name, "naam", 3, 80 );
					bValid = bValid && checkLength( email, "email", 6, 180 );
					bValid = bValid && checkLength( comment, "comment", 6, 2000 );

					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Geen geldig emailadres..." );
					
					if ( bValid ) {
						$( this ).dialog( "close" );
						$( "#contactForm" ).submit();
					}
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
		});
		$('#contactForm input').focus(function() {
			$(this).val('');
		});
		
		$('#contactForm textarea').focus(function() {
			$(this).val('');
		});
		
		$( "#create-user" )
			.button()
			.click(function() {
				$( "#contact" ).dialog( "open" );
			});
	});

// confirm update socialmedia
$(function() {
	$( "#dialog-message" ).dialog({
		modal: true
	});
});

$(function() {
	$( "a#sitemap, button" ).button();
	$( "a#sitemap" ).click(function() { return true; });
});

$('#submit').click(function() 
{
	return confirm('Wilt u deze kenmerken opslaan?');
});
