if(typeof Prototype=='object') {
	var MB = Class.create({
		initialize: function(div) {
			this.Stations=new Array();
			this.SearchString='';
			this.MaxID=0;
			this.Date=null;
			
			this.ParentDiv=$(div);
			
			this.initDiv();
			this.getStations();
		},
		
		initDiv: function() {
			var me=this;
			
			this.MainDiv=$(document.createElement('div'));
			this.MainDiv.className='MBMain';
			
			while(this.ParentDiv.hasChildNodes()) {
				this.MainDiv.appendChild(this.ParentDiv.removeChild(this.ParentDiv.firstChild));
			}
			
			this.StatusDiv=$(document.createElement('div'));
			this.StatusDiv.className='MBStatus';
			this.StatusDiv.innerHTML='Initialisiere...';
			this.MainDiv.appendChild(this.StatusDiv);
			
			this.ToolTipDiv=$(document.createElement('div'));
			this.ToolTipDiv.className='MBToolTip';
			this.ToolTipDiv.style.visibility='hidden';
			this.MainDiv.appendChild(this.ToolTipDiv);
			
			this.StationDiv=$(document.createElement('div'));
			this.StationDiv.className='MBStations';
			this.MainDiv.appendChild(this.StationDiv);
			
			this.ParentDiv.appendChild(this.MainDiv);
		},
		
		status: function(text) {
			this.StatusDiv.innerHTML=text;
		},
		
		showToolTip: function(station) {
			this.ToolTipDiv.innerHTML=station.getToolTipText();
			var h = this.ToolTipDiv.clientHeight;
			var w = this.ToolTipDiv.clientWidth;
			var dx = 15;
			var dy = 15;
			var px = station.X+dx;
			var py = station.Y+dy;
			if(px+w>=this.MainDiv.clientWidth) px=station.X-dx-w;
			if(py+h>=this.MainDiv.clientHeight) py=station.Y-dy-h;
			this.ToolTipDiv.style.left=px+"px";
			this.ToolTipDiv.style.top=py+"px";
			this.ToolTipDiv.style.visibility='visible';
		},
		
		hideToolTip: function() {
			this.ToolTipDiv.innerHTML='';
			this.ToolTipDiv.style.left="0px";
			this.ToolTipDiv.style.top="0px";
			this.ToolTipDiv.style.visibility='hidden';
		},
		
		showStationViewer: function(station) {
			window.open('stationviewer.php?sc='+station.Code,station.Code,'width=420,height=280');
		},
		
		getStations: function() {
			this.status('Lade Stationen...');
			
			var me=this;
			var myAjax = new Ajax.Request('json.php', {
				method: 'post',
				parameters: {command: 'getstations'},
				onSuccess: function(data) { me.getStationsSuccess(data); },
				onFailure: function() { me.getStationsFailure(); }
			});
		},
		
		getStationsSuccess: function(response) {
			this.status('Verarbeite Stationen...');
			
			var data=response.responseText.evalJSON();
			
			this.MainDiv.removeChild(this.StationDiv);
			
			this.Stations=new Array();
			for(var tmpStationcode in data) {
				var tmpStation = data[tmpStationcode];
				this.Stations[tmpStationcode] = new MBStation(this, tmpStationcode, tmpStation.n, tmpStation.x, tmpStation.y);
			}
			
			this.MainDiv.appendChild(this.StationDiv);
			
			this.status('Stationen geladen...');
			
			this.getData();
		},
		
		getStationsFailure: function() {
			this.status('Fehler beim Holen der Stationen!');
		},
		
		getData: function() {
			this.status('Lade Messwerte...');
			
			var me=this;
			var myAjax = new Ajax.Request('json.php', {
				method: 'post',
				parameters: {command: 'getdata', maxid: this.MaxID},
				onSuccess: function(data) { me.getDataSuccess(data); },
				onFailure: function() { me.getDataFailure(); }
			});
		},
		
		getDataSuccess: function(response) {
			this.status('Verarbeite Messwerte...');
			
			var data=response.responseText.evalJSON();
			
			this.Date=new Date(data.date*1000);
			this.MaxID=data.maxid;
			for(var tmpStationcode in data.values) {
				var tmpStation = this.Stations[tmpStationcode];
				if(tmpStation!=null) tmpStation.setValue(new Date(data.values[tmpStationcode].d*1000), data.values[tmpStationcode].v, data.values[tmpStationcode].c);
			}
			
			this.status('Stand ' + FormatDate(this.Date,'d.m.Y H:i'));
		},
		
		getDataFailure: function() {
			this.status('Fehler beim Holen der Stationen!');
		}
	});
	
	var MBStation = Class.create({
		initialize: function(mb, code, name, x, y) {
			var me=this;
			this.Parent=mb;
			this.Code=code;
			this.Name=name;
			this.X=parseInt(x);
			this.Y=parseInt(y);
			
			this.Timeout=90;
			
			this.LastDate=null;
			this.LastValue=null;
			this.LastColor=null;
			
			this.Div=$(document.createElement('div'));
			this.Div.className='MBStation';
			this.Div.setStyle('position: absolute; width: 11px; height: 11px; left: ' + (this.X-5) + 'px; top: ' + (this.Y-5) + 'px');
			
			this.Div.onmouseover=function() { mb.showToolTip(me); };
			this.Div.onmouseout=function() { mb.hideToolTip(); };
			this.Div.onclick=function() { mb.showStationViewer(me); };
			
			this.refresh();
			this.Parent.StationDiv.appendChild(this.Div);
		},
		
		isOnline : function() {
			if(this.LastDate==null) return false;
			if(this.Parent.Date==null) return true;
			return ((this.Parent.Date-this.LastDate)/60000<this.Timeout);
		},
		
		refresh : function() {
			if(this.isOnline()) {
				var tmpColor="000000"+parseInt(this.LastColor).toString(16);
				this.Div.style.backgroundColor="#"+(tmpColor.substr(tmpColor.length-6));
			} else {
				this.Div.style.backgroundColor="#404040"
			}
		},
		
		setValue : function(d, v, c) {
			this.LastDate=d;
			this.LastValue=v;
			this.LastColor=c;
			
			this.refresh();
		},
		
		getToolTipText : function() {
			var ret='<b>'+this.Name+'</b><br>';
			if(this.isOnline()) {
				ret=ret+Math.round(this.LastValue)+' nSv/h um '+FormatDate(this.LastDate,'H:i');
			} else {
				ret=ret+"Offline";
			}
			return ret;
		},
		
		getStationViewerText : function() {
			return ret='<b>'+this.Name+'</b> ('+this.Code+')<br>';
		}
	});
	
	var FormatDate = function(d, f) {
		var tmp=f;
		var PadLeft=function(v,l) {
			var ret=('0000000000'+v);
			return ret.substr(ret.length-l);
		}
		
		tmp=tmp.replace(/d/g,PadLeft(d.getDate(),2));
		tmp=tmp.replace(/m/g,PadLeft(d.getMonth()+1,2));
		tmp=tmp.replace(/Y/g,PadLeft(d.getFullYear(),4));
		tmp=tmp.replace(/H/g,PadLeft(d.getHours(),2));
		tmp=tmp.replace(/i/g,PadLeft(d.getMinutes(),2));
		tmp=tmp.replace(/s/g,PadLeft(d.getSeconds(),2));
		
		return tmp;
	}
} else {
	alert('Prototype nicht geladen');
}

