$(document).ready(function(){
	checkBrowser();
	posities();
	resize_viewport();
	addFunctions();
	pageHeight();
	setTimeout("pageHeight()",1000); //safari fix, image height unknown
	ie6();
	randomNR = Math.floor(Math.random()*999999999);
	options = { slidebypx: '625', datasrc: "/data/latest.js?"+randomNR, itemsperslide: 3 };
	$('body#tab_index div#nos-videobox-recentvideos').nosslidevideos(options);
	
	randomNR = Math.floor(Math.random()*999999999);
	options = { datasrc: "/data/audio_latest.js?"+randomNR };
	$('div#nos-audiobox-recentaudios').nosslideaudios(options);

	fix_footer();
});

var browser = '';

function checkBrowser(){
	if(navigator.userAgent.toLowerCase().indexOf('msie 6')>0){
		browser = 'ie6';
	}
}

function ie6(){
	if(browser=='ie6'){
		//
	}
}

function switchBox(newId) {
	//$("#tab-nieuws-video").removeClass();
	if (newId=='tab-nieuws-video') {
		$("#nieuwsvideos-content").css('display','block');
		$("#nieuwsaudios-content").css('display','none');
	} else {
		$("#nieuwsaudios-content").css('display','block');
		$("#nieuwsvideos-content").css('display','none');
	}	
}

function defaultClickActions(eventvar, objectvar){
	eventvar.preventDefault();
	$(objectvar).blur();
}


function fix_footer(){
	if($("html").hasClass("ie6")){
		$("div#container").height("100%");
	} else {
		$("div#container").height("auto");
	}
	var doc_height = $(document).height();
	$("div#container").height(doc_height+50);
	$("div#onder").show();
}


function addFunctions(){    

	//index gids
	$("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");
		
		$("div#content-chunk-tv-body-wrap").show();
		$("div#content-chunk-radio-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");
		
		$("div#content-chunk-tv-body-wrap").hide();
		$("div#content-chunk-radio-body-wrap").show();
		
	});

	
	$("#wedstrijd_1").bind("click",function(e){
		$("#wedstrijd_1 .klassement").toggle();
		if($("#wedstrijd_1").hasClass("actief")){
			$("#wedstrijd_1").addClass("inactief");
			$("#wedstrijd_1").removeClass("actief");
		}
		else{
			$("#wedstrijd_1").addClass("actief");
			$("#wedstrijd_1").removeClass("inactief");
		}
	});

	$("#wedstrijd_2").bind("click",function(e){
		$("#wedstrijd_2 .klassement").toggle();
		if($("#wedstrijd_2").hasClass("actief")){
			$("#wedstrijd_2").addClass("inactief");
			$("#wedstrijd_2").removeClass("actief");
		}
		else{
			$("#wedstrijd_2").addClass("actief");
			$("#wedstrijd_2").removeClass("inactief");
		}
	});		

		$("#wedstrijd_3").bind("click",function(e){
		$("#wedstrijd_3 .klassement").toggle();
		if($("#wedstrijd_3").hasClass("actief")){
			$("#wedstrijd_3").addClass("inactief");
			$("#wedstrijd_3").removeClass("actief");
		}
		else{
			$("#wedstrijd_3").addClass("actief");
			$("#wedstrijd_3").removeClass("inactief");
		}
	});

	$("#wedstrijd_4").bind("click",function(e){
		$("#wedstrijd_4 .klassement").toggle();
		if($("#wedstrijd_4").hasClass("actief")){
			$("#wedstrijd_4").addClass("inactief");
			$("#wedstrijd_4").removeClass("actief");
		}
		else{
			$("#wedstrijd_4").addClass("actief");
			$("#wedstrijd_4").removeClass("inactief");
		}
	});	
	
}

function pageHeight(){
	windowHoogte = $(window).height(); 
	containerHoogte = $("#container").height()+90;
	if(browser=='ie6'){
		containerHoogte = $("#container").height()+120;
	}
	/*alert(containerHoogte);
	if(containerHoogte<windowHoogte){
		topHoogte = windowHoogte - containerHoogte;
	}
	else if(browser=='ie6'){
		topHoogte = 0;
	}
	else{
		topHoogte = 27;
	}
	*/
	//document.getElementById('onder').style.top = containerHoogte + 'px';
	//document.getElementById('onder').style.display = 'block';
}
	 
function posities() {
	var x,y;
		if (self.innerHeight) // all except Explorer
		{
			x = self.innerWidth;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			x = document.documentElement.clientWidth;
		}
		else if (document.body) // other Explorers
		{
			x = document.body.clientWidth;
		}
		
	if(x<1400) {
		document.getElementById('sfeer').style.background = "url(/gfx/bck_schaats_sfeer_kl.jpg)";
	}
	else {
		document.getElementById('sfeer').style.background = "url(/gfx/bck_schaats_sfeer.jpg)";	
	}
}

function resize_viewport() {
  $.event.remove( this, "resize", resize_viewport);
  $.event.add( this, "resize", resize_viewport);
  posities();
}

