
window.addEventListener('load', initialize, false);

//----------------------------------------------------------------------------------- Framework functions -----------------------------------------------------------------------------------------------------------

//------------------------------------Globals----------------------------------------------
var body;
var continuewith = new GOON(new Array(), new Array());
var active = 0;
//------------------------------------End Globals------------------------------------------

function GOON(funcnames, funcparams){
	this.pointer = -1;
	this.functions = funcnames;
	this.funcparams = funcparams;
}

function continue_(){
	continuewith.pointer++;
	if(continuewith.pointer >= continuewith.functions.length){
		active = 0;
		return;
	} else
		continuewith.functions[continuewith.pointer](continuewith.funcparams[continuewith.pointer]);
}

function $(id) {
	return document.getElementById(id);
}

function getStyle(el, styleProp)	{
		return document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
	}

function findPos(obj) {
	if(getStyle(obj, "position") == "fixed" || getStyle(obj, "position") == "absolute")
		return [parseInt(getStyle(obj, "left")), parseInt(getStyle(obj, "top"))];
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}


function getDim(obj) {
	var y = obj.offsetHeight;
	var x = obj.offsetWidth;
		x = x - parseInt(getStyle(obj, "padding-left")) - parseInt(getStyle(obj, "padding-right")) - parseInt(getStyle(obj, "border-left-width")) - parseInt(getStyle(obj, "border-right-width"));
	y = y - parseInt(getStyle(obj, "padding-top")) - parseInt(getStyle(obj, "padding-bottom")) - parseInt(getStyle(obj, "border-top-width")) - parseInt(getStyle(obj, "border-bottom-width"));
		
	return [x, y];
}


// continue function with params: show, withcontinue
function shadebody(params) {
	var show = parseInt(params[0]);	
	var withcontinue = parseInt(params[1]);
	
	if(show == 1){
		$("blende").style.display = "block";
	} else {
		$("blende").style.display = "none";
	}
	if(withcontinue == 1)continue_();
}


// continue function with params: show, withcontinue
function scrollbars(params) {
	var show = parseInt(params[0]);
	var withcontinue = parseInt(params[1]);

	if(show == 0){
				body.style.overflow = "hidden";
				if(withcontinue == 1)continue_();
	} else {
				body.style.overflow = "auto";
				if(withcontinue == 1)continue_();
	}
}

// continue function with params: id, alpha, withcontinue
function fade(params){
	var intfading;
	var actfade;
	var endfade;
	var fadeadd;
	var divtofade;
	var withcontinue = parseInt(params[2]);
	
	if(withcontinue == 1)active = 1;

	divtofade = $(params[0]);
		actfade = parseInt(getStyle(divtofade, "opacity")*100);
	
	endfade = parseInt(params[1]);
	if(endfade != 0)divtofade.style.display = "block";
	if(actfade == endfade){
		continue_();
		return;
	}
	if(actfade < endfade)
		fadeadd=15;
	else
		fadeadd=-15;
	intfading = setInterval( function() {
		actfade+=fadeadd;
				if(actfade == 100)
			divtofade.style.opacity = 0.99;
		else if(actfade == 0)
			divtofade.style.opacity = 0.01;
		else
			divtofade.style.opacity = actfade*0.01;
				if((actfade>=endfade && fadeadd>0)||(actfade<=endfade && fadeadd<0))
		{
			if(endfade == 0)divtofade.style.display = "none";
			clearInterval(intfading);
			if(withcontinue == 1)continue_();
		}
	}, 25);
}


