////////////////////////////////////////////////////////
///// funkcje pomocnicze ////////////////////////////////

function disableSelection(target){
/*
if (typeof target.onselectstart!="undefined") //IE route
	target.onselectstart=function(){return false}
else 
	target.onmousedown=function(){return false}
	
target.style.cursor = "default"*/
}
function enableSelection(target){
/*
if (typeof target.onselectstart!="undefined") //IE route
	target.onselectstart=function(){return true}
else 
	target.onmousedown=function(){return true}
*/	
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
function getElementPosition(elemID){
var offsetTrail = elemID;
var offsetLeft = 0;
var offsetTop = 0;
while (offsetTrail){
offsetLeft += offsetTrail.offsetLeft;
offsetTop += offsetTrail.offsetTop;
offsetTrail = offsetTrail.offsetParent;
}

if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined'){
offsetLeft += document.body.leftMargin;
offsetTop += document.body.topMargin;
}
return {left:offsetLeft,top:offsetTop};
}
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
function mouseDown(e){
  e = e || window.event;
  if (e.preventDefault)
  {
    e.preventDefault();
  }
}
function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** 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)
                handleWheel(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);
window.onmousewheel = document.onmousewheel = wheel;
var isCtrl = false; 

document.onkeyup=function(e){ 
	if(window.event)
	{
		if (window.event.keyCode == 17) { isCtrl=false; }
	}
	else
		if (e.which == 17) isCtrl=false; 
} 
document.onkeydown=function(e){ 
	if(window.event)
	{
		if (window.event.keyCode == 17) isCtrl=true;
	}
	else
		if (e.which == 17) isCtrl=true; 
}
/*
document.oncontextmenu = function(e){
    return false; 
}
*/
////////////////////////////////////////////////////////

function Azymut(xp,yp,xk,yk){
	var dx = xk - xp;
	var dy = yk - yp;
	var a = Math.atan(dy/dx);
	var az = a;
	if ((dy>0)&&(dx<0)) az = Math.PI + a;
	if ((dy<0)&&(dx<0)) az = Math.PI + a;
	if ((dy<0)&&(dx>0)) az = (2*Math.PI) + a;
	
	if ((dy==0)&&(dx>0)) az = 0;
	if ((dy>0)&&(dx==0)) az = Math.PI*0.5;
	if ((dy==0)&&(dx<0)) az = Math.PI;
	if ((dy<0)&&(dx==0)) az = Math.PI*1.5;
	
	return az*(180/Math.PI);
}

function Point(x,y){
   this.x = x;
   this.y = y;
}
function Box(xMin,yMin,xMax,yMax){
	this.xMin = xMin;
	this.yMin = yMin;
	this.xMax = xMax;
	this.yMax = yMax;
	this.crs = '';
	this.isPointIn = function(point)
	{
      if ((point.x>=this.xMin)&&(point.y>=this.yMin)&&(point.x<=this.xMax)&&(point.y<=this.yMax))
      return true;
      else
      return false;
   }
	this.isPointIn = function(x,y)
	{
      if ((x>=this.xMin)&&(y>=this.yMin)&&(x<=this.xMax)&&(y<=this.yMax))
      return true;
      else
      return false;
   }
   
	this.isOverlapedWith = function(box)
	{
		if ((box.xMin>this.xMax)||(box.yMax<this.yMin)||(box.xMax<this.xMin)||(box.yMin>this.yMax))
		return false
		else
		return true;
   }
   this.copyTo = function(box)
   {
		box.xMin = this.xMin;
		box.yMin = this.yMin;
		box.xMax = this.xMax;
		box.yMax = this.yMax;
   }
}
function WMSLayerAtr(name,title,queryable){
	//konstruktor
	this.name = name;
	this.legendUrl =null;
	this.logoUrl =null;
	if (title) this.title = title; else this.title = name;
	if (queryable) this.queryable = queryable; else this.queryable = false;
}
function WMSServiceAtr(){
	this.name = '';
	this.title = '';
	this.layers = new Array();
	this.supportedCRS = new Array();
	this.supportedFormats = new Array();
	this.boxes = new Array();
	this.onload = null;
	this.getCapability = function(wmsServer,version){
		var url = wmsServer.trim();
		
		var adress = url;
		var num = url.lastIndexOf("?");
        var num2 = url.lastIndexOf("&");
		var znak='';
		if (num==url.length){
         //jest dobrze
		}
		else
		if (num2==url.length){
        //jest dobrze
		}
		else{
         if ((num==-1) &&(num2==-1)) znak = '?';
         if (num!=-1) znak='&';
		}
		
		adress+=znak;
		adress+="service=WMS&version="+version+"&request=GetCapabilities";
		this.adres = adress;
		
		adress = adress.replace("?","(znak_zapyt)");
		while(adress.indexOf("&")!=-1){
			adress = adress.replace("&","(znak_and)");
		}
		var wsa = this;
		advAJAX.get({
		url: "geturl.php?url="+adress+"&charset=UTF-8&content=text/xml",
		onSuccess : function(obj)
		{
			wsa.createfromXML(obj.responseXML,version);
			if (wsa.onload!=null) wsa.onload();
		},
		onError : function(obj) { alert("Error: " + obj.status); }
		});

	}
	this.createfromXML = function(xml,version){
	
        var service = xml.getElementsByTagName('Service')[0];
		
		if ((service.getElementsByTagName('Name').length>0)&&(service.getElementsByTagName('Name')[0].hasChildNodes()==true))
        this.name = service.getElementsByTagName('Name')[0].childNodes[0].data;    
		
		if ((service.getElementsByTagName('Title').length>0)&&(service.getElementsByTagName('Title')[0].hasChildNodes()==true))
        this.title = service.getElementsByTagName('Title')[0].childNodes[0].data;
		
		if (version==null) version = '1.1.1';
		
		if (version=='1.1.1')
		var crs = xml.getElementsByTagName('SRS');
		if (version=='1.3.0')
		var crs = xml.getElementsByTagName('CRS');
		
        for (i=0;i<crs.length;i++)
		if (this.isCRS(crs[i].childNodes[0].data)==false)
		this.supportedCRS.push(crs[i].childNodes[0].data);
		
		var formaty = xml.getElementsByTagName('GetMap')[0].getElementsByTagName('Format');
        for (i=0;i<formaty.length;i++) this.supportedFormats.push(formaty[i].childNodes[0].data);
		
		var layers = xml.getElementsByTagName('Layer');
		
		for (i=0;i<layers.length;i++){
            var layer = layers[i];
            var layProp = new WMSLayerAtr();

            layProp.opaque =layer.getAttribute("opaque");            
			layProp.queryable =layer.getAttribute("queryable");

            for (j=0;j<layer.childNodes.length;j++){
            var node = layer.childNodes[j];
            if (node.nodeType==1){
					if ((node.nodeName=='aaa')){
						for (k=0;k<node.childNodes.length;k++){
							var node2 = node.childNodes[k];
						}
					}
					if ((node.nodeName=='Name')){
						var node2 = node.childNodes[0];
						if (node2.nodeType==3)
						layProp.name = node2.data;
					}	
					if ((node.nodeName=='Title')){
						var node2 = node.childNodes[0];
						if (node2.nodeType==3)
						layProp.title = node2.data;
					}
					if ((node.nodeName=='LatLonBoundingBox')){
						layProp.xmin = node.getAttribute("minx");
						layProp.ymin = node.getAttribute("miny");
						layProp.xmax = node.getAttribute("maxx");
						layProp.ymax = node.getAttribute("maxy");						
					}
					if ((node.nodeName=='Style')){
						var node2 = node.getElementsByTagName('LegendURL')[0].getElementsByTagName('OnlineResource')[0];						
						layProp.legendUrl = node2.getAttribute("xlink:href");
					}
					if ((node.nodeName=='Attribution')){
						if(node.getElementsByTagName('LogoURL')[0]){
							var node2 = node.getElementsByTagName('LogoURL')[0].getElementsByTagName('OnlineResource')[0];						
							layProp.logoUrl = node2.getAttribute("xlink:href");
							//alert(layProp.logoUrl);
						}
					}					
				}
            }
            this.layers.push(layProp);
        }
		
		var xmin,ymin,xmax,ymax;
		xmin = this.layers[0].xmin;
		ymin = this.layers[0].ymin;
		xmax = this.layers[0].xmax;
		ymax = this.layers[0].ymax;
		for (i=0;i<this.layers.length;i++){
			if (this.layers[i].xmin<xmin) xmin = this.layers[i].xmin;
			if (this.layers[i].ymin<ymin) ymin = this.layers[i].ymin;
			if (this.layers[i].xmax>xmax) xmax = this.layers[i].xmax;
			if (this.layers[i].ymax>ymax) ymax = this.layers[i].ymax;
		}
		this.xmin = xmin;
		this.ymin = ymin;
		this.xmax = xmax;
		this.ymax = ymax;		
	}
	this.isCRS = function(crs){
		for (j=0;j<this.supportedCRS.length;j++)
		if (this.supportedCRS[j] == crs) return true; 
		return false;
	}
}




var iGeoMapLightObject=null;

