var _W=window,_D=document,popupEl,nrSteps=20,dragObj,dragX,dragY,dragZ=1,close_n=new Image(13,13),close_h=new Image(13,13);
_D.onmouseup=function(){try{dragObj=_D.onselectstart=_D.onmousemove=null}catch(e){}};

// Preload all rollover images
var ro = ['button_abonneer_ro.png','button_download_ro.png','button_speelaf_ro.png','ga_ro.gif','live_ro.jpg','play_blue_ro.gif','play_white_ro.gif','rss_ro.png','terugnaarhome_ro.gif','webkanalen_ro.jpg'];
function _init() {
	for (x in ro) {
		var tmp = new Image;
		tmp.src = '/pics/' + ro[x];
	}
}

close_h.src = '/pics/close_hi.gif';
close_n.src = '/pics/close.gif';

function sniff(){
	var t=this,T=true,F=false,n=navigator,ua=n.userAgent.toLowerCase();
	t.ver=parseInt(n.appVersion);
	t.mac=ua.indexOf('mac')!=-1;
	t.win=!t.mac&&ua.indexOf('wind')!=-1||ua.indexOf('16bit')!=-1;
	t.safari=ua.indexOf('safari')!=-1&&t.mac;
	t.konq=ua.indexOf('konqueror')!=-1;
	t.khtml=t.safari||t.konq;
	t.opera=_W.opera?T:F;
	t.opera5=ua.indexOf('opera 5')!=-1||ua.indexOf('opera/5')!=-1;
	t.opera6=ua.indexOf('opera 6')!=-1||ua.indexOf('opera/6')!=-1;
	t.opera7=t.opera&&_D.createComment?T:F;
	t.ie5=t.ie55=t.ie6=F;
	t.ie=_D.all&&!t.opera&&ua.indexOf('msie')!=-1?T:F;
	if(t.ie){
		t.ieBox=_D.compatMode!='CSS1Compat';
		t.ie5=!_D.fireEvent?T:F;
		t.ie55=_D.fireEvent&&!_D.createComment?T:F;
		t.ie6=_D.fireEvent&&_D.createComment?T:F
	}
	t.gecko=!t.khtml&&!_D.all&&!t.opera&&_D.getElementById?T:F;
	t.ns4=n.appName=='Netscape'&&t.ver==4
}
var is=new sniff(),op=is.opera;
function gE(e){if(_D.getElementById)return _D.getElementById(e);if(_D.all)return _D.all[e]}
function sE(e){e.style.visibility='visible'}
function hE(e){e.style.visibility='hidden'}
function SE(e){e.style.display=''}
function HE(e){e.style.display='none'}
function sX(e,x){op?e.style.pixelLeft=x:e.style.left=x+'px'}
function sY(e,y){op?e.style.pixelTop=y:e.style.top=y+'px'}
function sW(e,w){op?e.style.pixelWidth=w:e.style.width=w+'px'}
function sH(e,h){op?e.style.pixelHeight=h:e.style.height=h+'px'}
function wH(e,h){e.innerHTML=h}

function getWinSize(what) {
	if (what == 'height')
		return (is.ie) ? (is.ieBox) ? _D.body.clientHeight : _D.documentElement.clientHeight : _W.innerHeight;
	if (what == 'width')
		return (is.ie) ? (is.ieBox) ? _D.body.clientWidth : _D.documentElement.clientWidth : _W.innerWidth;
}

function setEvent(el,evt,state,fn) {
	if (state) {
		if (el.addEventListener)
			el.addEventListener(evt,fn,false);
		if (el.attachEvent)
			el.attachEvent('on'+evt,fn);
	}
	else {
		if (el.removeEventListener)
			el.removeEventListener(evt,fn,false);
		if (el.detachEvent)
			el.detachEvent('on'+evt,fn);
	}
}