// continue function with params: type(1 = fixed height, 2 = fixed width), width, height, id, withmargin, withcontinue
function resize(params) {
	var intresizing;
	var actx;
	var acty;
	var endx;
	var endy;
	var step;
	var stepsx;
	var stepsy;
	var width = parseInt(params[1]);
	var height = parseInt(params[2]);		
	var withmargin = parseInt(params[4]);
	var count;
	var divtoresize;
	var withcontinue = parseInt(params[5]);
	
	if(withcontinue == 1)active = 1;
	
	divtoresize = $(params[3]);
	divtoresize.style.display = "block";
	
	switch(parseInt(params[0])){
		case 1:
			divtoresize.style.height = height+"px";
			if(withmargin == 1)divtoresize.style.marginTop = -0.5*height+"px";
		break;
		case 2:
			divtoresize.style.width = width+"px";
			if(withmargin == 1)divtoresize.style.marginLeft = -0.5*width+"px";
		break;
	}
	var temp = getDim(divtoresize);
	count = 7;
	actx = temp[0];
	stepsx = new Array();
	stepsx.push(0);
	step = (width-actx)/count;
	endx = actx;
	while(step != 0){
		if( Math.abs(width-endx) < Math.abs(2*step) ){
			step = Math.round(0.4*step);
		}
		endx += step; 
		stepsx.push(step);
	}
	endx = stepsx.length;
	acty = temp[1];
	stepsy = new Array();
	stepsy.push(0);
	step = (height-acty)/count;
	endy = acty;
	while(step != 0){
		if( Math.abs(height-endy) < Math.abs(2*step) ){
			step = Math.round(0.4*step);
		}
		endy += step;
		stepsy.push(step);
	}
	endy = stepsy.length;
	if(endx > endy){
		for(var i = 0; i < endx - endy; i++)stepsy.push(0);
	} else {
		for(var i = 0; i < endy - endx; i++)stepsx.push(0);
		endx = endy;
	}
	step = 0;
	intresizing = setInterval( function(){
		actx+=stepsx[step];
		acty+=stepsy[step];
		divtoresize.style.visibility = 'hidden';		divtoresize.style.width = actx + "px";
		divtoresize.style.height = acty + "px";
		if(withmargin == 1){
			divtoresize.style.marginLeft = actx*-0.5 + "px";
			divtoresize.style.marginTop = acty*-0.5 + "px";
		}
		divtoresize.style.visibility = 'visible';		step++;	
		if(step == endx) {
			divtoresize.style.width = width + "px";
			divtoresize.style.height = height + "px";
			if(withmargin == 1){
				divtoresize.style.marginLeft = width*-0.5 + "px";
				divtoresize.style.marginTop = height*-0.5 + "px";
			}
			if(height == 0 || width == 0)divtoresize.style.display = "none";
			clearInterval(intresizing);
			if(withcontinue == 1)continue_();
		}		
	}, 25);
}


// continue function with params: type(1 = top fixed, 2 = left fixed), left, top, id, withcontinue
function move(params) {
	var intmoving;
	var actx;
	var acty;
	var endx;
	var endy;
	var step;
	var stepsx;
	var stepsy;
	var count;
	var leftie;
	var topie;
	var divtomove;
	var withcontinue = parseInt(params[4]);
	
	if(withcontinue == 1)active = 1;
	
	divtomove = $(params[3]);
	divtomove.style.display = "block";
	
	if(getStyle(divtomove, "position") == "static"){
		divtomove.style.position = "absolute";
	}
	
	leftie = parseInt(params[1]);
	topie = parseInt(params[2]);
	switch(parseInt(params[0])){
		case 1:
			divtomove.style.top = topie+"px";
		break;
		case 2:
			divtomove.style.left = leftie+"px";
		break;
	}
	var temp = findPos(divtomove);
	count = 10;
	actx = temp[0];
	stepsx = new Array();
	stepsx.push(0);
	step = (leftie-actx)/count;
	endx = actx;
	while(step != 0){
		if( Math.abs(leftie-endx) < Math.abs(2*step) ){
			step = Math.round(0.4*step);
		}
		endx += step; 
		stepsx.push(step);
	}
	endx = stepsx.length;
	acty = temp[1];
	stepsy = new Array();
	stepsy.push(0);
	step = (topie-acty)/count;
	endy = acty;
	while(step != 0){
		if( Math.abs(topie-endy) < Math.abs(2*step) ){
			step = Math.round(0.4*step);
		}
		endy += step;
		stepsy.push(step);
	}
	endy = stepsy.length;
	if(endx > endy){
		for(var i = 0; i < endx - endy; i++)stepsy.push(0);
	} else {
		for(var i = 0; i < endy - endx; i++)stepsx.push(0);
		endx = endy;
	}
	step = 0;
	intmoving = setInterval( function(){
		divtomove.style.visibility = 'hidden';		actx+=stepsx[step];
		acty+=stepsy[step];
		divtomove.style.left = actx + "px";
		divtomove.style.top = acty + "px";
		divtomove.style.visibility = 'visible';		step++;	
		if(step == endx) {
			divtomove.style.left = leftie + "px";
			divtomove.style.top = topie + "px";
			clearInterval(intmoving);
			if(withcontinue == 1)continue_();
		}
	}, 25);
}

//------------------------------------ Ajax functions -------------------------------------


function getHTTP(adr, result, params) {
	var xmlhttp;
	try { 
		xmlhttp = new XMLHttpRequest();
	} catch (e) { 
		try { 
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) { 
			try	{ 
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			} catch (e) { alert("WARNING: Your browser doesn't support AJAX. Please get Firefox at http://www.mozilla-europe.org/");	}
		}
	}
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4){
			if(xmlhttp.status == 200){
				if(result)result(xmlhttp.responseText, params);
			} else {
				errormsg(adr);
			}
		}
	}
	var temp = adr.indexOf("?");
	var sendstring = "";
	if(temp != -1){
		sendstring = adr.substr(temp+1);
		adr = adr.substr(0, temp);
	}
	xmlhttp.open("POST", adr, true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
	xmlhttp.send(sendstring);
}

