var mainMap = {	
	
	/*****Globalne spremenljivke******/
	zoomLevel: 0,
	zoomLimit: 14,
	width: -1,
	DSLDOn: "OFF",
	height: -1,
	tip_poti: -1,
	
	layers: new Array(),
	identifyItems: new Array(),
	no_layers: 0,
	xmlhttp: new XMLHttpRequest(),
	
	centerPoint: {
		x: 0,
		y: 0
	},
	resolution: -1,
	
	extent:{	/****Trenutni extent*******/
		minx: -180, 
		miny: -90,
		maxy: 180, 
		maxx: 90
	},
	
	orgExtent: { /*****Originalni extent je tisti, na katerega so vezane omejitve....********/
		minx: -180, 
		miny: -90,
		maxy: 180, 
		maxx: 90
	},
	
	baseLayer: {
		name:		"",
		address: 	"",
		layers:		"",
		service: 	"",
		version:	"",
		request:	"",
		style:		"",
		exceptions:	"",
		format:		"",
		src:		""
	},
	
	/*****END: Globalne spremenljivke******/
	
	/******Basic Funkcijonalnosti**************/
	setBaseLayer: function(newBaseLayer){
		this.baseLayer  = newBaseLayer;
	},
	addLayer: function(layer){
		this.layers[this.no_layers] = layer;
		this.no_layers++;
	},
	setOriginalExtent: function(newOriginalExtent)
	{
		this.orgExtent.maxx = newOriginalExtent.maxx;
		this.orgExtent.minx = newOriginalExtent.minx;
		this.orgExtent.maxy = newOriginalExtent.maxy;
		this.orgExtent.miny = newOriginalExtent.miny;
		this.setExtent(newOriginalExtent);
	},
	init: function() /*****Funkcija inicializira vse pomembne podatke*******/
	{
		var dWidth = this.extent.maxx - this.extent.minx;
		this.resolution = dWidth/this.width;
		
		this.centerPoint.x = this.extent.minx + Math.round((this.extent.maxx - this.extent.minx)/2) ;
		this.centerPoint.y = this.extent.miny + Math.round((this.extent.maxy - this.extent.miny)/2) ;
		var a = 0;
	},
	setExtent: function( newExtent ){
		this.extent = newExtent;
	},
	
	setSize: function(newWidth, newHeight){
		this.width = newWidth;
		this.height = newHeight;
		var newDiv = document.getElementById("mapImage");
		newDiv.style.width = newWidth+"px";
		newDiv.style.height = newHeight +"px";
		
	},
	limitExtent: function()
	{
/*

		var dx = 0;
		var dy = 0;
		
		if(this.orgExtent.minx > this.extent.minx)
			dx = this.orgExtent.minx - this.extent.minx
		else if (this.orgExtent.maxx < this.extent.maxx)
			dx = this.orgExtent.maxx - this.extent.maxx
		
		this.extent.minx +=dx;
		this.extent.maxx +=dx;
		
		if(this.orgExtent.miny > this.extent.miny)
			dy = this.orgExtent.miny - this.extent.miny
		else if (this.orgExtent.maxy < this.extent.maxy)
			dy = this.orgExtent.maxy - this.extent.maxy
		
		this.extent.miny +=dy;
		this.extent.maxy +=dy;
		*/
	},
	
	draw: function(){
		this.init();
		this.limitExtent();
		var visibleLayers = this.baseLayer.layers;
		
		var i = 0 ;
		for( i = 0 ; i < this.no_layers ; i++)
		{
			if(this.layers[i].visible == true)
			{
				visibleLayers = visibleLayers +"," +this.layers[i].layers;
			}
		}
		
		var newDiv = document.getElementById("mapImage");
		var newSrc ="LAYERS="		+	escape(visibleLayers)				+"&"+
					"SERVICE=" 		+ 	escape(this.baseLayer.service)		+"&"+
					"VERSION=" 		+ 	escape(this.baseLayer.version)		+"&"+
					"REQUEST=" 		+ 	escape(this.baseLayer.request)		+"&"+
					"STYLES=" 		+ 	escape(this.baseLayer.style)		+"&"+
					"EXCEPTIONS="	+ 	escape(this.baseLayer.exceptions)	+"&"+
					"FORMAT="		+ 	escape(this.baseLayer.format)		+"&"+
					"SRS=" 			+ 	escape(this.baseLayer.src)			+"&"+
					"BBOX=" 		+ 	this.extent.minx+","+this.extent.miny+","+this.extent.maxx+","+this.extent.maxy	+ "&"+
					"WIDTH=" 		+ 	this.width							+"&"+ 
					"HEIGHT=" 		+ 	this.height	;
		if(this.DSLDOn.indexOf("OFF")==-1)
			newDiv.src = this.baseLayer.address+"?"+newSrc+this.DSLDOn;
		else
			newDiv.src = this.baseLayer.address+"?"+newSrc;
		
	},
	/******END: Basic Funkcijonalnosti**************/
	
	/******Uporabniške funkcijonalnosti*****/
	zoomStepIn: function()
	{
		
    if( this.zoomLevel < this.zoomLimit)
		{
      
			this.zoomLevel++;
			this.resolution /= 2.5;
			var maxx = this.centerPoint.x + this.resolution*this.width/2;
			var minx = this.centerPoint.x - this.resolution*this.width/2;
			
			var maxy = this.centerPoint.y + this.resolution*this.height/2;
			var miny = this.centerPoint.y - this.resolution*this.height/2;
			
			this.extent.minx = minx;
			this.extent.maxx = maxx;
			this.extent.miny = miny;
			this.extent.maxy = maxy;
      
      var dx = (window.xmax + window.xmin)/2;
    var dy = (window.ymax + window.ymin)/2;
    
    var posw, posh;
   // alert("dx:" + dx +"\n" + "dy:" + dy + "\n" + "minx:" + this.extent.minx + "\n" +"maxx:" +  this.extent.maxx +"\n" + "miny:" + this.extent.miny + "\n" + "maxy:" +this.extent.maxy);
   
     var showpos = document.getElementById("idShowPosition");
    showpos.style.display = 'block';
    if (dx >= this.extent.minx && dx <= this.extent.maxx)// && dy <= this.extent.miyx && dy >= this.extent.maxy)
    {                  
      var rx = (dx - this.extent.minx)/(this.extent.maxx - this.extent.minx);
      var ry = (dy - this.extent.miny)/(this.extent.maxy - this.extent.miny);
   
      var wWidth = window.wWidth;
      var wHeight = window.wHeight;
      var toppos =  ry*wHeight;
      var leftpos=  rx*wWidth;
      
     // alert(leftpos + "\n" + toppos);
      if (leftpos < wWidth - 13 && leftpos > 0 && toppos > 13 && toppos < wHeight)
      {
        showpos.style.top = wHeight - toppos;
        showpos.style.left = leftpos;   
      }
      else
        showpos.style.display = 'none';
    }
    else
      showpos.style.display = 'none';
		if (showGPSPosition == false)
      showpos.style.display = 'none';
			this.draw();
		}
	},
	zoomStepOut: function()
	{
		if(this.zoomLevel>0)
		{
			this.zoomLevel--;
			this.resolution *= 2.5;
			var maxx = this.centerPoint.x + this.resolution*this.width/2;
			var minx = this.centerPoint.x - this.resolution*this.width/2;
			
			var maxy = this.centerPoint.y + this.resolution*this.height/2;
			var miny = this.centerPoint.y - this.resolution*this.height/2;
			
			this.extent.minx = minx;
			this.extent.maxx = maxx;
			this.extent.miny = miny;
			this.extent.maxy = maxy;
      
      var dx = (window.xmax + window.xmin)/2;
    var dy = (window.ymax + window.ymin)/2;
    
    var posw, posh;
   // alert("dx:" + dx +"\n" + "dy:" + dy + "\n" + "minx:" + this.extent.minx + "\n" +"maxx:" +  this.extent.maxx +"\n" + "miny:" + this.extent.miny + "\n" + "maxy:" +this.extent.maxy);
   
      var showpos = document.getElementById("idShowPosition");
    showpos.style.display = 'block';
    if (dx >= this.extent.minx && dx <= this.extent.maxx)// && dy <= this.extent.miyx && dy >= this.extent.maxy)
    {
            
      var rx = (dx - this.extent.minx)/(this.extent.maxx - this.extent.minx);
      var ry = (dy - this.extent.miny)/(this.extent.maxy - this.extent.miny);
   
      var wWidth = window.wWidth;
      var wHeight = window.wHeight;
      var toppos =  ry*wHeight;
      var leftpos=  rx*wWidth;
      
     // alert(leftpos + "\n" + toppos);
      if (leftpos < wWidth - 13 && leftpos > 0 && toppos > 13 && toppos < wHeight)
      {
        showpos.style.top = wHeight - toppos;
        showpos.style.left = leftpos;   
      }
      else
        showpos.style.display = 'none';
    }
    else
      showpos.style.display = 'none';
		 if (showGPSPosition == false)
      showpos.style.display = 'none';
			this.draw();
		}
	},
	zoomToExtent: function(minx,miny,maxx,maxy)
	{
		var dWidth = maxx - minx;
		this.resolution = dWidth/this.width;
		if(this.resolution>0)
		{
			this.zoomLevel = 7;
			
			this.extent.minx = minx;
			this.extent.maxx = maxx;
			this.extent.miny = miny;
			this.extent.maxy = maxy;
			this.centerPoint.x = ((maxx - minx)/2)+minx;
			this.centerPoint.y = ((maxy - miny)/2)+miny;
			
			this.draw();
		}
		else
			alert("Wrong zoom Extent");
	},
	
	pan: function(deltaPoint)
	{
		this.extent.minx += deltaPoint[0]*this.resolution;
		this.extent.maxx += deltaPoint[0]*this.resolution;
		this.extent.miny += deltaPoint[1]*this.resolution;
		this.extent.maxy += deltaPoint[1]*this.resolution;
    
    //položaj gps
    var dx = (window.xmax + window.xmin)/2;
    var dy = (window.ymax + window.ymin)/2;
    
    var posw, posh;
   // alert("dx:" + dx +"\n" + "dy:" + dy + "\n" + "minx:" + this.extent.minx + "\n" +"maxx:" +  this.extent.maxx +"\n" + "miny:" + this.extent.miny + "\n" + "maxy:" +this.extent.maxy);
   
    var showpos = document.getElementById("idShowPosition");
    showpos.style.display = 'block';
    
    if (dx >= this.extent.minx && dx <= this.extent.maxx && dy > this.extent.miny && dy < this.extent.maxy)
    {                
      var rx = (dx - this.extent.minx)/(this.extent.maxx - this.extent.minx);
      var ry = (dy - this.extent.miny)/(this.extent.maxy - this.extent.miny);
   
      var wWidth = window.wWidth;
      var wHeight = window.wHeight;
      var toppos =  ry*wHeight;
      var leftpos=  rx*wWidth;
      
     // alert(leftpos + "\n" + toppos);
      if (leftpos < wWidth - 13 && leftpos > 0 && toppos > 13 && toppos < wHeight)
      {
        
        showpos.style.top = wHeight - toppos;
        showpos.style.left = leftpos;   
      }
      else
        showpos.style.display = 'none';
    }
    else
      showpos.style.display = 'none';
    if (showGPSPosition == false)
      showpos.style.display = 'none';
		this.draw();
	},
	
	XMLProgressFunction: function()
	{
		if ( (mainMap.xmlhttp.readyState == 4) && (mainMap.xmlhttp.status == 200) )
		{
			var ttxt = mainMap.xmlhttp.responseText;
			while(ttxt.indexOf("<table:HTN_SI_PON") >= 0 )
			{
				ttxt = ttxt.substring(ttxt.indexOf("<table:HTN_SI_PON"));
			
				var ponudba = ttxt.substring(0,ttxt.indexOf("</table:HTN_SI_PON"));
				var tmpPon = ponudba.split("<table:");
				var elCordX = 0;
				var elCordY = 0;
				
				for (x in tmpPon)
				{
					if(tmpPon[x].indexOf("KOORDINATA_X") >= 0)
						elCordX = x;
					if(tmpPon[x].indexOf("KOORDINATA_Y") >= 0)
						elCordY = x;
					tmpPon[x] = tmpPon[x].substring(tmpPon[x].indexOf(">")+1,tmpPon[x].indexOf("</"));
				}
				
				var cX = parseFloat(tmpPon[elCordX]);
				var cY = parseFloat(tmpPon[elCordY]);
				
				var sX = (cX - mainMap.extent.minx)/(mainMap.extent.maxx-mainMap.extent.minx);
				sX = Math.round(sX*mainMap.width);
				var sY = (cY - mainMap.extent.miny)/(mainMap.extent.maxy-mainMap.extent.miny);
				sY = Math.round(mainMap.height-(sY*mainMap.height));
				
      var featureid = "";
      var idtempot = "";
      var start = ponudba.indexOf("gml:id=");
      
      start = ponudba.indexOf(".", start)+1;
      var end = ponudba.indexOf(">", start)-1;
      featureid = ponudba.substring(start, end);
      
      
      start = ponudba.indexOf("<table:ID_INFORMACIJA_TEM_POT>");
      start = ponudba.indexOf(">", start) + 1;
      end = ponudba.indexOf("<", start);
      idtempot = ponudba.substring(start, end);
      
        
				var ponudbaItem = {
					ime:	      tmpPon[3],
					x:		      cX,
					y:		      cY,
					screenX:	  sX,
					screenY:	  sY,
          fid:        featureid,
          idtp:      idtempot
				};
				ttxt = ttxt.substring(ttxt.indexOf("</table:HTN_SI_PON"));
				var zastavica = true;
        for (var kk = 0; kk < mainMap.identifyItems.length; kk++)
        {					
          if (mainMap.identifyItems[kk].ime == ponudbaItem.ime)						
          {					
            zastavica = false;
          }
          
        }
        if (zastavica)
        {
          mainMap.identifyItems.push(ponudbaItem);
        }
			}
			doIdentifyResponse();
		}
	},
	
	Identify: function()
	{
		var appAddres = document.URL.substring(0,document.URL.lastIndexOf('/'));
		
		var newDiv = document.getElementById("mapImage");
		
		var visibleLayers = "";
		var i = 0 ;
		for( i = 0 ; i < this.no_layers ; i++)
		{
			if(this.layers[i].visible == true && this.layers[i].layers.indexOf("_POT_") == -1)
			{
				visibleLayers = visibleLayers +"," +this.layers[i].layers;
			}
		}
		if(visibleLayers != "")
		{
			visibleLayers = visibleLayers.substring(1);
			var req = this.baseLayer.address.replace("wms" , "wfs");
			
			req = req + '?';
			req = req + escape("request=GetFeature");
			req = req + escape("&version=1.1.0");
			req = req + escape("&typeName="+visibleLayers); //sloji, nad katerimi izvajamo identifikacijo
			req = req + escape("&Filter=%3Cogc:Filter%20xmlns:ogc=%22http://www.opengis.net/ogc%22%20xmlns:gml=%22http://www.opengis.net/gml%22%3E%3CAnd%3E%3C");
			req = req + escape("ogc:BBOX%3E%3Cogc:PropertyName%3EGEOMETRY%3C/ogc:PropertyName%3E%3Cgml:Box%20");
			req = req + escape("srsName=%22http://www.opengis.net/gml/srs/epsg.xml%23217000%22%3E%3C");
			req = req + escape("gml:coordinates%3E");
				req = req + escape(this.extent.minx+","+this.extent.miny); // MIN KOORDINATE
			req = req + escape("%20");
				req = req + escape(this.extent.maxx+","+this.extent.maxy); // MAX KOORDINATE
			req = req + escape("%3C/gml:coordinates%3E%3C/gml:Box%3E%3C/ogc:BBOX%3E%3C");
			req = req + escape("PropertyIsEqualTo%3E%3CPropertyName%3E");
				req = req + escape("ID_INFORMACIJA_TEM_POT");		//PROPERTY NAME
			req = req + escape("%3C/PropertyName%3E%3CLiteral%3E")
				req = req + escape(this.tip_poti);			//LITERAL VALUE
			req = req + escape("%3C/Literal%3E%3C/PropertyIsEqualTo%3E%3C/And%3E%3C/ogc:Filter%3E");
			var reqstring = appAddres+"/Proxy.jsp?url="+req;//escape(req);
			try 
			{
				if (!(window.XMLHttpRequest)) //za IE
				{
					this.xmlhttp =  new ActiveXObject("Microsoft.XMLHTTP");
					this.xmlhttp.onreadystatechange = this.XMLProgressFunction;
					this.xmlhttp.open("GET", reqstring, true);
					this.xmlhttp.send(null);			
				}
				else //za mozzilo
				{
					this.xmlhttp =  new XMLHttpRequest();
					this.xmlhttp.onreadystatechange = this.XMLProgressFunction;
					this.xmlhttp.open("GET", reqstring, true);
					this.xmlhttp.send(null);
				}
			}
			catch(e)
			{
				alert(e);
			}
		}
	}
	/******END: Uporabniške funkcijonalnosti*****/
};