function makePopup(e, width, height, title, url) {
	killPopup(1);
	height += 24;
	var wh = getWinSize('height'), ww = getWinSize('width'), st, sl;
	if (!e) {
		e = new Object();
		e.clientX = ww/2-width/2;
		e.clientY = wh/2-height/2;
	}
	var top = e.clientY, left = e.clientX;
	if (is.ie) {
		sl = (is.ieBox) ? _D.body.scrollLeft : _D.documentElement.scrollLeft;
		st = (is.ieBox) ? _D.body.scrollTop  : _D.documentElement.scrollTop;
	}
	else {
		sl = _W.pageXOffset;
		st = _W.pageYOffset;
	}
	if (e.clientX + width > ww)
		left = ww-width-5;
	if (e.clientY + height > wh)
		top = wh-height-5;

	popupEl = _D.createElement('div');
	popupEl.onmousedown=function(event){dragStart(event,this)};
	popupEl.className = 'popup';
	_D.body.appendChild(popupEl);
	sizePopup(escape(title),url,left+sl,top+st,width,height,nrSteps);
}

function sizePopup(title,url,left,top,width,height,step) {
	// New and improved: with slomogrow effect!
	var html, mouse;
	if (step < 0) {
		mouse = (is.ie) ? 'onmouseover="this.src=close_h.src" onmouseout="this.src=close_n.src"' : '';
		html  = '<table class="popupCaption" cellpadding="0" cellspacing="3"><tr>';
		html += '<td width="99%" align="center">'+unescape(title)+'</td>';
		html += '<td width="1%" align="right"><img src="/pics/close.gif" class="ptr" width="13" height="13" vspace="2" alt="Close" onclick="killPopup(0)" '+mouse+' /></td>';
		html += '</tr></table>';
		wH(popupEl, html+'<iframe frameborder="0" src="'+url+'" style="width:'+width+'px;height:'+(height-24)+'px"></iframe>');
		return;
	}
	sX(popupEl, left + step*width/nrSteps/2);
	sY(popupEl, top + step*height/nrSteps/2);
	sW(popupEl, width - step*width/nrSteps);
	sH(popupEl, height - step*height/nrSteps + (is.safari?2:0));
	setTimeout("sizePopup('"+title+"','"+url+"',"+left+","+top+","+width+","+height+","+(step-1)+")",0);
}

function killPopup(now) {
	if (popupEl) {
		var code = '_D.body.removeChild(popupEl);popupEl=null;';
		if (now)
			eval(code);
		else
			setTimeout(code,10);
	}
}

function dragStart(e,el) {
	if (!e)e=_W.event;
	dragObj = el;
	dragObj.style.zIndex = dragZ++;
	dragX = e.clientX - dragObj.offsetLeft;
	dragY = e.clientY - dragObj.offsetTop;
	_D.onmousemove=dragMove;
	_D.onselectstart=function(){return false};
}

function dragMove(e) {
	if (!e)e=_W.event;
	if(dragObj) {
		sX(dragObj,e.clientX - dragX);
		sY(dragObj,e.clientY - dragY);
	}
}

function Abonneer(id) {
	makePopup(null, 500, 100, 'Abonneer', '/abonneer.php?'+parseInt(id));
}

function bookmarkLink() {
	var linktekst = 'Voeg pagina toe aan favorieten';

	if(is.opera) {
		document.write('<a href="http://www.radiocast.nl/" title="Radiocast.nl" rel="sidebar">' + linktekst + '</a>');
	} else if (is.gecko) {
		document.write('<a href="#" onclick="window.sidebar.addPanel(\'Radiocast.nl\', \'http://www.radiocast.nl\', \'\');return false;">' + linktekst + '</a>');
	} else if (is.ie && !is.mac) {
		document.write('<a href="#" onclick="window.external.AddFavorite(\'http://www.radiocast.nl\',\'Radiocast.nl\'); return false;">' + linktekst + '</a>');
	} else {
		document.write('<a href="#" onclick="alert(\'Sorry, uw browser ondersteund deze functionaliteit niet.\'); return false;">' + linktekst + '</a>');
	}
}