//---------------------------------- Init functions -------------------------------------------


function initialize(){
	body = document.body;
	var node;	
	if(!$("blende")){
		var fade = 0.8;
				var node = document.createElement("div");
		node.style.width = "100%";
		node.style.height = "100%";
		node.style.background = "#000000";
		node.style.opacity = fade;
				node.id = "blende";
		node.style.position = "fixed";
		node.style.left = "0px";
		node.style.top = "0px";
		node.style.display = "none";
		node.style.zIndex = 100;
		body.appendChild(node);
	}
	
	custominit();
}

/*
move: type(1 = top fixed, 2 = left fixed), left, top, id, withcontinue
resize: type(1 = fixed height, 2 = fixed width), width, height, id, withmargin, withcontinue
fade: id, alpha, withcontinue
scrollbars: show, withcontinue
shadebody: show, withcontinue

continuewith = new GOON( new Array(functions), new Array(new Array(params)) );
function(new Array(params));
*/

//----------------------------------------- Webpage custom functions ---------------------------------------------------------------

function custominit() {
	var node;
	if(!$("incbox")){
		node = document.createElement("div");
		node.id = "incbox";
		node.style.position = "fixed";
		node.style.left = "100px";
		node.style.top = "100px";
		node.style.color = "#000000";
		node.style.padding = "4px";
		node.style.border = "2px solid #bbbbbb";
		node.style.zIndex = 1000;
		node.style.background = "#ffffff";
		node.style.height = "auto";
		node.style.width = "400px";
		node.style.display = "none";
		node.style.overflow = "hidden";
		body.appendChild(node);
	}
	
	document.addEventListener('keypress', taste, false);

	$('searchinput').addEventListener('keypress', searchkey, false);
	$("searchinput").focus();
}

function errormsg(page) {
	alert("Die Seite '" + page + "' wurde nicht gefunden.");
}

//----------------------------------------------------------------------------------- END Framework functions --------------------------------------------------------------------------------------------------------


var player = null;
var cat = "root";
var disablecontrollistener = 0;
var nextitem = 0;

function playerReady(obj) {
	player = $("playerid");
	player.addControllerListener("ITEM", "nextvideo");
	player.addControllerListener("PLAYLIST", "playlistload");
	getshows("root");
}

//----------------------------------------------- Playlist Funktionen --------------------------------------

function getshows(category) {
	getHTTP("shows.php?category="+category, gotshows, category);
}

function gotshows(responseText, category){
	$("showsdiv").innerHTML = responseText;
	$("showsdiv").scrollTop = 0;
	var i = 1;
	var curpos = 0;
	var funcArray = new Array();
	var paramArray = new Array();
	while($("show"+i)){
		funcArray.push(move);
		paramArray.push(new Array(2, 0, curpos, "show"+i, 1));
		i++;
		curpos+=104;
	}
	paramArray.push(new Array(null));
	continuewith = new GOON( funcArray, paramArray );
	continue_();
	// -------------- All cases of autostart --------------------------
	if(category == "root") {
		if($("show1")) {
			var playlist = "playlist.php?rootrandom=" + $("rootrandom").innerHTML;
			player.sendEvent("LOAD", playlist);
		}
	}
	if(category == "aktsendung") {
		if($("show1")) {
			var playlist = "playlist.php?category="+category;
			player.sendEvent("LOAD", playlist);
		}
	}
	if(category == "intview") {
		if($("show1")) {
			var playlist = "playlist.php?category=intview";
			player.sendEvent("LOAD", playlist);
		}
	}
	// ------------------- End -------------------------------------------
}

function nextvideo(obj) {
	setTimeout( function(){
		if(disablecontrollistener == 1)return;
		var file = player.getPlaylist()[obj.index].title;
		fade(new Array("vidtext", 0, 0));
		setTimeout( function() {
			$("vidtextmouse").style.width = "562px";
			$("vidtextmouse").style.height = "68px";
			$("vidtext").style.overflow = "visible";
			$("vidtext").style.width = "760px";
			$("vidtext").style.height = "auto";
			$("vidtext").style.display = "block";
			getHTTP("vidtext.php?file="+file, gotvidtext, null);
		}, 900);
	}, 100);
}

function playlistload(obj){
	if(disablecontrollistener == 1)return;
	player.sendEvent("ITEM", nextitem);
}


