/*
Datei: scriptmain.js
Datum: 13.01.2010
Autor: AOD-Media Inhaber Andre Dabow
Beschreibung: stellt die Funktionen der Seite zur Verfügung
*/
var svgueberblick, svgDoc, svgWin, rect;
var Width, Height,wd,ht;
var kachel_b = 118, kachel_h = 114;
var karte_b = 15458, karte_h = 15960;
var map_width, map_height;
var svgRoot;
var touren, akt_tour = null;
var ueb_width, ueb_height;
var zindex = 1;
var imgs = new Array();
var firstload = true;
var rect;
var faktor = 64;
var panActive = false;
var cleanXY = false;
var vk;

function initSVG() {
isSVG = ( navigator.appName.indexOf("Explorer") != -1 ) ? isSVGControlInstalled() : true;
if (isSVG==true) {
	svgDoc = getSVG( "map" );
	svgueberblick = getSVG( "ueberblick" );
	svgRoot = svgDoc.getElementById('root');
	svgWin = ( navigator.appName.indexOf("Explorer") != -1 ) ? svgDoc.parentWindow : svgDoc.defaultView;
	vb = getViewBox();

	if ( is_admin == true ) {
		firstload = false;
		ladeHintergrund();
		Event.observe(svgDoc, "click", newObject);
	}
	else {
		x1 = Math.floor(vb[0]/kachel_b)*kachel_b;
		y1 = Math.floor(vb[1]/kachel_h)*kachel_h;
		anz = Math.ceil(vb[2]/kachel_b) + 3;
		i = 0;
		for ( y=0; y<anz; y++ ) {
			for ( x=0; x<anz; x++ ) {
				bildx = Math.floor(x1 / kachel_b + x);
				bildy = Math.floor(y1 / kachel_h + y);
				imgs[i] = "bg100/k_" + bildy + "_" + bildx + ".png";
				i++;
			}
		}
		preloader(imgs,ladeHintergrund);
	}
	svgDoc.getElementById("navi").setAttributeNS(null, "transform", eval('"translate('+vb[0]+','+vb[1]+') scale(0.7)"') );

	map_height = document.getElementById('kartenausschnitt').offsetHeight;
	map_width = document.getElementById('kartenausschnitt').offsetWidth;
	rect = svgueberblick.getElementById('pan_rect');
	pan_width = parseInt(map_width / 15);
	pan_height = parseInt(map_height / 15);
	rect.setAttributeNS(null, 'width', eval ( '"'+pan_width+'px"') );
	rect.setAttributeNS(null, 'height', eval ( '"'+pan_height+'px"') );
	setze_rechteck(vb[0],vb[1]);

	if (tour_empf > 0) tour(tour_empf);
	if (anb_cx > 0) {
		zentrieren(anb_cx,anb_cy);
		var vis = svgDoc.createAttribute("style");
		vis.nodeValue = "visibility:visible";
		svgDoc.getElementById(anb_id).setAttributeNode(vis);
	}
}
}

function pCoord(a,w,h) {
if (a=="x") return (navigator.appName.indexOf("Explorer") != -1) ? Math.ceil(document.body.clientWidth/2 - w/2 + document.body.scrollLeft) : Math.ceil(window.innerWidth/2 - w/2 + window.pageXOffset);
else return (navigator.appName.indexOf("Explorer") != -1) ? Math.ceil(document.body.clientHeight/2 + h/2 + document.body.scrollTop) : Math.ceil(window.innerHeight/2 + h/2 + window.pageYOffset);
}

function getSVG ( id ) {
var svgdoc = null;
var object = $(id);
if (object != null) {
	if (object.contentDocument != null) {
		svgdoc = object.contentDocument;
		}
	else if (typeof object.getsvgDocument != 'undefined') {
		svgdoc = object.getsvgDocument();
		}
	}
if ( navigator.appName.indexOf("Explorer") != -1 && svgdoc == null )
	svgdoc = document.embeds[id].getsvgDocument();
return svgdoc;
}

