function page(id, path, name)
{
	this.get = page_get;
	this.action = page_action;
	this.back = page_back;
	this.effect = page_effect;
	this.refresh = page_refresh;
	this.show = page_show;
	this.resize = page_resize;
	this.init = page_init;
	
	// Variables
	this.width = 0;
	this.pages = new Array();
	this.refreshes = 0;
	
	this.id = id;
	this.path = path;
	this.name = name;
	
	window.onresize = this.resize;
	
	// Init this script:
	this.init();
	
	function page_init() {
		this.resize();
	}
	
	function page_get(file, overwrite_id)
	{
		var ajax = new XMLHttpRequest();
		ajax.open("POST", this.path + "get.php?__id=" + this.id + "&__file=" + file, false);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		ajax.send("");
		
		if(ajax.responseText != "404")
		{
			var id = overwrite_id ? this.pages.length - 1 : this.pages.length;
			
			// Create an array if it is a new page
			this.pages[id] = this.pages[id] ? this.pages[id] : new Array();
			this.pages[id]['file'] = file;
			
			// Check if we need to refresh the page
			if(!overwrite_id)
			{
				this.pages[id]['page'] = document.createElement("DIV");
				this.pages[id]['page'].className = "pageDiv";
				document.getElementById('container').appendChild(this.pages[id]['page']);
			}
			
			// Get the object:
			var get = "";
			eval(ajax.responseText);
			
			// Print and unset the toString method
			this.pages[id]['page'].innerHTML = get;
			get.toString = "";
			
			// Call init
			if(get.script_init)
			{
				eval(get.script_init);
			}
			
			// Keep the object
			this.pages[id]['object'] = get;
			
			if(!overwrite_id)
				this.effect(1);
				
			// Set the timer to refresh page:
			if(get.refresh_every && !overwrite_id)
				page.refresh(get.refresh_every, true);
		}
	}
	function page_action(file, do_return)
	{
		var ajax = new XMLHttpRequest();
		ajax.open("POST", this.path + "action.php?__id="+this.id+"&__file=" + file, false);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		ajax.send("");
		
		if(do_return)
			return ajax.responseText;
	}
	function page_show(linkId, infoId)
	{
		document.getElementById(linkId).style.display = "none";
		document.getElementById(infoId).style.display = "block";
	}
	
	function page_resize()
	{
		var width = document.documentElement.clientWidth;
		var height = document.documentElement.clientHeight;
		
		var controlHeight = document.getElementById("control").offsetHeight;
		
		//if(this.width != width && this.width)
			// Maybe refresh page?
			
		document.getElementById("container").style.height = (height - controlHeight) + "px";
	}
	
	function page_refresh(seconds, only_start)
	{
		// Only start timer? or do something
		if(!only_start)
		{
			var id = this.pages.length - 1;
			this.get(this.pages[id]['file'], true);
		}
		
		// Set the timer (again):
		if(seconds)
			this.refreshes = window.setTimeout("page.refresh("+seconds+")", seconds * 1000);
	}
	function page_back()
	{
		// save form
		
		// Send exit code
		var id = this.pages.length - 1;
		
		if(this.pages[id]['object'].script_exit)
			eval(this.pages[id]['object'].script_exit);
		
		// do effect
		this.effect(0);
	}
	
	
	function page_effect(newPage)
	{
		if(this.refreshes)
		{
			window.clearTimeout(this.refreshes);
			this.refreshes = "";
		}
		var id = this.pages.length - 1;
		if(newPage == 1)
		{
			if(id != 0)
				//this.pages[id]['page'].style.width = this.width + "px";
			//else
				pageNew();
		}
		else
		{
			pageBack();
		}
	}
}

var control = new control;

