$(document).ready(function(){
	randomNR = Math.floor(Math.random()*999999999);
	options = { slidebypx: '426', datasrc: "/data/latest.json?"+randomNR, itemsperslide: 3 };
	$('body#index div#nos-videobox-recentvideos').nosslidevideos(options);
	$('body#artikel div#nos-videobox-recentvideos').nosslidevideos(options);	

	posities();
	resize_viewport();	
	
	$("ul.closed").css('display','none');
	$("h3.archief-periode").css('cursor','pointer');
	$("h3.archief-periode").click( function() {
		if ($(this).next().css('display')=='none') {
			$(this).next().slideDown();
		} else {
			$(this).next().slideUp();
		}
	});
	
});

function showFoto(src,alt) {
	$('#media').html('<img class="" src="' + src + '" alt="' + alt + '" /><br/><div class="description"><p>' + alt + '</p></div>');
}


function rView(tcmid) {
	url = 'http://player.nos.nl/index.php/media/rview/tcmid/' + tcmid + '/embed/false/';
	document.write("<img src="+url+" width=\"1\" height=\"1\" />");
}


function playMedia(tcmId, divID, type) {
//function playVideo(videoURL, divID,tellerName) {
//	var flashURL;
//	flashURL	=	"autostart=true";
//	flashURL	=	flashURL + "&height=218";
//	flashURL	=	flashURL + "&file=" + videoURL;
//	flashURL	=	flashURL + "&width=352";
//	flashURL	=	flashURL + "&bufferlength=8";
//		
//	var F0 = { movie:"http://player.nos.nl/nos/media/flash/mediaplayer.swf", width:"352", height:"218", 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 + '&amp;category=nieuws&ns__t=' + rnumber;
//	sitestat(siteStatUrl);
	$('#' + divID).html("");
	$('#' + divID).flash(
    	{ 
      		src: 'http://s.nos.nl/swf/player.swf',
      		width: 352,
      		height: 218,
			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(videoURL, divID,tellerName) {
//	var flashURL;
//	flashURL	=	"autostart=true";
//	flashURL	=	flashURL + "&height=218";
//	flashURL	=	flashURL + "&file=" + videoURL;
//	flashURL	=	flashURL + "&width=352";
//	flashURL	=	flashURL + "&bufferlength=8";
//	flashURL 	= 	flashURL + '&image=http://player.nos.nl/data/thumbnails/default.jpg';
//		
//	var F0 = { movie:"http://player.nos.nl/nos/media/flash/mediaplayer.swf", width:"352", height:"218", 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 + '&amp;category=nieuws&ns__t=' + rnumber;
//	sitestat(siteStatUrl);
//}


function calcRan(){
	return(Math.round(Math.random()*10000000));
}

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 posities(){
	breedte = breedte_view();
/*	if(breedte>1439){
		document.getElementById('logo').style.display = 'none';
//		document.getElementById('logo_abs').style.display = 'block';
	}
	else{
		document.getElementById('logo').style.display = 'block';
//		document.getElementById('logo_abs').style.display = 'none';		
	}
*/
	if(breedte>1420){
		document.getElementById('bck_kr').style.display = 'block';		
	}
	else{
		document.getElementById('bck_kr').style.display = 'none';		
	}
	hoogte = hoogte_view()+15;
	document.getElementById('bck_kr').style.height = hoogte + 'px';
}

function breedte_view(){
	var breedte;
	if(self.innerWidth!=undefined)breedte= [self.innerWidth];
	else{
	var D= document.documentElement;
	if(D) breedte= [D.clientWidth];
	}
	return breedte;
}

function hoogte_view(){
	var windowHeight = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.body.offsetHeight);
	var pageHeight = document.getElementById('container').offsetHeight;
	if(windowHeight>pageHeight){
		return windowHeight;
	}
	else{
		return pageHeight;
	}
}

function resize_viewport() {
  $.event.remove( this, "resize", resize_viewport);
  $.event.add( this, "resize", resize_viewport);
  posities();
}

function getMaand(maand){
	var maandAr = new Array(null, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	for(m=0;m<maandAr.length;m++){
		if(maand == maandAr[m]){
			return(m);
		}		
	}
	return(''); //if fails
}


function trim(value) {
  value = value.replace(/^\s+/,'');
  value = value.replace(/\s+$/,'');
  return value;
}

function cutTitel(titel){
	maxLengte = 39;
	stopChar = 0;
	titel = trim(titel);
	if(titel.length>maxLengte){
		for(t=maxLengte;t>0;t--){
			if(titel.charAt(t)==' '){
				stopChar = t;
				break;
			}
		}
		titel = titel.substring(0,stopChar)+' ...';
		return(titel);
	}
	return titel;
}

function makeDate(pubdate){
	 datumArray = pubdate.split(',');
	 datumArray = datumArray[1].split(' 2');
	 datumArray = datumArray[0].split(' ');
	 dag = datumArray[1];
	 maand = getMaand(datumArray[2]);
	 return(dag + '-' + maand);
}

function parseNews(rss,div,aantalBlokken){
	 ran = calcRan();
	 url = Array();
	 titel = Array();
	 description = Array();
	 pubdate = Array();	 
	 output = '';
	 k = 0;
	
	$.ajax({
	 type: "GET",
	 url: rss+'?'+ran,
	 dataType: "xml",
	   success: function(xml){	  
			 $(xml).find('item').each(function(){		
			 url[k] = $(this).find('link').text();			   
 			 titel[k] = $(this).find('title').text();
			 titel[k] = cutTitel(titel[k]);
			 description[k] = $(this).find('description').text();
			 pubdate[k] = makeDate($(this).find('pubDate').text());
			 k++;			
   		 }); //close each(
		 output = "<h2><span class=\"bron\">RVD</span> <span>Nieuws feed</span></h2>";
		 output += "<ul class=\"lijst\">";
		 for(j=0;j<aantalBlokken;j++){
		 	output += '<li><span class="right">'+pubdate[j]+'</span><a href="'+url[j]+'" title="'+titel[j]+'" target=\"_blank\">&gt;&nbsp;'+titel[j]+'</a></li>';
		 }		 
		 output += "</ul>";
		 output += "<div><a href=\"http://www.koninklijkhuis.nl\" target=\"_blank\">&gt;&gt; Ga naar de website van de RVD</a></div>";
	
		 document.getElementById(div).innerHTML = output;
	 	 posities();  //fix firefox
	  }
	 });//close $.ajax(	
}

function writeEmbed(tcmid){
	value = '<object width="352" height="198"><param name="movie" value="http://s.nos.nl/swf/player.swf"></param><param name="wmode" value="transparent"></param><param name="allowScriptAccess" value="always"></param><param name="allowfullscreen" value="true"></param><param name="flashvars" value="skin=http://s.nos.nl/swf/skin/nos.swf&plugins=http://s.nos.nl/swf/plugins/nosstats,http://s.nos.nl/swf/plugins/nosembed&autostart=false&file=http://content.nos.nl/content/playlist/video/fragment/' + tcmid + '.xml&bufferlength=12" /><embed src="http://s.nos.nl/swf/player.swf" type="application/x-shockwave-flash" wmode="transparent" width="352" height="198" allowfullscreen="true" allowScriptAccess="always" flashvars="skin=http://s.nos.nl/swf/skin/nos.swf&plugins=http://s.nos.nl/swf/plugins/nosstats,http://s.nos.nl/swf/plugins/nosembed&autostart=false&file=http://content.nos.nl/content/playlist/video/fragment/' + tcmid + '.xml&bufferlength=12"></embed></object>'
	document.embedform.embed.value = value;
}