mdown=0;

function divcenter(dname)
{
	d=document.getElementById(dname);
	if (!d) return;
  e=document.getElementById('rc_main');
	xpos=i5main.pagedx*128-i5main.msl- 3-192-parseInt(d.style.width)/2;
	ypos=i5main.pagedy*128-i5main.mst-20-192-parseInt(d.style.height)/2;
 	d.style.left=xpos+"px";
 	d.style.top=ypos+"px";
	status=d.style.width;
}

function fxlinktype(a)
{
	document.forms.rc_form.elements.iconid.value=alinksymbols[a.value];
	d=document.getElementById("ficonid");
	d.value=alinksymbols[a.value];
	linktypeupdate();
}

function linktypeupdate()
{
  zo=i5main.zoom;
  i5main.oxp0++;
	i5main.noxp0++;
  i5main.printmap(i5main.xpos*zo,i5main.ypos*zo);
}

function zset(zs)
{
  if (g_zoom > 0) g_zoom--;
  zo=i5main.zoom;
  i5main.zoom=zoomstages[g_zoom];
  i5main.oxp0=99999;
  i5main.printmap(i5main.xpos*zo,i5main.ypos*zo);
}

function zodo()
{
  if (g_zoom > 0) g_zoom--;
	else return;
  zo=i5main.zoom;
  i5main.zoom=zoomstages[g_zoom];
  i5main.oxp0=99999;
  i5main.printmap(i5main.xpos*zo,i5main.ypos*zo);
}

function zoup()
{
  if (g_zoom < zoomstages.length-1) g_zoom++;
	else return;
  zo=i5main.zoom;
  i5main.zoom=zoomstages[g_zoom];
  i5main.oxp0=99999;
  i5main.printmap(i5main.xpos*zo,i5main.ypos*zo);
}

function movemap(x,y)
{
  i5main.xpos=i5main.xpos+x*i5main.zoom*256;
  i5main.ypos=i5main.ypos+y*i5main.zoom*256;
  zo=i5main.zoom;
  i5main.printmap(i5main.xpos*zo,i5main.ypos*zo);
  i5over.printmap(i5main.xpos*zo,i5main.ypos*zo);
}

function maphome()
{
	i5main.xpos=homepos.xpos;
	i5main.ypos=homepos.ypos;
	i5main.zoom=homepos.zoom;
  zo=i5main.zoom;
  i5main.printmap(i5main.xpos*zo,i5main.ypos*zo);
}

function i5_print(xci,yci)
{
	// Koordinate
	xc=Math.floor(xci/this.zoom);
  yc=Math.floor(yci/this.zoom);
	// optionen from form
	opt='';
	if (this==i5main) {
		if (document.forms.rc_form.elements.xpos) document.forms.rc_form.elements.xpos.value=xci;
		if (document.forms.rc_form.elements.ypos) document.forms.rc_form.elements.ypos.value=yci;
		if (document.forms.rc_form.elements.zoom) document.forms.rc_form.elements.zoom.value=this.zoom;
		for (i=0; i < document.forms.rc_form.elements.length; i++) {
			if (document.forms.rc_form.elements[i].type=='checkbox') {
			  if (document.forms.rc_form.elements[i].checked) opt=opt+'&'+document.forms.rc_form.elements[i].name+'='+document.forms.rc_form.elements[i].value;
			}	else {
				opt=opt+'&'+document.forms.rc_form.elements[i].name+'='+document.forms.rc_form.elements[i].value;
			}
		}
	}
  // constant values
  e=document.getElementById(this.divname+'fix');
  this.pagedx=Math.ceil(e.clientWidth/256)+1;
  this.pagedy=Math.ceil(e.clientHeight/256)+1;
	// size of mapcontainer
 	this.mapdiv=document.getElementById(this.divname);
  this.mapdiv.style.width=this.pagedx*256+'px';
  this.mapdiv.style.height=this.pagedx*256+'px';
  this.mapdiv.style.cursor='move';
  this.dx2=e.clientWidth/2;
  this.dy2=e.clientHeight/2;
  // uper left corner of coordinate
  this.xp0=Math.floor((xc-this.dx2)/256);
  this.yp0=Math.floor((yc-this.dy2)/256);
	// position of this
	this.xpos=xc;
	this.ypos=yc;

	// inside 
	this.msl=0-(xc-this.dx2-this.oxp0*256);
	this.mst=0-(yc-this.dy2-this.oyp0*256);
	this.mapdiv.style.left=this.msl+"px";
	this.mapdiv.style.top=this.mst+"px";

	// edit
	if (document.forms.rc_form.elements.iconid.value) divcenter('xediticonid');

	// do not neet new map
	if (this.xp0==this.oxp0 && this.yp0==this.oyp0) return;
	
	if (this.xp0==this.noxp0 && this.yp0==this.noyp0) return;
	this.noxp0=this.xp0;
	this.noyp0=this.yp0;

	// collect options
  par="map="+this.mapid+','+this.xp0+','+this.yp0+','+this.zoom+','+this.pagedx+','+this.pagedy+','+opt;
	window.status=par;
	//this.mapdiv.style.visibility='hidden';
	this.ajax.connect("i5data.php", "GET", par, fnWhenDone,this);
}

