//Version 1.2
//PGeoPoint
function PGeoPoint(Lat, Lng) {
	this._Lat=Number(Lat);
	this._Lng=Number(Lng);
}
PGeoPoint.prototype.lat=function() {return this._Lat;}
PGeoPoint.prototype.lng=function() {return this._Lng;}

//PGeoRectangle
function PGeoRectangle(TopLeft, BottomRight) {
	this._TopLeft=TopLeft;
	this._BottomRight=BottomRight;
}
PGeoRectangle.prototype.getTopLeft=function() {return this._TopLeft;}
PGeoRectangle.prototype.getBottomRight=function() {return this._BottomRight;}

//PImgPoint
function PImgPoint(X, Y) {
	this._X=Number(X);
	this._Y=Number(Y);
}
PImgPoint.prototype.getX=function() {return this._X;}
PImgPoint.prototype.getY=function() {return this._Y;}


//PGeoRectangle
function PImgRectangle(TopLeft, BottomRight) {
	this._TopLeft=TopLeft;
	this._BottomRight=BottomRight;
}
PImgRectangle.prototype.getTopLeft=function() {return this._TopLeft;}
PImgRectangle.prototype.getBottomRight=function() {return this._BottomRight;}

//-----------------PMap
function PMap(div, mapname) {

	this._ContainerWidth=div.clientWidth;//PMap_getElementSize(div);
	this._ContainerHeight=div.clientHeight;
	
	this.MainContainer=document.createElement("div");
	this.MainContainer.style.width=this._ContainerWidth+"px";
	this.MainContainer.style.height=this._ContainerHeight+"px";
	this.MainContainer.style.position="absolute";	
	this.MainContainer.style.clear="both";
	this.MainContainer.style.overflow="hidden";
	div.appendChild(this.MainContainer);


	this._Container=document.createElement("div");
	this._Container.style.position="relative";
	this.MainContainer.appendChild(this._Container);
	this._ImgCellWidth=256;
	this._ImgCellHeight=256;
	

	this._Container.style.width=this._ContainerWidth+"px";
	this._Container.style.height=this._ContainerHeight+"px";	

	this._Container.appendChild(PMap_CreateDOM(this._ImgCellWidth, this._ImgCellHeight));	

	
	this._Zoom;
	var thisObj=this;
	this.MousePos;
	this._Container.onmousemove=_onmousemove;
	this._Container.onclick=_onmouseclick;
	
	this._Maps=PMap_GetMapsInfo();
	this._MapInfo;
	this._Controls=new Array();	
	this._ShownBound;	
	this._ShiftShown;
	this._CoorCenter;
	//PMap_getElementPosition(this._Container);
	this._ZoomControl=new PZoomControl;
	this._ZoomControl.Draw(this);
	
	this._ScalebarControl=new PScalebarControl;
	this._ScalebarControl.map=this;
	this._ScalebarControl.Draw();

	
	this._MapWindowPos=PMap_getAbsolutePosition(this.MainContainer);
	this._MapFragPos=new Object();
	this._MapFragCoor=new Object();
	this._onClickShift=true;
	
	function _onmousemove(evt) {
		evt=PMapEvent.getEvent();
		var x=evt.pageX-thisObj._MapWindowPos.left+thisObj._MapFragPos.left+thisObj._MapFragCoor.x;
		var y=evt.pageY-thisObj._MapWindowPos.top+thisObj._MapFragPos.top+thisObj._MapFragCoor.y;
		var pt=new PImgPoint(x,y);
		thisObj.MousePos=thisObj.ConvertToGeo(pt);// new PImgPoint(x, y);
	}
	
	function _onmouseclick(evt) {
	    if (thisObj._onClickShift)
	        thisObj.setCenter(thisObj.MousePos, thisObj.getZoom());
	}
	

}

