/* 
Needed to play video and audio items on the website 
Also used for changing of statistics
*/

function changeStatistics() {
	var sUrl = '/statistieken/statistieken/overzicht/' + $("#overzicht").attr('value') + '/club/' + $("#club").attr('value');
	document.location.href = sUrl;
}

function sitestat(ns_l) {
	ns_l+="&ns__t="+new Date().getTime();ns_pixelUrl=ns_l;
	ns_0=top.document.referrer;
	ns_0=(ns_0.lastIndexOf("/")==ns_0.length-1)?ns_0.substring(ns_0.lastIndexOf("/"),0):ns_0;
	if(ns_0.length>0)ns_l+="&ns_referrer="+escape(ns_0);
	if(document.images) {
		ns_1=new Image();ns_1.src=ns_l;
	} else
	document.write("<img src="+ns_l+" width=\"1\" height=\"1\" />");
}
function displayDate() {
	var dayNames = new Array("zondag","maandag","dinsdag","woensdag","donderdag","vrijdag","zaterdag");
	var monthNames = new Array("januari","februari","maart","april","mei","juni","juli","augustus","september","oktober","november","december");
	var now = new Date();
	displayDate = "<p>" + dayNames[now.getDay()] + " " + now.getDate() + " " + monthNames[now.getMonth()] + " " + now.getFullYear() + "</p>";
	return displayDate
}

function playVideo(videoURL,divID,tellerName) {
	videoURL = videoURL.replace('tcm:','tcm-');
	$("#" + divID).html('');
	$("#" + divID).flash(
	{ 
  		src: 'http://s.nos.nl/swf/player.swf',
  		width: 378,
  		height: 213,
		wmode: 'opaque',
		allowfullscreen: 'true',
  		flashvars: { 
  			skin: 'http://s.nos.nl/swf/skin/nos.swf',
  			plugins: 'http://s.nos.nl/swf/plugins/nosstats',
  			autostart: 'false', 
  			file: 'http://content.nos.nl/content/playlist/video/fragment/'+videoURL+'.xml',
  			bufferlength: '12',
  			autostart: 'true'
      		}
		},
	{ version: 8 }
	);
}

function playVideoSL(videoURL,divID,vTitle) {
	$.getJSON("/video.php?tcm=" + videoURL, function(json){
	  	var cnt = document.getElementById(divID);
		var src = '/jwplayer/wmvplayer.xaml';
		var cfg = {
			autostart:'true',
			image: 'http://player.nos.nl' + json.thumb,
			file:json.url,
			height:'218',
			width:'352'
		};
		var ply = new jeroenwijering.Player(cnt,src,cfg);
		
		var rnumber		= Math.floor(Math.random()*10000);
		if (vTitle!='') videoURL = vTitle;
		var siteStatUrl	= 'http://nl.sitestat.com/klo/nosrtv/s?player.' + videoURL + '&amp;category=nieuws&ns__t=' + rnumber;
		sitestat(siteStatUrl);
	});
}

function playVideoSLNoStart(videoURL,divID,vTitle) {
	$.getJSON("/video.php?tcm=" + videoURL, function(json){
	  	var cnt = document.getElementById(divID);
		var src = '/jwplayer/wmvplayer.xaml';
		var cfg = {
			autostart:'false',
			image: 'http://player.nos.nl' + json.thumb,
			file:json.url,
			height:'213',
			width:'378'
		};
		var ply = new jeroenwijering.Player(cnt,src,cfg);
		
		var rnumber		= Math.floor(Math.random()*10000);
		if (vTitle!='') videoURL = vTitle;
		var siteStatUrl	= 'http://nl.sitestat.com/klo/nosrtv/s?player.' + videoURL + '&amp;category=nieuws&ns__t=' + rnumber;
		sitestat(siteStatUrl);
	});
}

function playAudio(videoURL,divID,tellerName) {
	videoURL = videoURL.replace('tcm:','tcm-');
	videoURL = videoURL.replace('http://content.nos.nl/player/play/','');
	videoURL = videoURL.replace('/','');
	$("#" + divID).html('');
	$("#" + divID).flash(
	{ 
  		src: 'http://s.nos.nl/swf/player.swf',
  		width: 378,
  		height: 213,
		wmode: 'opaque',
		allowfullscreen: 'true',
  		flashvars: { 
  			skin: 'http://s.nos.nl/swf/skin/nos.swf',
  			plugins: 'http://s.nos.nl/swf/plugins/nosstats',
  			autostart: 'false', 
  			file: 'http://content.nos.nl/content/playlist/audio/fragment/'+videoURL+'.xml',
  			bufferlength: '12',
  			autostart: 'true'
      		}
		},
	{ version: 8 }
	);
}