var fnWhenDone = function (txt,a) 
{ 
	a.mapdiv.style.visibility='visible'; 	
	a.oxp0=a.xp0;
	a.oyp0=a.yp0;
 	a.msl=0-(a.xpos-a.dx2-a.xp0*256);
 	a.mst=0-(a.ypos-a.dy2-a.yp0*256);
 	a.mapdiv.style.left=a.msl+"px";
 	a.mapdiv.style.top=a.mst+"px";
	a.mapdiv.innerHTML=txt; 
	if (document.forms.rc_form.elements.iconid.value) divcenter('xediticonid');
}

function i5mousemove(ev)
{
  if (!ev) ev=window.event;
  if (mdown) {
		if (gmapdiv==i5main || gmapdiv==i5over) {
      gmapdiv.xpos-=ev.clientX-gmapdiv.ex0;
      gmapdiv.ypos-=ev.clientY-gmapdiv.ey0;
      gmapdiv.ex0=ev.clientX;
      gmapdiv.ey0=ev.clientY;
      if (gmapdiv.slave) gmapdiv.slave.printmap(gmapdiv.xpos*gmapdiv.zoom,gmapdiv.ypos*gmapdiv.zoom);
	    gmapdiv.printmap(gmapdiv.xpos*gmapdiv.zoom,gmapdiv.ypos*gmapdiv.zoom);
		} 
		if (gmapdiv==i5edit) {
      gmapdiv.xpos+=ev.clientX-gmapdiv.ex0;
      gmapdiv.ypos+=ev.clientY-gmapdiv.ey0;
      gmapdiv.ex0=ev.clientX;
      gmapdiv.ey0=ev.clientY;
		  gmapdiv.mapdiv.style.left=gmapdiv.xpos+"px";
  		gmapdiv.mapdiv.style.top=gmapdiv.ypos+"px";
		}
    gmapdiv.mapdiv.style.cursor='move';
  } else {
	  gmapdiv.mapdiv.style.cursor='default';
	}
  return false;
}

function i5mousedown(ev)
{
  if (!ev) ev=window.event;
  window.status="down: x = " + ev.clientX + "/ y = " + ev.clientY + " b = "+ ev.button;
  mdown=1;
  gmapdiv.mapdiv.style.cursor='move';
  gmapdiv.mapdiv.onmouseout='';
  gmapdiv.ex0=ev.clientX;
  gmapdiv.ey0=ev.clientY;

	return false;
}

function i5mouseup(ev)
{
  if (!ev) ev=window.event;
  //window.status="up: x = " + ev.clientX + "/ y = " + ev.clientY + " b = "+ ev.button;
  mdown=0;
  gmapdiv.mapdiv.style.cursor='default';
  //gmapdiv.mapdiv.onmousemove='';
  //gmapdiv.mapdiv.onmousedown='';
  //gmapdiv.mapdiv.onmouseup='';
	return true;
}

function i5mouseover(div)
{
  if (mdown) return false;
	if (div.divname=='rc_edit') div.mapdiv=document.getElementById('rc_edit');
  gmapdiv=div;
  div.mapdiv.style.cursor='move';
  div.mapdiv.onmousemove=i5mousemove;
  div.mapdiv.onmousedown=i5mousedown;
  div.mapdiv.onmouseup=i5mouseup;
  document.onmouseup=i5mouseup;
  return false;
}