//------------------INIT FUNCTION BEGIN ---------------------
PMap_GetMapsInfo=function() {
	var mp=new Array();
	var doc=PMap_LoadConfigFile();
	
	var root=doc.getElementsByTagName("maps")[0];
	var xM;	
	var im;	
	for(var i=0; i<root.childNodes.length; i++) {
		xM=root.childNodes[i];
		if (xM.nodeType==1) {
			im=new Object();
			im.Lat1=Number(xM.getAttribute("Lat1"));
			im.Lng1=Number(xM.getAttribute("Lng1"));
			im.Lat2=Number(xM.getAttribute("Lat2"));
			im.Lng2=Number(xM.getAttribute("Lng2"));	
			
			im.X1=Number(xM.getAttribute("X1"));
			im.Y1=Number(xM.getAttribute("Y1"));
			im.X2=Number(xM.getAttribute("X2"));
			im.Y2=Number(xM.getAttribute("Y2"));
								
			im.RateX=(im.X2-im.X1)/(im.Lng2-im.Lng1);
			im.RateY=(im.Y2-im.Y1)/(im.Lat2-im.Lat1);

			im.Alias=xM.getAttribute("Alias");			
			mp.push(im);	
		}
	}	
	return mp;		
}

PMap_LoadConfigFile=function() {
	var doc;
	if(window.ActiveXObject)
		doc=new ActiveXObject("Microsoft.XmlDom");
	else doc=document.implementation.createDocument("", "", null)
	if (doc==null) {alert("Error! Your browser does not support Pilion paths maps"); return;}
	doc.async=false;
	doc.load("maps/maps.xml");
	return doc;
}

function PMap_CreateDOM(img_w, img_h) {
	var dv=document.createElement("div");
	dv.style.position="absolute";
	dv.style.width=1024+"px";
	dv.style.height=1024+"px";
	var img;
	for(var y=0; y<1024; y+=img_h) {
		for(var x=0; x<1024; x+=img_w) {
			img=document.createElement("img");
			img.style.width=img_w+"px";
			img.style.height=img_h+"px";
			img.style.position="absolute";
			img.style.top=y+"px";
			img.style.left=x+"px";
			//img.border="1";
			dv.appendChild(img);
		}		
	}
	return dv;
}

//------------------INIT FUNCTION END---------------------

//------------------PROPERTY---------------------
PMap.prototype.getContainer=function() {return this._Container;}

PMap.prototype.getDOMObject=function() {return this._Container;}
PMap.prototype.getCoorCenter=function() {return this._CoorCenter;}
PMap.prototype.getZoom=function() {return this._Zoom;}
PMap.prototype.getWidth=function() {return this._ContainerWidth;}
PMap.prototype.getHeight=function() {return this._ContainerHeight;}
PMap.prototype.setShiftOnClick=function(vl) {this._onClickShift=vl;}

//------------------METHODS------------------
PMap.prototype.setCenter=function(Coor, zoom) {
	this._Zoom=zoom;	
	this.CoorCenter=Coor;
	this.LoadMapInfo();	
	var ic=this.ConvertToImg(Coor);
	var x1=(Math.floor(ic.getX()/this._ImgCellWidth)-2)*this._ImgCellWidth;
	var y1=(Math.floor(ic.getY()/this._ImgCellHeight)-2)*this._ImgCellHeight;
	this._MapFragCoor.x=x1; this._MapFragCoor.y=y1;
	this.LoadImages();
	
	this._ShiftShown=new PImgPoint(this.getShiftLeft(ic), this.getShiftTop(ic));
	
	this._MapFragPos.top=this._ShiftShown.getY();
	this._MapFragPos.left=this._ShiftShown.getX();	
	
	this._Container.style.top=-this._ShiftShown.getY()+"px";
	this._Container.style.left=-this._ShiftShown.getX()+"px";
	
	this._ScalebarControl.setImage('maps/scalebar'+zoom+'.gif');
	
	this._ZoomControl.highlightZoom(zoom);
	this.Redraw();
	
	//alert(1);
}


