function init() {
	pngfix();
	//populateNews();
	populateRNC();
	//populateCamjo2();
	requestHeader();
}

function winOpen (adr, name, w, h, scr) {
	par = "left=" + Math.floor((screen.width-w)/2) + ",width=" + w;
	par += ",top=" + Math.floor((screen.height-h)/2) + ",height=" + h;
	par += ",scrollbars=" + (scr?"yes":"no");
	par += ",resizable=" + (scr?"yes":"no");
	par += ",status=no";
	return window.open (adr, name, par);
}

function addVideo() {
	var tid = 'AMExI7JqLXFErkfDmx';
	nm = winOpen("http://www.video4all.nl/src/ext/upl_movie.php?tID="+tid,"nmUpload",785,557,false);
	nm.focus();
	return false;
}

function requestHeader() {
	var xmlhttp = new XMLHttpRequest();  
	xmlhttp.open("GET", "index.php?xmlhttp=updatemedia", false);  
	xmlhttp.send('');
}

function searchArchive() {
	/*var year = document.getElementById('startyear').value;
	var month = document.getElementById('startmonth').value;
	var day = document.getElementById('startday').value;
	
	var startdate = '';
	//if (year.selectedIndex > -1 && month.selectedIndex > -1 && day.selectedIndex> -1)
	startdate = year +''+ month +''+ day;*/
	
	var startdate = document.getElementById('vanaf');
	var enddate = document.getElementById('tot');

	/*var year = document.getElementById('endyear').value;
	var month = document.getElementById('endmonth').value;
	var day = document.getElementById('endday').value;	
	
	var enddate;
	enddate = year +''+ month +''+ day;*/

	var search = document.getElementById('searchstr').value;

	//if (startdate.length > 0) {
		var uri = 'index.php?xmlhttp=archive&startdate='+startdate+'&enddate='+enddate+'&search='+search
		Sarissa.updateContentFromURI(uri,document.getElementById('archive'),false,function() { document.getElementById('rncitem').style.display = 'none';document.getElementById('rncitem').innerHTML = '';});
	//}
}

function lionheadUpdateSelectFromURI(uri,obj) {
	var req = new XMLHttpRequest();
	req.open("GET",uri,false);
	req.send("");
	var doc = req.responseXML;
	var els = doc.getElementsByTagName('option');
	obj.options.length=0;
	for (var i=0;i<els.length;i++) {
		obj.options[i]=new Option(Sarissa.getText(els[i],true),els[i].getAttribute('value'));
	}
}

function populateCamjo2(sort,offset) {
	var uri = 'index.php?xmlhttp=channel';
	Sarissa.updateContentFromURI(uri,document.getElementById('camjo'));
}

function populateCamjo(sort,offset) {
	var sort = (sort)	? sort : 'new';
	var uri = 'index.php?xmlhttp=camjo&order='+sort;
	
	if (offset)
		uri += '&offset='+offset;
	
	Sarissa.updateContentFromURI(uri,document.getElementById('camjo'));
	return false;
}

function showCamjo(uri) {
	var uri = 'index.php?xmlhttp=camjo&show='+uri;

	var spacer = document.getElementById('spacer');
	if (!spacer)
		spacer = document.getElementById('archive');

	try {
		while (spacer.childNodes[0])
	    	spacer.removeChild(spacer.childNodes[0]);
	} catch(err) {
	}	

	var el = document.createElement('div');
	el.id='rncitem';
	spacer.appendChild(el);

	Sarissa.updateContentFromURI(uri,document.getElementById('rncitem'),false,function(node,elem) { elem.style.display = 'block'; });
	return false;
}

var sel;

function playArchive(hash, div, search,channel) {

	if (hash.length > 0 && hash != 'null') {
		var flashvars = {
			mh: hash,
			logo: 'lib/images/logo.png',
			config: 'rned1',
			setting:	'H264',
			config: 'rned1',
			condition: search
		};
		
		if (channel && channel.length > 0)
			flashvars.chh = channel;
					
		var params = {
			allowfullscreen: true
		};

		swfobject.embedSWF("md/mdplayer/MediaDepotPlayer.swf",div,"640","380","9.0.0","expressInstall.swf",flashvars,params);
	} else { 
		document.getElementById(div).innerHTML = 'Foutieve aanvraag!';
	}
	
	return false;
	
}