function i5mouseout(div)
{
  mdown=0;
  gmapdiv=div;
  div.mapdiv.style.cursor='default';
  div.mapdiv.onmousemove='';
  div.mapdiv.onmousedown='';
  div.mapdiv.onmouseup='';
  return false;
}



function loaded()
{
  document.mf.dxy0.value=document.documentElement.clientWidth;
  document.mf.dxy1.value=document.documentElement.clientHeight;
}

function sized()
{
  document.mf.dxy0.value=document.documentElement.clientWidth;
  document.mf.dxy1.value=document.documentElement.clientHeight;
  document.mf.submit();
}

function redraw()
{
	i5over.printmap(gmapdiv.xpos*gmapdiv.zoom,gmapdiv.ypos*gmapdiv.zoom);
	i5main.printmap(gmapdiv.xpos*gmapdiv.zoom,gmapdiv.ypos*gmapdiv.zoom);
}


i5main={ 
  xc:0, 
  yc:0, 
  zoom:2,
  db:'none',
	mapid:0, 
  pagedx:1, 
  pagedy:1, 
  mapdx:1,
  mapdy:1,
  iarray:new Array(),
  printmap:i5_print,
  mout:i5mouseout,
  mover:i5mouseover,
  divname:'rc_main',
	symbols:new Array(),
	ajax:new XHConn(),
	wait:0,
	oxp0:99999,
	oyp0:99999
}

i5over={ 
  xc:0, 
  yc:0, 
  zoom:1,
  db:'none', 
	mapid:0, 
  pagedx:1, 
  pagedy:1, 
  mapdx:1,
  mapdy:1,
  iarray:new Array(),
  printmap:i5_print,
  mout:i5mouseout,
  mover:i5mouseover,
  divname:'rc_over',
	ajax:new XHConn(),
	wait:0,
	oxp0:99999,
	oyp0:99999
}

i5edit={
	xc:300,
	yc:300,
  mout:i5mouseout,
  mover:i5mouseover,
	divname:'rc_edit'
}

homepos={};

window.onresize=redraw;

function ajax()
{
  try { return new XMLHttpRequest(); }
  catch (e) { try { return new ActiveXObject("Microsoft.XMLHTTP"); } 
  						catch (e) { try { return new ActiveXObject("Msxml2.XMLHTTP"); } 
  												catch (e) { alert('No Ajax') } 
												}
						}
	return null;
}

// ajax
function XHConn()
{
  var xmlhttp, bComplete = false;
	var wait=0;
	var req;
	xmlhttp=ajax();
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone,xthis)
  {
    if (!xmlhttp) return 0;
		if (xmlhttp.readyState != 0) {
			xmlhttp.abort();
			delete xmlhttp;
			xmlhttp=ajax();
		}
		xmlhttp.onreadystatechange = function() {
     	if (xmlhttp.readyState == 4) {
				if (xmlhttp.responseText.length > 0) {
       		fnDone(xmlhttp.responseText,xthis);
				}
			}
		}
		req=sURL+"?"+sVars;
		xmlhttp.open("GET",req, true);
    xmlhttp.send('');
		xthis.wait++;
    return 1;
  };
  return this;
}

// mousewheel
function handle(delta) 
{
  if (delta < 0) zodo();
  else zoup();
}
/** Event handler for mouse wheel event. */
function wheel(event)
{
  var delta = 0;
  if (!event) event = window.event; /* For IE. */

  if (event.wheelDelta) { 
		delta = event.wheelDelta/120;/* IE/Opera. */
  	if (window.opera) delta = -delta; /** In Opera 9, delta differs in sign as compared to IE. */
  } else if (event.detail) { /** In Mozilla, sign of delta is different than in IE. * Also, delta is multiple of 3.                */
   	delta = -event.detail/3;
  }
  /** If delta is nonzero, handle it. Basically, delta is now positive if wheel was scrolled up, and negative, if wheel was scrolled down.         */
  if (delta) handle(delta);
  /** Prevent default actions caused by mouse wheel. That might be ugly, but we handle scrolls somehow anyway, so don't bother here..         */
  if (event.preventDefault) event.preventDefault();
  event.returnValue = false;
}

if (window.addEventListener) window.addEventListener('DOMMouseScroll', wheel, false);/** DOMMouseScroll is for mozilla. */
window.onmousewheel = document.onmousewheel = wheel;/** IE/Opera. */