// Ebenen ein- und ausblenden
function ebene( id ) {
eb = svgDoc.getElementById( eval('"E'+id+'"') );
vis = eb.style.getPropertyValue('visibility');
el = "ebene"+id;
if ( vis == "visible" ) {
	eb.setAttributeNS(null, 'style', 'visibility:hidden');
	document.symbol.elements[el].checked = false;
	}
else {
	eb.setAttributeNS(null, 'style', 'visibility:visible');
	document.symbol.elements[el].checked = true;
	}
}

function getViewBox() {
viewb = new String(svgRoot.getAttribute("viewBox"));
viewboxes = viewb.split(' ');
x = parseInt(viewboxes[0]);
y = parseInt(viewboxes[1]);
Width = parseInt(viewboxes[2]);
Height = parseInt(viewboxes[3]);
return new Array( x, y, Width, Height );
}

function setViewBox( x, y, w, h ) {
vb = getViewBox();
x = Math.floor( x / kachel_b ) * kachel_b;
y = Math.floor( y / kachel_h ) * kachel_h;
viewBoxneu = x + " " + y + " " + w + " " + h;
svgRoot.setAttributeNS(null, "viewBox",viewBoxneu);
scale = 0.7 * (2-zindex);
svgDoc.getElementById("navi").setAttributeNS(null,  "transform", eval('"translate('+x+','+y+') scale('+scale+')"') );
}

function ladeHintergrund() {
vb = getViewBox();
x1 = Math.floor(vb[0]/kachel_b)*kachel_b;
y1 = Math.floor(vb[1]/kachel_h)*kachel_h;
anz = Math.ceil(vb[2]/kachel_b) + 3;
i = 0;

for ( y=0; y<anz; y++ ) {
	ywert = y1 + y * kachel_h;
	for ( x=0; x<anz; x++ ) {
		element = "k_" + x + "_" + y;
		kachel = svgDoc.getElementById(element);
		xwert = x1 + x * kachel_b;
		kachel.setAttributeNS(null, "x",xwert + "px");
		kachel.setAttributeNS(null, "y",ywert + "px");
		if ( firstload == true ) {
			src = imgs[i];
			i++;
			}
		else {
			bildx = Math.floor(x1 / kachel_b + x);
			bildy = Math.floor(y1 / kachel_h + y);
			src = "bg100/k_" + bildy + "_" + bildx + ".png";
			}
		kachel.setAttributeNS('http://www.w3.org/1999/xlink', "href", src);
		}
	}
if ( firstload == true ) firstload = false;
//svgDoc.getElementById("blind").setAttributeNS(null, "x",x1 + "px");
//svgDoc.getElementById("blind").setAttributeNS(null, "y",y1 + "px");
}

function verschiebe(dir) {
vb = getViewBox();
var x = vb[0];
var y = vb[1];
breite = kachel_b;// * (2-zindex);
hoehe = kachel_h;// * (2-zindex);

switch (dir) {
	case "links": x = x - breite; break;
	case "rechts": x = x + breite; break;
	case "oben": y = y - hoehe; break;
	case "unten": y = y + hoehe; break;
	}
if ( (dir == "links" && vb[0] > 0) || (dir == "oben" && vb[1] > 0) || (dir == "rechts" && vb[0] < 14900) || (dir == "unten" && vb[1] < 15500) )
	{
	setViewBox(x,y,vb[2],vb[3]);
	ladeHintergrund();
	setze_rechteck(x,y);
	}
}

// Klick auf Übersichtskarte wählt Kartenausschnitt
function pansprung(Event) {
if ( panActive == false ) {
	vb = getViewBox();
	posx = document.all ? window.event.clientX : Event.pageX;
	posy = document.all ? window.event.clientY : Event.pageY;
	rect_width = parseInt(rect.getAttribute("width").match( /\d+/ ));
	rect_height = parseInt(rect.getAttribute("height").match( /\d+/ ));
	ueb_width = 250;
	ueb_height = 265;
	rect_x = posx - rect_width / 2;
	rect_y = posy - rect_height / 2;

	x_max = ueb_width - rect_width;
	y_max = ueb_height - rect_height;
	if ( rect_x < 0 ) rect_x = 0;
	if ( rect_y < 0 ) rect_y = 0;
	if ( rect_x > x_max ) rect_x = x_max;
	if ( rect_y > y_max ) rect_y = y_max;

	x_neu = parseInt( (rect_x * (karte_b / ueb_width)/kachel_b) ) * kachel_b;
	y_neu = parseInt( (rect_y * (karte_h / ueb_height)/kachel_h) ) * kachel_h;

	setViewBox( x_neu, y_neu, vb[2], vb[3] );
	ladeHintergrund();
	setze_rechteck( x_neu, y_neu );
	}
}

