/*-----------------------------------------------------------------------
interface.js

version: 	4.1
author:		sebastian kupke
email:		sebastian.kupke@baral-geohaus.de
website:	http://www.baral.de
-----------------------------------------------------------------------*/

/* =namespace interface
-----------------------------------------------------------------------*/
ws.i = {
	
	/* =ws.i.map
		Parameter:
			options					hash
		Description:
			Method to control the map.
			Method can receive a set of options with a hash.
			All options are optional. If not set, the default is taken.
			If you set the x and the y option and load is not set to true, nothing will happen.
			You should always set load to true.
		Example:
			ws.i.map({
				scale: 5000,
				x: 3450000,
				y: 5400000,
				load: true
			});
		Options:
			x						number							3450000															The x coordinate of the center
			y						number							5400000															The y coordinate of the center
			bbox					object							{minx: 3450000, miny: 5400000, maxx: 3451000, maxy: 5401000}	
			scale					number							2500															The scalefactor
			service					string							'<service>'														The id of the service
			level					string							'<level>'														The id of the level. The scale will be calculated by the interface. It will overwrite other scales if they where set (scale).
			layer					string							'<layer>'														The id of the layer
			pin						boolean							true															Shows the pin. If pinX and pinY is not set, it will be centered to the map center
			pinX					number							3450000															Sets the pin to the x coordinate
			pinY					number							5400000															Sets the pin to the y coordinate
			pinImg					string							'mypin.gif'														Sets the pin image
			pinImgWidth				number							25																Sets the pin image width in pixel
			pinImgHeight			number							25																Sets the pin image height in pixel
			pinTooltipHeader		string							'This is the title of the tooltip'								Sets the header of the pin tooltip
			pinTooltipContent		string							'This is the content of the tooltip'							Sets the content of the pin tooltip
			loaded					function						function() {alert('map loaded');}								function is executed after a map was successfully loaded
			load					boolean							true|1|'true'													Load a map
	-----------------------------------------------------------------------*/
	map: function(opts) {
		
		// x
		if (opts.x) {
			ws.map.center.x = opts.x;
		}
		
		// y
		if (opts.y) {
			ws.map.center.y = opts.y;
		}
		
		// scale
		if (opts.scale) {
			ws.map.setScaleFactor(opts.scale);
		}
		
		// bbox
		if (opts.bbox) {
			ws.map.center.x = (opts.bbox.minx + opts.bbox.maxx) / 2;
			ws.map.center.y = (opts.bbox.miny + opts.bbox.maxy) / 2;
			
			var tmpScaleX = (opts.bbox.maxx - opts.bbox.minx) / $('#map_div').width();
			var tmpScaleY = (opts.bbox.maxy - opts.bbox.miny) / $('#map_div').height();
			
			ws.map.scale = tmpScaleX > tmpScaleY ? tmpScaleX : tmpScaleY;
		}
		
		// service
		if (opts.service) {
			if (ws.map.getService(opts.service)) {
				ws.map.curService = opts.service;
			}
		}

		// level
		if (opts.level) {
			var service = ws.map.getService();
			if (service.getLevel(opts.level)) {
				var level = service.setCurLevel(opts.level);
				
				var scaleNumber = ws.map.getScaleNumber();
				if (level.maxScale > scaleNumber || level.minScale < scaleNumber) {
					service.setCurLevel(level.id);
					ws.map.setScaleFactor((level.minScale - level.maxScale) / 2 + level.maxScale);
				}
			}
		}

		// layer
		if (opts.layer) {
			var service = ws.map.getService();
			var level = service.getLevel();
			if (level.getLayer(opts.layer)) {
				level.setCurLayer(opts.layer);
			}
		}

		// pin
		if (opts.pin) {
			if (!opts.pinX) {
				ws.map.pin.point.x = ws.map.center.x;
			}
			
			if (!opts.pinY) {
				ws.map.pin.point.y = ws.map.center.y;
			}
			
			ws.map.pin.show();
		}
		
		// pinX
		if (opts.pinX) {
			ws.map.pin.point.x = opts.pinX;
		}
		
		// pinY
		if (opts.pinY) {
			ws.map.pin.point.y = opts.pinY;
		}
		
		// pinImg
		if (opts.pinImg) {
			ws.map.pin.img = opts.pinImg;
		}
		
		// pinImgWidth
		if (opts.pinImgWidth) {
			ws.map.pin.imgWidth = opts.pinImgWidth;
		}
		
		// pinImgHeight
		if (opts.pinImgHeight) {
			ws.map.pin.imgHeight = opts.pinImgWidth;
		}
		
		// pinTooltipHeader
		if (opts.pinTooltipHeader) {
			ws.map.pin.tooltipHeader = opts.pinTooltipHeader;
		}
		
		// pinTooltipContent
		if (opts.pinTooltipContent) {
			ws.map.pin.tooltipContent = opts.pinTooltipContent;
		}
		
		// loaded
		if (opts.loaded) {
			ws.map.loaded = opts.loaded;
		}
		
		// load
		if (opts.load) {
			ws.map.load();
		}
	},
	
	/* =ws.i.tst
		Parameter:
			options					hash
		Description:
			Method to control the toaster.
			Method can receive a set of options with a hash.
			All options are optional. If not set, the default is taken.
		Example:
			ws.i.tst({
				type: 'show',
				header: 'Copyright',
				content: 'This map belongs to me.'
			});
		Options:
			type					String							'show'|'hide'											With the parameter type you can show or hide the toaster. Default is 'show'
			header					String							'This will be the header'								Sets the header of the toaster
			content					String							'This will be the content'								Sets the content of the toaster
	-----------------------------------------------------------------------*/
	tst: function(opts) {

		var type = opts.type || 'show';
		
		if (type == 'show') {
			var header = opts.header || '';
			var content = opts.content || '';
			
			ws.tst.show(header, content);
		} else if (type == 'hide') {
			ws.tst.hide();
		}
	},
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//stadtfix anfang
	
	pin:{		
		set:function (points) {
			                       			
		        var obj = [];			
			var Img_sum=points.length;
			
			var xp =new Array();
			var yp =new Array();
						  
			var width = $('#map_div').width();
			var height = $('#map_div').height();
							
			var widthHalf = width / 2;
			var heightHalf = height / 2;
			
			var lux = ws.map.center.x - widthHalf * ws.map.scale;
			var luy = ws.map.center.y + heightHalf * ws.map.scale;
			
			for (var i = 0; i<points.length; i++){
				var wapper = document.createElement("div");
                                wapper.id="wapper_Id";				
				document.getElementById("wrapper").style.zIndex=0;					   
				var img = document.createElement("img");
				document.body.appendChild(img);
				img.setAttribute("id","oImg");
				img.setAttribute("name","oImg");	
				img.src =points[i].p;				
				img_width=img.width;
				img_height=img.height;
				// Pixel diff from left upper
				xp[i] = (points[i].x  - lux) / ws.map.scale;
				yp[i] = (luy - points[i].y) / ws.map.scale;				
				img.style.position = "absolute";				
				img.style.left =xp[i]-img_width/2+ "px";
				img.style.top = yp[i]-img_height/2 + "px";
				wapper.appendChild(img);
				document.body.appendChild(wapper);	
				obj[i] =img;
	////  funktion anfang 			
				if (String(points[i].t) != 'undefined'){ 			
					ws.pin.imgMauseOver(img,points[i].t);
				}
				if (String(points[i].l) != 'undefined'){
		    	ws.pin.imgOnClick(img,points[i].l);
		    }
	//// funktion end			
				
			}
			return obj;
	        },
		
		clear:function (){
			oEle = document.getElementsByName('oImg');
			var Img_sum=oEle.length;
			for(var i=0;i<Img_sum;i++){
				var idObject = document.getElementById('oImg');   
				if (idObject != null)   
				    idObject.parentNode.removeChild(idObject);
				}  			
		}
	},
//stadtfix end
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	/* =namespace interface modules
	-----------------------------------------------------------------------*/
	m: {
		/* =ws.i.m.layer
			Parameter:
				options					hash
			Description:
				Method to control the module layer.
				Method can receive a set of options with a hash.
				All options are optional. If not set, the default is taken.
			Example:
				ws.i.m.layer({
					load: true
				});
			Options:
				load					boolean							true									Loads the layer tree
		-----------------------------------------------------------------------*/
		layer: function(opts) {
			
			// load
			if (opts.load) {
				$.ajax({
			        url: 'scripts/modules/layer/layer.gsp' + ws.c.ur,
			        data: 'service=' + ws.map.curService,
			        dataType: 'html',
			        success: function(data){
			        	$('#m_m_content_layer').html(data);
			        }
			    });
			}
		}	
	}
}






