function changeStand(itemObj) {
	$("#klassementenTab > li").removeClass('actief');
	document.getElementById('tabelA').style.display = 'none';
	document.getElementById('tabelB').style.display = 'none';
	document.getElementById('tabelC').style.display = 'none';	
	document.getElementById('tabelD').style.display = 'none';
	document.getElementById('tabelD').style.display = 'block';
	$("#tabelD").load(itemObj.value,'',function() {
		fix_footer();
		$("#tabelDli").addClass('actief');
	});
}

function livetab(id){
	$("#tabs > li").removeClass('actief');
	document.getElementById('tab_contentA').style.display = 'none';
	document.getElementById('tab_contentB').style.display = 'none';	
	document.getElementById('tab_contentC').style.display = 'none';	
	document.getElementById(id).style.display = 'block';
	$("#tabs > li." + id).addClass('actief');
}

function changeUitslag(itemObj) {
	$("#klassementenTab > li").removeClass('actief');
	document.getElementById('tabelA').style.display = 'none';
	document.getElementById('tabelB').style.display = 'none';
	document.getElementById('tabelC').style.display = 'none';	
	document.getElementById('tabelD').style.display = 'none';
	document.getElementById('tabelC').style.display = 'block';
	$("#tabelC").load(itemObj.value,'',function() {
		fix_footer();
		$("#tabelCli").addClass('actief');
	});
}

function changeLoting(itemObj) {
	$("#tabelB").load(itemObj.value,'',function() {
		fix_footer();
	});
}
 
 
function klassementTab(id){
	$("#klassementenTab > li").removeClass('actief');
	document.getElementById('tabelA').style.display = 'none';
	document.getElementById('tabelB').style.display = 'none';
	document.getElementById('tabelC').style.display = 'none';	
	document.getElementById('tabelD').style.display = 'none';		
	document.getElementById(id).style.display = 'block';
	if (id=='tabelA') {
		$("#tabelA").load('/data/static/current.html',0, function() {
			fix_footer();
		});
		$("#tabelAli").addClass('actief');
	}
	if (id=='tabelB') {
		$("#tabelB").load('/data/static/current_loting.html',0, function() {
			fix_footer();
		});
		$("#tabelBli").addClass('actief');
	}
	if (id=='tabelC') {
		$("#tabelC").load('/data/static/current.html',0, function() {
			fix_footer();
		});
		$("#tabelCli").addClass('actief');
	}
	if (id=='tabelD') {
		$("#tabelD").load('/data/static/klassement_heren.html',0, function() {
			fix_footer();
		});
		$("#tabelDli").addClass('actief');
	}
}

function showImage(videoURL,vTitle,divID) {
	var html=	'<img src="'+videoURL+'" />';
	$("#"+divID).html(html);
}

function playMedia(tcmId,divID,type) {
//	var flashURL;
//	flashURL	=	"autostart=true";
//	flashURL	=	flashURL + "&height=232";
//	flashURL	=	flashURL + "&file=http://player.nos.nl/index.php/embedded/playlist/tcmid/" + videoURL;
//	flashURL	=	flashURL + "&width=378";
//	flashURL	=	flashURL + "&bufferlength=8";
//	
//	var F0 = { movie:"http://player.nos.nl/nos/media/flash/mediaplayer.swf", width:"378", height:"232", quality:"high", bgcolor:"#000000", majorversion:"7", build:"0",  allowscriptaccess:"true", allowfullscreen:"true",  flashvars: flashURL };
//	UFO.create(F0, divID);
//	
//	var rnumber		= Math.floor(Math.random()*10000);
//	var siteStatUrl	= 'http://nl.sitestat.com/klo/nosrtv/s?player.' + tellerName + '&ns_webdir=nieuws&category=nieuws&ns__t=' + rnumber;
//	sitestat(siteStatUrl);
	var userAgent = navigator.userAgent.toLowerCase();
    if (userAgent.search('ipad')>1) {
        var videoHtml = '<video id="uitzending-video" ';
        videoHtml = videoHtml + 'src="http://content.nos.nl/content/playlist/' + type + '/fragment/' + tcmId + '.mp4"';
        videoHtml = videoHtml + 'width="378" height="212" controls="controls" autoplay="autoplay"></video>';
        $("#" + divID).html( videoHtml );
    } else {  
		$('#' + 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'
	          		}
	    		},
	    	{ version: 8 }
		);
    }
}

//function playAudio(url,divID,tellerName) {
//	var flashURL;
//	flashURL	=	"autostart=true";
//	flashURL	=	flashURL + "&file=http://player.nos.nl/index.php/embedded/playlist/tcmid/" + url;
//	flashURL	=	flashURL + "&width=378";
//	flashURL	=	flashURL + "&bufferlength=8";
//	flashURL	=	flashURL + "&image=http://www.nos.nl/static/global/player/images/bg_audio.jpg";
//	
//	var F0 = { movie:"http://player.nos.nl/nos/media/flash/mediaplayer.swf", width:"378", height:"232", quality:"high", bgcolor:"#000000", majorversion:"7", build:"0",  allowscriptaccess:"true", allowfullscreen:"true",  flashvars: flashURL };
//	UFO.create(F0, divID);
//	
//	var rnumber		= Math.floor(Math.random()*10000);
//	var siteStatUrl	= 'http://nl.sitestat.com/klo/nosrtv/s?player.' + tellerName + '&ns_webdir=nieuws&category=nieuws&ns__t=' + rnumber;
//	sitestat(siteStatUrl);
//}


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 MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