function gotvidtext(responseText, noinsert) {
	if(!noinsert)$("vidtext").innerHTML = responseText;
	for (i = 0; i < document.images.length; i++) {
    	if(document.images[i].complete != true){
			setTimeout( function(){
				gotvidtext(null, 1);
			}, 300);
			return;
		}
	}
	var sizes = getDim($("vidtext"));
	if(sizes[1] > 66) {
		$("vidtextmouse").onclick = function() {
			$("maximize").onclick = function() {
				$("maximize").src = "maximize.png";
				$("vidtextmouse").style.display = "block";
				resize(new Array(0, 560, 66, "vidtext", 0, 0));
			}
			$("maximize").src = "minimize.png";
			$("vidtextmouse").style.display = "none";
			resize(new Array(0, 760, sizes[1], "vidtext", 0, 0));
		};
		$("vidtextmouse").style.display = "block";
		$("maximize").style.display = "block";
	} else {
		$("vidtextmouse").onclick = null;
		$("vidtextmouse").style.display = "none";
		$("maximize").style.display = "none";
	}
	$("vidtext").style.overflow = "hidden";
	$("vidtext").style.width = "560px";
	$("vidtext").style.height = "66px";
	fade(new Array("vidtext", 100, 0));
}

function playtrack(track, category) {
	nextitem = track;
	var playlist = "playlist.php?category="+category;
	setTimeout( function() {
		if(category != cat && category != "without"){
			cat = category;
			player.sendEvent("LOAD", playlist);
		} else {
			player.sendEvent("ITEM", track);			
		}
	}, 1000);
}

function playaftersearchtrack(track, category) {
	closeinc();
	category += "&autostart=no";
	setTimeout( function() {
		getshows(category);
		playtrack(track, category);
	}, 1000);
}

//-------------------------------------------- End Playlist Funktionen --------------------------------------

// page = pagename ( ohne.php ), postvars = Array("name1=value1", "name2=value") 
function inc(page, postvars) {

	page += ".php";
	if(postvars){
		page += "?";
		for(i = 0; i < postvars.length; i++) {
			var temp = postvars[i].indexOf("=");
			var varname = postvars[i].substr(0, temp);
			var varvalue = encodeURIComponent(postvars[i].substr(temp+1));
			page += varname + "=" + varvalue + "&";
		}
	}

	getHTTP(page, gotinc, null );
}

function gotinc(responseText){

	if(player.getConfig().state == "PLAYING")player.sendEvent("PLAY", false);
	$("playerid").style.visibility = "hidden";

	$("incbox").style.visibility = "hidden";
	$("incbox").style.display = "block";
	$("incbox").style.height = "auto";
	$("incbox").innerHTML = responseText;
	var sizes = getDim($("incbox"));
	
	setTimeout( function(){
		$("incbox").style.width = "0px";
		$("incbox").style.visibility = "visible";
		continuewith = new GOON( new Array(resize), new Array(new Array(1, 400, sizes[1], "incbox", 0, 1)) );
		shadebody(new Array(1, 1));
	}, 10);
	setTimeout( function(){
		if($("searchinput2")){
			$('searchinput2').addEventListener('keypress', searchkey2, false);
			$("searchinput2").focus();
		}
	}, 1000);
}

function closeinc() {
	continuewith = new GOON( new Array(shadebody, function(){$("playerid").style.visibility = "visible";player.sendEvent("PLAY", true);} ), new Array(new Array(0, 1), null) );
	resize(new Array(2, 400, 0, "incbox", 0, 1));
}




function preventDefaultAction(aEvent) {	
		aEvent.preventDefault();
		}

function taste(aEvent) {
	aEvent = aEvent ? aEvent : window.event;
	
	if( aEvent.keyCode == 27 ) {
		if( $("incbox").style.display == "block" ) {
			closeinc();
		} else {
			player.sendEvent("PLAY", false);
		}
				preventDefaultAction(aEvent);
				return false;
	}
	if( aEvent.keyCode == 19 ) {
		if( $("incbox").style.display != "block" ) {
			player.sendEvent("PLAY", false);
		}
				preventDefaultAction(aEvent);
				return false;
	}
}

function searchkey(aEvent) {
	aEvent = aEvent ? aEvent : window.event;
	
	if( aEvent.keyCode == 13 ) {
		inc('search', Array('query='+$('searchinput').value));
		
				preventDefaultAction(aEvent);
				return false;
	}
}

function searchkey2(aEvent) {
aEvent = aEvent ? aEvent : window.event;
	
	if( aEvent.keyCode == 13 ) {
		inc('search', Array('query='+$('searchinput2').value));
		
				preventDefaultAction(aEvent);
				return false;
	}
}