function pansprung2(transX,transY) {
if ( panActive == true ) {
	vb = getViewBox();
	rect_x = Math.round(rect.getAttribute("x").match( /\d+/ ))+transX;
	rect_y = Math.round(rect.getAttribute("y").match( /\d+/ ))+transY;

	x_neu = Math.round( ( rect_x * faktor ) / kachel_b) * kachel_b;
	y_neu = Math.round( ( rect_y * faktor ) / kachel_h) * kachel_h;

	setViewBox( x_neu, y_neu, vb[2], vb[3] );
	ladeHintergrund();
	x = x_neu / faktor - transX;
	y = y_neu / faktor - transY;
	rect.setAttributeNS(null, 'x', eval ( '"'+x+'"') );
	rect.setAttributeNS(null, 'y', eval ( '"'+y+'"') );
	}
}

function zentrieren(cx,cy)
	{
	vb = getViewBox();
	x_neu = Math.round( (cx - vb[2]/2)/kachel_b ) * kachel_b;
	y_neu = Math.round( (cy - vb[3]/2)/kachel_h) * kachel_h;
	setViewBox( x_neu, y_neu, vb[2], vb[3] );
	ladeHintergrund();
	setze_rechteck( x_neu, y_neu );
	}

var req2 = false;
function tour(t_id) {
if (t_id > 0) {
	tour_id = eval("'tour"+t_id+"'");
	t_id = null;
	}
else tour_id = document.forms['routeplaner'].elements['route'].value;
ort_id = document.forms[1].elements['ort'].value;
o_id = ort_id.match( /\d+/ );
cx = parseInt(ort_x[o_id]);
cy = parseInt(ort_y[o_id]);

if ( is_admin == true ) {
	clearInfo();
	$('tour_btn').style.visibility = 'visible';
	if ( tour_id == "0" ) tour_id = false;
	}

if ( tour_id == null || !tour_id )
	{
	if ( is_admin == true ) {
		$('tour_btn').style.visibility = 'hidden';
		}
	else {
		if ( akt_tour != null ) {
			start = akt_tour+'_start';
			ziel = akt_tour+'_ziel';
			svgDoc.getElementById(akt_tour).style.setProperty('visibility', 'hidden', '');
			if ( svgDoc.getElementById(start) != null ) svgDoc.getElementById(start).style.setProperty('visibility', 'hidden', '');
			if ( svgDoc.getElementById(ziel) != null ) svgDoc.getElementById(ziel).style.setProperty('visibility', 'hidden', '');
			akt_tour = null;
			}
		}
	}
else {
	start = tour_id+'_start';
	ziel = tour_id+'_ziel';
	svgDoc.getElementById(tour_id).style.setProperty('visibility', 'visible', '');
	if ( svgDoc.getElementById(start) != null ) svgDoc.getElementById(start).style.setProperty('visibility', 'visible', '');
	if ( svgDoc.getElementById(ziel) != null ) svgDoc.getElementById(ziel).style.setProperty('visibility', 'visible', '');
	if ( akt_tour != null ) {
		start = akt_tour+'_start';
		ziel = akt_tour+'_ziel';
		svgDoc.getElementById(akt_tour).style.setProperty('visibility', 'hidden', '');
		if ( svgDoc.getElementById(start) != null ) svgDoc.getElementById(start).style.setProperty('visibility', 'hidden', '');
		if ( svgDoc.getElementById(ziel) != null ) svgDoc.getElementById(ziel).style.setProperty('visibility', 'hidden', '');
		}
	akt_tour = tour_id;
	t_id = String(tour_id).match( /\d+/ );
	cx = parseInt(tour_x[t_id]);
	cy = parseInt(tour_y[t_id]);
	if ( is_admin == false ) {
		req2 = initRequest();
		req2.onreadystatechange = writeTourInfos;
		req2.open("GET", "inc/tourInfos.php"+'?tour='+t_id+'&'+new Date().getTime(), true);
		req2.send(null);
		tour_stats(t_id);
	}
}
zentrieren(cx,cy);
}

