
	var uagent   = navigator.userAgent.toLowerCase();
	var isSafari = ( (uagent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc.") );
	var isOpera  = (uagent.indexOf('opera') != -1);
	var isWebtv  = (uagent.indexOf('webtv') != -1);
	var isIe     = ( (uagent.indexOf('msie') != -1) && (!isOpera) && (!isSafari) && (!isWebtv) );
	var isIe4    = ( (isIe) && (uagent.indexOf("msie 4.") != -1) );
	var isMoz    = ( (navigator.product == 'Gecko')  && (!isOpera) && (!isWebtv) && (!isSafari) );
	var isNs     = ( (uagent.indexOf('compatible') == -1) && (uagent.indexOf('mozilla') != -1) && (!isOpera) && (!isWebtv) && (!isSafari) );
	var isNs4    = ( (isNs) && (parseInt(navigator.appVersion) == 4) );
	var isKon    = (uagent.indexOf('konqueror') != -1);
	
	var Map = 
	{
		width : 406,
		height : 243,
		placeId : "map-instance",
		src : "map/map.jpg",
		place : null,
		items : [],
		wgsStartX : 12.086090,
		wgsEndX : 18.841970,
		wgsStartY : 51.059571,
		wgsEndY : 48.550320,
		wgsX : 0,
		wgsY : 0,
		categoryId : 1,
		htmlMap : null,
		regionInstance : null,
		backButton : null,
		regionButton : null,
		toolbarTitle : null,
		
		init : function()
		{
			this.place = document.getElementById( this.placeId );
			
			if( this.place == null ) return;
			
			var toolbar = document.createElement( "div" );
			toolbar.style.width = this.width + "px";
			toolbar.style.height = "16px";
			toolbar.style.margin = "10px 0 0 0";
			toolbar.style.background = "#151315";
			
			this.toolbarTitle = document.createElement( "b" );
			this.toolbarTitle.className = "map-region-title";
			toolbar.appendChild( this.toolbarTitle );
			
			this.regionButton = document.createElement( "a" );
			this.regionButton.innerHTML = "Zobrazit seznam optik";
			this.regionButton.setAttribute( "href", "#" );
			this.regionButton.style.display = "none";
			this.regionButton.className = "map-region-all";
			
			this.backButton = document.createElement( "a" );
			this.backButton.innerHTML = "Zpět";
			this.backButton.setAttribute( "href", "#" );
			this.backButton.style.display = "none";
			this.backButton.className = "map-region-back";
			this.backButton.onclick = function()
			{
				Map.showMap();
			}
			
			toolbar.appendChild( this.backButton );
			toolbar.appendChild( this.regionButton );
			
			this.renderMap();
			
			this.place.appendChild( toolbar );
		},
		
		showRegion : function( regionId )
		{
			this.autoLoadItems( regionId );
		},
		
		autoLoadItems : function( regionId )
		{
			Map.clearItems();
			
			doRequest = function()
			{	
				if ( ! xmlobj.isReady() )
				{
					return;
				}
					
				var text = xmlobj.handler.responseText;
				
				var response = null;
				
				try
				{
					eval( text );
					
					if( response == null ) return;
					
					if( response.length )
					{
						for( i in response )
						{
							Map.addItem( response[i] );
						}
					}
					
					Map.renderRegion( regionId );
				}
				catch(e)
				{alert(text);}
			}
			
			var xmlobj = new ajax();
			xmlobj.onReady( doRequest );
			xmlobj.process( "cnv.map?category=" + this.categoryId + "&region=" + regionId );
		},
		
		getPlaceTop : function()
		{
			var temp = this.place;
			
			var top = temp.offsetTop;
			
			while( (temp = temp.offsetParent) != null )
			{
				top += temp.offsetTop;
			}
			
			if( isIe )
			{
					return top;
			}
			else
			{
				return (top+20);
			}
		},
		
		clearItems : function()
		{
			this.items = [];
		},
		
		getPlaceLeft : function()
		{
			var temp = this.place;
			
			var left = temp.offsetLeft;
			
			while( (temp = temp.offsetParent) != null )
			{
				left += temp.offsetLeft;
			}
			
			return left;
		},
		
		renderMap : function()
		{			
			MapTooltip.init();
			
			var r = document.createElement( "div" );
			r.style.width = this.width + "px";
			r.style.height = this.height + "px";
			r.style.display = "none";
			
			this.regionInstance = r;
			
			this.place.appendChild( this.regionInstance );
			
			var i = document.createElement( "div" );
			i.style.width = this.width + "px";
			i.style.height = this.height + "px";
			
			var map = document.createElement( "map" );
			map.name = "cz";
			map.id = "cz";
			map.style.cursor = "pointer";
			
			var img = document.createElement( "img" );
			img.src = this.src;
			img.src2 = this.src;
			img.useMap = "#cz";
			
			i.appendChild( img );
			i.appendChild( map );
			
			var pragImg;
			var pragArea;
			var pragRegion;
			
			for( j in MapData.regions )
			{
				var region = MapData.regions[j];
				
				var area = document.createElement( "area" );				
				area.shape = "poly";
				area.coords = region.coords;
				area.region = region.id;
				
				area.onmouseover = function(){img.src = "./map/" + this.region + ".jpg"; MapEvents.regions.mouseOver(this.region);}
				area.onmouseout = function(){img.src = img.src2; MapEvents.regions.mouseOut();}
				area.onclick = function(){Map.showRegion(this.region);}
				
				map.appendChild( area );
			}
			
			this.instance = i;
			
			this.place.appendChild( this.instance );
		},
		
		showMap : function()
		{
			document.getElementById( "our-partners-text" ).style.display = "none";
			this.regionInstance.style.display = "none";
			this.instance.style.display = "";
			this.backButton.style.display = "none";
			this.regionButton.style.display = "none";
			this.toolbarTitle.innerHTML = "";
		},
		
		renderRegion : function( regionId )
		{
			var region = MapData.getRegionById( regionId );
			
			if( region == null ) return;
			
			document.getElementById( "our-partners-text" ).style.display = "block";
			
			this.instance.style.display = "none";
			this.regionInstance.innerHTML = "";
			this.regionInstance.style.width = region.width + "px";
			this.regionInstance.style.height = region.height + "px";
			this.regionInstance.style.display = "block";
			this.regionInstance.style.background = "url(./map/" + region.id + "b.png) no-repeat";			
			
			this.backButton.style.display = "";
			this.regionButton.style.display = "";
			this.toolbarTitle.innerHTML = region.title;
			
			this.regionButton.onclick = function()
			{
				document.getElementById( "lat" ).value = "r:" + region.id;
				document.forms[ "search" ].submit();
			}
			
			this.wgsStartX = region.wgsStartX;
			this.wgsStartY = region.wgsStartY;
			this.wgsX = myRound( (region.wgsEndX - region.wgsStartX)/region.width, 4 );
			this.wgsY = myRound( (region.wgsEndY - region.wgsStartY)/region.height, 4 );
			
			var x,y;
			
			var pxX = this.getPlaceLeft() + region.width;
			var pxY = this.getPlaceTop() + region.height;
			
			if( this.items.length )
			{
				for( j in this.items )
				{
					var item = this.items[j];
					
					y = this.getY( myRound( item.y, 4 ) );
					x = this.getX( myRound( item.x, 4 ) );
					
					if( x > pxX || y > pxY ) continue;
					
					if( !isIe )
					{
						item.style.top = (y - 17) + "px";
						item.style.left = (x - 2) + "px";
					}
					else
					{
						item.style.top = y + "px";
						item.style.left = x + "px";
					}
					
					this.regionInstance.appendChild( item.instance );
				}
			}
		},
		
		getX : function( wgs )
		{
			return this.getPlaceLeft() + Math.round( (wgs - this.wgsStartX)/this.wgsX );
		},
		
		getY : function( wgs )
		{
			return this.getPlaceTop() +  Math.round( (wgs - this.wgsStartY)/this.wgsY );
		},
		
		addItem : function( item )
		{
			this.items.push( new MapItem( item ) );
		}
	}
	
	function MapItem( item )
	{
		this.x = item.x;
		this.y = item.y;
		this.title = item.title;
		this.location = item.location;
		this.address = item.address;
		
		this.instance = document.createElement( "div" );
		this.style = this.instance.style;
		
		this.style.width = "15px";
		this.style.height = "16px";
		this.style.background = "url(./map/pointer.png) no-repeat";
		this.style.position = "absolute";
		this.style.zIndex = 130;
		this.style.cursor = "pointer";
		
		var self = this;
		
		this.instance.onmouseover = function(){ MapEvents.items.mouseOver(self); };
		this.instance.onmouseout = function(){ MapEvents.items.mouseOut(self); };
		this.instance.onclick = function(){ MapEvents.items.goToDetail(self); };
	}
	
	MapItem.prototype.getHtml = function()
	{
		return "<b>" + this.title + "</b><br />" + this.address;
	}
	
	var MapEvents =
	{
		items :
		{
			mouseOver : function( item )
			{
				MapTooltip.setHtml( item.getHtml() );
				MapTooltip.show();
			},
			
			mouseOut : function( item )
			{
				MapTooltip.hide();
			},
			
			goToDetail : function( item )
			{
				window.location = item.location;
			}
		},
		
		regions :
		{
			mouseOver : function( regionId )
			{
				var title = MapData.getRegionById( regionId ).title;
				
				MapTooltip.setHtml( "<b>" + title + "</b>" );
				MapTooltip.show();
			},
			
			mouseOut : function()
			{
				MapTooltip.hide();
			}
		}
	}
	
	var MapTooltip =
	{
		instance : null,
		state : 0,
		
		init : function()
		{
			var i  = document.createElement( "div" );
			var is = i.style;
			
			i.className = "map-tooltip";
			i.style.display = "none";
			is.position = "absolute";
			is.zIndex = 150;
			
			if( isMoz )
			{
				document.captureEvents( Event.MOUSEMOVE );
			}
			
			document.onmousemove = function(e)
			{
				if( MapTooltip.state == 0 ) return;
				
				e = e || window.event;
				
				var cursor = {x:0, y:0};
				
				if( e.pageX || e.pageY )
				{
					cursor.x = e.pageX;
					cursor.y = e.pageY;
				} 
				else
				{
					var de = document.documentElement;
					var b = document.body;

					cursor.x = e.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
					cursor.y = e.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
				}
				
				MapTooltip.instance.style.left = (cursor.x + 20) + "px";
				MapTooltip.instance.style.top = (cursor.y + 20) + "px";
			}
			
			this.instance = i;
			
			Map.place.appendChild( this.instance );
		},
		
		setHtml : function( html )
		{
			this.instance.innerHTML = html;
		},
		
		show : function()
		{
			this.state = 1;
			document.onmousemove();
			this.instance.style.display = "block";
		},
		
		hide : function()
		{
			this.state = 0;
			this.instance.style.display = "none";
		}
	}
	
	var MapData =
	{
		regionsWGS :
		[
			{id:"a",title:"Praha",width:278,height:203,wgsStartX:14.239610,wgsEndX:14.692040,wgsStartY:50.166530,wgsEndY:49.947770},
			{id:"s",title:"Středočeský kraj",width:302,height:247,wgsStartX:13.412470,wgsEndX:15.543640,wgsStartY:50.608490,wgsEndY:49.513500},
			{id:"c",title:"Jihočeský kraj",width:312,height:244,wgsStartX:13.552970,wgsEndX:15.586470,wgsStartY:49.610710,wgsEndY:48.549690},
			{id:"p",title:"Plzeňský kraj",width:193,height:241,wgsStartX:12.408980,wgsEndX:13.834530,wgsStartY:50.102710,wgsEndY:48.934640},
			{id:"k",title:"Karlovarský kraj",width:250,height:183,wgsStartX:12.125800,wgsEndX:13.293090,wgsStartY:50.460510,wgsEndY:49.901920},
			{id:"u",title:"Ústecký kraj",width:268,height:242,wgsStartX:12.933280,wgsEndX:14.665920,wgsStartY:51.059175,wgsEndY:50.089680},
			{id:"l",title:"Liberecký kraj",width:357,height:239,wgsStartX:14.346260,wgsEndX:15.644490,wgsStartY:51.027580,wgsEndY:50.480460},
			{id:"h",title:"Královohradecký kraj",width:300,height:232,wgsStartX:15.105760,wgsEndX:16.579870,wgsStartY:50.785890,wgsEndY:50.037130},
			{id:"e",title:"Pardubický kraj",width:300,height:199,wgsStartX:15.385200,wgsEndX:16.864400,wgsStartY:50.203384,wgsEndY:49.568920},
			{id:"j",title:"Vysočina",width:264,height:244,wgsStartX:14.905160,wgsEndX:16.422050,wgsStartY:49.849530,wgsEndY:48.942740},
			{id:"b",title:"Jihomoravský kraj",width:332,height:237,wgsStartX:15.531920,wgsEndX:17.650980,wgsStartY:49.634410,wgsEndY:48.607190},
			{id:"m",title:"Olomoucký kraj",width:162,height:238,wgsStartX:16.733390,wgsEndX:17.916930,wgsStartY:50.444500,wgsEndY:49.269690},
			{id:"z",title:"Zlínský kraj",width:286,height:239,wgsStartX:17.117070,wgsEndX:18.412280,wgsStartY:49.544300,wgsEndY:48.862260},
			{id:"t",title:"Moravskoslezký kraj",width:300,height:240,wgsStartX:17.155190,wgsEndX:18.853510,wgsStartY:50.317980,wgsEndY:49.379850},
		],
		
		getRegionById : function( id )
		{
			for( i in this.regionsWGS )
			{
				if( this.regionsWGS[i].id == id )
				{
					return this.regionsWGS[i];
				}
			}
		},
		
		regions :
		[
			{id:"a",x:129,y:87,coords:"135,109 147,104 151,106 154,104 153,100 155,96 145,87 135,92 130,92 129,99 134,104 134,109"},
			{id:"s",x:78,y:47,coords:"77,90 84,87 86,81 91,82 94,78 100,79 103,76 113,74 116,69 137,70 137,62 141,62 146,56 156,59 162,51 164,55 170,48 183,58 184,66 181,71 188,78 197,80 197,88 195,91 201,95 196,104 204,112 201,122 186,130 183,136 187,143 171,144 166,149 156,142 154,148 147,151 132,148 124,147 120,149 114,146 106,150 96,148 92,130 99,125 102,110 88,102 82,96 78,96 77,91 "},
			{id:"c",x:79,y:143,coords:"79,198 83,192 83,183 90,181 96,164 95,158 96,157 96,150 108,150 113,145 117,148 122,149 126,145 133,149 141,148 149,151 155,147 156,143 166,149 168,159 168,173 181,182 191,183 192,188 195,191 203,192 205,194 200,203 204,208 204,212 193,206 190,204 184,207 180,206 172,199 167,204 167,223 159,223 150,231 150,239 143,237 138,237 134,233 127,241 117,237 110,237 107,233 110,230 96,218 90,209 79,198 83,192 83,183 89,180 98,164 95,159 97,155 96,149"},
			{id:"p",x:17,y:91,coords:"76,91 79,92 79,97 82,97 88,101 93,101 98,106 102,107 104,114 100,117 100,125 93,128 95,138 97,148 97,157 97,164 90,180 84,183 84,191 80,196 76,200 72,198 72,192 65,183 60,183 45,159 37,159 30,150 29,142 25,138 25,133 23,127 17,120 21,116 21,112 26,106 33,108 35,107 37,108 40,106 53,107 65,98 70,100 75,96 74,91 77,91"},
			{id:"k",x:1,y:54,coords:"54,59 59,60 63,64 69,66 69,75 71,82 71,87 75,91 75,95 70,100 65,98 53,107 40,107 38,108 35,108 32,109 26,106 23,102 22,97 6,81 8,78 0,68 5,63 14,74 28,58 39,59 46,54 54,60"},
			{id:"u",x:54,y:3,coords:"77,92 84,88 83,84 86,82 90,83 95,79 100,80 103,77 105,79 116,70 131,71 137,70 137,63 141,63 146,57 143,53 142,48 142,40 148,31 151,31 160,26 156,22 157,17 153,15 154,10 147,6 145,7 138,4 135,8 142,14 139,19 133,19 130,23 121,25 117,25 112,27 108,33 91,33 89,38 85,42 81,40 73,44 68,51 60,51 53,60 60,62 63,65 70,67 69,78 72,83 70,87 78,92"},
			{id:"l",x:142,y:9,coords:"145,57 149,57 152,59 157,59 162,52 166,54 172,47 182,53 183,58 191,61 196,62 200,64 205,60 209,60 212,57 211,33 204,30 199,31 199,28 194,22 194,14 187,10 185,12 181,8 178,10 179,15 175,23 169,23 164,28 158,25 153,31 148,31 142,38 142,48 145,57"},
			{id:"h",x:182,y:33,coords:"183,58 183,63 185,67 182,71 188,76 188,79 196,80 197,84 199,87 197,92 200,96 208,93 218,93 224,91 227,93 235,90 246,100 251,101 254,94 267,90 267,86 257,77 256,73 254,73 251,73 247,67 253,61 256,61 261,53 256,46 253,47 250,45 246,48 242,45 238,49 236,46 236,43 228,43 225,36 220,38 212,34 212,47 210,55 212,58 208,60 206,59 200,64 196,60 191,62 183,57"},
			{id:"e",x:197,y:90,coords:"200,95 196,104 204,112 200,121 209,127 215,127 225,134 226,138 230,139 232,135 238,139 242,139 256,149 264,149 267,145 281,148 280,143 284,140 283,136 277,125 278,116 277,112 282,105 283,99 285,92 283,91 275,101 268,93 267,90 255,93 252,100 246,101 235,90 227,92 221,91 209,92 200,95"},
			{id:"j",x:166,y:122,coords:"202,121 198,123 198,127 187,130 183,137 187,143 172,144 166,148 169,156 169,173 175,176 182,182 184,182 192,183 192,189 200,192 202,190 206,195 200,203 205,210 212,207 216,208 227,199 239,199 247,193 247,185 249,176 255,168 254,158 257,156 256,149 242,139 238,139 233,134 230,139 227,139 225,133 215,127 209,127 201,121 197,127 187,129 183,137 188,142 175,145 172,144 167,149"},
			{id:"b",x:204,y:146,coords:"256,149 257,155 254,159 254,168 249,175 249,179 246,183 246,193 239,199 229,199 216,207 212,207 208,210 205,207 202,212 207,214 214,219 218,215 233,228 240,229 254,230 258,224 264,224 272,230 283,233 287,235 290,234 298,221 304,219 315,224 319,222 328,224 330,220 324,210 317,209 309,202 306,201 305,196 300,196 305,190 301,187 301,175 288,166 283,168 281,154 278,152 281,148 268,146 264,149 257,149"},
			{id:"m",x:278,y:67,coords:"286,92 286,97 283,99 283,105 277,112 279,117 278,125 284,136 284,140 281,143 282,150 278,152 283,167 288,166 302,175 302,180 308,178 313,175 313,168 321,171 325,168 331,168 331,164 333,162 339,162 340,160 348,154 333,141 334,137 331,135 326,136 319,135 316,130 305,126 301,121 304,115 302,110 307,105 311,88 317,82 310,80 305,74 288,67 287,70 290,75 291,79 297,82 297,87 285,92"},
			{id:"z",x:301,y:156,coords:"301,179 301,186 304,189 299,196 305,197 306,201 317,209 323,209 331,220 336,220 340,214 345,214 346,207 349,205 356,205 361,198 364,180 376,175 378,170 371,164 370,161 364,159 352,158 346,155 340,163 333,161 331,164 332,166 330,169 325,168 323,171 314,168 313,175 309,178 302,179"},
			{id:"t",x:302,y:80,coords:"316,84 311,89 311,94 303,111 305,115 301,122 308,127 318,131 318,135 332,135 335,137 333,141 347,155 352,159 364,159 370,159 378,169 387,158 398,159 407,154 403,148 402,142 391,136 388,128 388,120 384,121 377,117 374,119 365,112 360,112 356,105 353,107 354,110 347,114 340,107 339,102 330,98 333,93 339,89 336,80 334,84 316,85"},
		]
	}
	
	function myRound( num, dec )
	{
		return Math.round( num*Math.pow( 10, dec ) )/Math.pow( 10,dec );
	}