function populateRNC(date, div) {

	if (div) {
		div.className = 'timeline selected';
		if (sel && sel != div)
			sel.className = 'timeline';
		sel = div;
	} else {
		var tmp = document.getElementById('tlplace');
		if (tmp) {
			tmp.childNodes[0].className = 'timeline selected';
			sel = tmp.childNodes[0];
		}
	}

	var uri = 'index.php?xmlhttp=imagedate&date='+date;
	obj = document.getElementById('date');
	if (obj)
		Sarissa.updateContentFromURI(uri,obj);

	var uri = 'index.php?xmlhttp=rnc&uitzending&date='+date;
	
	var spacer = document.getElementById('spacer');
	if (!spacer)
		return;
	else {
		try {
			while (spacer.childNodes[0])
		    	spacer.removeChild(spacer.childNodes[0]);
		} catch(err) {
		}
	}
		//spacer.removeChild(document.getElementById('rncitem'));

	var el = document.createElement('div');
	el.id='rncitem';
	spacer.appendChild(el);
	/*var el = document.createElement('div');
	el.id='omschrijving';
	spacer.appendChild(el);*/
	obj = document.getElementById('rncitem');
	if (obj) {
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET", uri, false);
		xmlhttp.send(''); 
		var doc = xmlhttp.responseXML;

		try {
			var hash = doc.getElementsByTagName('hash')[0].firstChild.nodeValue;
			var chh  = doc.getElementsByTagName('chh')[0].firstChild.nodeValue;
			//var text = doc.getElementsByTagName('omschrijving')[0].firstChild.nodeValue;
		} catch (error) {
			if (hash == undefined) hash = 'null';
			if (chh == undefined) chh = 'null';
			//if (text == undefined) text = '';
		}
		
		if (hash.length > 0 && hash != 'null') {
			flashvars = {
				mh: hash,
				chh: chh,
				logo: 'lib/images/logo.png',
				config: 'rned1',
				setting:	'H264'
			};

			params = {
				allowfullscreen: true
			};
					
			swfobject.embedSWF("md/mdplayer/MediaDepotPlayer.swf","rncitem","640","380","9.0.0","expressInstall.swf",flashvars,params);
			//document.getElementById('omschrijving').innerHTML = text;
		} else { 
			//obj.innerHTML = 'Geen uitzending beschikbaar!';
			document.getElementById('rncitem').innerHTML = 'Geen uitzending beschikbaar!';
		}
		
		//for (var i=0;i<els.length;i++) {
		//	alert(Sarissa.getText(els[i],true));
		//}
		//Sarissa.updateContentFromURI(uri,obj,false,function() { document.getElementById('rncitem').style.display = 'none';document.getElementById('rncitem').innerHTML = '';});
	} 
	return false;
}

function showRNC(uri,id,date) {
	var uri = 'index.php?xmlhttp=rnc&show='+uri+'&id='+id+'&datum='+date;
	Sarissa.updateContentFromURI(uri,document.getElementById('rncitem'),false,function(node,elem) { elem.style.display = 'block'; });
	return false;
}

function populateNews(obj) {
	if (!obj)
		obj = document.getElementById('regio');

	try {
		document.getElementById('news').innerHTML = document.getElementById('hidden').innerHTML;

		var show = (obj) ? obj[obj.selectedIndex].value : 'random';
		var uri = 'index.php?xmlhttp=news&omroep='+show;
		Sarissa.updateContentFromURI(uri,document.getElementById('news'),false);
	} catch (E) {
		
	}
		return false;
}

function showNews(omroep,id) {
	var uri = 'index.php?xmlhttp=news&show='+id+'&omroep='+omroep;
	el = document.getElementById('item'+omroep+id);
	if (el.style.display == 'block')
		el.style.display = 'none';
	else {
		el.innerHTML = document.getElementById('hiddensmall').innerHTML;
		el.style.display='block';
		Sarissa.updateContentFromURI(uri,el,false,function(node,elem) { elem.style.display = 'block'; },true);
	}
	return false;
}

function pngfix() {
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	if ((version >= 5.5)  && (version < 7)  && (document.body.filters)) 
	{
	   for(var i=0; i<document.images.length; i++)
	   {
	      var img = document.images[i]
	      var imgName = img.src.toUpperCase()
	      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	      {
	         var imgID = (img.id) ? "id='" + img.id + "' " : ""
	         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
	         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
	         var imgStyle = "display:inline-block;" + img.style.cssText 
	         if (img.align == "left") imgStyle = "float:left;" + imgStyle
	         if (img.align == "right") imgStyle = "float:right;" + imgStyle
	         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
	         var strNewHTML = "<span " + imgID + imgClass + imgTitle
	         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
	         img.outerHTML = strNewHTML
	         i--
	      }
	   }
	}
}

//var current = null;

function changeDisplay(div) {
	try {
		obj = document.getElementById(div);

		if (current && current.length > 0 && current != obj.id)
			document.getElementById(current).style.display = 'none';

		if (obj) {
			if (!obj.style.display || obj.style.display == 'none') {
				obj.style.display = 'block';
				current = obj.id;
			} else {
				obj.style.display = 'none';
				current = null;
			}
		}
	} catch(err) {
		alert(err);
	}
	return false;
}

$(function() {
	$('.header').click(function() {
		$('#listmaster').children('div').slideUp('slow');
	
		val = $(this).attr("id").replace("head","div");
		$('#'+val).slideDown(1000);
	});
});