function writeTourInfos() {
 if(req2.readyState == 4)
  {
	if(req2.status == 200) {
		$('info').innerHTML = req2.responseText;
    }
	else {
		alert(req2.statusText);
	}
	if(document.all && !window.opera) {
		req2.abort();
    }
  }
}

function lokalInfo( table, id ) {
open( eval('"../../info.php?tab='+table+'&id='+id+'"') ,"Informationen","dependent=yes,scrollbars=yes,directories=no,location=no,menubar=no,resizable=yes,status=no,toolbar=no,width=500,height=420,screenX=300,screenY=200");
}

function getInfos(art,id) {
	switch(art) {
		case "foto": action = "showFoto"; break;
		case "video": action = "showVideo"; break;
		case "sight": action = "showFoto"; break;
	}
	req3 = initRequest();
	req3.onreadystatechange = eval(action);
	req3.open("GET", "inc/getInfos.php?art="+art+'&id='+id+"&"+new Date().getTime(), true);
	req3.send(null);
}

function showFoto() {
if(req3.readyState == 4) {
	if(req3.status == 200) {
		wd=350; ht=250;
		Tip(req3.responseText, SHADOW,true, CLOSEBTN,true, STICKY,true, FIX,[pCoord('x',wd,ht),pCoord('y',wd,ht)], DURATION,0);
    }
	else {
		alert(req3.statusText);
	}
	if(document.all && !window.opera) {
		req3.abort();
    }
  }
}

function showVideo(id) {
// Breite des Fensters
w = 396;
// Höhe des Fensters
h = 375;
screenX = (navigator.appName.indexOf("Explorer") != -1) ? 'left='+Math.ceil(document.documentElement.clientWidth/2 - w/2) : 'screenX='+Math.ceil(window.innerWidth/2 - w/2);
screenY = (navigator.appName.indexOf("Explorer") != -1) ? 'top='+Math.ceil(document.documentElement.clientHeight/2 - h/2) : 'screenY='+Math.ceil(window.innerHeight/2 - h/2);
deps = eval('"dependent=yes,scrollbars=yes,directories=no,location=no,menubar=no,resizable=yes,status=no,toolbar=no,width='+w+',height='+h+','+screenX+','+screenY+'"');
open( eval('"../../video.php?id='+id+'"') ,"Kurzvideo",deps);
}

function showSight(table,id) {
title = (table=='pp') ? "Parkplatz" : "Sehenswürdigkeit";
open( eval('"../../sight.php?tab='+table+'&id='+id+'"') ,title,"dependent=yes,scrollbars=yes,directories=no,location=no,menubar=no,resizable=yes,status=no,toolbar=no,width=600,height=370,screenX=100,screenY=200");
}

function setze_rechteck( x, y ) {
x = x / faktor;
y = y / faktor;
rect.setAttributeNS(null,"transform","translate(0,0)");
rect.setAttributeNS(null, 'x', eval ( '"'+x+'"') );
rect.setAttributeNS(null, 'y', eval ( '"'+y+'"') );
cleanXY = true;
}

function tour_stats(id) {
req = initRequest();
req.open("GET", "inc/tourStats.php"+'?tour='+id+'&'+new Date().getTime(), true);
req.send((document.all && !window.opera)?null:true);
}

function initRequest() {
if(window.XMLHttpRequest)
  {
   try {
     req = new XMLHttpRequest();
    }
   catch(e) {
     req = false;
    }
   }
  else if(window.ActiveXObject)
   {
    try
     {
      req = new ActiveXObject("Msxml2.XMLHTTP");
     }
    catch(e)
     {
      try
       {
        req = new ActiveXObject("Microsoft.XMLHTTP");
       }
      catch(e)
       {
        req = false;
       }
     }
   }
return(req)
}
