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

partnerid=0;

function printmap(e)
{
    if (partnerid==0) {
        partnerid=map.linkid;
        partnerX=0;
        partnerY=0;
    }
    e.href='print2.php?p='+map.zoom+','+map.centerY+','+map.centerX+','+partnerY+','+partnerX+'&mapid='+map.mapid+'&mt='+map.map_type+'&za='+zoom_avail[map.zoom]+'&linkid='+partnerid;
    return true;
}

function po_getInnerSize(e)
{
    var h=-1;
    var w=-1;
    if (e.innerHeight) {
        h=e.innerHeight;
        w=e.innerWidth;
    } else if (e.clientHeight) {
        h=e.clientHeight;
        w=e.clientWidth;
    }
    return [w,h];
}

function po_getClientSize(e)
{
    var h=-1;
    var w=-1;
    if (window.innerHeight) {
        h=window.innerHeight;
        w=window.innerWidth;
    } else if (document.documentElement.clientHeight) {
        h=document.documentElement.clientHeight;
        w=document.documentElement.clientWidth;
    } else if (document.body.clientHeight) {
        h=document.body.clientHeight;
        w=document.body.clientWidth;
    }
    if (dispmode!=1) {
        w=Math.max(720,w);
        h=Math.max(480,h);
    }
    return [w,h];
}

function po_adjustMinHight(element_name,red)
{
    var e=document.getElementById(element_name);
    var wh=po_getClientSize(e)
    var h=Math.max(120,wh[1]-red);
    if (e) e.style.minHeight=h+'px';
}
function po_adjustHight(element_name,red)
{
    var e=document.getElementById(element_name);
    var wh=po_getClientSize(e)
    var h=Math.max(120,wh[1]-red);
    if (e) e.style.height=h+'px';
}

function po_adjustWidth(element_name,red)
{
    var e=document.getElementById(element_name);
    var wh=po_getClientSize(e)
    var w=Math.max(560,wh[0]-red);
    if (e) e.style.width=w+'px';
}


function po_onLoad(){

    var mh=183;
    var xh=440;
    var xh2=284;

    if (document.all && !window.opera && !window.XMLHttpRequest) {
        var e=document.getElementById('rightmap');
        e.style.left='-20px';
        mh=190;
        xh=450;
    }
    
    // new map

    //po_adjustHight('body',0);

    if (dispmode==1) {
        mh=4;
        po_adjustHight('body',mh);
        po_adjustHight('containermap',mh);
        po_adjustHight('rasinfo_map_fix_main',mh);

        po_adjustWidth('containermap',4);
        po_adjustWidth('rasinfo_map_fix_main',4);

        po_adjustHight('linklist1',mh+40);
        po_adjustHight('linklist2',mh+40);

    } else {

        po_adjustHight('rasinfo_map_fix_main',mh);
        po_adjustWidth('rasinfo_map_fix_main',290);
        po_adjustWidth('rc_reiter_none',286);

        po_adjustHight('containermap',mh);
        po_adjustHight('linklist1',mh);
        po_adjustHight('linklist2',mh);
        po_adjustHight('rightmap',mh);

        po_adjustHight('resultliste',xh+10);
        po_adjustHight('resultlisteb',xh2+10);
        po_adjustHight('resultliste1',xh+10);
        po_adjustHight('resultliste2',xh+10);
        po_adjustHight('resultliste2b',xh2+10);

        po_adjustMinHight('resultlisteborder1',xh+20);
        po_adjustMinHight('resultlisteborder2',xh+20);
        po_adjustMinHight('resultlisteborder2b',xh2+20);

        po_adjustHight('noadvertising',mh+82);
    }

    if (map) {
        if (scaleredrawtimer) clearTimeout(scaleredrawtimer);
        scaleredrawtimer=setTimeout(function() {
            var wh=po_getInnerSize(map.odiv);
            width=256*(1+Math.floor(wh[0]/256));
            height=256*(1+Math.floor(wh[1]/256));
            map.div.style.width=width+'px';
            map.div.style.height=height+'px';
            map.width=wh[0];
            map.height=wh[1];
            var lon=map.GetLonX(map.centerX);
            var lat=map.GetLatY(map.centerY);
            //map.runonce=0;
            map.SetPos(lon,lat,map.zoom);
        },250);
    }
    window.onresize=po_onLoad;
}

scaleredrawtimer=null;
var map=null;
//window.onload=po_onLoad;

dispmode=0;
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;
po_onLoad();

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)
{
    this.odiv=document.getElementById(mapo);
    this.div=document.getElementById(mapi);
    this.ovr=document.getElementById(ovel);
    var wh=po_getInnerSize(this.odiv);
    width=256*(1+Math.floor(wh[0]/256));
    height=256*(1+Math.floor(wh[1]/256));
    this.div.style.width=width+'px';
    this.div.style.height=height+'px';
    // height,width of the outer div!
    this.width=wh[0];
    this.height=wh[1];
    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 (dispmode==0) {
        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+'&za='+zoom_avail[this.zoom];
            }
        }
    }

    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;

    partnerid=linkid;
    partnerX=map.GetXpos(xlon);
    partnerY=map.GetYpos(ylat);

    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;
    partnerid=0;
}

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='275px';
        e.style.top='65px';
        //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);
}



