//-------------------------------------------------------------------------------------------------
//- Top-Line-Stuff --------------------------------------------------------------------------------
function _zero(v) { return v <= 9 ? '0' + v : '' + v; }
function ShowTime()
{
	var loc = new Date();
	var locS = 'Loc: ' + _zero(loc.getHours()) + ':' + _zero(loc.getMinutes()) + ':' + _zero(loc.getSeconds());
	var utc = new Date();
	var utcS = 'UTC: ' + _zero(utc.getUTCHours()) + ':' + _zero(utc.getUTCMinutes()) + ':' + _zero(utc.getUTCSeconds());
	SetInnerHTML(utcS, 'utc');
	SetInnerHTML(locS, 'loc');
	setTimeout("ShowTime()", 1000);
}


//-------------------------------------------------------------------------------------------------
//- Common stuff ----------------------------------------------------------------------------------
function GetMonthName(dat)
{
	var month = new Array('Januar','Februar','M&auml;rz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember');
	return month[dat.getMonth()];
}

function GetYear(dat)
{
	return dat.getFullYear();
}

function SetInnerHTML(txt, id)
{
	document.getElementById(id).innerHTML = txt;
	return true;
}


//-------------------------------------------------------------------------------------------------
//- DSP Infos related stuff -----------------------------------------------------------------------
var dspID = 2;
var dspLastURL = '';

function GetDspLastURL()
{
	if (parent.frames["mnu"]) return parent.frames["mnu"].dspLastURL;
	if (parent.parent.frames["mnu"]) return parent.parent.frames["mnu"].dspLastURL;
	return dspLastURL;
}

function SetDspLastURL(url)
{
	if (parent.frames["mnu"]) parent.frames["mnu"].dspLastURL = url;
	if (parent.parent.frames["mnu"]) parent.parent.frames["mnu"].dspLastURL = url;
	dspLastURL = url;
}

function ShowDsp(dspURL)
{
	if ( parent.parent.document.getElementById('dsp') )
	{
		parent.parent.document.getElementById('dsp').src = '';
		parent.parent.document.getElementById('dsp').src = dspURL;
	}
	if ( parent.document.getElementById('dsp') )
	{
		parent.document.getElementById('dsp').src = '';
		parent.document.getElementById('dsp').src = dspURL;
	}
	SetDspLastURL(dspURL);
	return true;
}

function OpenDsp(dspURL)
{
	parent.document.getElementById('dsp').src = '';
	parent.document.getElementById('dsp').src = dspURL;
	SetDspLastURL(dspURL);
	return true;
}


//-------------------------------------------------------------------------------------------------
//- Row Infos related stuff -----------------------------------------------------------------------
var rowID = 3;
var rowOrgW = 150;
var rowLastURL = '';

function GetRowLastURL()
{
	if (parent.frames["mnu"]) return parent.frames["mnu"].rowLastURL;
	if (parent.parent.frames["mnu"]) return parent.parent.frames["mnu"].rowLastURL;
	return rowLastURL;
}

function SetRowLastURL(url)
{
	if (parent.frames["mnu"]) parent.frames["mnu"].rowLastURL = url;
	if (parent.parent.frames["mnu"]) parent.parent.frames["mnu"].rowLastURL = url;
	rowLastURL = url;
}

function CloseRow()
{
	var fsizes = parent.document.getElementById('botfrs').cols.split(',');
	if ( fsizes[rowID] > 0 )
	{
		rowOrgW = fsizes[rowID];
		fsizes[rowID] = 0;
		parent.document.getElementById('botfrs').cols=fsizes[0] + ',' + fsizes[1] + ',' + fsizes[2] + ',' + fsizes[3];
	}
	SetRowLastURL('');
	return true;
}

function OpenRow(rowURL)
{
	var fsizes = parent.document.getElementById('botfrs').cols.split(',');
	fsizes[rowID] = rowOrgW;
	parent.document.getElementById('botfrs').cols=fsizes[0] + ',' + fsizes[1] + ',' + fsizes[2] + ',' + fsizes[3];
	parent.document.getElementById('row').src = rowURL;
	SetRowLastURL(rowURL);
	return true;
}


//-------------------------------------------------------------------------------------------------
//- Selection related stuff -----------------------------------------------------------------------
var selID = 1;
var selOrgW = 150;
var selLastURL = '';

function GetSelLastURL()
{
	if (parent.frames["mnu"]) return parent.frames["mnu"].selLastURL;
	if (parent.parent.frames["mnu"]) return parent.parent.frames["mnu"].selLastURL;
	return selLastURL;
}