PMap.prototype.getShiftTop=function(ic) {
	return eval(ic.getY()-this._MapFragCoor.y-this._ContainerHeight/2);
}

PMap.prototype.getShiftLeft=function(ic) {
	return eval(ic.getX()-this._MapFragCoor.x-this._ContainerWidth/2);

}

PMap.prototype.LoadImages=function() {
	var src;
	var ImgIndex=0;
	var prnt=this._Container.childNodes[0];
	for(y=0; y<4; y++) {
		for(x=0; x<4; x++) {
			src="mapservice.aspx?x="+eval(x*this._ImgCellWidth+this._MapFragCoor.x)+
			"&y="+eval(y*this._ImgCellHeight+this._MapFragCoor.y)+
			"&z="+this._Zoom+'&444=603'+"&ln="+language;
			//alert(src);
			prnt.childNodes[ImgIndex].src=src;
			//prnt.childNodes[ImgIndex].border=1;
			ImgIndex++;
		}	
	}
}

PMap.prototype.LoadMapInfo=function() {
	var mp=null;
	this._MapInfo=null;
	for(var i=0; i<this._Maps.length; i++) {
		if (this._Maps[i].Alias==this._Zoom) this._MapInfo=this._Maps[i];
	}
	if (this._MapInfo==null) {alert("Can not find info about map for zoom: "+this._Zoom); return;}
	
	//this.RateX=mp.RateX;
	//this.RateY=mp.RateY;	
	//this.TopLeftMap=new PGeoPoint(mp.Lat1, mp.Lng1);	
}

PMap.prototype.ConvertToImg=function(point) {
	var Y=(point.lat()-this._MapInfo.Lat1)*this._MapInfo.RateY;
	var X=(point.lng()-this._MapInfo.Lng1)*this._MapInfo.RateX;
	var pt=new PImgPoint(X, Y)
	return pt;
}

PMap.prototype.ConvertToGeo=function(point) {
	return new PGeoPoint((point.getY()-this._MapInfo.Y1)/this._MapInfo.RateY+this._MapInfo.Lat1,
				(point.getX()-this._MapInfo.X1)/this._MapInfo.RateX+this._MapInfo.Lng1);
}


PMap.prototype.ConvertToWindow=function(point) {
	var imgCoor=this.ConvertToImg(point);
	return new PImgPoint(imgCoor.getX()-this._MapFragCoor.x,
	imgCoor.getY()-this._MapFragCoor.y);
}



PMap.prototype.getMapEvent=function() {
	var me=new PMapEvent(this);
	return me;
}

PMap.prototype.Redraw=function() {
	for(var i=0; i<this._Controls.length; i++) 
	this._Controls[i].Draw(this);
}

PMap.prototype.addControl=function(ctrl) {
	this._Controls.push(ctrl);
	ctrl.map=this;
	ctrl.Draw();
}

PMap.prototype.sendControlsToBack=function() {
    for(var i=0; i<this._Controls.length; i++) this._Controls[i].sendControlsToBack();
}



//-------------------PMarker
function PMarker(point) {
	this.Point=point;
	this.Ctrl=null;
	this.ctrlLabel=null;
	this.DbId=0;
	this.index=0;
	this.map=null;
	this.Icon="img/star.gif";
	this.Caption="";
	this._Visible=true;
	this._ctrlMarker=null
	
	function bringFront() {
	    alert(1);
	    theObj.map.sendControlsToBack();
	    theObj.bringToFront();
	    PMapEvent.addEventHandler(mk, "onmouseover", new Function("e", "redir(e, "+i+");")); 
	}
}

PMarker.prototype.setPoint=function(coor) {
	this.Point=coor;
	if (map) this.Draw(map);
}