function handleWheel(delta){
         if (iGeoMapLightObject!=null){
	if (iGeoMapLightObject.mouseIn==true)
	{
		if (delta > 0)
		iGeoMapLightObject.zoomPlusWheel();
        if (delta < 0)
		iGeoMapLightObject.zoomMinusWheel();
	}
	}
}
function zoomImages(){
	if (iGeoMapLightObject.action==2)
	{
		iGeoMapLightObject.anim += 0.2;
		var wsp = iGeoMapLightObject.levels[iGeoMapLightObject.destLevel]/iGeoMapLightObject.levels[iGeoMapLightObject.level];
		var dx = iGeoMapLightObject.tmpRealX - iGeoMapLightObject.view.x;
		var dy = iGeoMapLightObject.tmpRealY - iGeoMapLightObject.view.y;		
		var x = iGeoMapLightObject.tmpRealX;
		var y = iGeoMapLightObject.tmpRealY;
		
		var wspAnim = 1;
		if (iGeoMapLightObject.destLevel>iGeoMapLightObject.level)
		{
			wspAnim = 1+((wsp-1)*iGeoMapLightObject.anim);
		}
		else
		{
			wspAnim = 1+((wsp-1)*(iGeoMapLightObject.anim));
		}
		
		for (i=0;i<iGeoMapLightObject.services.length;i++)
		{
				var box = iGeoMapLightObject.services[i].tmpBox;
				iGeoMapLightObject.services[i].box.xMin = x-((x-box.xMin)*wspAnim);
				iGeoMapLightObject.services[i].box.xMax = x+((box.xMax-x)*wspAnim);
				iGeoMapLightObject.services[i].box.yMin = y-((y-box.yMin)*wspAnim);
				iGeoMapLightObject.services[i].box.yMax = y+((box.yMax-y)*wspAnim);
				if (iGeoMapLightObject.isAnimation==true)
				iGeoMapLightObject.setDiv(iGeoMapLightObject.services[i].box,iGeoMapLightObject.services[i].div);		
		}
		
		if (iGeoMapLightObject.anim == 1)
		{
			iGeoMapLightObject.action = 0;
			iGeoMapLightObject.block = false;
			
			
			dx/=wsp;
			dy/=wsp;
			iGeoMapLightObject.view.x=iGeoMapLightObject.tmpRealX-dx;
			iGeoMapLightObject.view.y=iGeoMapLightObject.tmpRealY-dy;			
				
			iGeoMapLightObject.level = iGeoMapLightObject.destLevel;
			iGeoMapLightObject.box = iGeoMapLightObject.getBoxFromActualParameters();	
			
			wsp=1;
			for (i=0;i<iGeoMapLightObject.services.length;i++)
			{
				var box = iGeoMapLightObject.services[i].tmpBox;
				iGeoMapLightObject.services[i].box.xMin = x-((x-box.xMin)*wsp);
				iGeoMapLightObject.services[i].box.xMax = x+((box.xMax-x)*wsp);
				iGeoMapLightObject.services[i].box.yMin = y-((y-box.yMin)*wsp);
				iGeoMapLightObject.services[i].box.yMax = y+((box.yMax-y)*wsp);	
				iGeoMapLightObject.setDiv(iGeoMapLightObject.services[i].box,iGeoMapLightObject.services[i].div);	
			}			
			
			iGeoMapLightObject.refresh();
		}		
	}
}
function zoomImagesExtent(){
	if (iGeoMapLightObject.action==4)
	{
		iGeoMapLightObject.anim += 0.1;
		var wsp = iGeoMapLightObject.levels[iGeoMapLightObject.destLevel]/iGeoMapLightObject.levels[iGeoMapLightObject.level];
		var dx = iGeoMapLightObject.tmpRealX - iGeoMapLightObject.view.x;
		var dy = iGeoMapLightObject.tmpRealY - iGeoMapLightObject.view.y;	
		
		var xs = iGeoMapLightObject.view.x;
		var ys = iGeoMapLightObject.view.y;
		
		var x = iGeoMapLightObject.tmpRealX;
		var y = iGeoMapLightObject.tmpRealY;
		
		var dxs = xs-iGeoMapLightObject.tmpRealX;
		var dys = ys-iGeoMapLightObject.tmpRealY;
		
		var wspAnim = 1;
		if (iGeoMapLightObject.destLevel>iGeoMapLightObject.level)
		{
			wspAnim = 1+((wsp-1)*iGeoMapLightObject.anim);
		}
		else
		{
			wspAnim = 1+((wsp-1)*(iGeoMapLightObject.anim));
		}
		
		x = iGeoMapLightObject.tmpRealX-(dxs*iGeoMapLightObject.anim)/wsp;
		y = iGeoMapLightObject.tmpRealY-(dys*iGeoMapLightObject.anim)/wsp;	
		
		//document.getElementById('info').innerHTML+=iGeoMapLightObject.view.x+"  "+x+"<br/>";	
		
		for (i=0;i<iGeoMapLightObject.services.length;i++)
		{
				var box = iGeoMapLightObject.services[i].tmpBox;
				iGeoMapLightObject.services[i].box.xMin = x-((x-box.xMin)*wspAnim);
				iGeoMapLightObject.services[i].box.xMax = x+((box.xMax-x)*wspAnim);
				iGeoMapLightObject.services[i].box.yMin = y-((y-box.yMin)*wspAnim);
				iGeoMapLightObject.services[i].box.yMax = y+((box.yMax-y)*wspAnim);
				if (iGeoMapLightObject.isAnimation==true)
				iGeoMapLightObject.setDiv(iGeoMapLightObject.services[i].box,iGeoMapLightObject.services[i].div);		
		}
		
		if (iGeoMapLightObject.anim >= 1)
		{
			iGeoMapLightObject.block = false;
			iGeoMapLightObject.action = 0;
			iGeoMapLightObject.view.x=iGeoMapLightObject.tmpRealX;
			iGeoMapLightObject.view.y=iGeoMapLightObject.tmpRealY;			
				
			iGeoMapLightObject.level = iGeoMapLightObject.destLevel;
			iGeoMapLightObject.box = iGeoMapLightObject.getBoxFromActualParameters();	
			
			wsp=1;
			for (i=0;i<iGeoMapLightObject.services.length;i++)
			{
				var box = iGeoMapLightObject.services[i].tmpBox;
				iGeoMapLightObject.services[i].box.xMin = x-((x-box.xMin)*wsp);
				iGeoMapLightObject.services[i].box.xMax = x+((box.xMax-x)*wsp);
				iGeoMapLightObject.services[i].box.yMin = y-((y-box.yMin)*wsp);
				iGeoMapLightObject.services[i].box.yMax = y+((box.yMax-y)*wsp);	
				iGeoMapLightObject.setDiv(iGeoMapLightObject.services[i].box,iGeoMapLightObject.services[i].div);
			}			
			
			iGeoMapLightObject.refresh();
		}		
	}
}
function moveImages(){
	if (iGeoMapLightObject.action==5){
		iGeoMapLightObject.anim += 0.1;
		var d = 300/iGeoMapLightObject.scale();
		var wsp = d*iGeoMapLightObject.anim;
		var wspX = wsp*iGeoMapLightObject.dx;
		var wspY = wsp*iGeoMapLightObject.dy;
		
		
		for (i=0;i<iGeoMapLightObject.services.length;i++){
				var box = iGeoMapLightObject.services[i].tmpBox;
				iGeoMapLightObject.services[i].box.xMin = box.xMin+wspX;
				iGeoMapLightObject.services[i].box.xMax = box.xMax+wspX;
				iGeoMapLightObject.services[i].box.yMin = box.yMin+wspY;
				iGeoMapLightObject.services[i].box.yMax = box.yMax+wspY;
				if (iGeoMapLightObject.isAnimation==true)
				iGeoMapLightObject.setDiv(iGeoMapLightObject.services[i].box,iGeoMapLightObject.services[i].div);		
		}
		
		if (iGeoMapLightObject.anim >= 1){
			iGeoMapLightObject.action = 0;
			

			var wspX = d*iGeoMapLightObject.dx;
			var wspY = d*iGeoMapLightObject.dy;
	
			iGeoMapLightObject.view.x-=iGeoMapLightObject.dx*d;
			iGeoMapLightObject.view.y-=iGeoMapLightObject.dy*d;			
			iGeoMapLightObject.box = iGeoMapLightObject.getBoxFromActualParameters();	
			
			for (i=0;i<iGeoMapLightObject.services.length;i++)
			{
				var box = iGeoMapLightObject.services[i].tmpBox;
				iGeoMapLightObject.services[i].box.xMin = box.xMin;
				iGeoMapLightObject.services[i].box.xMax = box.xMax;
				iGeoMapLightObject.services[i].box.yMin = box.yMin;
				iGeoMapLightObject.services[i].box.yMax = box.yMax;
				iGeoMapLightObject.setDiv(iGeoMapLightObject.services[i].box,iGeoMapLightObject.services[i].div);		
			}				
			
			iGeoMapLightObject.block = false;			
			iGeoMapLightObject.refresh();
		}		
	}
}

function Text(x,y,text,color,size,rotate){

	this.text = text;
	
	if (color!=null)
		this.color = color;
	else
		this.color = '#000';
		
	if (size!=null)
		this.size = size;
	else
		this.size = 12;	
		
	if (rotate!=null)
		this.rotate = rotate;
	else
		this.rotate = 0;	
		
	this.x = x;
	this.y = y;

		
	this.system = null;	
	this.raphaelObject = null;
	
	this.setRotate = function(angel){
		this.rotate = angel;
	}
	
	this.createRaphaelObject = function(){

	
		var x = this.system.xToWorkspace(this.x);
		var y = this.system.yToWorkspace(this.y);
		this.raphaelObject = this.system.paper.text(x,y,this.text);
		
		if (this.rotate!=0){
			this.raphaelObject.rotate(this.rotate);
		}
		this.raphaelObject.attr("font-size", this.size);
		this.raphaelObject.attr("fill", this.color);
		this.raphaelObject.attr("font-family", "Arial");
	}
	this.getBox = function(){
		return new Box(x,y,x,y);
	}
	this.removeRahaelObject = function(){
		if (this.raphaelObject!=null) {
			this.raphaelObject.remove();
			this.raphaelObject = null;
		}	
	}
	this.setSystem = function(system){
		this.system = system;
	}
	
}

