$(document).ready(function() {
	/* FUNCTIONS */
	/* Character counter */	
	jQuery.fn.counter = function() {
	  $(this).each(function() {
	    var max = $(this).attr('maxlength');
	    var val = $(this).attr('value');
	    var cur = 0;
	    if(val)
	    	cur = val.length;
	    var left = max-cur;
	    $(this).after("<div class='counter'>Nog <strong>"+left.toString()+"</strong> tekens over</div>");
	    var c = $(this).next(".counter"); 
	    $(this).keyup(function(i) {
	      var max = $(this).attr('maxlength');
	      var val = $(this).attr('value');
	      var cur = 0;
	      if(val)
	        cur = val.length;
	      var left = max-cur;

	      if (left < 0)
	      {
		i.preventDefault();
		left = 0;
	      }
	      $(this).next(".counter").find('strong').text(left.toString());
	      return this;
	    });
	    $(this).keypress(function(i) {
	      var max = $(this).attr('maxlength');
	      var val = $(this).attr('value');
	      var cur = 0;
	      if(val)
	        cur = val.length;
	      var left = max-cur;
		  
		  var key = i.which;
		  if(key >= 33 || key == 13)
		  {
			  if (left < 0 )
			  {
				i.preventDefault();
			  }
		  }
	      return this;
	    });
	  });
	  return this;
	}
	/* Justify height of columns */
	jQuery.fn.vjustify=function() {
	    var maxHeight=0;
	    this.each(function(){
	        if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
	    });
	    this.each(function(){
	        $(this).height(maxHeight + "px");
	        if (this.offsetHeight>maxHeight) {
	            $(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
	        }
	    });
	};

	/* Tab widgets initialiseren */
	$(".tabs-widget" ).tabs();
	$('.tabs-widget > ul').tabs({ fx: { opacity: 'toggle', duration: 'fast' } });  


	/* Service navigation */
	$servicenav = 'close';
	$('ul#service-nav li > a')
		.bind('click',function() {
			$(this).parent().parent().find('li > ul').hide('fast');
			$(this).parent().find('ul').toggle('fast');
			if ($(this).css('display') == 'block') {
				$servicenav = 'open';
			}
			else {
				$servicenav = 'close';
			}
		});
	$('ul#service-nav li > a')
		.bind('mouseenter',function() {
			if ($servicenav == 'open') {
				$(this).parent().parent().find('li > ul').hide('fast');
				$(this).parent().find('ul').toggle('fast');	
			}
		});
	$('ul#service-nav li > ul')
		.bind('mouseleave',function() { 
			$(this).toggle('fast');
			$servicenav = 'close';
		})
	/* Add character counter to textarea */	
	//$('.comments #your-comment textarea').counter(); 
	/* check voor agenda proxy */
	if($('.comments #your-comment textarea')) { $('.comments #your-comment textarea').counter(); }

	/* Add video overlays */
	$('#breaking-video .panel li a img').each(function() {
		var src = $(this).attr('src');
		$(this)
			.css('background-image','url('+src+')')
			.attr('src','images/slidebox/video-overlay_85x60.png');
	});
	
	
	
	/* Profiles */
	
	 $("h6.label").click(function(){
			  $(this).next("div").slideToggle("slow")
			  .siblings("div:visible").slideUp("slow");
			  $(this).toggleClass("active");
			  $(this).siblings("h6").removeClass("active");
			});
	
	
	/*** add target=_blank to external links in articles ***/ 	
	
	//$('. a').attr('target','_blank');
	$('.article-body a').each(function(){
		if(this.href.indexOf(location.hostname) == -1) { 
				$(this).attr('target', '_blank');
		}
	});
	
	
	// /*** Event Tracking ArticleBottom Teasers ***/
	
		// /*** Teasers op sectie Nieuws ***/ 
				
		// $('.wrapper.nieuws .articlebottom:first .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Nieuws',  'Artikel - Nieuws');
		// });
		
		// $('.wrapper.nieuws .articlebottom.nieuws .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Nieuws',  'Artikel - Nieuws');
		// });
		
		// $('.wrapper.nieuws .articlebottom.lifestyle .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Nieuws',  'Artikel - Lifestyle');
		// });
		
		// $('.wrapper.nieuws .articlebottom.biz .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Nieuws',  'Artikel - Biz');
		// });
		
		// $('.wrapper.nieuws .articlebottom.sport .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Nieuws',  'Artikel - Sport');
		// });
	
	
		// /*** Teasers op sectie Sport ***/ 
	
		// $('body.sw .articlebottom .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Sport',  'Artikel - Sport');
		// });


		// /*** Teasers op sectie Regio ***/ 
		
		// if(window.sectie == 25){ // Uitsluiten dat het om SW Regio gaat
			// $('.wrapper.regio .articlebottom.regio .item a').click(function(){
				// pageTracker._trackEvent('ArticleBottom', 'Klik - Regio',  'Artikel - Regio');
			// });
			
			// $('.wrapper.regio .articlebottom.nieuws .item a').click(function(){
				// pageTracker._trackEvent('ArticleBottom', 'Klik - Regio',  'Artikel - Nieuws');
			// });
			
			// $('.wrapper.regio .articlebottom.lifestyle .item a').click(function(){
				// pageTracker._trackEvent('ArticleBottom', 'Klik - Regio',  'Artikel - Lifestyle');
			// });
			
			// $('.wrapper.regio .articlebottom.sport .item a').click(function(){
				// pageTracker._trackEvent('ArticleBottom', 'Klik - Regio',  'Artikel - Sport');
			// });
			
			// $('.wrapper.regio .articlebottom.biz .item a').click(function(){
				// pageTracker._trackEvent('ArticleBottom', 'Klik - Regio',  'Artikel - Biz');
			// });
		// }
	
	
		// /*** Teasers op sectie Economie ***/ 
	
		// $('.wrapper.economie .articlebottom:first .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Biz',  'Artikel - Biz');
		// });
		
		// $('.wrapper.economie .articlebottom.nieuws .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Biz',  'Artikel - Nieuws');
		// });
		
		// $('.wrapper.economie .articlebottom.lifestyle .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Biz',  'Artikel - Lifestyle');
		// });
		
		// $('.wrapper.economie .articlebottom.sport .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Biz',  'Artikel - Sport');
		// });
		
		// $('.wrapper.economie .articlebottom.biz .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Biz',  'Artikel - Biz');
		// });
	
	
		// /*** Teasers op sectie Multimedia ***/ 
		
		// $('.wrapper.multimedia .articlebottom.biz .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Biz',  'Artikel - Biz');
		// });
	
	
		// /*** Teasers op sectie Consument ***/ 
	
		// $('.wrapper.consument .articlebottom:first .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Biz',  'Artikel - Biz');
		// });
		
		// $('.wrapper.consument .articlebottom.nieuws .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Biz',  'Artikel - Nieuws');
		// });
		
		// $('.wrapper.consument .articlebottom.lifestyle .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Biz',  'Artikel - Lifestyle');
		// });
		
		// $('.wrapper.consument .articlebottom.sport .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Biz',  'Artikel - Sport');
		// });
		
		// $('.wrapper.consument .articlebottom.biz .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Biz',  'Artikel - Biz');
		// });
		
		
		// /*** Teasers op sectie Lifestyle ***/ 
	
		// $('.wrapper.life .articlebottom:first .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Lifestyle',  'Artikel - Lifestyle');
		// });
		
		// $('.wrapper.life .articlebottom.lifestyle .item a').click(function(){
			// pageTracker._trackEvent('ArticleBottom', 'Klik - Lifestyle',  'Artikel - Lifestyle');
		// });
		
	/*** Event Tracking Promoboxen 
	
	$('.oxynade p.title').addClass('module');
	$('.slot a').click(function(){
		pb_module = $(this).parents('.slot').children('.module').text();
		pb_label = $(this).parent().children(':last').text();
		//alert (SectionName);
		//alert (location.pathname);
		//alert ('module = '+pb_module);
		//alert ('label = '+pb_label);
		pageTracker._trackEvent('PromoBox', 'Klik - '+SectionName+' - '+padnaam_trimmed+' - '+pb_module,  pb_module+' - '+pb_label);
	});
	
	***/ 	
});



/* These functions rely on images being fully loaded, so $(document).ready() won't work */

$(window).load(function() {
	/* Adjust column heights where needed */
	$(".stretch").vjustify();
});
