var mapProjections;
var initProjections = function() {
	mapProjections = [{
	    projection: new OpenLayers.Projection("epsg:3034"),
	    units: "m",
	    maxExtent: new OpenLayers.Bounds(2122254.2378, 1164627.9290, 5955457.4541, 5021872.0731)
	},{
	    projection: new OpenLayers.Projection("epsg:3040"),
	    units: "m",
	    maxExtent: new OpenLayers.Bounds(203872.6488, 3056487.4888, 796127.3512, 7384416.4233)
	},{
	    projection: new OpenLayers.Projection("epsg:3041"),
	    units: "m",
	    maxExtent: new OpenLayers.Bounds(229578.6300, 3988111.9622, 770421.3700, 6914547.3835)
	},{
	    projection: new OpenLayers.Projection("epsg:3042"),
	    units: "m",
	    maxExtent: new OpenLayers.Bounds(225370.7346, 3849419.9580, 774629.2654, 6914547.3835)
	},{
	    projection: new OpenLayers.Projection("epsg:3043"),
	    units: "m",
	    maxExtent: new OpenLayers.Bounds(238379.2278, 4265559.3497, 761620.7722, 6914547.3835)
	},{
	    projection: new OpenLayers.Projection("epsg:3044"),
	    units: "m",
	    maxExtent: new OpenLayers.Bounds(231991.5007, 4065788.6515, 768008.4993, 7768690.1087)
	},{
	    projection: new OpenLayers.Projection("epsg:3045"),
	    units: "m",
	    maxExtent: new OpenLayers.Bounds(227879.8880, 3932632.6543, 772120.1120, 8325798.2469)
	},{
	    projection: new OpenLayers.Projection("epsg:3046"),
	    units: "m",
	    maxExtent: new OpenLayers.Bounds(225536.5500, 3854967.2384, 774463.4500, 8325798.2469)
	},{
	    projection: new OpenLayers.Projection("epsg:3047"),
	    units: "m",
	    maxExtent: new OpenLayers.Bounds(225536.5500, 3854967.2384, 774463.4500, 8325798.2469)
	},{
	    projection: new OpenLayers.Projection("epsg:3048"),
	    units: "m",
	    maxExtent: new OpenLayers.Bounds(224544.8015, 3821684.0528, 775455.1985, 8325798.2469)
	},{
	    projection: new OpenLayers.Projection("epsg:3049"),
	    units: "m",
	    maxExtent: new OpenLayers.Bounds(228726.6722, 3960371.8888, 771273.3278, 8325798.2469)
	},{
	    projection: new OpenLayers.Projection("epsg:4326"),
	    units: "dd",
	    initialExtent: new OpenLayers.Bounds(-10.6700, 34.5000, 31.5500, 71.0500),
	    maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90)
	}];
};

var map;
var geoportal;
var searchGrid;