function SetSelLastURL(url)
{
	if (parent.frames["mnu"]) parent.frames["mnu"].selLastURL = url;
	if (parent.parent.frames["mnu"]) parent.parent.frames["mnu"].selLastURL = url;
	selLastURL = url;
}

function CloseSel()
{
	var fsizes = parent.document.getElementById('botfrs').cols.split(',');
	if ( fsizes[selID] > 0 )
	{
		selOrgW = fsizes[selID];
		fsizes[selID] = 0;
		parent.document.getElementById('botfrs').cols=fsizes[0] + ',' + fsizes[1] + ',' + fsizes[2] + ',' + fsizes[3];
	}
	SetSelLastURL('');
	return true;
}

function OpenSel(selURL)
{
	var fsizes = parent.document.getElementById('botfrs').cols.split(',');
	fsizes[selID] = selOrgW;
	parent.document.getElementById('botfrs').cols=fsizes[0] + ',' + fsizes[1] + ',' + fsizes[2] + ',' + fsizes[3];
	parent.document.getElementById('sel').src = selURL;
	SetSelLastURL(selURL);
	return true;
}

function ShowSel(selURL)
{
	parent.parent.document.getElementById('sel').src = selURL;
	SetSelLastURL(selURL);
	return true;
}


//-------------------------------------------------------------------------------------------------
//- Menu related stuff ----------------------------------------------------------------------------
var mnuTmr;
var mnuOldW, mnuOldH, mnuOrgW, mnuExpId;
var mnuTabHeight = 20;
var mnuNTabs = 6;

function SpareBot()
{
	if (window.innerHeight) // NS
		return 0;
	else if (document.body && document.body.clientHeight) // IE
		return 0;
	else return 0; // beide Funktionen unbekannt
}

function getInnerHeight()
{
	if (window.innerHeight) // NS
		return window.innerHeight;
	else if (document.body && document.body.clientHeight) // IE
		return document.body.clientHeight;
	else return 0; // beide Funktionen unbekannt
}

function getInnerWidth()
{
	if (window.innerWidth) // NS
		return window.innerWidth;
	else if (document.body && document.body.clientWidth) // IE
		return document.body.clientWidth;
	else return 0; // beide Funktionen unbekannt
}

function ExpandMe(myid, selURL, dspURL, rowURL)
{
	for (i=1;i<mnuNTabs;i++) document.getElementById('ifr'+i).style.height = '0';
	document.getElementById(myid).style.height  = getInnerHeight() - (mnuNTabs * mnuTabHeight) - SpareBot();
	mnuExpId = myid;

	if (selURL.length) 
		OpenSel(selURL);
	else
		CloseSel();

	OpenDsp(dspURL);

	if (rowURL.length) 
		OpenRow(rowURL);
	else
		CloseRow();

	return true;
}

function ReSizeMe()
{
	var i;

	if ( (mnuOldW != getInnerWidth()) || (mnuOldH != getInnerHeight()))
	{
		mnuOldW = getInnerWidth();
		mnuOldH = getInnerHeight();
		for (i=1;i<mnuNTabs;i++) document.getElementById('ifr'+i).width = mnuOldW;
		ExpandMe(mnuExpId, GetSelLastURL(), GetDspLastURL(), GetRowLastURL());
	}
	return true;
}

function SizeMe(myid, selURL, dspURL, rowURL)
{
	mnuOldW = 0;
	mnuOldH = 0;
	ExpandMe(myid, selURL, dspURL, rowURL);
	mnuTmr = window.setInterval('ReSizeMe()',100);
	return true;
}


//-------------------------------------------------------------------------------------------------
//- Biz Bar Details -------------------------------------------------------------------------------
var bizBarText1 = '';
//var bizBarText1 = 'Merkur, Venus u. Saturn am frühen Abendhimmel';
//var bizBarText1 = 'Sternbedeckung durch den Mond';
//var bizBarText2 = 'Huygens Landung auf Titan';