PMarker.prototype.Draw=function() {
		if (this.Ctrl) this.Hide();
		if (!this._Visible) return;
		this.CreateControls();
		var p=map.ConvertToWindow(this.Point);
		
		this._ctrlMarker.style.top=p.getY()-50+"px";
		this._ctrlMarker.style.left=p.getX()-12+"px";
		map.getContainer().appendChild(this._ctrlMarker);
		//alert(PMapEvent);
		//PMapEvent.addEventHandler(this._ctrlMarker, "onclick", PMarker.bringFront); 

		this.Ctrl.style.top=p.getY()-37-this.Ctrl.height/2+"px";
		this.Ctrl.style.left=p.getX()-this.Ctrl.width/2+"px";
		map.getContainer().appendChild(this.Ctrl);
		
		if (this.ctrlLabel) {
			this.ctrlLabel.style.top=p.getY()+"px";
			this.ctrlLabel.style.left=p.getX()+"px";
			map.getContainer().appendChild(this.ctrlLabel);
		}
		
}

PMarker.prototype.CreateControls=function() {
	if (!this.Ctrl) {
	
	    this._ctrlMarker=document.createElement("img");
	    this._ctrlMarker.src='img/marker.gif';
	    this._ctrlMarker.style.zIndex=300;
	    this._ctrlMarker.style.position="absolute";

		var img=document.createElement("img");
		img.src=this.Icon;
		img.style.zIndex=300;
		img.style.position="absolute";
		this.Ctrl=img;
		
		if (this.Caption!='') {
			var div=document.createElement("div");
			div.style.position="absolute";
			div.innerHTML=this.Caption;
			div.className="maplabel";
			div.style.zIndex=300;
			this.ctrlLabel=div;
		}
	}
}

PMarker.prototype.Hide=function() {
        try{
		if (this.Ctrl) map.getContainer().removeChild(this.Ctrl);
		if (this.ctrlLabel) map.getContainer().removeChild(this.ctrlLabel);
		if (this._ctrlMarker) map.getContainer().removeChild(this._ctrlMarker);		
		}catch(e){}
}

PMarker.prototype.bringToFront=function() {
    this.Ctrl.style.zIndex=101;
    this.ctrlLabel.style.zIndex=102;
    this._ctrlMarker.style.zIndex=100;
}

PMarker.prototype.sentToBack=function() {
    this.Ctrl.style.zIndex=51;
    this.ctrlLabel.style.zIndex=52;
    this._ctrlMarker.style.zIndex=50;
}

PMarker.prototype.setVisible=function(vl) {this._Visible=vl; this.Draw();}
PMarker.prototype.getDOMObject=function() {return this.Ctrl;}


//------------------PZoomControl-------------
function PZoomControl() {
	theObj=this;
	this.map;
	this.lbls=new Array();
	this.ChangeZoom1=function (e) {theObj.ChangeZoom(1, e); return false;}
	this.ChangeZoom2=function (e) {theObj.ChangeZoom(2, e); return false;}
	this.ChangeZoom3=function (e) {theObj.ChangeZoom(3, e); return false;}

	
	this.ChangeZoom=function(zm, e) {
        theObj.setZoom(zm);
		if (e && e.preventDefault) e.preventDefault();
		if (!e.preventDefault) e.returnValue=false;
		return false;
	} 		

}

PZoomControl.prototype.setZoom=function(zm) {
        this.highlightZoom(zm);
		var pt=this.map.CoorCenter;	
		this.map.setCenter(pt, zm);
}

PZoomControl.prototype.highlightZoom=function(zm) {
	    for(var i=1; i<4; i++) {this.lbls[i].className="mapzoomlabel";}
	    this.lbls[zm].className="mapzoomlabelselected";
}


PZoomControl.prototype.Create=function() {	
	var root=document.createElement("div");
	this.lbls[1]=this.CreateZoomLabel("Zoom1", this.ChangeZoom1);
	this.lbls[2]=this.CreateZoomLabel("Zoom2", this.ChangeZoom2);
	this.lbls[3]=this.CreateZoomLabel("Zoom3", this.ChangeZoom3);
	
	root.appendChild(this.lbls[1]);
	root.appendChild(this.lbls[2]);
	root.appendChild(this.lbls[3]);
	root.style.top="10px";
	root.style.left="10px";
	root.style.position="absolute";
	return root;
}

