/*
das bern-ost objekt
*/
var bernost = new _bernost();

function _bernost()
{
// methoden
	this.bestaetige = bestaetige;
	this.check_xml = check_xml;
	this.drag_an = drag_an;
	this.drag_bg_fade_in = drag_bg_fade_in;
	this.drag_hide = drag_hide;
	this.drag_show = drag_show;
	this.find_position = find_position;
	this.go = go;
	this.grow = grow;
	this.hide_form = hide_form;
	this.id = id;
	this.pageScrollOffset = pageScrollOffset;
	this.request_pw = request_pw;
	this.show_form = show_form;
	this.shrink = shrink;
	this.style = style;
	this.toggle_menu = toggle_menu;
	this.ajax_update_main = ajax_update_main;
	this.radio_play = radio_play;

// drag&drop handler
	// thanks to webtoolkit.info
	this.drag_handler = {
		_oElem : null,
		attach : function(oElem) {
			oElem.onmousedown = bernost.drag_handler._dragBegin;
			oElem.dragBegin = new Function();
			oElem.drag = new Function();
			oElem.dragEnd = new Function();
			return oElem;
		},
		_dragBegin : function(e) {
			var oElem = bernost.drag_handler._oElem = this;
			if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '270px'; }
			if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '120px'; }
			var x = parseInt(oElem.style.left);
			var y = parseInt(oElem.style.top);
			e = e ? e : window.event;
			oElem.mouseX = e.clientX;
			oElem.mouseY = e.clientY;
			oElem.dragBegin(oElem, x, y);
			document.onmousemove = bernost.drag_handler._drag;
			document.onmouseup = bernost.drag_handler._dragEnd;
			return false;
		},
		_drag : function(e) {
			var oElem = bernost.drag_handler._oElem;
			var x = parseInt(oElem.style.left);
			var y = parseInt(oElem.style.top);
			e = e ? e : window.event;
			oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
			oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';
			oElem.mouseX = e.clientX;
			oElem.mouseY = e.clientY;
			oElem.drag(oElem, x, y);
			return false;
		},
		_dragEnd : function() {
			var oElem = bernost.drag_handler._oElem;
			var x = parseInt(oElem.style.left);
			var y = parseInt(oElem.style.top);
			oElem.dragEnd(oElem, x, y);
			document.onmousemove = null;
			document.onmouseup = null;
			bernost.drag_handler._oElem = null;
		}
	}

// funkctionen
	function radio_play(n)
	{
		switch(n)
		{
			case 0:
				url='http://bern-ost.s2.onlinestream.de/listen1.m3u';
			break;
			case 1:
				url='http://bern-ost.s2.onlinestream.de/listen1.ram';
			break;
			case 2:
				url='http://bern-ost.s2.onlinestream.de/listen1.m3u';
			break;
		}
		window.open(url);
	}

	function drag_an()
	{
		var dragable1 = bernost.drag_handler.attach(bernost.id('drag_elem'));
	}
	function drag_show(url)
	{
		if (arguments.length>2)
		{ // breite und höhe des fensters
			bernost.id('drag_frame').style.width = arguments[1]+'px';
			bernost.id('drag_elem').style.width = arguments[1]+'px';
			bernost.id('drag_frame').style.height = arguments[2]+'px';
			bernost.id('drag_elem').style.height = arguments[2]+30+'px';
		}
		bernost.id('drag_frame').src = url;
		bernost.id('drag_elem').style.display = 'block';
		if ((arguments.length>3)&&(arguments[3]=='fade'))
		{
			document.getElementById('drag_bg').style.opacity = 0;
			document.getElementById('drag_bg').style.filter = 'alpha(opacity=0)';
			bernost.id('drag_bg').style.display = 'block';
			bernost.drag_bg_fade_in(0);
		}
	}
	function drag_hide()
	{
		bernost.id('drag_elem').style.display = 'none';
		bernost.id('drag_bg').style.display = 'none';
		bernost.id('drag_frame').href = 'about:blank;';
	}
	function drag_bg_fade_in(count)
	{
		bernost.id('drag_bg').style.opacity = 0.75;
		bernost.id('drag_bg').style.filter = 'alpha(opacity=75)';
/*
		fade_level = count;
		if (fade_level<7.5)
		{
			bernost.id('drag_bg').style.opacity = fade_level/10;
			bernost.id('drag_bg').style.filter = 'alpha(opacity='+(fade_level*10)+')';
			window.setTimeout('bernost.drag_bg_fade_in('+(fade_level+2.5)+')',75);
		}
*/
	}
	function check_xml(i)
	{
		try { var req = new XMLHttpRequest(); } 
		catch (e) {
			try { var req = new ActiveXObject('Msxml2.XMLHTTP'); } 
			catch (e) {
				try { var req = new ActiveXObject('Microsoft.XMLHTTP'); }
				catch (e) { return false; }
			}
		}
		req.onreadystatechange = function(){
			if(req.readyState!=4) return;
			if(req.status!=200) return;
		}
		req.open('GET','_ajax/enable_ajax.php?q='+i,'');
		req.send(null); // wenn's geht... dann geht's!
	}
	function request_pw()
	{
		// das inputfeld "email" wird überprüft
		// und ggf. ein "passwort vergessen" request ausgelöst
		var email=this.id('req_email').value;
		if (email!='')
		{
			document.pwform.action='_ajax/send_password.php';
			document.pwform.submit();
		}
	}
	function show_form(formid,linkid)
	{
		style(linkid,"display='none'");
		style(formid,"display='block'");
	}
	function hide_form(formid,linkid)
	{
		style(formid,"display='none'");
		style(linkid,"display='block'");
	}
	function bestaetige(message,nexturl)
	{
		if (confirm(message))
			this.go(nexturl);
	}
	function pageScrollOffset()
	{
		if (window.pageYOffset)
			// firefox usw.
			return window.pageYOffset;
		else
			// ie 4+
			return document.documentElement.scrollTop;
	}
	function find_position(obj_id)
	{
		// gibt die koordinaten einer beliebigen id zurück
		var curleft = curtop = 0;
		obj = this.id(obj_id);
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		return [curleft,curtop];
	}
	function toggle_menu(id,maximum)
	{
		// grow / shrink nicht verwenden
		if (this.id(id).style.height=='0px')
		{
			this.grow(id,0,maximum);
		}
		else
		{
			this.shrink(id,maximum,0);
		}
	}
	function grow(id,jetzt,limit)
	{
		style(id,'height="'+limit+'px"');
	}
	function shrink(id,jetzt)
	{
		style(id,'height="0px"');
	}
	function ajax_update_main(url)
	{
		// benötigt prototype.js
		new Ajax.Updater('bomcon', url, {
		  method: 'get'
		});
		bernost.drag_hide();
		return false;
	}


	// shortcuts
	function style(id,s) 
	{ return eval('bernost.id("'+id+'").style.'+s); }
	function id(o)
	{ return document.getElementById(o); }
	function go(url)
	{ top.location.href = url; }

}