function StartFed()
{
	var s = '<span onclick="parent.STATE.location.href=\'state.php?state=10500\';">' + bizBarText1 + '</span>';
//	    s = s + '&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;';
//	    s = s + '<span onclick="window.open(\'http://www.esa.int/SPECIALS/Cassini-Huygens/index.html\');">' + bizBarText2 + '</span>';
	document.getElementById('divFed').innerHTML = s;
	return true;
}
//-------------------------------------------------------------------------------------------------



    var http_request = false;

    function macheRequest(url) {

        http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // zu dieser Zeile siehe weiter unten
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
            return false;
        }
        http_request.onreadystatechange = alertInhalt;
        http_request.open('GET', url, true);
        http_request.send(null);

    }

    function alertInhalt() {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
//		var sidS = 'Loc: ' + _zero(loc.getHours()) + ':' + _zero(loc.getMinutes()) + ':' + _zero(loc.getSeconds());
//		var locS = 'Loc: ' + _zero(loc.getHours()) + ':' + _zero(loc.getMinutes()) + ':' + _zero(loc.getSeconds());
//		var utcS = 'UTC: ' + _zero(utc.getUTCHours()) + ':' + _zero(utc.getUTCMinutes()) + ':' + _zero(utc.getUTCSeconds());
//		SetInnerHTML(utcS, 'utc');
		var xmldoc = http_request.responseXML;
                var root_node = xmldoc.getElementsByTagName('loc').item(0);
		SetInnerHTML('LOC: '+root_node.firstChild.data, 'loc');
                var root_node = xmldoc.getElementsByTagName('gmt').item(0);
		SetInnerHTML('UTC: '+root_node.firstChild.data, 'utc');
                var root_node = xmldoc.getElementsByTagName('sidLoc').item(0);
		SetInnerHTML('SID: '+root_node.firstChild.data, 'sid');
            } else {
                alert('Bei dem Request ist ein Problem aufgetreten.');
            }
        }

    }

function ShowTimeAJAX()
{
	macheRequest('/common/factoryPHP/sidTime.php');
	setTimeout("ShowTimeAJAX()", 1000);
}

var data;

    function getInhalt() {
		var xmldoc = http_request.responseXML;

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
		        // display data into data container
			    data=http_request.responseText.split('\r\n');
				drawCanvas();
            }
        }
    }

    function macheRequestFile(url) {

        http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/plain');
                // zu dieser Zeile siehe weiter unten
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
            return false;
        }
        http_request.onreadystatechange = getInhalt;
        http_request.open('GET', url, true);
        http_request.send(null);

    }

var zoomPosX = 0, zoomPosXold = 24, zoomPosXoldS = 0;
var zoomPosY = 0, zoomPosYold = 100; zoomPosYoldS = 100;
var _x, _y, _x1, _y1, doDraw;

