var bounds;
var myMap;
var wms;
var wms_continent;
var NameArea;
var idArea;
var posPlus;
var param_filter;
var tuna_map;
var top_continent;
var inputParameters;

OpenLayers.ProxyHost = "/figis/proxy/cgi-bin/proxy.cgi?url=";
//OpenLayers.Util.alphaHack = function() {return true;} //IE6-ubuntu testing

var myDomain=document.domain;

var MyURLGeo;

/*if (myDomain.indexOf("192.168.1.197")>-1) {
        MyURLGeo='http://192.168.1.197:8484';
} else {
        MyURLGeo='http://127.0.0.1:8484';
}*/

if (myDomain.indexOf("figis02")>-1 ||myDomain.indexOf("193.43.36.238")>-1||myDomain.indexOf("www-data.fao.org")>-1) {
        MyURLGeo='http://193.43.36.238:8484';
} else {
        MyURLGeo='http://www.fao.org';
}

OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
	defaultHandlerOptions: {
		'single': true,
		'double': false,
		'pixelTolerance': 0,
		'stopSingle': false,
		'stopDouble': false
	},
	initialize: function(options) 
	{
		this.handlerOptions = OpenLayers.Util.extend(
		{}, this.defaultHandlerOptions);
		OpenLayers.Control.prototype.initialize.apply(this, arguments);
		this.handler = new OpenLayers.Handler.Click(this, {'click': this.trigger}, this.handlerOptions);
	},
	trigger: function(e) 
	{
		var lonlat = myMap.getLonLatFromViewPortPx(e.xy);
		/*" + NameArea + "*/

		// support GetFeatureInfo
		document.getElementById('nodelist').innerHTML = "Loading... please wait...";
		var params = {
			SERVICE: "WMS",
			REQUEST: "GetFeatureInfo",
			EXCEPTIONS: "application/vnd.ogc.se_xml",
			BBOX: myMap.getExtent().toBBOX(),
			X: e.xy.x,
			Y: e.xy.y,
			INFO_FORMAT: 'text/html',
			QUERY_LAYERS: 'fifao:TS_FI_TA',
			FEATURE_COUNT: 50,
			Layers: 'fifao:TS_FI_TA',
			Styles: 'polygon',
			Srs: 'EPSG:4326',
			WIDTH: myMap.size.w,
			HEIGHT: myMap.size.h,
			format: 'image/png',
			gs_url: MyURLGeo + '/figis/geoserver/ows',
			source: inputParameters};
		OpenLayers.loadURL("/figis/proxy-servlet/proxy", params, this, this.setHTML, this.setHTML);

		document.getElementById('brkdwn_nodelist').innerHTML = "Loading... please wait...";
		var brkdwn_params = {
			SERVICE: "WMS",
			REQUEST: "GetFeatureInfo",
			EXCEPTIONS: "application/vnd.ogc.se_xml",
			BBOX: myMap.getExtent().toBBOX(),
			X: e.xy.x,
			Y: e.xy.y,
			INFO_FORMAT: 'text/html',
			QUERY_LAYERS: 'fifao:TS_FI_TA_BREAKDWN',
			FEATURE_COUNT: 50,
			Layers: 'fifao:TS_FI_TA_BREAKDWN',
			Styles: 'polygon',
			Srs: 'EPSG:4326',
			WIDTH: myMap.size.w,
			HEIGHT: myMap.size.h,
			format: 'image/png',
			gs_url: MyURLGeo + '/figis/geoserver/ows',
			source: inputParameters,
			specbrkdwn: true};
		OpenLayers.loadURL("/figis/proxy-servlet/proxy", brkdwn_params, this, this.setBRKDWNHTML, this.setBRKDWNHTML);
		OpenLayers.Event.stop(e);
	},

	// sets the HTML provided into the brkdwn_nodelist element
	setHTML: function(response)
	{
	   var text = response.responseText;
	   if (text.indexOf("Exception") < 0)
			document.getElementById('nodelist').innerHTML = text;
	   else
			document.getElementById('nodelist').innerHTML = "";
	},

	// sets the HTML provided into the brkdwn_nodelist element
	setBRKDWNHTML: function(response)
	{
	   var text = response.responseText;
	   if (text.indexOf("Exception") < 0)
			document.getElementById('brkdwn_nodelist').innerHTML = text;
	   else
			document.getElementById('brkdwn_nodelist').innerHTML = "";
	}
});