/*
End of video and audio playing functions
*/

/* JavaScript equivalent of PHP's in_array
*/

function in_array(needle, haystack, strict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true

    var found = false, key, strict = !!strict;

    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }

    return found;
}

/* END in_array */


function get_randomnr(){
	return Math.floor(Math.random()*999999999);
}

function defaultClickActions(eventvar, objectvar){
	eventvar.preventDefault();
	$(objectvar).blur();
}

function fix_footer_deelnemers(){
	var this_height = $("div#content-deelnemers").height();
	if(this_height > 550){
		$("div#wrapper").height(this_height + 350);
	}
}

function fix_footer_content(){
    var this_height = $("div#content-chunk-misc").height();
    if(this_height > $("div#content").height()){
        $("div#content").height(this_height + 150);
    }
}

function fix_footer_club(){
   var this_height = $("div#maincontent").height();
   $("div#footer").css('top',this_height + 550);
}


$('body#content-chunk-misc').ready(function(){
    fix_footer_content();
});

$('body#os08-page-nederlanders div#content-deelnemers').ready(function(){
	fix_footer_deelnemers();
});

function fix_footer(){
	if($("html").hasClass("ie6")){
		$("div#wrapper").height("100%");
	} else {
		$("div#wrapper").height("auto");
	}
	
	var doc_height = $(document).height();

	$("div#wrapper").height(doc_height+100);
	$("div#footer").show();
}