function zoomDraw(x1,y1,x2,y2) {
	var y1, y2, y3, y4, y5, xa, xStep
	var xw, yw;
	var canvas = document.getElementById('canvas1');

	zoomPosX = Math.min(x1,x2);
	zoomPosY = canvas.height-40 - Math.min(y1,y2);
	xw = Math.abs(x1-x2);
	yw = Math.abs(y1-y2);

	if(canvas.getContext){
		var context = canvas.getContext('2d');
		canvas.width=canvas.width;
		context.fillStyle = "rgb(255, 255, 255)";
		context.strokeStyle = "rgb(0, 0, 0)";
		context.fillRect(0, 0, canvas.width, canvas.height);
		context.beginPath();
	    context.strokeRect(50,20,canvas.width-100,canvas.height-40);
		context.closePath();
	    context.fill();
		context.fillStyle = "rgb(0, 0, 0)";
		context.font = "8pt Arial";
		y1 = (canvas.height-40)+20;
		y2 = (canvas.height-40)/4*3+20;
		y3 = (canvas.height-40)/4*2+20;
		y4 = (canvas.height-40)/4*1+20;
		y5 = 20;
		xa = 50;
		xStep = (canvas.width-100)/24;
//		alert (zoomPosYold + ", " + zoomPosYoldS + ":: " + zoomPosY + ", " +yw+":::"+(zoomPosYold-zoomPosYoldS));
		t1 = zoomPosYold-zoomPosYoldS + zoomPosYoldS/(canvas.height-40)  * (zoomPosY-yw);
		t5 = zoomPosYold-zoomPosYoldS + zoomPosYoldS/(canvas.height-40)  * zoomPosY;
		t2 = t1+(t5-t1)/4;
		t3 = t1+(t5-t1)/2; //zoomPosYold/(canvas.height-40)  * (zoomPosY/2-yw);
		t4 = t1+(t5-t1)/4*3; //zoomPosYold/(canvas.height-40)  * (zoomPosY/4*3-yw);
		zoomPosYold = t5;
		zoomPosYoldS = t5-t1;

		context.fillText(Math.round(t1*10)/10+"%", 20, y1+3);
		context.fillText(Math.round(t2*10)/10+"%", 20, y2+3);
		context.fillText(Math.round(t3*10)/10+"%", 20, y3+3);
		context.fillText(Math.round(t4*10)/10+"%", 20, y4+3);
		context.fillText(Math.round(t5*10)/10+"%", 20, y5+3);

		context.font = "bold 8pt Arial";
		context.fillStyle = "rgb(51, 102, 255)";
		context.fillText(data[3], 2, y5-10);
		context.fillStyle = "rgb(51, 204, 255)";
		context.fillText(data[4],32, y5-10);
		context.fillStyle = "rgb(51,   0, 255)";
		context.fillText(data[5],62, y5-10);

		context.fillStyle = "rgb(0, 0, 0)";
		context.fillText("@ " + data[0] + " Time in UTC",92, y5-10);

		context.font = "8pt Arial";
		s1 = zoomPosXoldS+zoomPosXold/(canvas.width-100)  * (zoomPosX);
		s2 = zoomPosXoldS+zoomPosXold/(canvas.width-100)  * (zoomPosX+xw/4);
		s3 = zoomPosXoldS+zoomPosXold/(canvas.width-100)  * (zoomPosX+xw/2);
		s4 = zoomPosXoldS+zoomPosXold/(canvas.width-100)  * (zoomPosX+xw/4*3);
		s5 = zoomPosXoldS+zoomPosXold/(canvas.width-100)  * (zoomPosX+xw);
		zoomPosXoldS= s1;
		zoomPosXold = s5-s1;

		xh = s1;
		xm = Math.round((xh-Math.floor(xh))*60);
		xh = Math.floor(xh);
		if (xh<10) xh = "0"+xh;
		if (xm<10) xm = "0"+xm;
		context.fillText(xh+":"+xm+"h", xa-15,          y1+15);
		xh = s2;
		xm = Math.round((xh-Math.floor(xh))*60);
		xh = Math.floor(xh);
		if (xh<10) xh = "0"+xh;
		if (xm<10) xm = "0"+xm;
		context.fillText(xh+":"+xm+"h", xa-15+6*xStep,  y1+15);
		xh = s3;
		xm = Math.round((xh-Math.floor(xh))*60);
		xh = Math.floor(xh);
		if (xh<10) xh = "0"+xh;
		if (xm<10) xm = "0"+xm;
		context.fillText(xh+":"+xm+"h", xa-15+12*xStep, y1+15);
		xh = s4;
		xm = Math.round((xh-Math.floor(xh))*60);
		xh = Math.floor(xh);
		if (xh<10) xh = "0"+xh;
		if (xm<10) xm = "0"+xm;
		context.fillText(xh+":"+xm+"h", xa-15+18*xStep, y1+15);
		xh = s5;
		xm = Math.round((xh-Math.floor(xh))*60);
		xh = Math.floor(xh);
		if (xh<10) xh = "0"+xh;
		if (xm<10) xm = "0"+xm;
		context.fillText(xh+":"+xm+"h", xa-15+24*xStep, y1+15);

		context.beginPath();
		context.strokeStyle = "rgb(255, 0, 0)";
		context.moveTo(45, y2);
		context.lineTo(canvas.width-50, y2);
		context.moveTo(45, y3);
		context.lineTo(canvas.width-50, y3);
		context.moveTo(45, y4);
		context.lineTo(canvas.width-50, y4);

		for (i=0; i<25; i++)
		{
			context.moveTo(xa+i*xStep, y1+5);
			if ((i==6) || (i==12) || (i==18))
				context.lineTo(xa+i*xStep, y5);
			else
				context.lineTo(xa+i*xStep, y1);
		}
		context.closePath();
		context.stroke();
		context.fill();


		xStep = (canvas.width-100) / (zoomPosXold);

		context.beginPath();
		context.strokeStyle = "rgb(0, 0, 0)";
	    context.rect(50,20,canvas.width-100,canvas.height-40);
		context.save();
		context.clip();
		context.closePath();

		context.beginPath();
		context.strokeStyle = "rgb(0, 255, 0)";
		context.moveTo(xa+(data[1]-zoomPosXoldS)*xStep,y1);
		context.lineTo(xa+(data[1]-zoomPosXoldS)*xStep,y5);
		context.moveTo(xa+(data[2]-zoomPosXoldS)*xStep,y1);
		context.lineTo(xa+(data[2]-zoomPosXoldS)*xStep,y5);
		context.closePath();
		context.stroke();
		context.fill();
		context.closePath();

		context.beginPath();
		context.strokeStyle = "rgb(51, 102, 255)";

		xStep = (canvas.width-100) / (zoomPosXold*3600) * 5;
		arr = data[6].split(",");
		context.moveTo(xa-(zoomPosXoldS*720)*xStep, y1-(arr[0]-(t1/100))*(canvas.height-40)/zoomPosYold*100);
		for (i=7; i<data.length-1; i++)
		{
			arr = data[i].split(",");
			context.lineTo(xa+(i-(zoomPosXoldS*720))*xStep,y1-(arr[0]-(t1/100))*(canvas.height-40)/(t5/100));
		}
		context.moveTo(xa,y1);
		context.closePath();
		context.stroke();

		context.beginPath();
		context.strokeStyle = "rgb(51, 204, 255)";
		arr = data[6].split(",");
		context.moveTo(xa-(zoomPosXoldS*720)*xStep, y1-(arr[1]-(t1/100))*(canvas.height-40)/zoomPosYold*100);
		for (i=7; i<data.length-1; i++)
		{
			arr = data[i].split(",");
			context.lineTo(xa+(i-(zoomPosXoldS*720))*xStep,y1-(arr[1]-(t1/100))*(canvas.height-40)/(t5/100));
		}
		context.moveTo(x1,y1);
		context.closePath();
		context.stroke();

		context.beginPath();
		context.strokeStyle = "rgb(51,  00, 255)";
		arr = data[6].split(",");
		context.moveTo(xa-(zoomPosXoldS*720)*xStep, y1-(arr[2]-(t1/100))*(canvas.height-40)/zoomPosYold*100);
		for (i=7; i<data.length-1; i++)
		{
			arr = data[i].split(",");
			context.lineTo(xa+(i-(zoomPosXoldS*720))*xStep,y1-(arr[2]-(t1/100))*(canvas.height-40)/(t5/100));
		}
		context.moveTo(x1,y1);
		context.closePath();
		context.stroke();

		context.beginPath();
		context.strokeStyle = "rgb(255,  0, 0)";
		arr = data[6].split(",");
		context.moveTo(xa-(zoomPosXoldS*720)*xStep, y1-(arr[3])*(canvas.height-40));
		context.lineWidth=2;
		for (i=7; i<data.length-1; i++)
		{
			arr = data[i].split(",");
			if (arr[3] == 0)
			{
				context.moveTo(xa+(i-(zoomPosXoldS*720))*xStep, y1-(0.99)*(canvas.height-40));
			}
			else
				context.lineTo(xa+(i-(zoomPosXoldS*720))*xStep, y1-(0.99)*(canvas.height-40));
//			context.lineTo(xa+(i-(zoomPosXoldS*720))*xStep, y1-(arr[3]-(t1/100))*(canvas.height-40)/(t5/100));
		}
		context.moveTo(x1,y1);
		context.closePath();
		context.stroke();
		context.restore();
	}
}