function initMap(paramFile) {
	inputParameters = paramFile;
	
	// pink tile avoidance
	OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
	// make OL compute scale according to WMS spec
	OpenLayers.DOTS_PER_INCH = 25.4 / 0.28;

	bounds = new OpenLayers.Bounds(-180,-90,180,90);

	var options = {controls: [],
                maxExtent: bounds,
                maxResolution: 0.703125,
				numZoomLevels: 10,
                projection: "EPSG:4326",
                units: 'degrees'
    };
				
	myMap = new OpenLayers.Map('mapTunaAtlas', options);									
		
	fao_areas = new OpenLayers.Layer.WMS("FAO Fishing Areas", MyURLGeo + "/figis/geoserver/wms?",
					{
						layers:'fifao:FAO_MAJOR,fifao:UN_CONTINENT',
						format: OpenLayers.Util.alphaHack() ? "image/gif": "image/png",
                        //tiled: 'true',
                        //tilesOrigin : "-180.0,-90.0",
						transparent: true
					},
					{
						buffer:0, 
						isBaseLayer: false,
						wrapDateLine: true, 
						ratio: 1
					}
				);

	tuna_map =  new OpenLayers.Layer.WMS("Tuna Atlas", "/figis/proxy-servlet/proxy?gs_url="+MyURLGeo+"/figis/geoserver/ows&source="+inputParameters,
					{
						layers:'fifao:TS_FI_TA',
						format: OpenLayers.Util.alphaHack() ? "image/gif": "image/png",
                        //tiled: 'true',
                        //tilesOrigin : "-180.0,-90.0",
						transparent:true
					},
					{
						buffer:0, 
						isBaseLayer: false,
						wrapDateLine: true, 
						ratio: 1
					}
				);

	tuna_map_brkdwn =  new OpenLayers.Layer.WMS("Tuna Atlas - BreakDown", "/figis/proxy-servlet/proxy?gs_url="+MyURLGeo+"/figis/geoserver/ows&specbrkdwn=true&source="+inputParameters,
					{
						layers:'fifao:TS_FI_TA_BREAKDWN',
						format: OpenLayers.Util.alphaHack() ? "image/gif": "image/png",
                        //tiled: 'true',
                        //tilesOrigin : "-180.0,-90.0",
						transparent:true
					},
					{
						buffer:0, 
						isBaseLayer: false,
						wrapDateLine: true, 
						ratio: 1
					}
				);
				
	world_grid =  new OpenLayers.Layer.WMS("5x5-deg Grid", MyURLGeo + "/figis/geoserver/gwc/service/wms?",
					{
						layers:'fifao:GRID_G5',
						format: OpenLayers.Util.alphaHack() ? "image/gif": "image/png",
                        //tiled: 'true',
                        //tilesOrigin : "-180.0,-90.0",
						transparent: OpenLayers.Util.alphaHack() ? false : true
					},
					{
						buffer:0, 
						isBaseLayer: OpenLayers.Util.alphaHack() ? true : false,
						wrapDateLine: true, 
						ratio: 1
					}
				);
				
	top_continent = new OpenLayers.Layer.WMS("Continents", MyURLGeo + "/figis/geoserver/gwc/service/wms?",
					{
						layers: 'fifao:UN_CONTINENT',
						format: OpenLayers.Util.alphaHack() ? "image/gif": "image/png",
                        //tiled: 'true',
                        //tilesOrigin : "-180.0,-90.0",
						transparent: OpenLayers.Util.alphaHack() ? true : false
					},
					{
						buffer:0, 
						isBaseLayer: OpenLayers.Util.alphaHack() ? false : true,
						wrapDateLine: true, 
						ratio: 1
					}
				);
	
	NameArea='FAO_MAJOR';
	idArea=0;
	posPlus=-1;
	
	if (OpenLayers.Util.alphaHack()){
		myMap.addLayers([world_grid, tuna_map, /*tuna_map_brkdwn,*/ top_continent]);
	} else {
		myMap.addLayers([top_continent, world_grid, tuna_map, /*tuna_map_brkdwn,*/ fao_areas]);
	}
	
	// build up all controls            
	myMap.addControl(new OpenLayers.Control.PanZoomBar());
	myMap.addControl(new OpenLayers.Control.Navigation());
	//myMap.addControl(new OpenLayers.Control.NavToolbar());
	myMap.addControl(new OpenLayers.Control.ScaleLine());
	myMap.addControl(new OpenLayers.Control.MousePosition());
	myMap.addControl(new OpenLayers.Control.LayerSwitcher());
	myMap.zoomToExtent(bounds);
	
	// support GetFeatureInfo
	var click = new OpenLayers.Control.Click();
	myMap.addControl(click);
	click.activate();
}