function Rectangle(scaled,offsetX,offsetY,x,y,width,height,fillColor,fillOpacity,lineColor,lineWidth,lineOpacity){
	this.scaled = scaled;
	this.offsetX = offsetX;
	this.offsetY = offsetY;	
	
	if (fillColor!=null)
		this.fillColor = fillColor;
	else
		this.fillColor = '#fff';
	
	if (fillOpacity!=null)
		this.fillOpacity = fillOpacity;
	else
		this.fillOpacity = 0.5;
		
	if (lineWidth!=null)
		this.lineWidth = lineWidth;
	else
		this.lineWidth = 1;	
		
	if (lineColor!=null)
		this.lineColor = lineColor;
	else
		this.lineColor = '#fff';
	
	if (lineOpacity!=null)
		this.lineOpacity = lineOpacity;
	else
		this.lineOpacity = 0.5;
		
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
		
	this.system = null;	
	this.raphaelObject = null;
	
	this.rotate = 0;
	
	this.setRotate = function(angel){
		this.rotate = angel;
	}
	
	this.createRaphaelObject = function(){
		var width = this.width;
		var height = this.height;
		var offsetX = this.offsetX; 
		var offsetY = this.offsetY; 
		
		if (this.scaled==true){
			this.width = this.width*this.system.scale();
			this.height = this.height*this.system.scale();
			
			offsetX = this.offsetX*this.system.scale();
			offsetY = this.offsetY*this.system.scale();			
		}		
		var x = this.system.xToWorkspace(this.x)-offsetX;
		var y = this.system.yToWorkspace(this.y)-offsetY;
		this.raphaelObject = this.system.paper.rect(x,y,width,height);
		
		if (this.rotate!=0){
			this.raphaelObject.rotate(this.rotate);
		}
		this.raphaelObject.attr({stroke:this.lineColor,fill:this.fillColor,opacity:this.fillOpacity});	
		this.raphaelObject.attr("stroke-opacity",lineOpacity);
		this.raphaelObject.attr("stroke-width",lineWidth);
		
	}
	this.getBox = function(){
		return new Box(x,y,x+width,y+height);
	}
	this.removeRahaelObject = function(){
		if (this.raphaelObject!=null) {
			this.raphaelObject.remove();
			this.raphaelObject = null;
		}	
	}
	this.setSystem = function(system){
		this.system = system;
	}
	
}
function Line(points,color,width){
	this.points = points;
	if (color!=null)
		this.color = color;
	else
		this.color = '#000';
	if (width!=null)
		this.width = width;
	else
		this.width = 1;
	this.system = null;	
	this.raphaelObject = null;

	this.createRaphaelObject = function(){
		if (this.raphaelObject!=null) this.raphaelObject.remove();		
		this.raphaelObject = this.system.paper.path({stroke: this.color,"stroke-width":this.width}).moveTo(this.system.xToWorkspace(this.points[0].x), this.system.yToWorkspace(this.points[0].y));
		var kk;
		for (kk=1;kk<this.points.length;kk++)
		this.raphaelObject.lineTo(this.system.xToWorkspace(this.points[kk].x), this.system.yToWorkspace(this.points[kk].y));
	
	}
	this.getBox = function(){
		var xmin=this.points[0].x,ymin=this.points[0].y,xmax=this.points[0].x,ymax=this.points[0].y;
		for(j456=0;j456<this.points.length;j456++){
			var p = this.points[j456];
			if (p.x<xmin) xmin = p.x;
			if (p.y<ymin) ymin = p.y;
			if (p.x>xmax) xmax = p.x;
			if (p.y>ymax) ymax = p.y;						
		}
		return new Box(xmin,ymin,xmax,ymax);
	}
	this.removeRahaelObject = function(){
		if (this.raphaelObject!=null) {
			this.raphaelObject.remove();
			this.raphaelObject = null;
		}	
	}
	this.setSystem = function(system){
		this.system = system;
	}
}
function Polygon(points,color,opacity){
	this.points = points;
	if (color!=null)
		this.color = color;
	else
		this.color = '#fff';
	
	if (opacity!=null)
		this.opacity = opacity;
	else
		this.opacity = 0.5
		
	this.system = null;	
	this.raphaelObject = null;
	
	var param = {fill:this.color,opacity:this.opacity};	

	this.createRaphaelObject = function(){
		this.raphaelObject = this.system.paper.path(param).moveTo(this.system.xToWorkspace(this.points[0].x), this.system.yToWorkspace(this.points[0].y));
		for (kk=1;kk<this.points.length;kk++)
		this.raphaelObject.lineTo(this.system.xToWorkspace(this.points[kk].x), this.system.yToWorkspace(this.points[kk].y));
	
	}
	this.getBox = function(){
		var xmin=this.points[0].x,ymin=this.points[0].y,xmax=this.points[0].x,ymax=this.points[0].y;
		for(j456=0;j456<this.points.length;j456++){
			var p = this.points[j456];
			if (p.x<xmin) xmin = p.x;
			if (p.y<ymin) ymin = p.y;
			if (p.x>xmax) xmax = p.x;
			if (p.y>ymax) ymax = p.y;						
		}
		return new Box(xmin,ymin,xmax,ymax);
	}
	this.removeRahaelObject = function(){
		if (this.raphaelObject!=null) {
			this.raphaelObject.remove();
			this.raphaelObject = null;
		}	
	}
	this.setSystem = function(system){
		this.system = system;
	}
}
function Geometry(){
	this.system = null;	
	this.objects = new Array();
	this.addObject = function(object){
		this.objects.push(object);
	}
	this.createRaphaelObject = function(){
		var i=0;
		for(i=0;i<this.objects.length;i++)
		this.objects[i].createRaphaelObject();
	}
	this.getBox = function(){
		var box = this.objects[0].getBox();
		
		var xmin=box.xMin,ymin=box.yMin,xmax=box.xMax,ymax=box.yMax;
		var i=0;
		for (i=0;i<this.objects.length;i++){
			box = this.objects[i].getBox();
			if (box.xMin<xmin) xmin = box.xMin;
			if (box.yMin<ymin) ymin = box.yMin;
			if (box.xMax>xmax) xmax = box.xMax;
			if (box.yMax>ymax) ymax = box.yMax;	
		}
		return new Box(xmin,ymin,xmax,ymax);
	}
	this.removeRahaelObject = function(){
		var i=0;
		for (i=0;i<this.objects.length;i++){
			this.objects[i].removeRahaelObject();
		}	
	}
	this.setSystem = function(system){
		var i=0;
		for (i=0;i<this.objects.length;i++){
			this.objects[i].setSystem(system);
		}
	}
}

// name - nazwa serwisu, przydatna przy wyszukiwaniu,
//url - adres wms
//imageFormat - format zwracanego obrazka 'image/png' 'image/jpeg'
//layers - nazwy warstw oddzielone przecinkiem n.p. dzialki,budynki,obreby
//transparent - okresla czy warstwa jest przezroczysta 'true'  'false'
//extentBox - obiekt Box okreslajacy zasieg przestrzenny serwsiu
//visLevelFrom - level od ktorego (wlacznie) serwis jest wyswietlany
//visLevelTo - level do ktorego (wlacznie) serwis jest wyswietlany
//index - kolejnosc  serwisu na mapie, im wiekszy tym bardziej na wierzchu
// version - wersja serwisu '1.1.1' '1.3' 