function getY( oElement )
{
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function getX( oElement )
{
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function zoomDown(e) {
	if (e.pageX || e.pageY) 	{
		_x = e.pageX;
		_y = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		_x = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		_y = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	_x = _x-getX(document.getElementById('canvas2'));
	_y = _y-getY(document.getElementById('canvas2'));
	doDraw = true;
}

function zoomMove(e) {
	var canvas = document.getElementById('canvas2');
	var context = canvas.getContext('2d');

	if (doDraw)
	{
		if (e.pageX || e.pageY) 	{
			_x1 = e.pageX;
			_y1 = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			_x1 = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			_y1 = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		_x1 = _x1-getX(document.getElementById('canvas2'));
		_y1 = _y1-getY(document.getElementById('canvas2'));
		context.beginPath();
		canvas.width=canvas.width;
		context.strokeStyle = "rgb(0, 0, 0)";
		context.rect(_x,_y,-_x+_x1,-_y+_y1);
		context.closePath();
		context.stroke();
	}
}

function zoomUp() {
	var canvas = document.getElementById('canvas2');
	doDraw = false;
	if ((Math.abs(_x-_x1) > 10) && (Math.abs(_y-_y1) > 10))
	{
		canvas.width=canvas.width;
		zoomDraw(_x,_y,_x1,_y1);
	}
}

function drawCanvas(){
	var canvas = document.getElementById('canvas1');
	var canvas2 = document.getElementById('canvas2');
	var y1, y2, y3, y4, y5, x1, xStep
	zoomDraw(0,0,canvas.width-100,canvas.height-40);
	canvas2.addEventListener('mousedown', zoomDown, false);
	canvas2.addEventListener('mousemove', zoomMove, false);
	canvas2.addEventListener('mouseup',   zoomUp, false);
}
