//iframe resizen voor IE
function resize(){
	try{
		var e = document.getElementById("frame").getElementsByTagName("iframe");
		for(var j in e){
			if(e[j].style){
				document.getElementById(j).style.height=(document.body.clientHeight-20)+"px";
				document.getElementById(j).style.width=document.getElementById("tabs").clientWidth+"px";
			}
		}
	}
	catch(e){}
}
function register(element, type, func) {
	if (element.addEventListener) {
		element.addEventListener(type, func, false)
	} else if (element.attachEvent) {
		element.attachEvent('on' + type,func)
	}
}
/*
resize en mousemove event activeren + first functions colsize,
xhr om stream list ophalen + cookie ophalen om de laatste gekozen stream weer te activeren
en de webcam openen indien het vorige keer was geopend
*/
window.onload = function(){
	if (navigator.userAgent.indexOf("MSIE") != -1){
		//window.onresize = resize;
		//resize();
	}
	if(location.pathname == "/" || location.pathname == "/index2.php" || location.pathname == "/beta.php"){
		var ck = getCookie("colsize");
		if(ck){
			colsize(ck);
		}		
		xhr("GET","st.json",function(){
			if(this.readyState==4){
				eval("window.streams = "+this.responseText);
				var obj = document.getElementById("slcstm")
				for(i=0;i<streams.length;i++){
					obj.options[i+1] = new Option(streams[i][2],i);
				}
				var ck = getCookie("stream");
				if(ck){
					for(i=0;i<streams.length;i++){
						if(streams[i][0] == ck){
							obj.options[i+1].selected = true;
							player(1,obj.value);
							break;
						}
					}
				}
			}
		});
		var ck = getCookie("webcam");
		if(ck){
			player(2);
		}
		register(document.body.children[1],"mousedown",mf.down)
		register(document.body.children[1],"mousemove",mf.move)
		register(document.body.children[1],"mouseup",mf.up)
	}
}
//functions voor mouse events
var colchange=false;
var mf = {
	so:0,
	pos:function(e){
		var posx = 0;
		var posy = 0;
		if (!e){ var e = window.event;}
		if (e.pageX || e.pageY){
			posx = e.pageX;
			posy = e.pageY;
		}else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft	+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		return posx;
	},
	down:function(e){
		mf.so = ws-mf.pos(e);
		colchange=true;
		var a = document.body.children[1].style;
		a.width = "100%";a.left = "0px";
		if (window.getSelection){
			window.getSelection().removeAllRanges();
		}
		//return false;
	},
	move:function(e){
		if(colchange && (mf.pos(e)+mf.so) != ws){
			colsize(mf.pos(e)+mf.so);
			//return false;
		}
	},
	up:function(e){
		colchange=false;
		document.body.children[1].style.width = null;
		colsize(ws);
		//return false;
	}
}
//function om de breedte van de vensters aan te passen + asprat uitvoeren
function colsize(e){
	if(e > 467){
		ws = e;
	} else{
		ws = 468;
	}
	setCookie("colsize",ws,"Y1");
	document.body.children[0].style.width = (ws)+"px";
	if(!colchange){
		document.body.children[1].style.left = (ws-3)+"px";
	}
	document.body.children[2].style.left = (ws*1)+"px";
	asprat(pr);
}
// function om de hoogte te bepalen voor de video player aan de hand van aspect ratio
var pr=false,ws=468;
function asprat(e){
	if(e){
		pr = e;
		var i = document.getElementById("play");
		if(e.split(":").length > 1){
			var as = e.split(":")
			var hg = (ws/as[0])*as[1];
			var hc = Math.ceil(hg);
			var hf = Math.floor(hg);
			if(!(hc%2)){
				hg = hc
			}
			else if(!(hf%2)){
				hg = hf;
			}
			else if(hg%2){
				hg++;
			}
			i.style.height = (hg+as[2]*1)+"px";
			return (hg+as[2]*1);
		} else if(e.split("X").length > 1){
			var as = e.split("X");
			i.children[0].style.width = (as[0]*1)+"px";
			i.children[0].style.height = (as[1]*1)+"px";
			i.children[0].style.margin = "auto";
		}
	}
}
//util function voor het ophalen van bestanden
function xhr(mt,fl,fn,dt){
	if (window.XMLHttpRequest){xmlhttp = new XMLHttpRequest();}
	else if(window.ActiveXObject){xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
	else{return}
	xmlhttp.onreadystatechange=fn
	xmlhttp.open(mt,fl,true);
	if(dt){
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp.setRequestHeader('Content-Length',dt.length);
	}
	xmlhttp.send(dt);
}
// class om een object element aan te maken op de DOM manier
function dom(tg,ob){
	if(typeof tg == "string"){
		var a = document.createElement(tg);
	} else if(typeof tg == "object"){
		var a = tg;
	}
	this.attr = function(t,v){
		a.setAttribute(t,v);
	}
	this.child = function(t,o){
		var d = new dom(t,o);
		a.appendChild(d.write());
	}
	this.text = function(t){
		a.appendChild(document.createTextNode(t));
	}
	this.param = function(n,v){
		this.child("param",[
			["attr","name",n],
			["attr","value",v]
		])
	}
	var nt = new Array();
	this.flvar = function(n,v){
		nt.push(n+"="+v);
	}
	var st = new Array();
	this.style = function(n,v){
		st.push(n+":"+v);
	}	
	this.event = function(e,f){
		if (a.attachEvent) {
			//a.attachEvent(e,f);
		} else if (a.addEventListener) {
			a.addEventListener(e,f,false);
		} else {
			eval("a.on"+e+"=f");
		}
	}
	//--java chat
	var sml = 85;
	this.smile = function(n,v){
		if(typeof n == "string"){
			this.param("style:smiley"+sml.toString(10),n+" ico/"+v+".gif");
			sml++;
		}
		if(typeof n == "object"){
			for(var e in n){
				this.param("style:smiley"+sml.toString(10),n[e]+" ico/"+v+".gif");
				sml++;
			}
		}
	}
	var cmi = 2;
	this.cmd = function(v){
		this.param("comcommand"+cmi.toString(10),v);
		cmi++;
	}
	//
	this.write = function(id){
		if(nt.length){
			this.param("flashvars",nt.join("&"));
		}
		if(st.length){
			this.attr("style",st.join(";"));
		}
		if(id){
			if(typeof id == "string"){
				document.getElementById(id).appendChild(a);
			} else if(typeof id == "object"){
				id.appendChild(a);
			}
		} else{
			return a;
		}
	}
	for(var e in ob){
		if(!isNaN(e)){
			var t = new Array()
			for(var i=1;i<ob[e].length;i++){
				t.push("ob[e]["+i+"]");
			}
			eval("this."+ob[e][0]+"("+t.join()+")");
		}
	}
}
//listner toevoegen aan wmv om de function uit te voeren bij volume wijziging
function addListeners(){
	if(p.view) {
		p.addListener('VOLUME',function(e){
			setCookie("volume",e.toString(),"Y1");
		});
	}
	else {
		setTimeout("addListeners()",50);
	}
}
//objecten (wmv/npo/chat/cam) wegschrijven naar pagina met class ob
var m=false,n=true
function player(t,e){
	if(t == 1){
		var i = document.getElementById("play");
		if(m){
			p.sendEvent("STOP");
		}
		if(i.children[0]){
			i.removeChild(i.children[0]);
		}
		if(e >= 0 && e < streams.length){
			setCookie("stream",streams[e][0],"Y1");
			if(streams[e][1] == "wmv"){
				m = true;
				var aspect;
				var c = {
					style:'true',
					showdigits:'false',
					autostart:'true',
					bufferlength:'1',
					width:false,
					height:false
				};
				if(getCookie("volume")){
					c.volume = getCookie("volume");
				}
				var aspect = streams[e][4];
				c.file = streams[e][3]
				if(streams[e][5]){
					c.logo = "logo/3fmwmv.png";
				}
				if(aspect){
					if(aspect.indexOf("C") != -1){
						var z = aspect.indexOf("C");
						c.aspect = aspect.substr(0,z);
						var as = c.crop = aspect.substr(z+1)
					} else{
						var as = c.aspect = aspect;
					}
					c.overstretch = 'fit';
					var hg = asprat(as+":20");
				}
				if(hg < 21){
					c.showicon = 'false';
					c.usefullscreen = 'false';
				}
				window.p = new jeroenwijering.Player(i,'wmvplayer.xaml',c);
				addListeners();
			}else if(streams[e][1] == "npo24"){
				asprat("16:9:0");
				m = false;
				Silverlight.createObject({properties:{
					id:i,
					source:"http://slplayer.nederland24.nl/xap/NPONL24.xap",
					windowless:true,
					initParams:{
						playerWidth:ws,
						showPreviousNextChannel:false,
						showViewModePIP:false,
						showViewModeMosaic:false,
						showViewModePIP:false,
						showList:false,
						showRelated:false,
						channelCode:streams[e][3]
					}
				}});
			}else if(streams[e][1] == "npoug"){
				asprat("16:9:0");
				m = false;
				if(streams[e][4]){
					var pl = "rss";
				} else{
					var pl = "ugslplaylist";
				}
				Silverlight.createObject({properties:{
					id:i,
					source:"http://embed.player.omroep.nl/sl/ugslplayer.xap",
					windowless:true,
					initParams:{
						playMode:"play",
						playlistUrlFormat:pl,
						playlistUrlHashMethod:"none",
						playlistUrl:"http://freaknacht.hole1988.nl/"+streams[e][3],
						viewMode:"playlist",
						playlistAdvanceDelaySeconds:"3"
					}
				}});
			}else if(streams[e][1] == "youtube"){
				m = false;
				i.appendChild(document.createElement("div"));
				var fl = new dom("object",[
					["attr","type","application/x-shockwave-flash"],
					["attr","data","http://www.youtube.com/"+streams[e][3]],
					["param","allowScriptAccess","sameDomain"],
					["param","movie","http://www.youtube.com/"+streams[e][3]],
					["param","quality","high"],
					["param","bgcolor","#ffffff"],
					["param","wmode","opaque"],
					["write",i.children[0]]
				])
				asprat("746×413");
			}else if(streams[e][1] == "flash"){
				m = false;
				//i.appendChild(document.createElement("div"));
				var fl = new dom("object",[
					["attr","type","application/x-shockwave-flash"],
					["attr","data",streams[e][3]],
					["param","allowScriptAccess","always"],
					["param","allowfullscreen","true"],
					["param","movie",streams[e][3]],
					["param","quality","high"],
					["param","bgcolor","#000000"],
					["param","wmode","opaque"]
				])
				for (x in streams[e][4]){
					fl.flvar(x,streams[e][4][x]);
					//alert(x);
				}				
				fl.write(i)
				asprat(streams[e][5]);
			}else if(streams[e][1] == "iframe"){
				m = false;
				//i.appendChild(document.createElement("div"));
				var fl = new dom("iframe",[
					["attr","src",streams[e][3]],
					["attr","frameborder",0],
					["style","overflow","hidden"],
					["write",i]
				])
				asprat(streams[e][4]);
			}
		}else{
			pr = false;
			m = false;
			delCookie("stream");
		}
	}
	else if(t == 2){
		var i = document.getElementById("cam");
		if(n){
			setCookie("webcam",true,"Y1");
			var fl = new dom("object",[
				["attr","type","application/x-shockwave-flash"],
				["attr","data","http://live.3fm.nl/freaknacht/webcam/client/client.swf"],
				["param","allowScriptAccess","sameDomain"],
				["param","movie","http://live.3fm.nl/freaknacht/webcam/client/client.swf"],
				["param","quality","high"],
				["param","bgcolor","#ffffff"],
				["param","wmode","opaque"],
				["write",i.children[0]]
			])
			i.style.height = "206px";
			document.getElementById("play").style.top = "371px";
			document.getElementById("btncam").innerHTML = "webcam uit";
			n=false;
		}
		else{
			delCookie("webcam");
			i.children[0].removeChild(i.children[0].children[0]);
			document.getElementById("play").style.top = null;
			i.style.height = null;
			document.getElementById("btncam").innerHTML = "webcam aan";
			n=true;
		}
	}
	else if(t == 3){
		var f = document.getElementById("form");
		var nck = f.nick.value;
		var pwd = f.pass.value;
		var eml = f.mail.value
		var i = document.getElementById("app");
		var ch = new dom("object");
		ch.attr("type","application/x-java-applet");
		ch.attr("class","full");
		ch.param("code","IRCApplet.class");
		ch.param("archive","irc.jar,pixx.jar");
		ch.param("codebase","http://freaknacht.hole1988.nl/chat/")
		ch.param("fileparameter","pjirc.cfg")		
		if(nck.length){
			ch.param("nick",nck);
			ch.param("alternatenick",nck+"??");
			ch.param("name",nck);
		}
		else{
			ch.param("nick","freaknacht.hole1988.nl-???");
		}
		if(mode == 1 && pwd.length){
			ch.cmd("/nickserv ghost "+nck+" "+pwd);
			ch.cmd("/nick "+nck);
			ch.cmd("/nickserv identify "+pwd);
		}
		else if(mode == 2 && pwd.length){
			ch.cmd("/nickserv register "+pwd);
			ch.cmd("/nickserv set "+nck+" kill on");
			if(eml.length){
				ch.cmd("/nickserv set "+nck+" email "+eml);
			}
		}
		i.removeChild(i.children[0]);
		ch.write(i);
		return false;
	}
}
var comnow=0;
function com(e){
	window.scroll(0,0);
	if(e != comnow){
		var j = document.getElementById("tabs").children;
		j[e].className = "taba";
		j[comnow].className = "tabd";
		var k = document.getElementById("frame").children;
		k[e].style.visibility = "visible";
		k[comnow].style.visibility = "hidden";
		comnow = e;
	}
}
var mode = 0;
function mp(i){
	document.getElementById("pass").style.display = "inline";
	document.getElementById("btn").style.display = "none";
	if(i == 2){
		document.getElementById("mail").style.display = "inline";
	}
	mode = i;
}
function lc(i){
	if(i < 3){
		var win = "http://widget.mibbit.com/?settings=c5bd0dcd5fb18504611d32c3d2ce9181&amp;server=irc.lichtsnel.nl&amp;channel=%233fm&amp;nick=";
		if(i == 2){
			win += "&amp;promptPass=true";
		}
		var i = document.body;
		i.removeChild(i.children[0]);
		dom("iframe",[
			["attr","class","full"],
			["attr","src",win],
			["write",i]
		]);
		//document.location = win;
		return false;
	}else if(i < 6) {
		mode = i;
		document.getElementById("front").style.display = "none";
		document.getElementById("reg").style.display = "block";
		document.getElementById("app").style.width = "330px";
		var j = document.getElementById("reg").children;
		var k = 46;
		j[0].style.display = "inline";
		if(i == 3 || i == 5){
			j[1].style.display = "inline";
			k += 22;
			j[2].style.display = null;
			if(i == 3){
				j[2].style.display = "inline";
				k += 22;
			}
			j[3].style.display = null;
			j[4].style.display = null;
		} else{
			j[1].style.display = null;
			j[2].style.display = null;
			j[3].style.display = "inline";
			j[4].style.display = "inline";
			k += 44;
		}
		document.getElementById("app").style.height = k+"px";
	}else if(i == 6){
		var j = document.getElementById("reg").children;
		var win = "type="+mode+"&nick="+j[0].children[0].value;
		if(mode == 3 || mode == 5){
			win += "&pass1="+j[1].children[0].value;
			if(mode == 3){
				win += "&email="+j[2].children[0].value;
			}
		} else{
			win += "&pass1="+j[3].children[0].value;
			win += "&pass2="+j[4].children[0].value;
		}
		xhr("POST","irc.php",function(){
			if(this.readyState==4){
				eval(this.responseText)
			}
		},win);
	}else{
		document.getElementById("front").style.display = "block";
		document.getElementById("reg").style.display = "none";
		document.getElementById("app").style.width = "600px";
		document.getElementById("app").style.height = "100px";
	}
}