function initPrintMap(paramFile) {
	inputParameters = paramFile;
	
	var mapDiv = document.getElementById("mapTunaAtlas");
	
	var w = mapDiv.style.width;
		w = w.indexOf("px")>0?w.substring(0,w.indexOf("px")):w;
	var h = mapDiv.style.height;
		h = h.indexOf("px")>0?h.substring(0,h.indexOf("px")):h;
	
	// Adding images
	var format = OpenLayers.Util.alphaHack() ? "image%2Fgif": "image%2Fpng";
	var world_gird_div = document.createElement('div');
	world_gird_div.style.width = mapDiv.style.width;
	world_gird_div.style.height = mapDiv.style.height;
	world_gird_div.style.overflow = 'hidden'; 
	world_gird_div.style.position = 'absolute';
	world_gird_div.style.zIndex = 0;
	world_gird_div.setAttribute('world_gird_div_id', 'World Grid');
	world_gird_div.innerHTML = '<img style="width: '+mapDiv.style.width+'; height: '+mapDiv.style.height+'; position: relative;" class="olTileImage" src="' + MyURLGeo + '/figis/geoserver/wms?LAYERS=fifao%3AGRID_G5&amp;FORMAT='+format+'&amp;TRANSPARENT=false&amp;TILED=true&amp;TILESORIGIN=-180.0%2C-90.0&amp;SERVICE=WMS&amp;VERSION=1.1.1&amp;REQUEST=GetMap&amp;STYLES=&amp;EXCEPTIONS=application%2Fvnd.ogc.se_inimage&amp;SRS=EPSG%3A4326&amp;BBOX=-180,-90,180,90&amp;WIDTH='+w+'&amp;HEIGHT='+h+'"/>';

	var fao_areas_div = document.createElement('div');
	fao_areas_div.style.width = mapDiv.style.width;
	fao_areas_div.style.height = mapDiv.style.height;
	fao_areas_div.style.overflow = 'hidden'; 
	fao_areas_div.style.position = 'absolute';
	fao_areas_div.style.zIndex = 1;
	fao_areas_div.setAttribute('fao_areas_div_id', 'All Areas');
	fao_areas_div.innerHTML = '<img style="width: '+mapDiv.style.width+'; height: '+mapDiv.style.height+'; position: relative;" class="olTileImage" src="' + MyURLGeo + '/figis/geoserver/wms?LAYERS=fifao%3AFAO_MAJOR&amp;FORMAT='+format+'&amp;TILED=true&amp;TILESORIGIN=-180.0%2C-90.0&amp;TRANSPARENT=true&amp;SERVICE=WMS&amp;VERSION=1.1.1&amp;REQUEST=GetMap&amp;STYLES=&amp;EXCEPTIONS=application%2Fvnd.ogc.se_inimage&amp;SRS=EPSG%3A4326&amp;BBOX=-180,-90,180,90&amp;WIDTH='+w+'&amp;HEIGHT='+h+'"/>';

	var tuna_map_div = document.createElement('div');
	tuna_map_div.style.width = mapDiv.style.width;
	tuna_map_div.style.height = mapDiv.style.height;
	tuna_map_div.style.overflow = 'hidden'; 
	tuna_map_div.style.position = 'absolute';
	tuna_map_div.style.zIndex = 2;
	tuna_map_div.setAttribute('tuna_map_div_id', 'Tuna Atlas');
	tuna_map_div.innerHTML = '<img style="width: '+mapDiv.style.width+'; height: '+mapDiv.style.height+'; position: relative;" class="olTileImage" src="/figis/proxy-servlet/proxy?gs_url=' + MyURLGeo + '/figis/geoserver/ows&source='+inputParameters+'&amp;LAYERS=fifao%3ATS_FI_TA&amp;FORMAT='+format+'&amp;TRANSPARENT=true&amp;TILED=true&amp;TILESORIGIN=-180.0%2C-90.0&amp;SERVICE=WMS&amp;VERSION=1.1.1&amp;REQUEST=GetMap&amp;STYLES=&amp;EXCEPTIONS=application%2Fvnd.ogc.se_inimage&amp;SRS=EPSG%3A4326&amp;BBOX=-180,-90,180,90&amp;WIDTH='+w+'&amp;HEIGHT='+h+'"/>';

	var top_continent_div = document.createElement('div');
	top_continent_div.style.width = mapDiv.style.width;
	top_continent_div.style.height = mapDiv.style.height;
	top_continent_div.style.overflow = 'hidden'; 
	top_continent_div.style.position = 'absolute';
	top_continent_div.style.zIndex = 3;
	top_continent_div.setAttribute('top_continent_div_id', 'Top Continents');
	top_continent_div.innerHTML = '<img style="width: '+mapDiv.style.width+'; height: '+mapDiv.style.height+'; position: relative;" class="olTileImage" src="' + MyURLGeo + '/figis/geoserver/wms?LAYERS=fifao%3AUN_CONTINENT&amp;FORMAT='+format+'&amp;TRANSPARENT=true&amp;TILED=true&amp;TILESORIGIN=-180.0%2C-90.0&amp;SERVICE=WMS&amp;VERSION=1.1.1&amp;REQUEST=GetMap&amp;STYLES=&amp;EXCEPTIONS=application%2Fvnd.ogc.se_inimage&amp;SRS=EPSG%3A4326&amp;BBOX=-180,-90,180,90&amp;WIDTH='+w+'&amp;HEIGHT='+h+'"/>';

	mapDiv.appendChild(world_gird_div);
	if(!OpenLayers.Util.alphaHack()) 
		mapDiv.appendChild(fao_areas_div);
	mapDiv.appendChild(tuna_map_div);
	mapDiv.appendChild(top_continent_div);
}

ByRei_dynDiv.api.alter = function() {
	// notify OL that we changed the size of the map div
    myMap.updateSize();
}
