var GMES = {
	Version : "1.2.0-working",	
	map: null,
	bbox: null,
	layerList:null,
	request: null,
	gui:null,
	isDebugEnabled : false,
	
	log: function(){
		try{
			console.log.apply(console,arguments);
		}catch(e){
			try{
				opera.postError.apply(opera,arguments);
			}catch(e){
				alert(Array.prototype.join.call(arguments, " "));
			}
		}
	},
	
	debug: function(){
		if ((typeof TestCase != "undefined") || this.isDebugEnabled) {
			this.log("[DEBUG] - " + Array.prototype.join.call(arguments, " "));
		}
	},
	
	assert: function(booleanExpression,message){
		if(!booleanExpression) throw("GMES Assertion Error:"+(message!=null?message:""));
	},

	isArrayEmpty: function(array){
		return (array == null || typeof array == 'undefined' || array.length == 0);
	},
	
	showMap: function(ulx, uly, lrx, lry) {
		var bounds = new OpenLayers.Bounds(ulx,uly,lrx,lry);
		this.map.zoomToExtent(bounds);
	},
	
	registerGetFeatureInfo: function(){
		var that = this;
		
		var getFeatureInfoText= function(original){
			var exceptionRegEx = /exception/i;
			var unknownRegEx = /unknown/i;				   
			var featureInfoText = original;			
				   
			if(exceptionRegEx.test(featureInfoText) || unknownRegEx.test(featureInfoText)){
			  	featureInfoText = "There is no information.";
			}
			return featureInfoText;
		};
		
		var showPopUp = function(text,xy){				    
		    that.bbox.setVisibility(false);								    
			that.map.addPopup(new OpenLayers.Popup.AnchoredBubble("GetFeatureInfo", 
				that.map.getLonLatFromPixel(xy),
				null,getFeatureInfoText(text) ,null,true
			),true);
		}
		
		var requestFeatureInfo = function(wms,xy,responseHandler){
			var url =  wms.getFullRequestString({
					REQUEST: "GetFeatureInfo",
					EXCEPTIONS: "application/vnd.ogc.se_xml",
					BBOX: wms.map.getExtent().toBBOX(),
					X: xy.x,Y: xy.y,
					INFO_FORMAT: 'text/html',QUERY_LAYERS: wms.params.LAYERS,
					WIDTH: wms.map.size.w,HEIGHT: wms.map.size.h});
				that.debug(url);
				OpenLayers.loadURL(url, '', this, function(response){
					responseHandler(response.responseText,xy);
				});				
		}
		
		this.map.events.register('click', this.map, function (e) {
			var layers = that.map.layers.findAll(function(layer){
				return layer.getFullRequestString;
			});
			that.debug("Visible WMS Layers:"+layers);
			if(that.isArrayEmpty(layers)){
				that.gui.showInfo("No FeatureInfo available.");
			}else{
				var wms = layers[0];
				that.debug("Visible Layer: "+wms);
				requestFeatureInfo(wms,e.xy,showPopUp);				
				OpenLayers.Event.stop(e);
			}
		});		
	},	
			
	init: function(){		
		OpenLayers.ProxyHost = "proxy?url=";
		this.resetMap();
		GMES.request = new GMES.Request(GMES.layerList);
		GMES.registerGetFeatureInfo();
		GMES.request.requestWMCMap();
		this.map.setCenter(new OpenLayers.LonLat(10.2, 48.9), 5);
	},
	
	createBBoxLayer: function(){
		this.bbox = new OpenLayers.Layer.Boxes("Boxes");
		GMES.map.addLayer(this.bbox);
		var control = new OpenLayers.Control();
		OpenLayers.Util.extend(control, {
			draw: function () {
				this.box = new OpenLayers.Handler.Box( control, {done: this.notice}, {keyMask: OpenLayers.Handler.MOD_CTRL});        												
				this.box.activate();
			},
	
			notice: function (bounds) {
				var lb = GMES.map.getLonLatFromPixel(new OpenLayers.Pixel(bounds.left, bounds.bottom)); 
				var rt = GMES.map.getLonLatFromPixel(new OpenLayers.Pixel(bounds.right, bounds.top));
				var bbox = new OpenLayers.Bounds();
				bbox.extend(lb);
				bbox.extend(rt);
				var markerBox = new OpenLayers.Marker.Box(bbox);
				GMES.bbox.setVisibility(true);			
				GMES.bbox.clearMarkers();
				GMES.bbox.addMarker(markerBox);
				$('boundingbox').value =lb.lon.toFixed(2)+","+lb.lat.toFixed(2)+","+
										rt.lon.toFixed(2)+","+rt.lat.toFixed(2);
			}
		});
		return control;
	},

	resetMap: function(){
		GMES.map = new OpenLayers.Map('gmesmap',{theme:'js/OpenLayers-2.8/theme/default/style.css'});
		GMES.map.addLayer(new OpenLayers.Layer.Google("Google Satellite",{type: G_SATELLITE_MAP, numZoomLevels: 20}));				
		GMES.gui = new GMES.GUI();
		[new OpenLayers.Control.Navigation({zoomWheelEnabled:true,handleRightClicks:true}),		
		new OpenLayers.Control.MousePosition(),
		new OpenLayers.Control.ZoomBox(),
		this.createBBoxLayer()].each(function(control){
			 GMES.map.addControl(control); 
		});
		GMES.layerList = new GMES.LayerList(GMES.map);
	}
		
};