function WMSLayer(name,querable,visible){
	this.name = name;
	this.querable = querable;
	this.visible = visible;
	this.legendUrl = null;
	this.order = 0;
	this.setVisibility = function(visible){
		this.visible = visible;
		this.service.refresh();
	}
}
function compareLayers( a, b ){
  if ( a.order < b.order )
    return -1;
  if ( a.order > b.order )
    return 1;
  return 0; // a == b
}
function WMSService(name,title,url,imageFormat,transparent,extentBox,visLevelFrom,visLevelTo,index,version){

	var obj = this;	
	this.name = name;
	this.title = title;
	this.url = url.trim();
	this.imageFormat = imageFormat;
	this.layers = new Array();
	this.layIndex=0;
	this.layersString;
	this.querableLayersString;
	this.transparent = transparent;
	this.extentBox = extentBox;
	this.visLevelFrom = visLevelFrom;
	this.visLevelTo = visLevelTo;
	this.index = index;
	this.version = version;
	this.querable = false;
	this.level = 0;
	this.div = null;
	this.box = new Box(0,0,0,0);
	this.visible = true;
	this.functionStartLoading=null;
	this.functionEndLoading=null;
	this.legendImagesLoaded = false;
	this.getCapAtr = null;
	this.projection = null;
	this.addLayer = function(name,querable,visible,order){
		var lay = new WMSLayer(name,querable,visible);
		lay.service = this;
		if (order!=undefined)
		lay.order = order;
		else
		lay.order = this.layIndex;
		this.layers[this.layIndex] = lay;
		this.layIndex++;
		this.createLayersString();
		return lay;
	}
	this.createLayersString = function(){
	   	var s='';
		this.layers.sort(compareLayers);
		for (i=0;i<this.layIndex;i++)
		{
			if (this.layers[i].visible==true)
			{
				s+=','+this.layers[i].name;
			}
		}
		if (s!='')
		this.layersString = s.substring(1);
		else
		this.layersString = '';
	}
	this.createQuerableLayersString = function(){
	   	var s='';
		for (i=0;i<this.layIndex;i++)
		{
			if ((this.layers[i].visible==true)&&(this.layers[i].querable==true))
			{
				s+=','+this.layers[i].name;
			}
		}
		this.querableLayersString = s.substring(1);	
	}
	this.getMapRequest = function(box,width,height,projection){
		var adress = this.url;
		var num = this.url.lastIndexOf("?");
        var num2 = this.url.lastIndexOf("&");
	var znak='';
		if (num==this.url.length)
		{
         //jest dobrze
		}
		else
		if (num2==this.url.length)
		{
        //jest dobrze
		}
		else
		{
         if ((num==-1) &&(num2==-1)) znak = '?';
         if (num!=-1) znak='&';
		}
   
	
		
		adress+=znak;
		adress+="VERSION="+this.version+"&SERVICE=WMS&REQUEST=GetMap";
		adress+="&LAYERS="+this.layersString;
		if (this.projection==null)
		adress+="&SRS="+projection;
		else
		adress+="&SRS="+this.projection;
		if (this.projection==null)
		adress+="&BBOX="+box.xMin+","+box.yMin+","+box.xMax+","+box.yMax;
		else {
			//alert(projection+' '+this.projection);
			var pktMin = transform(box.xMin,box.yMin,projection,this.projection);
			var pktMax = transform(box.xMax,box.yMax,projection,this.projection);
			adress+="&BBOX="+pktMin.x+","+pktMin.y+","+pktMax.x+","+pktMax.y;
		}
		adress+="&WIDTH="+width;
		adress+="&HEIGHT="+height;
		adress+="&TRANSPARENT="+this.transparent;
		adress+="&FORMAT="+this.imageFormat;
		adress+="&styles="+this.przecinkiLayers;
		if (this.layersString!='')
		return adress;
		else
		return '';
	}
	this.getFeatureInfoRequest = function(box,width,height,projection,x,y){
		var adress = this.url;
		var num = this.url.lastIndexOf("?");
        var num2 = this.url.lastIndexOf("&");
		var znak='';
		if (num==this.url.length)
		{
         //jest dobrze
		}
		else
		if (num2==this.url.length)
		{
        //jest dobrze
		}
		else
		{
         if ((num==-1) &&(num2==-1)) znak = '?';
         if (num!=-1) znak='&';
		}
 
		
		adress+=znak;
		adress+="VERSION=1.1.1&SERVICE=WMS&REQUEST=GetFeatureInfo&INFO_FORMAT=text/html";
		adress+="&LAYERS="+this.querableLayersString;
		adress+="&QUERY_LAYERS="+this.querableLayersString;
		adress+="&SRS="+projection;
		adress+="&BBOX="+box.xMin+","+box.yMin+","+box.xMax+","+box.yMax;
		adress+="&WIDTH="+width;
		adress+="&HEIGHT="+height;
		adress+="&X="+x;
		adress+="&Y="+y;
		adress+="&TRANSPARENT=TRUE&FORMAT=image/png";
		adress+="&STYLES="+this.przecinki;
		return adress;
	}	
	this.draw = function(adress,code){	
		var image = new Image();
		var serw = this;
		image.onload =  function()
		{					
			if ((code == serw.actualCode)&&(iGeoMapLightObject.block==false))
			{
				serw.div.innerHTML = '<img style="position:relative;width:100%;height:100%" src="'+adress+'"/>';	
				iGeoMapLightObject.box.copyTo(serw.box);	
				iGeoMapLightObject.setDiv(serw.box,serw.div);
				serw.level = iGeoMapLightObject.level;
				if (serw.functionEndLoading!=null)
				serw.functionEndLoading();
				//alert(adress);
			}

        };
		if (serw.functionStartLoading!=null)
		serw.functionStartLoading();
		image.src = adress;
	}
	this.moveDiv = function(dx,dy){
		this.box.xMin+=dx;
		this.box.yMin+=dy;
		this.box.xMax+=dx;
		this.box.yMax+=dy;
		iGeoMapLightObject.setDiv(this.box,this.div);
	}
	this.markMoveDiv = function(dx,dy){
		var box = new Box(0,0,0,0);
		this.box.copyTo(box);
		box.xMin+=dx;
		box.yMin+=dy;
		box.xMax+=dx;
		box.yMax+=dy;
		iGeoMapLightObject.setDiv(box,this.div);
	}
	this.isAnyLayerQuerable = function(){
		for (i=0;i<this.layIndex;i++)
		{
			if ((this.layers[i].visible==true)&&(this.layers[i].querable==true))
			return true;
		}
		return false;
	}
	this.setLayersOrderWithString = function(text){
		var dt = text.split(',');
		dt.reverse();

		
		for (i=0;i<dt.length;i++)
		{
			var name = dt[i];
			for (j=0;j<this.layers.length;j++)
			{
				if (this.layers[j].name == name) 
				{
				   this.layers[j].order = i;
				}
				
			}
			
		}
		this.layers.sort(compareLayers);
		this.createLayersString();
	}
	this.setVisibility = function(visible){
		this.visible = visible;
		this.system.refresh();
	}
	this.refresh = function(){
		this.system.refresh();
		//alert(this.name);
	}
	this.zoomTo = function(){
		this.system.zoomToExtent(this.extentBox);
	}
	this.getLayerByName = function(name){
		var val = null; 
		if (name!=undefined){
		for (jjj=0;jjj<obj.layIndex;jjj++){
			var lay = obj.layers[jjj];
			if (lay){
				if (lay.name.trim().toLowerCase() == name.trim().toLowerCase()) val = lay;
			} else alert('nie ma warstwy');	
		}
		}
		return val;
	}
	this.removeLayers = function(){
		this.layers = new Array();
		this.layIndex=0;
	}
}
function iGeoMapLight(divId,zIndex,path){
	this.ownPath = path;
	iGeoMapLightObject = this;
	this.mainDiv = document.getElementById(divId);
	this.mainDiv.style.top='1px';
	//while(this.mainDiv.clientHeight==0){}

	disableSelection(document.body);
	
	this.baseZIndex = zIndex; 
	this.layerContainer = document.createElement('div');
	this.workspaceDiv = document.createElement('div');
	this.overlaysDiv = document.createElement('div');
	this.panDiv = null;
	this.infoDiv = null;
	this.minimapDiv = null;
	this.minimapWindow = null;
	this.services = new Array();
	this.box = new Box(0,0,0,0);
	this.minimapBox = new Box(0,0,0,0);
	this.levels = new Array();
	this.levelsDiv = new Array();
	
	this.level = 0;
	this.widthWorkspace = 0;
	this.heightWorkspace = 0;
	this.mousePos = new Point(0,0);
	this.mouseCoor = new Point(0,0);
	this.minimapMouse = new Point();
	this.view = new Point(0,0);
	this.projection = 'EPSG:2180';
	this.isAnimation = false;
	this.getFeatureWindow = null;
	this.block = false;
	// 0 - łapka
	// 1 - zaznaczanie obszarem
	// 2 - get feature info
	// 3 - mairka
	this.button = 0; 
	
	// 0 - nic się nei dzieje
	// 1 - przesówanie planszy myszkš
	// 2 - zoomowanie kółkiem
	// 3 - zoomowanie obszarem	
	// 4 - zoomowanie obszarem, proces przyblizania 
	// 5 - pan przyciskami
	this.action = 0;
	this.onclosegetfeatureinfo = null;
	
	window.setInterval("zoomImages()",10);
	window.setInterval("zoomImagesExtent()",20);
	window.setInterval("moveImages()",20);
	
	this.init = function(){
	
		this.mainDiv.style.zIndex = this.baseZIndex;
		this.mainDiv.appendChild(this.layerContainer);
		this.layerContainer.style.position = 'absolute';
		this.layerContainer.style.left = '0px';
		this.layerContainer.style.top = '0px';
		this.layerContainer.style.right = '0px';
		this.layerContainer.style.bottom = '0px';
		this.layerContainer.style.overflow = 'hidden';
		this.layerContainer.style.zIndex = this.baseZIndex+1;
		this.layerContainer.backgroundColor='#eee';
	
		this.mainDiv.appendChild(this.overlaysDiv);
		this.overlaysDiv.style.position = 'absolute';
		this.overlaysDiv.style.left = '0px';
		this.overlaysDiv.style.top = '0px';
		if (IE){
			this.overlaysDiv.style.left = '1px';
			this.overlaysDiv.style.top = '1px';		
		}
		this.overlaysDiv.style.right = '0px';
		this.overlaysDiv.style.bottom = '0px';
		this.overlaysDiv.style.zIndex = this.baseZIndex+50;	
		
		//alert(this.overlaysDiv.clientHeight);
	
		this.mainDiv.appendChild(this.workspaceDiv);
		this.workspaceDiv.style.position = 'absolute';
		this.workspaceDiv.style.left = '0px';
		this.workspaceDiv.style.top = '0px';
		this.workspaceDiv.style.right = '0px';
		this.workspaceDiv.style.bottom = '0px';
		this.workspaceDiv.style.backgroundImage = 'url('+this.ownPath+'igml/blank.png)';
		this.workspaceDiv.style.zIndex = this.baseZIndex+100;
		this.workspaceDiv.oncontextmenu = function(e){
			return false; 
		}
		if(this.workspaceDiv.addEventListener)
		{
			this.workspaceDiv.addEventListener('mousedown', mouseDown, true);
		}
		
		
		this.extentDiv = document.createElement('div');
		this.workspaceDiv.appendChild(this.extentDiv);
		this.extentDiv.style.position = 'absolute';
		this.extentDiv.style.left = '0px';
		this.extentDiv.style.top = '0px';
		this.extentDiv.style.right = '0px';
		this.extentDiv.style.bottom = '0px';
		this.extentDiv.style.overflow = 'hidden';
		this.extentDiv.style.display = 'none';
		this.extentDiv.style.border = '2px solid #3399ff';
		this.extentDiv.style.backgroundImage = 'url('+this.ownPath+'igml/ext_tlo.png)';
		this.extentDiv.style.zIndex = this.baseZIndex+101;
		
		this.infoDiv = document.createElement('div');
		this.infoDiv.style.position = 'absolute';
		this.infoDiv.style.left = '2px';
		this.infoDiv.style.bottom = '2px';
		this.infoDiv.style.right = '2px';
		this.infoDiv.style.height = '13px';
		this.infoDiv.style.padding = '2px 15px 2px 10px';
		this.infoDiv.style.fontFamily='tahoma';
		this.infoDiv.style.fontSize = '11px';
		this.infoDiv.id = 'infoDiv';
		this.infoDiv.style.backgroundImage='url('+this.ownPath+'igml/tlo.png)';
		this.infoDiv.style.zIndex=this.baseZIndex+101;
		this.mainDiv.appendChild(this.infoDiv);
	}
	this.setCursor = function(){
		if (this.button==0)
		{
			if (this.action==0)
			{
				this.workspaceDiv.style.cursor='default';
			}
			if (this.action==1)
			{
				this.workspaceDiv.style.cursor='move';
			}
			if (this.action==3)
			{
				this.workspaceDiv.style.cursor='crosshair';
			}
		}
		if (this.button==1)
		{
			this.workspaceDiv.style.cursor='crosshair';
			if (this.action==1)
			{
				this.workspaceDiv.style.cursor='move';
			}
		}	
		if (this.button==2)
		{
			this.workspaceDiv.style.cursor='default';
			if (this.action==1)
			{
				this.workspaceDiv.style.cursor='move';
			}
			if (this.action==3)
			{
				this.workspaceDiv.style.cursor='crosshair';
			}
		}	
		if (this.button==3)
		{
			this.workspaceDiv.style.cursor='crosshair';
			if (this.action==1)
			{
				this.workspaceDiv.style.cursor='move';
			}
			if (this.action==3)
			{
				this.workspaceDiv.style.cursor='crosshair';
			}
		}			
	}
	this.setButton = function(button){
		if (button!=this.button)
		{
			if (button==2) this.buttonAfterGetFeature = this.button;
			if (button==3) this.buttonAfterMiarka = this.button;
			
			this.button = button;
			this.setCursor();
			if (button == 2)
			{
				if (this.getFeatureWindow == null)
				{
					this.getFeatureWindow = new MyWindow(null,160,350,370,15,null,1000,200,100,26,true,true,'',true);
					var obj = this;
					this.getFeatureWindow.onclose = function()
					{
						obj.setButton(obj.buttonAfterGetFeature);
						if (obj.onclosegetfeatureinfo != null)
						obj.onclosegetfeatureinfo();
					}
					this.getFeatureWindow.show();
				}
				else
				{
					this.getFeatureWindow.removeAllTabs();
					this.getFeatureWindow.show();
				}

			}
			if (button == 3)
			{
				if (this.miarkaWindow == null)
				{
					this.miarkaWindow = new MyWindow(150,150,250,133,null,null,1000,200,100,26,true,false,'center',true);
					var label = new MyLabel(3,0,80,24,'Pomiar',11,'#fff','bold');
					this.miarkaWindow.addLabel(label);
					var obj = this;
					this.miarkaWindow.onclose = function()
					{
						if (iGeoMapLightObject.miarkaLinia!=null){
							iGeoMapLightObject.removeOverlay(iGeoMapLightObject.miarkaLinia); 
							iGeoMapLightObject.miarkaPoints.length = 0;
						}
						obj.setButton(obj.buttonAfterMiarka);
						if (obj.onclosemiarka != null)
						obj.onclosemiarka();
					}
					var div = document.createElement('div');
						div.style.position = 'absolute';
						div.style.top = '4px';
						div.style.left = '4px';
						div.style.right = '4px';
						div.style.height = '50px';
						iGeoMapLightObject.miarkaWindow.contentDiv.appendChild(div);
						
					div.innerHTML = 'Aby rozpocząć pomiar kliknij na mapę lewym przyciskiem myszy.';
					this.miarkaWindow.infoDiv = div;

					var butCzysc = new MyTextButton(6,50,50,26,'Czyść');
					iGeoMapLightObject.miarkaWindow.contentDiv.appendChild(butCzysc.mainDiv);
					butCzysc.functionOnClick = function(){
						if (iGeoMapLightObject.miarkaLinia!=null){
							iGeoMapLightObject.removeOverlay(iGeoMapLightObject.miarkaLinia); 
							iGeoMapLightObject.miarkaPoints.length = 0;
						}
					}
					
					this.miarkaWindow.show();
				}
				else
				{
					this.miarkaWindow.show();
					if (iGeoMapLightObject.miarkaLinia!=null){
						iGeoMapLightObject.removeOverlay(iGeoMapLightObject.miarkaLinia); 
						iGeoMapLightObject.miarkaPoints.length = 0;
					}
				}

			}			
		}
	}
	this.getBoxFromActualParameters = function(){
		var dx2 = (this.widthWorkspace/this.scale())*0.5;
		var dy2 = (this.heightWorkspace/this.scale())*0.5;
		return new Box(this.view.x-dx2,this.view.y-dy2,this.view.x+dx2,this.view.y+dy2);
	}
	this.pan = function(x,y){
		this.calculateWorkspaceSize();
		this.view.x = x;
		this.view.y = y;
		var box = this.getBoxFromActualParameters();
		this.box = box;
		for (i=0;i<iGeoMapLightObject.services.length;i++)
		{
			iGeoMapLightObject.services[i].div.innerHTML = '';
		}
		this.refresh();
	}
	this.addService = function(service){	
		this.services[this.services.length] = service;
		service.system = this;
		var div = document.createElement('div');
		div.style.position = 'absolute';
		div.style.top = '0px';
		div.style.left = '0px';
		div.style.right = '0px';
		div.style.bottom = '0px';
		div.style.zIndex = this.baseZIndex+2+service.index;
		service.div = div;
		this.layerContainer.appendChild(div);
		this.box.copyTo(service.box);
	}
	this.addLevel = function(level){
		this.levels[this.levels.length] = level;
		this.levels.sort();
	}
	this.scale = function(){
		return this.levels[this.level];
	}
	window.onresize = function(){
		if (iGeoMapLightObject!=null)
		{
			iGeoMapLightObject.calculateWorkspaceSize();
			iGeoMapLightObject.refresh();
		}
	}
	this.workspaceDiv.onmousemove = function(e){
		iGeoMapLightObject.calculateMousePosition(e);
		if (iGeoMapLightObject.action == 1)
		{
			var dx = iGeoMapLightObject.mouseCoor.x - iGeoMapLightObject.tmpRealX;
			var dy = iGeoMapLightObject.mouseCoor.y - iGeoMapLightObject.tmpRealY;

			for (i=0;i<iGeoMapLightObject.services.length;i++)
			{
				iGeoMapLightObject.services[i].markMoveDiv(dx,dy);
			}
		}
		if (iGeoMapLightObject.action == 3)
		{
			if (iGeoMapLightObject.mousePos.x>iGeoMapLightObject.tmpX)
			{			
				iGeoMapLightObject.extentDiv.style.right = (iGeoMapLightObject.widthWorkspace-iGeoMapLightObject.mousePos.x)+'px';
				iGeoMapLightObject.extentDiv.style.left = iGeoMapLightObject.tmpX+'px';
			}
			else
			{
				iGeoMapLightObject.extentDiv.style.left = (iGeoMapLightObject.mousePos.x)+'px';
				iGeoMapLightObject.extentDiv.style.right = (iGeoMapLightObject.widthWorkspace-iGeoMapLightObject.tmpX)+'px';			
			}
			if (iGeoMapLightObject.mousePos.y>iGeoMapLightObject.tmpY)
			{			
				iGeoMapLightObject.extentDiv.style.bottom = (iGeoMapLightObject.heightWorkspace-iGeoMapLightObject.mousePos.y)+'px';
				iGeoMapLightObject.extentDiv.style.top = iGeoMapLightObject.tmpY+'px';
			}
			else
			{
				iGeoMapLightObject.extentDiv.style.top = (iGeoMapLightObject.mousePos.y)+'px';
				iGeoMapLightObject.extentDiv.style.bottom = (iGeoMapLightObject.heightWorkspace-iGeoMapLightObject.tmpY)+'px';			
			}
				
			iGeoMapLightObject.extentDiv.style.display = 'block';	
		}
	}
	this.calculateWorkspaceSize = function(){  
		this.widthWorkspace = this.mainDiv.clientWidth;
		this.heightWorkspace = this.mainDiv.clientHeight; 
		if (this.paper) {
			this.paper.setSize(this.widthWorkspace,this.heightWorkspace);
		} else {
			this.paper = Raphael(this.overlaysDiv,this.widthWorkspace,this.heightWorkspace);
		}
	}
	this.calculateMousePosition = function(e){
		var pozX = 0;
		var pozY = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY)
		{
			pozX = e.pageX;
			pozY = e.pageY;
		}
		else
		if (e.clientX || e.clientY)
		{
			pozX = e.clientX + document.body.scrollLeft;
			pozY = e.clientY + document.body.scrollTop;
		}
		var offsetX = 0;
		var offsetY = 0;
		
		if (this.workspaceDiv!=null)
		offsetX = getElementPosition(this.workspaceDiv).left;
		if (this.workspaceDiv!=null)
		offsetY = getElementPosition(this.workspaceDiv).top;
		
		if (navigator.appVersion.indexOf("MSIE 7.")!=-1)	
		{
			offsetX+=2;
			offsetY+=2;
		}		
		this.mousePos.x = pozX - offsetX;
		this.mousePos.y = pozY - offsetY;
		
		
		this.mouseCoor.x = this.box.xMin+((this.box.xMax-this.box.xMin)*(this.mousePos.x/this.widthWorkspace));
		this.mouseCoor.y = this.box.yMax-((this.box.yMax-this.box.yMin)*(this.mousePos.y/this.heightWorkspace));
		if (this.infoDiv!=null)
		document.getElementById('infoDiv').innerHTML="X: "+Math.round(this.mouseCoor.y)+"  Y: "+Math.round(this.mouseCoor.x);
		
		if (this.minimapDiv!=null)
		{
			this.minimapMouse.x = this.mousePos.x - this.minimapDiv.offsetLeft;
			this.minimapMouse.y = this.mousePos.y - this.minimapDiv.offsetTop;
			//document.getElementById('infoDiv').innerHTML="X: "+Math.round(this.minimapMouse.x)+"  Y: "+Math.round(this.minimapMouse.y);
		}
	}
	this.refresh = function(){
		var listaAdresow = new Array();
				
		this.calculateWorkspaceSize();
		this.box = this.getBoxFromActualParameters();
		
		iGeoMapLightObject.refreshOverlays();
		
		this.drawExtentOnMinimap();
		for(i=0;i<this.levels.length;i++)
		{
			this.levelsDiv[i].style.backgroundImage='url('+this.ownPath+'igml/belka.png)';
		}
		this.levelsDiv[this.levels.length-this.level-1].style.backgroundImage='url('+this.ownPath+'igml/belkap.png)';
		
		for(i=0;i<this.services.length;i++)
		{
			this.services[i].div.style.zIndex = this.baseZIndex+2+this.services[i].index;
		}
		this.setCursor();
		
		for(i=0;i<this.services.length;i++)
		{	
			var przecinki = '';
			var ile = 0;
			var s='';
			for (j=0;j<this.services[i].layers.length;j++)
			{
				if ((this.services[i].layers[j].visible==true))
				{
					s+=','+this.services[i].layers[j].name;
					przecinki+=',';
					ile++;
				}
			}
			if (ile>0){
				this.services[i].layersString = s.substring(1);
				this.services[i].przecinkiLayers = przecinki.substring(1);
			} else {
			this.services[i].layersString = '';
			this.services[i].przecinkiLayers ='';
			}
			
		}
		
		for(i=0;i<this.services.length;i++)
		{	
			//alert('dfdfd '+this.services[i].layersString);
		}	
		
		for(i=0;i<this.services.length;i++)
		if (this.services[i].visible==true){
			var adress = this.services[i].getMapRequest(this.box,this.widthWorkspace,this.heightWorkspace,this.projection);
			//if (debug==true) alert(adress);
			//alert(this.services[i].name+" "+adress);
			if (((this.box.isOverlapedWith(this.services[i].extentBox)==true)||(this.services[i].extentBox.isOverlapedWith(this.box)==true))&&
				(this.services[i].visLevelFrom<=this.level)&&
				(this.services[i].visLevelTo>=this.level)&&(adress!=''))
				{
					this.services[i].div.style.display = 'block';
					
					
					var code = Math.random()*100;
 
					this.services[i].actualCode = code;
					
					this.services[i].draw(adress,code);
					listaAdresow.push(adress);
				}
				else
				{
					this.services[i].div.style.display = 'none';
					this.services[i].div.innerHTML = '';
					if (this.services[i].functionEndLoading!=null)
					this.services[i].functionEndLoading();
				}
		}	else {
					this.services[i].div.style.display = 'none';
					this.services[i].div.innerHTML = '';
					if (this.services[i].functionEndLoading!=null)
					this.services[i].functionEndLoading();		
		}
		
		if (this.onrefresh) this.onrefresh(this.view.x,this.view.y,this.level,listaAdresow,this.widthWorkspace,this.heightWorkspace);
	}		
	this.getFeatureInfo = function(){
		
		for( var i=0;i<this.services.length;i++)
		{	
			var s='';
			var prze = '';
			for ( var j=0;j<this.services[i].layIndex;j++)
			{
				if ((this.services[i].layers[j].visible==true)&&(this.services[i].layers[j].querable==true))
				{
					s+=','+this.services[i].layers[j].name;
					prze+=',';
				}	
			}
			if (s.trim()!=''){
				this.services[i].querableLayersString = s.substring(1);
				this.services[i].przecinki = prze.substring(1);
			}
			else{
				this.services[i].querableLayersString = null;
				this.services[i].przecinki = '';
			}
		}
		
		
		this.getFeatureWindow.removeAllTabs();
		while(this.getFeatureWindow.tabs.length!=0){}
		var xpostep = 0;
		var tabs = new Array();
		var ile = 0;		
		
		for(var i=0;i<this.services.length;i++){
				if ((this.services[i].visible==true)&&
				(this.box.isOverlapedWith(this.services[i].extentBox)==true)&&
				(this.services[i].visLevelFrom<=this.level)&&
				(this.services[i].visLevelTo>=this.level)&&
				(this.services[i].extentBox.isPointIn(this.mouseCoor.x,this.mouseCoor.y)==true)&&
				(this.services[i].querableLayersString!=null))
				{
					if (this.services[i].url.indexOf('geoportal')!=-1) {
						var pktMin = transform(iGeoMapLightObject.box.xMin,iGeoMapLightObject.box.yMin,this.projection,'EPSG:4326');
						var pktMax = transform(iGeoMapLightObject.box.xMax,iGeoMapLightObject.box.yMax,this.projection,'EPSG:4326');
						//alert(pktMin.x+' '+pktMin.y+' '+pktMax.x+' '+pktMax.y);
					
						var box84 = new Box(pktMin.x,pktMin.y,pktMax.x,pktMax.y);
					
						var igml = this;
						var service =igml.services[i];				
						var adress = iGeoMapLightObject.services[i].getFeatureInfoRequest(box84,iGeoMapLightObject.widthWorkspace,iGeoMapLightObject.heightWorkspace,'EPSG:4326',iGeoMapLightObject.mousePos.x,iGeoMapLightObject.mousePos.y);
					} else {				
						var igml = this;
						var service =igml.services[i];				
						var adress = iGeoMapLightObject.services[i].getFeatureInfoRequest(iGeoMapLightObject.box,iGeoMapLightObject.widthWorkspace,iGeoMapLightObject.heightWorkspace,this.projection,iGeoMapLightObject.mousePos.x,iGeoMapLightObject.mousePos.y);
					}
					
					//document.getElementById('konsola').value= adress;
					//alert(adress);

					
				var nameSer = this.services[i].title;
				if (nameSer.length>20){
					nameSer = nameSer.substring(0,20)+'...';
				}
							
							
				tabs[ile] = new MyTab(xpostep,131,24,nameSer,89768);
				igml.getFeatureWindow.addTab(tabs[ile]);
				//alert(nap[1]);
				tabs[ile].contentDiv.innerHTML ='<div style="padding:4px;width:96%;height:96%"><iframe src="'+adress+'" width="100%" height="100%"  frameborder="0" ></iframe></div>';
				tabs[0].set();
				xpostep += 130;
				ile++;
				if ((igml.getFeatureWindow.width-62)<(ile*131)) {
					igml.getFeatureWindow.width = (ile*131)+62;
					igml.getFeatureWindow.mainDiv.style.width = igml.getFeatureWindow.width+'px';
				}
						

				}
		}
				
	}
	this.xToWorkspace = function(x){
		var dx = this.box.xMax - this.box.xMin;
		var wsp = (x-this.box.xMin)/dx;
		return Math.round(wsp*this.widthWorkspace);
	}
	this.yToWorkspace = function(y){
		var dy = this.box.yMax - this.box.yMin;
		var wsp = (this.box.yMax-y)/dy;
		return Math.round(wsp*this.heightWorkspace);
	}
	this.setDiv = function(box,div){
		var left = this.xToWorkspace(box.xMin);
		var top = this.yToWorkspace(box.yMax);
		
		div.style.left = left+'px';
		div.style.top = top+'px';
		
		var right = this.xToWorkspace(box.xMax);
		var bottom = this.yToWorkspace(box.yMin);
		
		div.style.right = (this.widthWorkspace-right)+'px';
		div.style.bottom = (this.heightWorkspace-bottom)+'px';	
	}
	this.workspaceDiv.onmousedown = function(e){
		iGeoMapLightObject.calculateMousePosition(e);
		var isRight = isRightButtonPressed(e);
		
		if (iGeoMapLightObject.action == 0){
			if (isRight==true){
				iGeoMapLightObject.action = 1;				
				iGeoMapLightObject.tmpRealX = iGeoMapLightObject.mouseCoor.x;
				iGeoMapLightObject.tmpRealY = iGeoMapLightObject.mouseCoor.y;			
				iGeoMapLightObject.setCursor();	
				iGeoMapLightObject.block = true;
				iGeoMapLightObject.removeOverlayFromMap();
			}
		
			if (iGeoMapLightObject.button == 0){
				iGeoMapLightObject.removeOverlayFromMap();	
				if (isRight==false)
				{
					if (isCtrl==false)
					{
						iGeoMapLightObject.action = 1;				
						iGeoMapLightObject.tmpRealX = iGeoMapLightObject.mouseCoor.x;
						iGeoMapLightObject.tmpRealY = iGeoMapLightObject.mouseCoor.y;			
						iGeoMapLightObject.setCursor();	
						iGeoMapLightObject.block = true;
					} 
					else
					{
						iGeoMapLightObject.action = 3;				
						iGeoMapLightObject.tmpRealX = iGeoMapLightObject.mouseCoor.x;
						iGeoMapLightObject.tmpRealY = iGeoMapLightObject.mouseCoor.y;
						iGeoMapLightObject.tmpX = iGeoMapLightObject.mousePos.x;
						iGeoMapLightObject.tmpY = iGeoMapLightObject.mousePos.y;				
						iGeoMapLightObject.setCursor();	
						iGeoMapLightObject.block = true;
					}
				}

			}
			if (iGeoMapLightObject.button == 1){	
				iGeoMapLightObject.removeOverlayFromMap();
				if (isRight==false)
				{
					iGeoMapLightObject.action = 3;				
					iGeoMapLightObject.tmpRealX = iGeoMapLightObject.mouseCoor.x;
					iGeoMapLightObject.tmpRealY = iGeoMapLightObject.mouseCoor.y;
					iGeoMapLightObject.tmpX = iGeoMapLightObject.mousePos.x;
					iGeoMapLightObject.tmpY = iGeoMapLightObject.mousePos.y;				
					iGeoMapLightObject.setCursor();	
					iGeoMapLightObject.block = true;
				}
			}
			if (iGeoMapLightObject.button == 2){	
				if (isRight==false)
				{
					if (isCtrl==true)
					{
						iGeoMapLightObject.removeOverlayFromMap();
						iGeoMapLightObject.action = 3;				
						iGeoMapLightObject.tmpRealX = iGeoMapLightObject.mouseCoor.x;
						iGeoMapLightObject.tmpRealY = iGeoMapLightObject.mouseCoor.y;
						iGeoMapLightObject.tmpX = iGeoMapLightObject.mousePos.x;
						iGeoMapLightObject.tmpY = iGeoMapLightObject.mousePos.y;				
						iGeoMapLightObject.setCursor();	
						iGeoMapLightObject.block = true;
					} 
					else
					{
						iGeoMapLightObject.getFeatureInfo();
					}
				}

			}		
			if (iGeoMapLightObject.button == 3){	
				if (isRight==false){
					if (isCtrl==true)
					{
						iGeoMapLightObject.removeOverlayFromMap();
						iGeoMapLightObject.action = 3;				
						iGeoMapLightObject.tmpRealX = iGeoMapLightObject.mouseCoor.x;
						iGeoMapLightObject.tmpRealY = iGeoMapLightObject.mouseCoor.y;
						iGeoMapLightObject.tmpX = iGeoMapLightObject.mousePos.x;
						iGeoMapLightObject.tmpY = iGeoMapLightObject.mousePos.y;				
						iGeoMapLightObject.setCursor();	
						iGeoMapLightObject.block = true;
					} 
					else
					{
						
						
						if (!iGeoMapLightObject.miarkaPoints){
							iGeoMapLightObject.miarkaPoints = new Array();
						}
						
						var x = iGeoMapLightObject.mouseCoor.x;
						var y = iGeoMapLightObject.mouseCoor.y;
						
						iGeoMapLightObject.miarkaPoints.push(new Point(x,y));
						
						if (iGeoMapLightObject.miarkaLinia) iGeoMapLightObject.removeOverlay(iGeoMapLightObject.miarkaLinia);
						
						
						iGeoMapLightObject.miarkaLinia = new Geometry();
						iGeoMapLightObject.miarkaLinia.addObject(new Line(iGeoMapLightObject.miarkaPoints,'#000',1));
						
						
						var j3=0;
						var calkowita= 0;
						for(j3=0;j3<iGeoMapLightObject.miarkaPoints.length;j3++){
							var pk = iGeoMapLightObject.miarkaPoints[j3];
							
							var rec = new Rectangle(false,2,2,pk.x,pk.y,4,4,'#ddd',1,'#666',1,1);
							iGeoMapLightObject.miarkaLinia.addObject(rec);
							
							if (iGeoMapLightObject.miarkaPoints.length>1){
								if ((j3-1)>=0){
									var pp = iGeoMapLightObject.miarkaPoints[j3-1];
									var ps = new Point((pp.x+pk.x)*0.5,(pp.y+pk.y)*0.5);
									
									var az = 360-Azymut(pk.x,pk.y,pp.x,pp.y);
									
									if ((az>270)||(az<90)) az-=180;
									
									var dx = pp.x-pk.x;
									var dy = pp.y-pk.y;
									
									var odl = Math.sqrt(dx*dx+dy*dy);
									calkowita+= odl;
									odl =  Math.round(odl);
									
									var rec = new Rectangle(false,25,10,ps.x,ps.y,50,18,'#eee',1,'#666',1,1);
									rec.setRotate(az);
									iGeoMapLightObject.miarkaLinia.addObject(rec);			
									iGeoMapLightObject.miarkaLinia.addObject(new Text(ps.x,ps.y,''+odl+'m','#000',11,az+180));
								}
							}
						}						
						
						iGeoMapLightObject.miarkaWindow.infoDiv.innerHTML = 'Całkowita długość: '+Math.round(calkowita)+'m';
						
						iGeoMapLightObject.addOverlay(iGeoMapLightObject.miarkaLinia);
						iGeoMapLightObject.refreshOverlays();
						
					}
				}

			}				
		}
	}
	this.workspaceDiv.onmouseup = function(e){
		iGeoMapLightObject.calculateMousePosition(e);
		
		if (iGeoMapLightObject.action == 1)
		{
		    iGeoMapLightObject.action = 0;
			var dx = iGeoMapLightObject.mouseCoor.x - iGeoMapLightObject.tmpRealX;
			var dy = iGeoMapLightObject.mouseCoor.y - iGeoMapLightObject.tmpRealY;
			var pdx = Math.abs(dx*iGeoMapLightObject.scale());
			var pdy = Math.abs(dy*iGeoMapLightObject.scale());
			
			if ((pdx>10)||(pdy>10))
			{

			iGeoMapLightObject.view.x-=dx;
			iGeoMapLightObject.view.y-=dy;
			iGeoMapLightObject.box = iGeoMapLightObject.getBoxFromActualParameters();		

			for (i=0;i<iGeoMapLightObject.services.length;i++)
			{
				iGeoMapLightObject.services[i].moveDiv(0,0);
			}
			
			
			iGeoMapLightObject.refresh();
			}
			else
			{
				iGeoMapLightObject.action = 0;		
				for (i=0;i<iGeoMapLightObject.services.length;i++)
				{
					iGeoMapLightObject.services[i].markMoveDiv(0,0);
				}
				iGeoMapLightObject.refresh();
			}
			iGeoMapLightObject.setCursor();	
			iGeoMapLightObject.block = false;
		}
		if (iGeoMapLightObject.action == 3)
		{
			iGeoMapLightObject.action = 0;	
			iGeoMapLightObject.extentDiv.style.display = 'none';
			var x = (iGeoMapLightObject.tmpRealX + 	iGeoMapLightObject.mouseCoor.x)*0.5;
			var y = (iGeoMapLightObject.tmpRealY + 	iGeoMapLightObject.mouseCoor.y)*0.5;
			    
				
			var dx = iGeoMapLightObject.mouseCoor.x-iGeoMapLightObject.tmpRealX;
			var dy = iGeoMapLightObject.mouseCoor.y-iGeoMapLightObject.tmpRealY;
			
			var pdx = Math.abs(dx*iGeoMapLightObject.scale());
			var pdy = Math.abs(dy*iGeoMapLightObject.scale());
			if ((pdx>10)||(pdy>10))
			{
			
				if (iGeoMapLightObject.tmpRealX>iGeoMapLightObject.mouseCoor.x) dx*=-1;
				if (iGeoMapLightObject.tmpRealY>iGeoMapLightObject.mouseCoor.y) dy*=-1;
				var wsx =iGeoMapLightObject.widthWorkspace/dx;
				var wsy =iGeoMapLightObject.heightWorkspace/dy;

				var scale = wsx;
				if (wsy<wsx) scale = wsy;
				var min = 999999999;
				var level = 0;
				for (i=0;i<iGeoMapLightObject.levels.length;i++)
				{
					if ((Math.abs(scale-iGeoMapLightObject.levels[i])<min))
					{
						min =Math.abs(scale-iGeoMapLightObject.levels[i]);
						level = i;
					}
				}

				iGeoMapLightObject.zoomExtent(x,y,level);
			}
			iGeoMapLightObject.block = false;
		}
	}
	this.workspaceDiv.onmouseover = function(){
		iGeoMapLightObject.mouseIn = true;
	}
	this.workspaceDiv.onmouseout = function(){
		iGeoMapLightObject.mouseIn = false;
		iGeoMapLightObject.block = false;
	}	
	this.zoomPlusWheel = function(){
		this.zoom(this.mouseCoor.x,this.mouseCoor.y,this.level+1);
	}
	this.zoomMinusWheel = function(){
		this.zoom(this.mouseCoor.x,this.mouseCoor.y,this.level-1);
	}	
	this.zoom = function(x,y,level){
		if (this.action == 0)
		{	
			
			if ((level>=0)&&(level<this.levels.length))
			if (this.box.isPointIn(x,y)==true)
			{
				iGeoMapLightObject.removeOverlayFromMap();
				this.action = 2;
				this.tmpRealX = x;
				this.tmpRealY = y;
				this.anim = 0;
				this.destLevel = level;
				for (i=0;i<iGeoMapLightObject.services.length;i++)
				{
					iGeoMapLightObject.services[i].tmpBox = new Box(0,0,0,0);
					iGeoMapLightObject.services[i].box.copyTo(iGeoMapLightObject.services[i].tmpBox);
				}
				iGeoMapLightObject.block = true;
			}
		}
	}
	this.zoomExtent = function(x,y,level){
		if (this.action == 0)
		{	
			if ((level>=0)&&(level<this.levels.length))
			if (this.box.isPointIn(x,y)==true)
			{
				this.action = 4;
				this.tmpRealX = x;
				this.tmpRealY = y;
				this.anim = 0;
				this.destLevel = level;
				for (i=0;i<iGeoMapLightObject.services.length;i++)
				{
					iGeoMapLightObject.services[i].tmpBox = new Box(0,0,0,0);
					iGeoMapLightObject.services[i].box.copyTo(iGeoMapLightObject.services[i].tmpBox);
				}	
				iGeoMapLightObject.block = true;	
			}
		}
	}
	this.zoomToExtent = function(box){
		iGeoMapLightObject.removeOverlayFromMap();
	    var dx = box.xMax-box.xMin;
		var dy = box.yMax-box.yMin;
		var wsx =this.widthWorkspace/dx;
		var wsy =this.heightWorkspace/dy;

		var scale = wsx;
		if (wsy<wsx) scale = wsy;
		var min = 999999999;
		var level = 0;
		for (i=0;i<this.levels.length;i++){
			if (((scale-this.levels[i])<min)&&((scale-this.levels[i])>=0))
			{
				min =scale-this.levels[i];
				level = i;
			}
		}
		this.level = level;
		this.view.x =box.xMin+(dx*0.5);
		this.view.y =box.yMin+(dy*0.5);
		this.box = this.getBoxFromActualParameters();
		for (i=0;i<iGeoMapLightObject.services.length;i++)
		{
			iGeoMapLightObject.services[i].div.innerHTML = '';
		}
		this.refresh();
	}
	this.zoomToMaxExtent = function(){
		var box = new Box(9999999999,9999999999,-9999999999,-9999999999);
		for (i=0;i<this.services.length;i++)
		{
			if (this.services[i].extentBox.xMin<box.xMin) box.xMin = this.services[i].extentBox.xMin;
			if (this.services[i].extentBox.yMin<box.yMin) box.yMin = this.services[i].extentBox.yMin;
			if (this.services[i].extentBox.xMax>box.xMax) box.xMax = this.services[i].extentBox.xMax;
			if (this.services[i].extentBox.yMax>box.yMax) box.yMax = this.services[i].extentBox.yMax;
		}
		//alert(box.xMin+' '+box.yMin+' '+box.xMax+' '+box.yMax);
		this.zoomToExtent(box);
	}
	this.addZoomControl = function(){
		var img1 = new Image();
		img1.src=this.ownPath+'igml/plusp.png';
		var img2 = new Image();
		img2.src=this.ownPath+'igml/minusp.png';
		
		var d = this.levels.length*15;
		
		this.zoomDiv = document.createElement('div');
		this.zoomDiv.style.position = 'absolute';
		this.zoomDiv.style.top = '73px';
		this.zoomDiv.style.left = '25px';
		this.zoomDiv.style.width = '30px';
		this.zoomDiv.style.height = (d+52)+'px';
		this.zoomDiv.style.zIndex=this.baseZIndex+101;
		if(this.zoomDiv.addEventListener)
		{
			this.zoomDiv.addEventListener('mousedown', mouseDown, true);
		}		
		this.mainDiv.appendChild(this.zoomDiv);		
		
		
		
		this.plusDiv = document.createElement('div');
		this.plusDiv.style.position = 'absolute';
		this.plusDiv.style.top = '0px';
		this.plusDiv.style.left = '2px';
		this.plusDiv.style.width = '26px';
		this.plusDiv.style.height = '26px';
		this.plusDiv.style.cursor='pointer';
		this.plusDiv.style.backgroundImage='url('+this.ownPath+'igml/plus.png)';
		this.plusDiv.style.zIndex=this.baseZIndex+102;
		if(this.plusDiv.addEventListener)
		{
			this.plusDiv.addEventListener('mousedown', mouseDown, true);
		}		
		this.zoomDiv.appendChild(this.plusDiv);		
		
		var plus = this.plusDiv;
		this.plusDiv.onmouseover = function()
		{
			plus.style.backgroundImage='url('+iGeoMapLightObject.ownPath+'igml/plusp.png)';
		}
		this.plusDiv.onmouseout = function()
		{
			plus.style.backgroundImage='url('+iGeoMapLightObject.ownPath+'igml/plus.png)';
		}
		this.plusDiv.onmousedown = function()
		{
			plus.style.top='1px';
		}		
		this.plusDiv.onmouseup = function()
		{
			plus.style.top='0px';
			iGeoMapLightObject.zoom(iGeoMapLightObject.view.x,iGeoMapLightObject.view.y,iGeoMapLightObject.level+1);
		}
		
		
		this.minusDiv = document.createElement('div');
		this.minusDiv.style.position = 'absolute';
		this.minusDiv.style.bottom = '1px';
		this.minusDiv.style.left = '2px';
		this.minusDiv.style.width = '26px';
		this.minusDiv.style.height = '26px';
		this.minusDiv.style.cursor='pointer';
		this.minusDiv.style.backgroundImage='url('+this.ownPath+'igml/minus.png)';
		this.minusDiv.style.zIndex=this.baseZIndex+102;
		if(this.minusDiv.addEventListener)
		{
			this.minusDiv.addEventListener('mousedown', mouseDown, true);
		}		
		this.zoomDiv.appendChild(this.minusDiv);	

		var minus = this.minusDiv;
		this.minusDiv.onmouseover = function()
		{
			minus.style.backgroundImage='url('+iGeoMapLightObject.ownPath+'igml/minusp.png)';
		}
		this.minusDiv.onmouseout = function()
		{
			minus.style.backgroundImage='url('+iGeoMapLightObject.ownPath+'igml/minus.png)';
		}
		this.minusDiv.onmousedown = function()
		{
			minus.style.bottom='0px';
		}		
		this.minusDiv.onmouseup = function()
		{
			minus.style.bottom='1px';
			iGeoMapLightObject.zoom(iGeoMapLightObject.view.x,iGeoMapLightObject.view.y,iGeoMapLightObject.level-1);
		}		
		
		for(i=0;i<this.levels.length;i++){
			this.levelsDiv[i] = document.createElement('div');
			var belka = this.levelsDiv[i];
			belka.style.position = 'absolute';
			belka.style.top = ((i*15)+26)+'px';
			belka.style.left = '2px';
			belka.style.width = '26px';
			belka.style.height = '16px';
			//belka.style.cursor='pointer';
			belka.style.backgroundImage='url('+this.ownPath+'igml/belka.png)';
			belka.style.zIndex=this.baseZIndex+102;
			if(belka.addEventListener)
			{
				belka.addEventListener('mousedown', mouseDown, true);
			}		
			this.zoomDiv.appendChild(belka);	

		}
	}
	this.addPanControl = function(){
		var img1 = new Image();
		img1.src=this.ownPath+'igml/panleft.png';
		var img2 = new Image();
		img2.src=this.ownPath+'igml/panright.png';		
		var img3 = new Image();
		img3.src=this.ownPath+'igml/panup.png';		
		var img4 = new Image();
		img4.src=this.ownPath+'igml/pandown.png';		
		
		this.panDiv = document.createElement('div');
		this.panDiv.style.position = 'absolute';
		this.panDiv.style.top = '10px';
		this.panDiv.style.left = '10px';
		this.panDiv.style.width = '60px';
		this.panDiv.style.height = '60px';
		this.panDiv.style.backgroundImage='url('+this.ownPath+'igml/pan.png)';
		this.panDiv.style.zIndex=this.baseZIndex+101;
		if(this.panDiv.addEventListener)
		{
			this.panDiv.addEventListener('mousedown', mouseDown, true);
		}		
		this.mainDiv.appendChild(this.panDiv);
		
		var up = document.createElement('div');
		up.style.position = 'absolute';
		up.style.left = '21px';
		up.style.top = '4px';
		up.style.width = '16px';
		up.style.height = '15px';

		this.panDiv.appendChild(up);
		var pan = this.panDiv;
		up.onmouseover = function()
		{
			pan.style.backgroundImage='url('+iGeoMapLightObject.ownPath+'igml/panup.png)';
		}
		up.onmouseout = function()
		{
			pan.style.backgroundImage='url('+iGeoMapLightObject.ownPath+'igml/pan.png)';
		}
		up.onclick = function()
		{
			iGeoMapLightObject.panMove(0,-1);
		}

		var down = document.createElement('div');
		down.style.position = 'absolute';
		down.style.left = '22px';
		down.style.top = '40px';
		down.style.width = '16px';
		down.style.height = '15px';

		this.panDiv.appendChild(down);
		var pan = this.panDiv;
		down.onmouseover = function()
		{
			pan.style.backgroundImage='url('+iGeoMapLightObject.ownPath+'igml/pandown.png)';
		}
		down.onmouseout = function()
		{
			pan.style.backgroundImage='url('+iGeoMapLightObject.ownPath+'igml/pan.png)';
		}
		down.onclick = function()
		{
			iGeoMapLightObject.panMove(0,1);
		}		

		var left = document.createElement('div');
		left.style.position = 'absolute';
		left.style.left = '5px';
		left.style.top = '22px';
		left.style.width = '15px';
		left.style.height = '16px';

		this.panDiv.appendChild(left);
		var pan = this.panDiv;
		left.onmouseover = function()
		{
			pan.style.backgroundImage='url('+iGeoMapLightObject.ownPath+'igml/panleft.png)';
		}
		left.onmouseout = function()
		{
			pan.style.backgroundImage='url('+iGeoMapLightObject.ownPath+'igml/pan.png)';
		}	
		left.onclick = function()
		{
			iGeoMapLightObject.panMove(1,0);
		}
		
		var right = document.createElement('div');
		right.style.position = 'absolute';
		right.style.left = '40px';
		right.style.top = '22px';
		right.style.width = '15px';
		right.style.height = '16px';

		this.panDiv.appendChild(right);
		var pan = this.panDiv;
		right.onmouseover = function()
		{
			pan.style.backgroundImage='url('+iGeoMapLightObject.ownPath+'igml/panright.png)';
		}
		right.onmouseout = function()
		{
			pan.style.backgroundImage='url('+iGeoMapLightObject.ownPath+'igml/pan.png)';
		}	
		right.onclick = function()
		{
			iGeoMapLightObject.panMove(-1,0);
		}		
	}
	this.panMove = function(dx,dy){
		if (this.action == 0)
		{
			iGeoMapLightObject.removeOverlayFromMap();
			for (i=0;i<iGeoMapLightObject.services.length;i++)
			{
				iGeoMapLightObject.services[i].tmpBox = new Box(0,0,0,0);
				iGeoMapLightObject.services[i].box.copyTo(iGeoMapLightObject.services[i].tmpBox);
			}	
			this.dx = dx;
			this.dy = dy;
			this.action = 5;
			this.anim = 0;
			iGeoMapLightObject.block = true;
		}
	}
	this.addMinimap = function(title,image,xmin,ymin,xmax,ymax,width,height,ownWindow){
		this.minimapBox.xMin = xmin;
		this.minimapBox.yMin = ymin;
		this.minimapBox.xMax = xmax;
		this.minimapBox.yMax = ymax;
		this.widthMiniMap = width;
		this.heightMiniMap = height;
		
		if (ownWindow==false){
			this.minimapDiv = document.createElement('div');
			this.minimapDiv.style.position = 'absolute';
			this.minimapDiv.style.top = '3px';
			this.minimapDiv.style.right = '3px';
			this.minimapDiv.style.height = height+'px';
			this.minimapDiv.style.width = width+'px';
			this.minimapDiv.style.border = '1px solid black';
			this.minimapDiv.style.overflow='hidden';
			//this.minimapDiv.style.backgroundImage='url('+iGeoMapLightObjectPath+'tlo.png)';
			this.minimapDiv.style.zIndex = this.baseZIndex+101;
			this.workspaceDiv.appendChild(this.minimapDiv);
      
			this.minimapDiv.innerHTML = "<img src='"+image+"'/><div id='minimapExtentDiv' style='position:absolute;border:2px solid #6ac5ff'></div>";
      
			this.minimapDiv.onclick = function()
			{

				var mouseminimapx = iGeoMapLightObject.minimapMouse.x;
				var mouseminimapy = iGeoMapLightObject.minimapMouse.y;
			
				var x = iGeoMapLightObject.minimapBox.xMin+(mouseminimapx/iGeoMapLightObject.widthMiniMap)*(iGeoMapLightObject.minimapBox.xMax-iGeoMapLightObject.minimapBox.xMin);
				var y = iGeoMapLightObject.minimapBox.yMax-(mouseminimapy/iGeoMapLightObject.heightMiniMap)*(iGeoMapLightObject.minimapBox.yMax-iGeoMapLightObject.minimapBox.yMin);
        
				iGeoMapLightObject.pan(x,y);
			}
			if(this.minimapDiv.addEventListener)
			{
				this.minimapDiv.addEventListener('mousedown', mouseDown, true);
			}  
		}
		if (ownWindow==true)
		{
		    this.minimapWindow = new MyWindow(null,0,width+32,height+16+10+24,0,null,this.baseZIndex+200,200,100,24,true,false,'',true);
			var label = new MyLabel(3,0,80,24,title,11,'#fff','bold');
			this.minimapWindow.addLabel(label);
			this.mainDiv.appendChild(this.minimapWindow.mainDiv);
			this.minimapWindow.contentDiv.style.backgroundColor = '';
			this.minimapWindow.contentDiv.style.backgroundImage='url('+image+')';
			this.minimapWindow.contentDiv.innerHTML="<div id='minimapExtentDiv' style='position:absolute;border:2px solid #6ac5ff'></div>";
			this.minimapWindow.show();
			this.minimapWindow.contentDiv.onclick = function()
			{
				var mouseminimapx = iGeoMapLightObject.minimapWindow.mouseX;
				var mouseminimapy = iGeoMapLightObject.minimapWindow.mouseY;
			
				var x = iGeoMapLightObject.minimapBox.xMin+(mouseminimapx/iGeoMapLightObject.widthMiniMap)*(iGeoMapLightObject.minimapBox.xMax-iGeoMapLightObject.minimapBox.xMin);
				var y = iGeoMapLightObject.minimapBox.yMax-(mouseminimapy/iGeoMapLightObject.heightMiniMap)*(iGeoMapLightObject.minimapBox.yMax-iGeoMapLightObject.minimapBox.yMin);
        
				iGeoMapLightObject.pan(x,y);
			}
			if(this.minimapWindow.contentDiv.addEventListener)
			{
				this.minimapWindow.contentDiv.addEventListener('mousedown', mouseDown, true);
			}  
		}
	  

	}
	this.drawExtentOnMinimap = function(){
		if ((this.minimapDiv!=null)||(this.minimapWindow != null))
		{
			var box = this.box;
			//document.getElementById('info').innerHTML = box.toString();
			var dWidth = this.minimapBox.xMax - this.minimapBox.xMin;
			var dHeight = this.minimapBox.yMax - this.minimapBox.yMin;

			var left =Math.round(((box.xMin-this.minimapBox.xMin)/dWidth)*this.widthMiniMap);
			var top =-Math.round(((box.yMax-this.minimapBox.yMax)/dHeight)*this.heightMiniMap);

			var right =Math.round(((box.xMax-this.minimapBox.xMin)/dWidth)*this.widthMiniMap);
			var bottom =-Math.round(((box.yMin-this.minimapBox.yMax)/dHeight)*this.heightMiniMap);

			var zazn = document.getElementById('minimapExtentDiv');

			if (zazn!=null)
			{
				zazn.style.top = top+'px';
				zazn.style.left = left+'px';

				zazn.style.width= (right-left)+'px';
				zazn.style.height = (bottom-top)+'px';
			}
		}
	}
	this.showMinimap = function(){
		if (this.minimapWindow!=null)
		{			this.minimapWindow.mainDiv.style.display = 'block';
			
		}
	}
	this.setServicesOrderWithString = function(text){
		var dt = text.split(',');
		dt.reverse();
		
		for (i=0;i<dt.length;i++)
		{
			var name = dt[i];
			for (j=0;j<this.services.length;j++)
			{
				if (this.services[j].name == name) 
				{
				   this.services[j].index = i;
				}
			}
		}
		this.refresh();
	}
	this.getMaxIndex = function(){
		var max = 0;
		for (i23 = 0;i23<this.services.length;i23++){
			if (this.services[i23].index>max) max = this.services[i23].index;
		}
		return max;
	}
	this.addOverlay = function(overlay){
		if (!this.overlays) this.overlays = new Array();
		this.overlays.push(overlay);
		overlay.setSystem(this);
	}
	this.removeOverlay = function(overlay){
		if (this.overlays){
			for (ji=0;ji<this.overlays.length;ji++){
				if (this.overlays[ji] == overlay){
					this.overlays[ji].removeRahaelObject();
					this.overlays.remove(ji);
					return null;
				}
			}			
		}
	}
	this.removeOverlayFromMap = function(){
		if (this.overlays)
		for (iii=0;iii<this.overlays.length;iii++){
			this.overlays[iii].removeRahaelObject();
		}
	}
	this.refreshOverlays = function(){
		iGeoMapLightObject.removeOverlayFromMap();
		if (this.overlays)
		for (i=0;i<this.overlays.length;i++){
			this.overlays[i].createRaphaelObject();		
		}
	}
	
}

