/* 
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');
	var sUrl = '/statistieken/statistieken/overzicht/' + $("#overzicht").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 playMedia(tcmId,divID, type) {
	$('#' + divID).html("");
	$('#' + divID).flash(
    	{ 
      		src: 'http://s.nos.nl/swf/player.swf',
      		width: 378,
      		height: 212,
			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/' + type + '/fragment/' + tcmId + '.xml', 
      			bufferlength: '12',
      			autostart: 'true',
      			controlbar: 'over'
          		}
    		},
    	{ 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 + 150);
	}
}

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 + 500);
}

$('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+150);
	$("div#footer").show();
	fix_footer_club();
}
//TWEEDE FOOTER FIX, WERKT NIET GOED. Vraagt zichtbare window.heigt op, niet daadwerkelijke lengte van scherm.
function getWindowHeight() {
			var windowHeight = 0;
			if (typeof(window.innerHeight) == 'number') {
				windowHeight = window.innerHeight;
			}
			else {
				if (document.documentElement && document.documentElement.clientHeight) {
					windowHeight = document.documentElement.clientHeight;
				}
				else {
					if (document.body && document.body.clientHeight) {
						windowHeight = document.body.clientHeight;
					}
				}
			}
			return windowHeight;
		}
		function setFooter() {
			if (document.getElementById) {
				var windowHeight = getWindowHeight();
				if (windowHeight > 0) {
					var contentHeight = document.getElementById('content').offsetHeight;
					var footerElement = document.getElementById('footer');
					if (footerElement) {
						var footerHeight  = footerElement.offsetHeight;
						if (windowHeight - (contentHeight + footerHeight) >= 0) {
							footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
						}
						else {
							footerElement.style.top = '0px';
						}
					}
				}
			}
		}
		window.onload = function() {
			setFooter();
		}
		window.onresize = function() {
			setFooter();
		}

$(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();
	});
	
	
	/* ====== */

	$("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#tab-voetbal-radio span a").click(function(e){
		defaultClickActions(e,this);
		
		$("h3#tab-voetbal-tv").addClass("tabActief").removeClass("tabInActief");
		$("h3#tab-voetbal-radio").removeClass("tabActief").addClass("tabInActief");
		
		$("div#uitzendingen_televisie").hide();
		$("div#uitzendingen_radio").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();
	
});