/**
 * Count a Sitestat pageview by calling with the +teller+ as parameter (a string).
 */
function sitestat(ns_l) {
  ns_l+="&ns__t="+new Date().getTime();
  ns_pixelUrl=ns_l;
  if(document.images) {
     ns_1=new Image();
     ns_1.src=ns_l;
  } else {
    document.write("<img src=\""+ns_l+"\" width=\"1\" height=\"1\" />");
	}
}

/**
 * Open the mediaplayer in a popup window. This function is called for you when you put a rel=player attribute on a link.
 * width=786, height=422
 */
function mediaplayerpopup(urlParam) {
	return open(urlParam,'mediaplayer','resizable=yes,scrollbars=no,top=50,left=50,width=786,height=422,status=no,menubar=no,toolbar=no,location=no,directories=no');
}

/**
 * Tabbed lists on the website are always present in the DOM, but only the active tab is shown.
 * This function shows the clicked tab and hides the others.
 */
function activate_tab(tab) {
	$("li#" + tab + "-tab").addClass("current").siblings().removeClass("current");
	$("div#" + tab + "-content").addClass("tab-active").siblings(".tab-active").removeClass("tab-active");
}

/**
* Styleswitch stylesheet switcher built on jQuery
**/
function switchStylestyle(styleName) {
	createCookie('style', styleName, 365);	
	$('.sizeswitch a').removeClass("current");
	switchCurrent(styleName);
}

/**
 * This function is executed when the user clicks one of the thee A's for bigger/smaller letters.
 */
function switchCurrent(c) {
	if ( c == "large" ) {
		$('body').css('font-size', '77.5%')
		$('a.l').addClass("current");
	} else if ( c == "default" ) {
		$('body').css('font-size', '70%')
		$('a.d').addClass("current");
	} else {
		$('body').css('font-size', '62.5%')
		$('a.s').addClass("current");
	}
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
// /cookie functions



if(typeof jQuery == 'function'){

	$(document).ready(function() {
		
		/* If the browser does not support MP3, remove the audio tags completely.
		   needed for firefox, http://apirocks.com/html5/audio/quick  */
		if (document.createElement('audio').canPlayType) {
			if (!document.createElement('audio').canPlayType('audio/mpeg')) {
				$('audio').each(function() {
					$(this).after($(this).find('a'));
					$(this).remove();
				})
			}
		}

		// Makes <a href="http://example.com" rel="external"> open in a new window.
		$('a[rel*=external]').attr("target", "_blank");

		// Makes <a href="http://portal.omroep.nl/audioplayer/radio1" rel="player"> open in the player window.
		$('a[rel*=player]').click(function() {
			mediaplayerpopup(this.href);
			return false;
		});

		// When the terugluisteren-op-tijd form is submitted from the sidebar, open the form in the player window.
		$('#sidebar form#luister_selection').submit(function() {		
			mediaplayerpopup('');
			this.target = 'mediaplayer';
			return true;
		});

		$('body').addClass('js');
		
		// Focus form fields with class focus
		$(":input.focus").focus();

		/* Search Box */
		$('#q').click(function()
		{
			$(this).addClass('active');
		});

		if($('#q').val()) $('#q').addClass('active');

		$('dl.program dd a').hover(
			function() {
				$(this).parent().addClass('hover');
				$(this).parent().prev().addClass('hover');
			},
			function() {
				$(this).parent().removeClass('hover');
				$(this).parent().prev().removeClass('hover');
			}
		);

		// Activate all accordions
		$('ul.accordion').accordion({
			header: "h4",
			clearStyle: true,
			autoHeight: false
		});
		
		// Active the sports tabs on the homepage
		$('#secondary-content #sport-games div:first').addClass('tab-active');
		$('#secondary-content #sport-competitions li:first').addClass('current');

		/* Style Switcher */
		$('.styleswitch').click(function()
		{
			switchStylestyle(this.getAttribute("rel"));		
			return false;
		});
		var c = readCookie('style');
		if (c) switchStylestyle(c);

		switchCurrent(c);	
	});

	/*
	 * Translated default messages for the jQuery validation plugin.
	 * By Joost Baaij http://www.spacebabies.nl/ joost@spacebabies.nl
	 * Language: NL
	 * Skipped date/dateISO/number.
	 */
	jQuery.extend(jQuery.validator.messages, {
		required: "Dit veld is verplicht.",
		maxlength: jQuery.format("U kunt niet meer dan {0} karakters invoeren."),
		minlength: jQuery.format("U dient minimaal {0} karakters in te voeren."),
		rangelength: jQuery.format("U dient minimaal {0} en maximaal {1} karakters in te voeren."),
		email: "Een geldig emailadres is verplicht.",
		url: "Een geldig URL is verplicht.",
		date: "Een geldige datum is verplicht.",
		number: "Een geldig getal is verplicht.",
		digits: "Gebruik alleen cijfers.",
		equalTo: "Herhaal de invoer nogmaals.",
		range: jQuery.format("U dient een waarde tussen {0} en {1} in te voeren."),
		max: jQuery.format("U dient een waarde kleiner dan of gelijk aan {0} in te voeren."),
		min: jQuery.format("U dient een waarde groter dan of gelijk aan {0} in te voeren."),
		creditcard: "Een geldig creditcardnummer is verplicht."
	});
	
	 // Validate all forms with a classname of 'validate'
	$("form.validate").validate();   
	

}