var Drag = {

    obj : null,

    init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
    {
        o.onmousedown    = Drag.start;

        o.hmode            = bSwapHorzRef ? false : true ;
        o.vmode            = bSwapVertRef ? false : true ;

        o.root = oRoot && oRoot != null ? oRoot : o ;

        if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "170px";
        if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "430px";
        if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
        if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

        o.minX    = typeof minX != 'undefined' ? minX : null;
        o.minY    = typeof minY != 'undefined' ? minY : null;
        o.maxX    = typeof maxX != 'undefined' ? maxX : null;
        o.maxY    = typeof maxY != 'undefined' ? maxY : null;

        o.xMapper = fXMapper ? fXMapper : null;
        o.yMapper = fYMapper ? fYMapper : null;

        o.root.onDragStart    = new Function();
        o.root.onDragEnd    = new Function();
        o.root.onDrag        = new Function();
    },

    start : function(e)
    {
        var o = Drag.obj = this;
        e = Drag.fixE(e);
        var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
        var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
        o.root.onDragStart(x, y);

        o.lastMouseX    = e.clientX;
        o.lastMouseY    = e.clientY;

        if (o.hmode) {
            if (o.minX != null)    o.minMouseX    = e.clientX - x + o.minX;
            if (o.maxX != null)    o.maxMouseX    = o.minMouseX + o.maxX - o.minX;
        } else {
            if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
            if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
        }

        if (o.vmode) {
            if (o.minY != null)    o.minMouseY    = e.clientY - y + o.minY;
            if (o.maxY != null)    o.maxMouseY    = o.minMouseY + o.maxY - o.minY;
        } else {
            if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
            if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
        }

        document.onmousemove    = Drag.drag;
        document.onmouseup        = Drag.end;

        return false;
    },

    drag : function(e)
    {
        e = Drag.fixE(e);
        var o = Drag.obj;

        var ey    = e.clientY;
        var ex    = e.clientX;
        var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
        var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
        var nx, ny;

        if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
        if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
        if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
        if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

        nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
        ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

        if (o.xMapper)        nx = o.xMapper(y)
        else if (o.yMapper)    ny = o.yMapper(x)

        Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
        Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
        Drag.obj.lastMouseX    = ex;
        Drag.obj.lastMouseY    = ey;

        Drag.obj.root.onDrag(nx, ny);
        return false;
    },

    end : function()
    {
        document.onmousemove = null;
        document.onmouseup   = null;
        Drag.obj.root.onDragEnd(    parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
                                    parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
        Drag.obj = null;
    },

    fixE : function(e)
    {
        if (typeof e == 'undefined') e = window.event;
        if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
        if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
        return e;
    }
};




function qualitaetsparameter()
{

	// #qp -> display: none
	var table = document.getElementById('qp');
	table.style.display = 'none';
	
	// Links aufbauen
	var legende = document.getElementById('qp-legende');
	var z = 0;
	for (i=0;i<legende.childNodes.length;i++)
	{
		if ( legende.childNodes[i].nodeName=='LI')
		{
			var text = legende.childNodes[i].innerHTML;
			legende.childNodes[i].innerHTML = '';
			var linkElem = document.createElement('a');
			linkElem.href = '#close';
			linkElem.onclick = new Function("","showtable("+z+")");
			linkElem.innerHTML = text;
			legende.childNodes[i].appendChild(linkElem);
			z = z+1;
		}
	}
	
	// Hinweistext
	var hinweis = document.createElement('p');
	hinweis.id = 'qp-hinweis';
	hinweis.innerHTML = 'Klicken Sie auf den Namen Ihres Herkunftsgebietes in der Legende und Sie erhalten die Qualitätsparameter Ihres Trinkwassers in einem Fenster angezeigt.';
	document.getElementById('content').insertBefore(hinweis,document.getElementById('qpkarte'));
	
}

function closetable()
{
	if (document.getElementById('qp-maindiv'))
			document.getElementById('content').removeChild(document.getElementById('qp-maindiv'));
}

function showtable(nr)
{
	// entfernen
	closetable();
	
	// #qp
	var table = document.getElementById('qp');

	// Spalten-Daten in Arrays
	var Daten = new Array();
	var tbody = table.getElementsByTagName('tbody')[0];
	var tfoot = table.getElementsByTagName('tfoot')[0].firstChild.firstChild.innerHTML;
	var rows = tbody.getElementsByTagName('tr');
	var analyse = new Array();
	// zeilenweise auslesen
	for ( i=0; i<rows.length; i++ )
	{
		if ( i==0 ) var cols = rows[i].getElementsByTagName('th');
		else var cols = rows[i].getElementsByTagName('td');
		// spaltenweise
		a = 0;
		for ( x = 0; x<cols.length; x++ )
		{
			var speicher = cols[x].innerHTML;
			y = i*10+x;
			// analyse vom
			analyse[a] = -1;
			if ( x>1 && i==0 )
			{
				if (cols[x].lastChild.nodeName == 'P')
				{
					analyse[a] = cols[x].lastChild.innerHTML;
					cols[x].removeChild(cols[x].lastChild);
					cols[x].innerHTML+='**';
				}
				a+=1;
			}
			Daten[y] = cols[x].innerHTML;
			cols[x].innerHTML = speicher;
		}
	}
	//analyse[0] = 'Analyse vom 01.02.2006';
	
	// DIVs generieren
	// Gebietsdivs
	var gebietDiv = new Array();
	gebietDiv[0] = document.createElement('div');
	gebietDiv[1] = document.createElement('div');
	gebietDiv[2] = document.createElement('div');
	gebietDiv[3] = document.createElement('div');
	gebietDiv[4] = document.createElement('div');
	gebietDiv[5] = document.createElement('div');
	gebietDiv[0].id = 'div-fw-suedsachsen';
	gebietDiv[1].id = 'div-fw-thueringen';
	gebietDiv[2].id = 'div-twa-wilkau-hasslau';
	gebietDiv[3].id = 'div-tb-muelsen-st-niclas';
	gebietDiv[4].id = 'div-tb-ortmannsdorf';
	gebietDiv[5].id = 'div-tb-c-fraureuth';
	// Daten einfügen
	// ab hier mit innerHTML, da IE Probleme mit den DOM-Methoden hat (nur bei Tabelle?!?)
	for (i=0;i<6;i++)
	{
		// Schliessen
		var closeElem = document.createElement('img');
		closeElem.src='/images/close.gif';
		var AcloseElem = document.createElement('a');
		AcloseElem.href='#qp-legende';
		AcloseElem.id='close';
		AcloseElem.onclick=new Function("","closetable()");
		AcloseElem.appendChild(document.createTextNode('Fenster schließen '));
		AcloseElem.appendChild(closeElem);
		
		// 2 Tabellen a 16/17 Zeilen
		var datenTable = '<table class="firsttable">';
		for ( x=0;x<17;x++ )
		{
			var datenRow = '<tr>';
			// Zellen //1
			y = 0;
			var datenCell = '<td>';
			if (x==0) datenCell = '<th>';
			if (x>0) datenCell='<th class="th-left">';
			z = x*10+y;
			if ( y == 2 ) z = x*10+y+i;
			datenCell+= Daten[z];
			datenRow+= datenCell;
			// Zellen //2
			y = 2;
			var datenCell = '<td>';
			if (x>0) datenCell='<td class="versorger">';
			if (x==0) datenCell='<th class="versorger">';
			z = x*10+y;
			if ( y == 2 ) z = x*10+y+i;
			datenCell+= Daten[z];
			datenRow+= datenCell;
			// Zellen //3
			y = 1;
			var datenCell = '<td>';
			if (x==0) datenCell='<th class="th-right">';
			z = x*10+y;
			if ( y == 2 ) z = x*10+y+i;
			datenCell+= Daten[z];
			datenRow+= datenCell;
			datenTable+=datenRow;
		}
		// erste Tabelle
		gebietDiv[i].innerHTML+=datenTable;
		// Rest
		var datenTable = '<table>';
		var a = 0;
		for ( x=16;x<rows.length;x++ )
		{
			// 1.Zeile wiederholen
			if ( a==0 ) x = 0;
			var datenRow = '<tr>';
			// Zellen //1
			y = 0;
			var datenCell = '<td>';
			if (x==0) datenCell = '<th>';
			if (x>0) datenCell='<th class="th-left">';
			z = x*10+y;
			if ( y == 2 ) z = x*10+y+i;
			datenCell+= Daten[z];
			datenRow+= datenCell;
			// Zellen //2
			y = 2;
			var datenCell = '<td>';
			if (x>0) datenCell='<td class="versorger">';
			if (x==0) datenCell='<th class="versorger">';
			z = x*10+y;
			if ( y == 2 ) z = x*10+y+i;
			datenCell+= Daten[z];
			datenRow+= datenCell;
			// Zellen //3
			y = 1;
			var datenCell = '<td>';
			if (x==0) datenCell='<th class="th-right">';
			z = x*10+y;
			if ( y == 2 ) z = x*10+y+i;
			datenCell+= Daten[z];
			datenRow+= datenCell;
			datenTable+=datenRow;
			if ( a == 0 ) { x = 16; a = 1; }
		}
		gebietDiv[i].innerHTML+=datenTable;
		var pElem = document.createElement('p');
		pElem.innerHTML = tfoot;
		gebietDiv[i].appendChild(pElem);

		if ( analyse[i] != -1 )
		{
			analyse[i] = '**) '+analyse[i];
			var pElem2 = document.createElement('p');
			pElem2.innerHTML = analyse[i];
			pElem2.id = 'analyse';
			gebietDiv[i].appendChild(pElem2);
		}
		
		gebietDiv[i].insertBefore(AcloseElem, gebietDiv[i].firstChild);
	}
	
	// Hauptdiv
	var mainDiv = document.createElement('div');
	mainDiv.id = 'qp-maindiv';
	// Gebiet
	mainDiv.appendChild(gebietDiv[nr]);
	document.getElementById('content').appendChild(mainDiv);
	// Drag
	Drag.init(document.getElementById('qp-maindiv'));
}

schedule("qp", "qualitaetsparameter()\;");
