// JavaScript Document
//
// (C) 2009 Plot and Go
// V1.0 18.10.2008
// Jörg Singendonk


divid2map=new Array();
gmousedown=null;
to_refillreiter=null;
axreiter=new ajax_connect();
axmappop=new ajax_connect();
axpartnerpop=new ajax_connect();
axminipop=new ajax_connect();
hidepartnerpop=0;


function xnot()
{
	return(false);
}

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

/** Event handler for mouse wheel event. */
function wheel(event)
{
  var delta = 0;
  if (!event) event = window.event; /* For IE. */
	m=divid2map[this.id];
  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) {
		lon=map.GetLonX(m.centerX);
		lat=map.GetLatY(m.centerY);
		var z=m.zoom;
		if (delta > 0 && m.zoom < 17) {
			z++;
			while (zoom_avail[z]==0) {
				z++;
				if (z > 17) return;
			}
		}
		if (delta < 0 && m.zoom >  0) {
			z--
			while (zoom_avail[z]==0) {
				z--;
				if (z < 0) return;
			}
		}
		//m.zoom--;
		m.zoom=z;
		m.runonce=0;
		m.SetPos(lon,lat,m.zoom);
	}
  /** 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;
}


// Klasse für Karte
function rasinfo(mapi,mapo,ovel)
{
  var odiv=document.getElementById(mapo);
	this.div=document.getElementById(mapi);
	this.ovr=document.getElementById(ovel);
	width=256*(1+Math.floor(odiv.clientWidth/256));
  height=256*(1+Math.floor(odiv.clientHeight/256));
	this.div.style.width=width+'px';
	this.div.style.height=height+'px';
	// height,width of the outer div!
	this.width=odiv.clientWidth;
  this.height=odiv.clientHeight;
	this.div.onmousedown = mapdown;
	document.onmousemove = mapmove;
	document.onmouseup = mapup;
	this.div.oncontextmenu=xnot;

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

	this.name="ManiMap";
	this.mouse=0;
	divid2map[this.div.id]=this;
	this.runonce=0;
	this.originX=0;
	this.originY=0;
	popup_map=null;


}


// Set position
rasinfo.prototype.SetPos=function(xlon,ylat,zoom)
{
	//this.base=6371000.8/(scalefact/3779.5275590551181102362204724409);
	this.base=Math.pow(2,zoom+8);
	if (this.zoom!=zoom) {
  	this.zoom=zoom;
		this.runonce=0;
	}
	
	// pixel
	this.centerX=this.GetXpos(xlon);
	this.centerY=this.GetYpos(ylat);
	this.lat=ylat;
	this.lon=xlon;
	if (this.runonce==0) {
		this.div.innerHTML='';
		this.originX=this.centerX;
		this.originY=this.centerY;
		this.runonce=1;
		this.x1o=0;
		this.y1o=0;
	}
	this.DrawMap();
	if (popup_map && this==map) loadmappop(popup_map[0],popup_map[1],popup_map[2],0);
}
// x-pixel from latitude
rasinfo.prototype.GetYpos=function(lat)
{
	 return (Math.floor((1-Math.log(Math.tan(lat*Math.PI/180) + 1/Math.cos(lat*Math.PI/180))/Math.PI)/2 * this.base));
}
// y-pixel from longitude
rasinfo.prototype.GetXpos=function(lon) 
{
	return (Math.floor((lon+180)/360*this.base));
}
// lat from pixel
rasinfo.prototype.GetLonX=function(x) 
{
	return (x/this.base*360-180);
}
// lon from pixel
rasinfo.prototype.GetLatY=function(y) 
{
  var n=Math.PI-2*Math.PI*y/this.base;
  return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
}
// draw map
rasinfo.prototype.DrawMap=function()
{
	
	if (to_refillreiter) clearTimeout(to_refillreiter);
	to_refillreiter=setTimeout(refillreiter,200);
	
	// tile limits
	x1=Math.floor((this.centerX-this.width/2)/256)-1;
	x2=Math.floor((this.centerX+this.width/2)/256)+2;
	y1=Math.floor((this.centerY-this.height/2)/256)-1;
	y2=Math.floor((this.centerY+this.height/2)/256)+2;	
	
	// offset var map
	var xo=Math.floor(this.originX-this.centerX+this.width/2);
	var yo=Math.floor(this.originY-this.centerY+this.height/2);
	this.div.style.left=xo+'px';
	this.div.style.top=yo+'px';
	
	if (this.x1o==x1 && this.y1o==y1) {
		return;
	}
	for (x=x1; x < x2; x++) {
		for (y=y1; y < y2; y++) {
			var e=document.getElementById('x'+x+'y'+y);
			if (!e) {
				var xp=x*256-this.originX;
				var yp=y*256-this.originY;
				e=document.createElement("img");
				this.div.appendChild(e);
	    	e.id='x'+x+'y'+y;
	    	e.style.padding='0px';
	    	e.style.margin='0px';
	    	e.style.border='none';
	    	e.style.position='absolute';
				e.style.left=xp+'px';
				e.style.top=yp+'px';
				e.style.height='256px';
				e.style.widtht='256px';
				e.alt='';
				//e.onmousedown=xnot;
				//e.oncontextmenu=xnot;
	    	e.src='tile.php?tile='+this.zoom+','+y+','+x+'&mapid='+this.mapid+'&mt='+this.map_type;
			}
		}
	}

	this.x1o=x1;
	this.y1o=y1;
	
}

function mapdown(event)
{
  if (!event) event=window.event;
  window.status="down: x = " + event.clientX + "/ y = " + event.clientY + " b = "+ event.button;
	m=divid2map[this.id];
	if (m) {
		gmousedown=m;
		m.mouse=1;
		m.xmouse=event.clientX;
		m.ymouse=event.clientY;
	}
	return(false);
}

mdrawtimer=0;
function reDrawMap()
{
	//if (drawtimer) clearTimeout(drawtimer);
	m.DrawMap();
	mdrawtimer=0;
}

mdrawtimer=0;

function mapmove(event)
{
	m=gmousedown;
	if (!m) return(false);
	if (mdrawtimer) return(false);
  if (!event) event=window.event;
  //window.status="move: x = " + event.clientX + "/ y = " + event.clientY + " b = "+ event.button;
	if (m.mouse==1 && (m.xmouse-event.clientX || m.ymouse-event.clientY)) {
		m.centerX+=m.xmouse-event.clientX;
		m.xmouse=event.clientX;
		m.centerY+=m.ymouse-event.clientY;
		m.ymouse=event.clientY;
		mdrawtimer=setTimeout(reDrawMap,5);
		//m.DrawMap();
	}
	return(false);
}

function mapup(event)
{
	m=gmousedown;
	if (m) m.mouse=0;
	return(false);
}

function mapout(event)
{
	if (m) m.mouse=0;
	gmousedown=null;
	return(false);
}

// move pixel
rasinfo.prototype.mmove=function(xm,ym)
{
	this.centerX+=xm;
	this.centerY+=ym;
	this.DrawMap();
}

// maphome
function maphome()
{
	map.runonce=0;
	map.SetPos(map.homeX,map.homeY,map.homeZ);
}


// change level
rasinfo.prototype.mzoom=function(zdir)
{
	var z=this.zoom;
	if (zdir > 0 && this.zoom < 17) {
		z++;
		while (zoom_avail[z]==0) {
			z++;
			if (z > 17) return;
		}
	}
	if (zdir < 0 && this.zoom >  0) {
		z--
		while (zoom_avail[z]==0) {
			z--;
			if (z < 0) return;
		}
	}
	this.zoom=z;
	this.runonce=0;
	this.SetPos(this.GetLonX(this.centerX),this.GetLatY(this.centerY),this.zoom);

}

// change level
rasinfo.prototype.SavePos=function()
{
	this.homeX=this.GetLonX(this.centerX);
	this.homeY=this.GetLatY(this.centerY);
	this.homeZ=this.zoom;
}

function refillreiter()
{
	to_refillreiter=null;
	e=document.getElementById("rc_reiter_none");
	var xlon=map.GetLonX(map.centerX);
	var ylat=map.GetLatY(map.centerY);
	axreiter.connect("i5data.php?reiter="+map.mapid+' '+xlon+' '+ylat+' '+map.linktype+' '+map.linkid,htmlchangejs,e);
}

function reiterrepos(a)
{
	var xlon=map.GetLonX(map.centerX);
	var ylat=map.GetLatY(map.centerY);
	a.href=a.href+'&xc='+xlon+'&yc='+ylat;
}

function loadmappop(linkid,xlon,ylat,ppop)
{

	if (hidepartnerpop==1) return;

	popup_map=Array(linkid,xlon,ylat);
	
	var e=document.getElementById("mappop");
	var xp=map.GetXpos(xlon)-map.originX;
	var yp=map.GetYpos(ylat)-map.originY;

	if (e) {
		e.style.left=xp+'px';
		e.style.top=yp+'px';
		e.style.visibility='visible';
	} else {
		e=document.createElement("div");
   	e.id="mappop";
   	e.style.zIndex='20';
   	e.style.padding='0px';
   	e.style.margin='0px';
   	e.style.border='none';
   	e.style.position='absolute';
		e.style.left=xp+'px';
		e.style.top=yp+'px';
		map.div.appendChild(e);
	}
	axmappop.connect("i5data.php?mappop="+linkid,htmlchange,e);
}

function load_mappopcenter(linkid,xlon,ylat)
{

	popup_center=Array(linkid,xlon,ylat);

	var rasinfo_map_fix_main=document.getElementById('rasinfo_map_fix_main');
	var cedx=Math.floor(rasinfo_map_fix_main.clientWidth/2);
	var cedy=Math.floor(rasinfo_map_fix_main.clientHeight/2);

	e=document.getElementById("mappop_center");
	if (e) {
		e.style.left=cedx+'px';
		e.style.top=cedy+'px';
		e.style.visibility='visible';
	} else {
		e=document.createElement("div");
   	e.id="mappop_center";
   	e.style.zIndex='20';
   	e.style.padding='0px';
   	e.style.margin='0px';
   	e.style.border='none';
   	e.style.position='absolute';
		e.style.left=cedx+'px';
		e.style.top=cedy+'px';
		rasinfo_map_fix_main.appendChild(e);
	}
	
	axmappop.connect("i5data.php?mappop="+linkid,htmlchange,e);
	
}

function deletepartnerpop()
{
	var d=document.getElementById("partnerpop");
	if (d) d.style.visibility='hidden';
	var pall=document.getElementsByTagName('div');
  for(var i=0; i < pall.length; i++) {
		if (pall[i].className=='peon') pall[i].className='pe';
    if (pall[i].className=='ppeon') pall[i].className='ppe';
	}
	var d=document.getElementById("mappop");
	if (d) d.style.visibility='hidden';
	hidepartnerpop=1;
}

function loadpartnerpop(linkid)
{
	var e=document.getElementById("partnerpop");
	if (!e) {
		e=document.createElement("div");
   	e.id="partnerpop";
   	e.style.zIndex='20';
   	e.style.padding='0px';
		e.style.margin='0px';
   	e.style.border='1px solid #00923F';
   	e.style.position='absolute';
		e.style.left='375px';
		e.style.top='85px';
		//e.onclick='document.getElementById("head").removeChild(this)';
		d=document.getElementById("head");
		d.appendChild(e);
	}
	axpartnerpop.connect("i5data.php?partnerpop="+linkid,htmlchangevisible,e);
}

function peon(ele,lid,xlon,ylat,zlevel)
{
	deletepartnerpop();
	hidepartnerpop=0;
	ele.className='peon';
	document.forms.rc_form.elements.linkid.value=lid;
	map.SetPos(xlon,ylat,zlevel);
	map.SavePos();
	loadmappop(lid,xlon,ylat,0);
}

function ppeon(ele,lid,xlon,ylat,zlevel)
{
	deletepartnerpop();
	hidepartnerpop=0;
	ele.className='ppeon';
	document.forms.rc_form.elements.linkid.value=lid;
	map.SetPos(xlon,ylat,zlevel);
	map.SavePos();
	loadpartnerpop(lid);
	loadmappop(lid,xlon,ylat,0);
}