PZoomControl.prototype.CreateZoomLabel=function(title, fn) {
	var z1=document.createElement("div");
	z1.className="mapzoomlabel";
	var az1=document.createElement("a");
	az1.href="#";
	az1.innerHTML=title;
	PMapEvent.addEventHandler(az1, "click", function(e) {fn(e)});
	z1.appendChild(az1);
	return z1;	
}

PZoomControl.prototype.Draw=function(map) {
	this.map=map;
	var dv=this.Create();
	map.MainContainer.appendChild(dv);		
}


//------------------PScalebarControl-------------
function PScalebarControl() {
	this.map=null;
	this.Ctrl;
	this._img;		
	
}

PScalebarControl.prototype.setImage=function(imgName) {
   this._img.src=imgName;
}

PScalebarControl.prototype.Create=function() {	
	var root=document.createElement("div");
	this._img=document.createElement("img");
	this._img.src="maps/scalebar1.gif";
	root.appendChild(this._img);
	root.style.zIndex=500;
	root.style.position="absolute";
	return root;
}


PScalebarControl.prototype.Draw=function() {
	this.Ctrl=this.Create();
	this.Ctrl.style.left="10px";
	this.Ctrl.style.top=this.map.getHeight()-30+"px";
	this.map.MainContainer.appendChild(this.Ctrl);		
}



//-------------------PMapEvent--------
var PMapEvent=new Object();
PMapEvent.addEventHandler = function (Item, sEventType, fnHandler) {
	var oTarget=Item;
	if (Item.getDOMObject) oTarget=Item.getDOMObject();
    if (oTarget.addEventListener) {
        oTarget.addEventListener(sEventType, fnHandler, false);
    } else if (oTarget.attachEvent) {
        oTarget.attachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = fnHandler;
    }
};
        
PMapEvent.removeEventHandler = function (oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else { 
        oTarget["on" + sEventType] = null;
    }
};

PMapEvent.formatEvent = function (oEvent) {
    if (isIE && isWin) {
        oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
        oEvent.eventPhase = 2;
        oEvent.isChar = (oEvent.charCode > 0);
        //debug_write(document.documentElement.scrollTop);
        oEvent.pageX = oEvent.clientX+document.documentElement.scrollLeft;
        oEvent.pageY = oEvent.clientY+document.documentElement.scrollTop;        
        oEvent.preventDefault = function () {
            this.returnValue = false;
        };

        if (oEvent.type == "mouseout") {
            oEvent.relatedTarget = oEvent.toElement;
        } else if (oEvent.type == "mouseover") {
            oEvent.relatedTarget = oEvent.fromElement;
        }

        oEvent.stopPropagation = function () {
            this.cancelBubble = true;
        };

        oEvent.target = oEvent.srcElement;
        oEvent.time = (new Date).getTime();
    }
    return oEvent;
};

PMapEvent.getEvent = function() {
    if (window.event) {
        return this.formatEvent(window.event);
    } else {
        return PMapEvent.getEvent.caller.arguments[0];
    }
};


//-------------------------Other functions-----------------
var debug_message="";
function debug_write(msg) {
	var obj=document.getElementById("debugwindow");
	if (obj) {
		debug_message+=msg+"</br>";
		obj.innerHTML=debug_message;
	}
}

function PMap_getElementSize(elm) {
	var w=elm.clientWidth;
	return w;
}

function PMap_getAbsolutePosition(obj) {

	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return {left:curleft, top:curtop};
}


function PMap_getElementPos(elm) {
	var pos=new Object(); var tmp;
	tmp=elm.style.left; alert(tmp); pos.left=tmp.substr(0, tmp.length-2);
	tmp=elm.style.top; pos.top=tmp.substr(0, tmp.length-2);
	return pos;
}






            