var mapProjections;
var initProjections = function() {
	mapProjections = [{
		units: "dd",
		projection: new OpenLayers.Projection("epsg:4326"),
		initialExtent: new OpenLayers.Bounds(-10.6700, 34.5000, 31.5500, 71.0500),
		maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90)
	}/*, {
		units: "m",
		projection: new OpenLayers.Projection("epsg:102067"),
    maxExtent: new OpenLayers.Bounds(-905000,-1230000,-400000,-900000),
    initialExtent: new OpenLayers.Bounds(-905000,-1230000,-400000,-900000),
    scales: [50000000, 25000000, 10000000, 5000000, 2000000, 1000000, 500000, 200000, 100000, 50000, 20000, 10000, 5000, 2000, 1000]
	}*/, {
		units: "m",
		projection: new OpenLayers.Projection("epsg:900913"),
    maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34),
    initialExtent: new OpenLayers.Bounds(-4000000, 4000000, 5000000, 11400000),
    scales: [50000000, 25000000, 10000000, 5000000, 2000000, 1000000, 500000, 200000, 100000, 50000, 20000, 10000, 5000, 2000, 1000]
	}];
}

var map;
var geoportal;
var searchGrid;

function init () {
  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,
		panels:[ "layerSwitcher", "infoPanel", "owsPanel"],
		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, 200000, 100000, 50000, 20000, 10000, 5000, 2000, 1000],
		units: "m"
	}, mapProjections);

	initCatClientCompositon();

	var args = OpenLayers.Util.getParameters();

	if (args.q) {
		parseAddress(args.q);
		geoportal.toolsPanel.setActiveTab(geoportal.infoPanel);
		geoportal.infoPanel.maskOn();
	}

	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 osm4326 = new OpenLayers.Layer.WMS(OpenLayers.i18n("OpenStreetMap"),
		"http://apps.esdi-humboldt.cz/cgi-bin/tilecache/tilecache.cgi",
		{
			layers: 'osmeurope4326',
			format: "image/png"
		}, {
			displayInLayerSwitcher: true,
			group: "OpenStreetMap",
			visibility: true,
			buffer: 1,
			ratio: 1,
			projections: [new OpenLayers.Projection("epsg:4326")],
			transitionEffect: "resize",
			isBaseLayer: false,
//			attribution: "<a href=\"http://www.openstreetmap.org\">OpenStreetMap</a>",
			isBaseGroup: true,
			saveWMC: false,
			singleTile: false,
      tileSize: new OpenLayers.Size(256, 256)
		}),

		osm3035 = new OpenLayers.Layer.WMS(OpenLayers.i18n("OpenStreetMap"),
		"http://apps.esdi-humboldt.cz/cgi-bin/tilecache/tilecache.cgi",
		{
			layers: 'osmeurope3035',
			format: "image/png"
		}, {
			displayInLayerSwitcher: true,
			group: "OpenStreetMap",
			visibility: true,
			buffer: 1,
			ratio: 1,
			projections: [new OpenLayers.Projection("epsg:3035")],
			transitionEffect: "resize",
			isBaseLayer: false,
//			attribution: "<a href=\"http://www.openstreetmap.org\">OpenStreetMap</a>",
			isBaseGroup: true,
			saveWMC: false,
			singleTile: false,
      tileSize: new OpenLayers.Size(256, 256)
		}),

	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",
			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)
		}),

	osmmapnik = new OpenLayers.Layer.WMS(OpenLayers.i18n("OSM test"),
		" http://apps.esdi-humboldt.cz:8000",
		{
			layers: '__all__',
			format: "image/png"
		}, {
			displayInLayerSwitcher: true,
			group: "test",
			visibility: true,
			buffer: 1,
			alpha: true,
			projections: mapProjections,
			transitionEffect: "resize",
			isBaseLayer: false,
			isBaseGroup: false,
			saveWMC: false,
			singleTile: true
		}),

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

	gphy = new OpenLayers.Layer.Google(OpenLayers.i18n('Google physical'),
	{
		type: google.maps.MapTypeId.TERRAIN,
	},{
		projections: [new OpenLayers.Projection("epsg:900913")],
		isBaseLayer: false,
		isBaseGroup: true
	}*/);

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

var zoomToExtent = function () {
	if (!geoportal.map.getCenter()){
		geoportal.map.zoomToExtent(new OpenLayers.Bounds(2426378.0132, 1528101.2618, 6293974.6215, 5446513.5222));
	}
};

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);