function control()
{
	this.showing = 0;
	this.temp = new Array;
	
	this.show = control_show;
	this.play = control_play;
	this.prev = control_prev;
	this.next = control_next;
	this.forward = control_forward;
	this.rewind = control_rewind;
	
	this.up = control_up;
	this.down = control_down;
	this.left = control_left;
	this.right = control_right;
	this.enter = control_enter;
	
	this.teletext = control_teletext;
	this.channel = control_channel;
	this.commit_channel = control_commit_channel;
	this.record = control_record;
	this.tvguide = control_tvguide;
	this.showtv = control_showtv;
	
	this.back = control_back;
	this.zoom = control_zoom;
	this.mute = control_mute;
	
	this.menu = control_menu;
	this.info = control_info;
	
	this.volume = control_volume;
	this.percentage = control_percentage;
	this.refresh = control_refresh;
	
	function control_show()
	{
		if(!this.showing)
		{
			this.showing = 1;
			document.getElementById("control").style.display = "block";
		}
		else
		{
			this.showing = 0;
			document.getElementById("control").style.display = "none";
		}
	}
	
	function control_showtv()
	{
		page.action("control&action=tvguide");
	}
	function control_tvguide()
	{
		page.action("control&action=tvguide");
	}
	function control_record()
	{
		page.action("control&action=record");
	}
	function control_teletext()
	{
		page.action("control&action=teletext");
	}
	function control_channel(number)
	{
		if(this.temp["channel_timer"])
			window.clearTimeout(this.temp["channel_timer"]);
		
		this.temp["channel_timer"] = window.setTimeout("control.commit_channel()", 1500);
		
		this.temp["channel"] = this.temp["channel"] ? this.temp["channel"]+""+number : ""+number;
		
		document.getElementById("channel").innerHTML = this.temp["channel"];
	}
	function control_commit_channel()
	{
		page.action("control&action=channel&channel="+this.temp["channel"]);
		document.getElementById("channel").innerHTML = "";
		delete this.temp["channel"];
	}
	
	function control_play()
	{
		page.action("control&action=play");
	}
	function control_prev()
	{
		page.action("control&action=prev");
	}
	function control_next()
	{
		page.action("control&action=next");
	}
	function control_forward()
	{
		page.action("control&action=forward");
	}
	function control_rewind()
	{
		page.action("control&action=rewind");
	}
	
	function control_up()
	{
		page.action("control&action=up");
	}
	function control_down()
	{
		page.action("control&action=down");
	}
	function control_left()
	{
		page.action("control&action=left");
	}
	function control_right()
	{
		page.action("control&action=right");
	}
	function control_enter()
	{
		page.action("control&action=enter");
	}
	
	function control_mute()
	{
		page.action("control&action=mute");
	}
	function control_back()
	{
		page.action("control&action=go_back");
	}
	function control_zoom()
	{
		page.action("control&action=zoom");
	}
	
	function control_info()
	{
		page.action("control&action=show_info");
	}
	function control_menu()
	{
		page.action("control&action=show_menu");
	}
	
	
	function control_volume(evt)
	{
		// create cross-browser event detector:
		var node = (evt.target) ? evt.target : ((evt.srcElement)?evt.srcElement : null );
		evt = (evt) ? evt : ((event) ? event : null);

		// create mouse coordinates objects:
		xpo = evt.clientX;
		
		var volume = ((xpo - 20) / 2);
		volume = volume < 0 ? 0 : volume;
		volume = volume > 100 ? 100 : volume;
		
		
		page.action("control&action=set_volume&volume=" + volume);
		control.percentage(volume);
	}
	function control_percentage(percentage)
	{
		document.getElementById("progression").style.width = percentage + "%";
	}
	function control_refresh()
	{
		//this.percentage(page.action("control&action=get_volume", 1));
		//window.setTimeout("control.refresh()", 5000);
	}
}

window.onload = function()
{
	control.refresh();
	if(document.getElementById("progress"))
		document.getElementById("progress").onclick = control.volume;
}

function pageNew()
{
	var id = page.pages.length - 1;
	page.pages[id-1]['page'].style.display = "none";
	//page.pages[id]['page'].style.width = page.width + "px";
}
function pageBack()
{
	var id = page.pages.length - 1;
	temp_page = page.pages.pop();
	document.getElementById('container').removeChild(temp_page['page']);
	page.pages[id - 1]['page'].style.display = "block";
	
	if(page.pages[id - 1]['object'].refresh)
		page.refresh();
		
	if(page.pages[id - 1]['object'].refresh_every)
		page.refresh(page.pages[id - 1]['object'].refresh_every, true);
	return false;
}