$(document).ready(function() { 
	
	if($("html").hasClass("ff2") || $("html").hasClass("webkit")){
		$('ul#navbar').css('opacity', 0.9999);
	}
	
    $("h1, div.pngfix, img.pngfix, h3.content-tabhead").pngfix(); 
	
	/* Used on pages: /index/index */
	randomNR = Math.floor(Math.random()*999999999);
	options = { slidebypx: '426', datasrc: "/data/video/recent.json?"+randomNR, itemsperslide: 3 };
	$('body#eredivisie-page-homelive div#nos-videobox-recentvideos').nosslidevideos(options);
	
	options = { slidebypx: '500', datasrc: "/data/video/recent.json?"+randomNR, itemsperslide: 4, items: 12  };
	$('body#eredivisie-page-homenieuws div#nos-videobox-recentvideos').nosslidevideos(options);
	
	options = { slidebypx: '500', datasrc: "/data/video/recent.json?"+randomNR, itemsperslide: 4, items: 12  };
	$('body#eredivisie-page-homenieuws div#nos-videobox-recentvideosext').nosslidevideos(options);
	
	options = { slidebypx: '500', datasrc: "/data/video/recent_sv.json?"+randomNR, itemsperslide: 4, items: 12  };
	$('body#eredivisie-page-homenieuws div#nos-videobox-recentstudio').nosslidevideos(options);
	
	/* Used on pages: /nieuws/index /nieuws/artikel */
	randomNR = Math.floor(Math.random()*999999999);
	options = { slidebypx: '597', datasrc: "/data/video/recent.json?"+randomNR, itemsperslide: 4 };
	$('body#eredivisie-page-homenieuws div#nos-videobox-recentvideosext').nosslidevideos(options);

	/* Used on pages: /nieuws/index */
	randomNR = Math.floor(Math.random()*999999999);
	options = { datasrc: "/data/audio/recent.json?"+randomNR };
	$('body#eredivisie-page-homenieuws div#nos-audiobox-recentaudios').nosslideaudios(options);
	
	// UITGEZET JWE
	// $('form#nieuwsvideos-zoek-form fieldset').append('<a href="#" id="video-zoek-submit-link"><span>Zoek 1</span></a>');

	$("a#video-zoek-submit-link").bind("click",function(e){
		defaultClickActions(e,this);
		$("form#nieuwsvideos-zoek-form").submit();
	});
	
	$("input#video-zoek").bind("focus",function(e){
		var init_value = $(this).attr("value");
		if(init_value == '- type hier je zoekwoord -'){
			$(this).attr("value",'');
		}
	});
	
	$("input#video-zoek").bind("blur",function(e){
		var init_value = $(this).attr("value");
		if(init_value == ''){
			$(this).attr("value",'- type hier je zoekwoord -');
		}
	});
	/* ====== */

	/* Used on pages: /video/index /video/bekijk */
	$('form#video-zoek-form fieldset').append('<a id="zoekvideo-submit" href="#"><span>Zoek 2</span></a>');
	$("a#zoekvideo-submit").bind("click",function(e){
		defaultClickActions(e,this);
		document.location.href = '/video/zoek/keyword/' + $("input#zoekvideo-input").val();
	});
	
	
	/* ====== */
	
	/* TABBLADEN MEDIABOX OP FRONTPAGE */

	$("h3#tab-nieuws-video a").click(function(e){
		
		defaultClickActions(e,this);
		
		$("h3#tab-nieuws-video").removeClass("tabActief").addClass("tabInActief");
		$("h3#tab-nieuws-audio").addClass("tabActief").removeClass("tabInActief");
		$("h3#tab-nieuws-studio").addClass("tabActief").removeClass("tabInActief");
		
		$("div#nieuwsvideos-content").show();
		$("div#nieuwsaudios-content").hide();
		$("div#nieuwsstudio-content").hide();
		
		
	});
	
	$("h3#tab-nieuws-audio a").click(function(e){
		defaultClickActions(e,this);
		
		$("h3#tab-nieuws-video").addClass("tabActief").removeClass("tabInActief");
		$("h3#tab-nieuws-audio").removeClass("tabActief").addClass("tabInActief");
		$("h3#tab-nieuws-studio").addClass("tabActief").removeClass("tabInActief");
		
		$("div#nieuwsvideos-content").hide();
		$("div#nieuwsaudios-content").show();
		$("div#nieuwsstudio-content").hide();
		
	});
	
	$("h3#tab-nieuws-studio a").click(function(e){
		defaultClickActions(e,this);
		
		$("h3#tab-nieuws-video").addClass("tabActief").removeClass("tabInActief");
		$("h3#tab-nieuws-audio").addClass("tabActief").removeClass("tabInActief");
		$("h3#tab-nieuws-studio").removeClass("tabActief").addClass("tabInActief");
		
		$("div#nieuwsvideos-content").hide();
		$("div#nieuwsaudios-content").hide();
		$("div#nieuwsstudio-content").show();
		
	});
	
	/* ====== */
	
	
	/* ====== */

	$("h3#chunk-head-tabswitch-tv a").click(function(e){
		
		defaultClickActions(e,this);
		
		$("h3#chunk-head-tabswitch-tv").addClass("chunk-head-tabswitch-active");
		$("h3#chunk-head-tabswitch-radio").removeClass("chunk-head-tabswitch-active");
		$("h3#chunk-head-tabswitch-studio").removeClass("chunk-head-tabswitch-active");
		
		$("div#content-chunk-tv-body-wrap").show();
		$("div#content-chunk-radio-body-wrap").hide();
		$("div#content-chunk-studio-body-wrap").hide();
		
	});
	
	$("h3#chunk-head-tabswitch-radio a").click(function(e){
		defaultClickActions(e,this);
		
		$("h3#chunk-head-tabswitch-radio").addClass("chunk-head-tabswitch-active");
		$("h3#chunk-head-tabswitch-tv").removeClass("chunk-head-tabswitch-active");
		$("h3#chunk-head-tabswitch-studio").removeClass("chunk-head-tabswitch-active");
			
		$("div#content-chunk-tv-body-wrap").hide();
		$("div#content-chunk-studio-body-wrap").hide();
		$("div#content-chunk-radio-body-wrap").show();
		
	});
	
	$("h3#chunk-head-tabswitch-studio a").click(function(e){
		defaultClickActions(e,this);
		
		$("h3#chunk-head-tabswitch-radio").removeClass("chunk-head-tabswitch-active");
		$("h3#chunk-head-tabswitch-tv").removeClass("chunk-head-tabswitch-active");
		$("h3#chunk-head-tabswitch-studio").addClass("chunk-head-tabswitch-active");
			
		$("div#content-chunk-tv-body-wrap").hide();
		$("div#content-chunk-radio-body-wrap").hide();
		$("div#content-chunk-studio-body-wrap").show();
		
	});
	/* ====== */
	//$('#content_live_program > ul').tabs();
	fix_footer();
	
});