var init = function () {
	initProjections();
	OpenLayers.Util.IMAGE_RELOAD_ATTEMPTS = 2;
	HSLayers.feedbackScript = "/map/feedback.php";
	HSLayers.Layer.WarpedWMS.proxy = "/wwwlibs/warper";
	Proj4js.libPath = "/wwwlibs/proj4js/";

	OpenLayers.Marker.defaultIcon = function () {
		var size = new OpenLayers.Size(32, 32),
			offset = new OpenLayers.Pixel(-16, -32);
		return new OpenLayers.Icon(OpenLayers.Util.getImagesLocation() + "icons/blue.png", size, offset);
	};

	HS.setCookiePath('/');
	HS.setLang(HS.getLastLangCode(), true);
	OpenLayers.ProxyHost = "/cgi-bin/hsproxy.cgi?toEncoding=utf-8&url=";
	HSLayers.WMCManager.getImagesURL = "/wmc/images.php";
	HSLayers.WMCManager.inspireThemesURL = '/wmc/xsl/codelists_' + HS.getLang() + '.xml';
	HSLayers.WMCManager.wmcHandlerURL = "/wmc/index.php";
	var windowWidth = Ext.get("map").dom.scrollWidth,
		windowHeight = document.body.parentNode.clientHeight,
		headerHeight = Ext.select('#north').elements[0].clientHeight,
		mapHeight = windowHeight - headerHeight-1;

	geoportal = new HSLayers.MapPortal({
		layout: 'fit',
		renderTo: 'map',
		height: mapHeight,
		width: windowWidth,
		stateUrl: '/wwwlibs/statusmanager/index.php'
	});

	geoportal.initMap({
		projection: new OpenLayers.Projection("epsg:3035"),
		maxExtent: new OpenLayers.Bounds(-3000000, -4000000, 14000000, 11000000),
		initialExtent: new OpenLayers.Bounds(2426378.0132, 1528101.2618, 6293974.6215, 5446513.5222),
		scales: [50000000, 25000000, 10000000, 5000000, 2000000, 1000000, 500000, 250000, 100000, 50000, 20000, 10000, 5000, 2000, 1000],
		units: "m"
	}, mapProjections);

	initCatClientCompositon();

	var args = OpenLayers.Util.getParameters();

	if (args.wmc) {
		OpenLayers.loadURL (
			args.wmc,
			{_rand: Math.random()},
			geoportal,
			function(r) {
				geoportal.overwriteLocalMapContext = {
					newLayers:true, // add new layers
					oldLayers:true, // set visibility to old layers
					title:true // set title of map
					};
				var oldExtent = geoportal.map.getExtent();
				geoportal.parseWMC(r);
				geoportal.map.zoomToExtent(oldExtent);
			}
		);
	}

	var osm = new OpenLayers.Layer.WMS(OpenLayers.i18n("OpenStreetMap"),
		"http://osm.ccss.cz/cgi-bin/ows/wms/europe",
		{
			layers: 'default',
			format: "agg"
		}, {
			displayInLayerSwitcher: true,
			group: "OpenStreetMap",
			visibility: true,
			/*gutter:65,*/
			buffer: 1,
			ratio: 1,
			projections: mapProjections,
			transitionEffect: "resize",
			isBaseLayer: false,
			singleTile: false,
			displayOutsideMaxExtent: true,
			tileSize: new OpenLayers.Size(256, 256),
			attribution: "<a href=\"http://www.openstreetmap.org\">OpenStreetMap</a>",
			isBaseGroup: true,
			saveWMC: false
	}),

	urban3035 = new OpenLayers.Layer.WMS(OpenLayers.i18n("EEA Urban Atlas"),
		"http://apps.esdi-humboldt.cz/cgi-bin/tilecache/tilecache.cgi",
		{
			layers: 'urban3035',
			format: "image/png"
		}, {
			displayInLayerSwitcher: true,
			group: "EEA - Urban atlas",
			visibility: true,
			buffer: 1,
			alpha: true,
			projections: [new OpenLayers.Projection("epsg:3035")],
			scales: [500000, 200000, 100000, 50000, 20000, 10000, 5000, 2000, 1000],
			transitionEffect: "resize",
			isBaseLayer: false,
			isBaseGroup: false,
			saveWMC: false,
			singleTile: false,
      tileSize: new OpenLayers.Size(256, 256)
		});

	geoportal.map.addLayers([osm, urban3035]);
	geoportal.afterInit();
	window.setTimeout("zoomToExtent()",3000);
	geoportal.readStateControl.getState();
};


var zoomToExtent = function () {
	if (!geoportal.map.getCenter()){
		geoportal.map.zoomToMaxExtent();
	}
};

var onSimpleFormSubmit = function () {
	try {
		geoportal.searchGeoNames(document.simpleForm.anytext);
	} catch (e) {
		console.log(e);
	}
	return false;
};

var searchInCatalogue = function () {
	var anytext = document.simpleForm.anytext.value;
	window.location = "/catalogue/?anytext=" + anytext + "&menuId=menu0&type=";
};

Ext.EventManager.onWindowResize(function () {
	windowWidth = document.body.parentNode.clientWidth;
	windowHeight = document.body.parentNode.clientHeight;
	headerHeight = Ext.select('#north').elements[0].clientHeight;
	this.geoportal.setHeight(windowHeight - headerHeight-1);
	this.geoportal.setWidth(windowWidth);
}, geoportal);