function homeLink() {
	if (is.ie) {
		document.write('<a href="#" onclick="this.style.behavior=\'url(#default#homepage)\';this.setHomePage(\'http://www.radiocast.nl\');return false;">Maak dit mijn startpagina</a>');
	} else if (is.opera) {
		document.write('<a href="#" onclick="makePopup(null, 500, 200, \'Instructies\', \'/sethome.php?b=opera\');return false;">Maak dit mijn startpagina</a>');
	} else if (is.gecko) {
		document.write('<a href="#" onclick="makePopup(null, 500, 200, \'Instructies\', \'/sethome.php?b=gecko\');return false;">Maak dit mijn startpagina</a>');
	} else {
		document.write('<a href="#" onclick="alert(\'Sorry, uw browser ondersteund deze functionaliteit niet.\');return false;">Maak dit mijn startpagina</a>');
	}
}

function doSearch(query) {
	if ((query.replace(/\s+/, '')).length < 3) {
		alert('Uw zoekopdracht is te kort. U dient op een woord van minimaal drie karakters te zoeken.'); 
		return false;
	}
	document.location = "/?sct=zoek&q=" + urlencode(query);
}

function urlencode(val){
	var chr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*'()";
	var hex = '0123456789ABCDEF';

	var encoded = '';
	for (var i = 0; i < val.length; i++ ) {
		var ch = val.charAt(i);
	    if (ch == ' ') {
		    encoded += '+';
		} else if (chr.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += '+';
			} else {
				encoded += '%';
				encoded += hex.charAt((charCode >> 4) & 0xF);
				encoded += hex.charAt(charCode & 0xF);
			}
		}
	}
	
	return encoded;
}

// Uniforme rollover. Werkt zolang bestanden <naam>.<ext> en <naam>_ro.<ext> genoemd worden.
function RO(el) {
	if (!el.src) return false;

	var s = el.src;

	if (!s.match(/_ro\.[a-z]+$/i)) {
		el.src = s.replace(/(\.[a-z]+)$/i, "_ro$1");
	} else {
		el.src = s.replace(/_ro(\.[a-z]+)$/i, "$1");
	}
}

var locked = false;

function searchLock(tf, el) {
	locked = tf;
	
	if (el) {
		el.value = el.value == 'zoek binnen radiocast.nl' ? '' : !tf && el.value == '' ? 'zoek binnen radiocast.nl' : el.value;
		el.className = tf ? 'search' : el.value == 'zoek binnen radiocast.nl' ? 'initsearch' : 'search';
	}

	searchHover(tf);
}

function searchHover(tf,welke) {
	if (locked) return false;

	if(!welke) welke = 'zoek';

	var kleur = (tf ? "#39c" : "#f2f2f2");

	gE(welke + '_top').src = tf ? '/pics/zoek_top_ro.gif' : '/pics/zoek_top.gif';
	gE(welke + '_bottom').src = tf ? '/pics/zoek_bottom_ro.gif' : '/pics/zoek_bottom.gif';
	gE(welke + '_ga').src = tf ? '/pics/ga_ro.gif' : '/pics/ga.gif';
	gE(welke + '_ga').parentNode.style.background = kleur;
	gE(welke + '_input').parentNode.style.background = kleur;
}

function tabSwitch(to, from) {
	document.getElementById('switch_' + from).style.display = 'none';
	document.getElementById('switch_' + to).style.display = 'block';
}

function gemistTitels(id) {
	var dd = gE('gemistTitels');

	// Verwijder alle aanwezige titels
	dd.options.length = 0;

	dd.options[0] = new Option('kies programmatitel','0',false);

	// Voeg titels voor geselecteerde zender toe
	if (id && parseInt(id) > 0 && gemist[id]) {
		dd.disabled = false;
		var c = 0;
		for (x in gemist[id]) {
			c++;
			dd.options[c] = new Option(gemist[id][x][0],gemist[id][x][1]);
		}
	} else {
		dd.disabled = true;
	}
}

function visitTitel(href) {
	if (href == 0) {
		return false;
	}

	//window.open(href, '_blank');
	document.location = href;
}

function Play(pc,id) {
	window.open('/player/player.php?pc=' + pc + (id ? '&id=' + id : ''), 'podcastplayer', 'height=508,width=716,status=no,resizable=no');
}

function checkEnter(kc) {
	if (kc == 13) {
		doSearch(gE('zoek_input').value);
	}
}