/*-----------------------------------------------------------------------
Objects JavaScript File

version: 	4.1
author:		sebastian kupke
email:		sebastian.kupke@baral-geohaus.de
website:	http://www.baral.de
-----------------------------------------------------------------------*/

/* =namespace module objects
-----------------------------------------------------------------------*/
ws.m.objects = {
	
	// datasource
	datasource: 'objects',
	
	// maximum results
	maxResults: 100,
	
	// is the content field searchable
	searchContent: false,
	
	// zoomTo scale
	zoomtoScale: 4000,
	
	// delay
	delay: 800,
	
	// searchIfEmpty
	searchIfEmpty: true,
	
	// tooltipWidth
	tooltipWidth: 200,
	
	// variables for sizing the image objects
	imgAutosize: false,
	imgMinScale: 100000,
	imgMaxScale: 10000,
	imgMinSize: 15,
	imgMaxSize: 35,
	
	// is it possible to check more than one group?
	concurrentCheckedGroups: true,
	
	// check child groups
	checkChildGroups: false,
	
	// from interface
	iGroupChecked: '',
	iGroupCheckedAdd: '',
	iGroupUnchecked: '',
	iGroupUncheckedAdd: '',
	
	// object id which should be highlighted after ws.map.load()
	highlightObjectId: 0,
	
	animObj: {
		obj: null,
		startWidth: 0,
		startHeight: 0,
		curWidth: 0,
		curHeight: 0,
		curTop: 0,
		curLeft: 0,
		timeout: null
	},

	// all data from conf
	queryString: '',
	
	// default layout values
	defStyle: {
		r: '10',
		strokeWidth: '3',
		strokeWidthLine: '12',
		strokeColor: '#aece1a',
		strokeOpacity: '1.0',
		fillColor: '#e4f39e',
		fillOpacity: '1.0'
	},
	
	// temp objects for click an object
	clickStart: null,
	
	// language
	l: {
		searching: '',
		noObjectsFound: '',
		oneObjectFound: '',
		objectsFound: '',
		moreObjectsFound: '',
		loading: '',
		noCoords: ''
	},
	
	isMouseover: false,

	// objects which are stored in the browser
	objects: new ws.cl.Hash(),
	
	// one object
	Object: function(id, group, groupName, groupObjectTooltip, groupObjectHover, groupObjectImgDir, groupObjectDefaultImg, url, name, tooltip, type, style, shape, minx, miny, maxx, maxy) {
		this.id = id;
		this.group = group;
		this.groupName = groupName;
		this.groupObjectTooltip = groupObjectTooltip;
		this.groupObjectHover = groupObjectHover;
		this.groupObjectImgDir = groupObjectImgDir;
		this.groupObjectDefaultImg = groupObjectDefaultImg;
		this.url = url;
		this.name = name;
		this.tooltip = tooltip;
		this.type = type;
		this.style = style;
		this.shape = shape;
		this.minx = minx;
		this.miny = miny;
		this.maxx = maxx;
		this.maxy = maxy;
	},
	
	// init
	init: function(loadObjects) {
		
		// create the tree
		ws.m.objects.createTree();
		
		// search
		$('#m_objects_search_input').keyup(ws.m.objects.search);
		
		// extParams.mObjectsGroupChecked
		if (ws.m.objects.iGroupChecked) {
			if (ws.m.objects.iGroupChecked == 'all') {
				$('#m_objects_tree input').each(function() {
					$(this).attr('checked', 'checked');
				});
			} else {
				$('#m_objects_tree input').each(function() {
					$(this).attr('checked', false);
				});
				
				var groups = ws.m.objects.iGroupChecked.split(/,/);
				for (var i in groups) {
					$('#m_objects_tree input[value=' + groups[i] + ']').attr('checked', 'checked');
				}
			}
		}

		// extParams.mObjectsGroupCheckedAdd
		if (ws.m.objects.iGroupCheckedAdd) {
			var groups = ws.m.objects.iGroupCheckedAdd.split(/,/);
			for (var i in groups) {
				$('#m_objects_tree input[value=' + groups[i] + ']').attr('checked', 'checked');
			}
		}

		// extParams.mObjectsGroupUnchecked
		if (ws.m.objects.iGroupUnchecked) {
			if (ws.m.objects.iGroupUnchecked == 'all') {
				$('#m_objects_tree input').each(function() {
					$(this).attr('checked', false);
				});
			} else {
				$('#m_objects_tree input').each(function() {
					$(this).attr('checked', 'checked');
				});
				
				var groups = ws.m.objects.iGroupUnchecked.split(/,/);
				for (var i in groups) {
					$('#m_objects_tree input[value=' + groups[i] + ']').attr('checked', false);
				}
			}
		}

		// extParams.mObjectsGroupUncheckedAdd
		if (ws.m.objects.iGroupUncheckedAdd) {
			var groups = ws.m.objects.iGroupUncheckedAdd.split(/,/);
			for (var i in groups) {
				$('#m_objects_tree input[value=' + groups[i] + ']').attr('checked', false);
			}
		}
		
		// get objects
		if (loadObjects) {
			ws.m.objects.get({show: true, async: true});
		}
	},
	
	// create the nice tree from pure html with jquery treeview plugin
	createTree: function() {
		$('#m_objects_tree').treeview({
			animated: 'fast',
			alwaysOpen: true
		});
	},

	// counter delay variable
	searchCounter: 0,
	
	// a keyup was fired
	search: function() {
		ws.m.objects.searchCounter++;
		setTimeout('ws.m.objects.searchGo(' + ws.m.objects.searchCounter + ')', ws.m.objects.delay);
	},
	
	/* =Search in the database for objects with a given string
	-----------------------------------------------------------------------*/
	searchGo: function(currentCount) {
		if (currentCount == ws.m.objects.searchCounter) {

			$('#m_objects_search_result:hidden').slideDown('slow');
			
			ws.m.objects.searchCounter = 0;
			
			if (ws.m.objects.searchIfEmpty || $('#m_objects_search_input').val().match(/.*\S.*/g)) {
				
				ws.m.info('objects', 'load', ws.m.objects.l.searching);
				
				var groups = '';
				$('#m_objects_tree input').each(function (i) {
					groups += i > 0 ? ',' : '';
					groups += this.value;
				});
		
				$.ajax({
			        url: 'scripts/modules/objects/search.gsp' + ws.c.ur,
			        data: {
						search_input: $('#m_objects_search_input').val(),
						search_content: ws.m.objects.searchContent,
						groups: groups
					},
			        dataType: 'json',
			        type: 'post',
			        success: function(data){
		
						var resultList = '<ul>';
						
						var objs = data.objects;
						
						for (i = 0; i < objs.length; i++) {
							
							try {
								var id = objs[i].id;
								var group = objs[i].group;
								var groupName = objs[i].groupName;
								var objFunction = objs[i].objFunction;
								var name = objs[i].name;
								var tooltip = objs[i].tooltip;
								var type = objs[i].type;
								var minx = objs[i].minx;
								var miny = objs[i].miny;
								var maxx = objs[i].maxx;
								var maxy = objs[i].maxy;
								
								var clazz = i % 2 == 0 ? ' class="even" ' : ' class="odd" ';
								resultList += '<li ' + clazz + ' title="' + tooltip + '" onclick="' + objFunction + 'ws.m.objects.zoomto(' + id + ',' + group + ',\'' + type + '\',' + minx + ',' + miny + ',' + maxx + ',' + maxy + ');">' + name + '<span class="informal">' + groupName + '</span></li>';
								
							} catch (e) {}
						}
		
						resultList += '</ul>';
						
						$('#m_objects_search_result').html(resultList);
						
			        	if (data.hits == 0) {
			        		ws.m.info('objects', 'failure', ws.m.objects.l.noObjectsFound);
						} else if (data.hits == 1) {
							ws.m.info('objects', 'success', ws.m.objects.l.oneObjectFound);
						} else if (data.hits > ws.m.objects.maxResults) {
							ws.m.info('objects', 'warning', ws.m.objects.l.moreObjectsFound.replace(/\{0\}/, data.hits).replace(/\{1\}/, ws.m.objects.maxResults));
						} else {
							ws.m.info('objects', 'success', ws.m.objects.l.objectsFound.replace(/\{0\}/, data.hits));
						}
			        }
			    });
			} else {
				$('#m_objects_search_result').empty();
				ws.m.info('objects');
			}
		}
	},
	
	/* =Shows all objects within a specific group in the search result window
	-----------------------------------------------------------------------*/
	showGroupObjects: function(group) {

		ws.m.info('objects', 'load', ws.m.objects.l.searching);
		
		$('#m_objects_search_result:hidden').slideDown('slow');
		
		$.ajax({
	        url: 'scripts/modules/objects/get_group_objects.gsp' + ws.c.ur,
	        data: 'group=' + group,
	        dataType: 'json',
	        type: 'post',
	        success: function(data){

				var resultList = '<ul>';
				
				var objs = data.objects;
				
				for (i = 0; i < objs.length; i++) {
					
					try {
						var id = objs[i].id;
						var group = objs[i].group;
						var groupName = objs[i].groupName;
						var objFunction = objs[i].objFunction;
						var name = objs[i].name;
						var tooltip = objs[i].tooltip;
						var type = objs[i].type;
						var minx = objs[i].minx;
						var miny = objs[i].miny;
						var maxx = objs[i].maxx;
						var maxy = objs[i].maxy;
						
						var clazz = i % 2 == 0 ? ' class="even" ' : ' class="odd" ';
						resultList += '<li ' + clazz + ' title="' + tooltip + '" onclick="ws.m.objects.zoomto(' + id + ',' + group + ',\'' + type + '\',' + minx + ',' + miny + ',' + maxx + ',' + maxy + ');' + objFunction + '">' + name + '<span class="informal">' + groupName + '</span></li>';
						
					} catch (e) {
						
					}
				}

				resultList += '</ul>';
				
				$('#m_objects_search_result').html(resultList);
				
	        	if (data.hits == 0) {
	        		ws.m.info('objects', 'failure', ws.m.objects.l.noObjectsFound);
				} else if (data.hits == 1) {
					ws.m.info('objects', 'success', ws.m.objects.l.oneObjectFound);
				} else if (data.hits > ws.m.objects.maxResults) {
					ws.m.info('objects', 'warning', ws.m.objects.l.moreObjectsFound.replace(/\{0\}/, data.hits).replace(/\{1\}/, ws.m.objects.maxResults));
				} else {
					ws.m.info('objects', 'success', ws.m.objects.l.objectsFound.replace(/\{0\}/, data.hits));
				}
	        }
	    });
	},
	
	/* =Get all objects from checked groups from the database
	 * 
	 * @param group This group was clicked, so check it. If concurrent groups are not allowed, uncheck all other groups
	 * @param show If show is true, not only load the objects in the objects hash, but show them in the map
	 * @param async If async is true, the objects will be loaded asynchron
	-----------------------------------------------------------------------*/
	get: function(opts) {
		
		var group = opts.group;
		var show = opts.show || false;
		var async = opts.async || false;
		
		ws.m.info('objects', 'load', ws.m.objects.l.loading);
		
		// only if concurrent groups are not allowed
		if (group && !ws.m.objects.concurrentCheckedGroups) {
			// do checking/unchecking only if a group was checked, not when a group was unchecked
			if ($('#m_objects_tree input[value=' + group + ']').attr('checked')) {
				$('#m_objects_tree input:checked').each(function() {
					if ($(this).val() != group) {
						$(this).attr('checked', '');
					}
				});
			}
		} else if (group) {
			if (ws.m.objects.checkChildGroups) {
				// check/uncheck all child-groups
				if ($('#m_objects_tree input[value=' + group + ']').attr('checked')) {
					$('#m_objects_group_li_' + group + ' input:checkbox').each(function() {
						$(this).attr('checked', 'checked');
					});
				} else {
					$('#m_objects_group_li_' + group + ' input:checkbox').each(function() {
						$(this).attr('checked', '');
					});
				}
			}
		}
		
		var groups = '';
		
		$('#m_objects_tree input:checked').each(function (i) {
			groups += i > 0 ? ',' : '';
			groups += this.value;
		});
		
		$.ajax({
	        url: 'scripts/modules/objects/tree_search.gsp' + ws.c.ur,
	        data: {
				groups: groups
			},
	        dataType: 'json',
	        type: 'post',
	        async: async,
	        success: function(data){
			
				var size = data.objects.length;
				
	        	if (size == 0) {
	        		ws.m.info('objects', 'failure', ws.m.objects.l.noObjectsFound);
				} else if (size == 1) {
					ws.m.info('objects', 'success', ws.m.objects.l.oneObjectFound);
				} else if (size >= ws.m.objects.maxResults) {
					ws.m.info('objects', 'warning', ws.m.objects.l.moreObjectsFound.replace(/\{0\}/,data.hits).replace(/\{1\}/,ws.m.objects.maxResults));
				} else {
					ws.m.info('objects', 'success', ws.m.objects.l.objectsFound.replace(/\{0\}/,size));
				}
	
	        	ws.m.objects.objects = new ws.cl.Hash();
	
				var objs = data.objects;
				
				for (i = 0; i < objs.length; i++) {
					try {
						var id = objs[i].id;
						var group = objs[i].group;
						var groupName = objs[i].groupName;
						var groupObjectTooltip = objs[i].groupObjectTooltip;
						var groupObjectHover = objs[i].groupObjectHover;
						var groupObjectImgDir = objs[i].groupObjectImgDir;
						var groupObjectDefaultImg = objs[i].groupObjectDefaultImg;
						var url = objs[i].url;
						var name = objs[i].name;
						var tooltip = objs[i].tooltip;
						var type = objs[i].type;
						var style = objs[i].style;
						var shape = objs[i].shape;
						var minx = objs[i].minx;
						var miny = objs[i].miny;
						var maxx = objs[i].maxx;
						var maxy = objs[i].maxy;
						
						ws.m.objects.objects.set(id, new ws.m.objects.Object(id, group, groupName, groupObjectTooltip, groupObjectHover, groupObjectImgDir, groupObjectDefaultImg, url, name, tooltip, type, style, shape, minx, miny, maxx, maxy));
						
					} catch (e) {}
				}
				
				if (show) {
					ws.m.objects.show();
				}
	        }
	    });
	},
	
	/* =Shows all the objects from the hash ws.m.objects.objects in the map.
	 * In IE VML is used
	 * In all other browsers SVG is used
	 * Shape types can be image, point, line, polygon
	-----------------------------------------------------------------------*/
	show: function() {
		
		var extent = ws.map.getExtentCoords();
		var scaleNumber = ws.map.getScaleNumber();
		
		var mapDivWidth = $('#map_div').width();
		var mapDivHeight = $('#map_div').height();
		
		var objectsDiv = $('#m_objects_div').empty().width(mapDivWidth).height(mapDivHeight);
		
		if ($.browser.msie) {
			/* =VML
			-----------------------------------------------------------------------*/
			for (var i in ws.m.objects.objects.items) {
				
				var obj = ws.m.objects.objects.items[i];
				
				if (obj && obj.type && obj.shape && ((obj.maxx > extent.minx && obj.minx < extent.maxx) && (obj.maxy > extent.miny && obj.miny < extent.maxy))) {
					
					/* =image
					-----------------------------------------------------------------------*/
					if (obj.type == 'image') {
						
						var image = ws.g.vml.createImage(obj, mapDivWidth, mapDivHeight, scaleNumber);
						objectsDiv.append(image);
						
					/* =point
					-----------------------------------------------------------------------*/
					} else if (obj.type == 'point') {
						
						var point = ws.g.vml.createPoint(obj, mapDivWidth, mapDivHeight);
						objectsDiv.append(point);
					
					/* =line
					-----------------------------------------------------------------------*/
					} else if (obj.type == 'line') {
						
						var line = ws.g.vml.createLine(obj, mapDivWidth, mapDivHeight);
						objectsDiv.append(line);
					
					/* =polygon
					-----------------------------------------------------------------------*/
					} else if (obj.type == 'polygon') {
						
						var polygon = ws.g.vml.createPolygon(obj, mapDivWidth, mapDivHeight);
						objectsDiv.append(polygon);
						
					} else {
						alert('No valid geometry type!');
					}
				}
			}
			
		} else {
			/* =SVG
			-----------------------------------------------------------------------*/
			var svgRoot = document.createElementNS(ws.g.svg.ns, 'svg');
			
			for (var i in ws.m.objects.objects.items) {
				
				var obj = ws.m.objects.objects.items[i];
				
				if (obj && obj.type && obj.shape && ((obj.maxx > extent.minx && obj.minx < extent.maxx) && (obj.maxy > extent.miny && obj.miny < extent.maxy))) {
					
					/* =image
					-----------------------------------------------------------------------*/
					if (obj.type == 'image') {
						
						var image = ws.g.svg.createImage(obj, mapDivWidth, mapDivHeight, scaleNumber);
						svgRoot.appendChild(image);
						
					/* =point
					-----------------------------------------------------------------------*/
					} else if (obj.type == 'point') {
						
						var point = ws.g.svg.createPoint(obj, mapDivWidth, mapDivHeight);
						svgRoot.appendChild(point);
						
					/* =line
					-----------------------------------------------------------------------*/
					} else if (obj.type == 'line') {
						
						var line = ws.g.svg.createLine(obj, mapDivWidth, mapDivHeight);
						svgRoot.appendChild(line);
						
					/* =polygon
					-----------------------------------------------------------------------*/
					} else if (obj.type == 'polygon') {
						
						var polygon = ws.g.svg.createPolygon(obj, mapDivWidth, mapDivHeight);
						svgRoot.appendChild(polygon);
						
					} else {
						alert('Cannot draw geometry, type is missing!')
					}
				}
			}
		
			// insert it into the dom
			$('#m_objects_div').append(svgRoot);
		}
		
		/* =tooltip
		-----------------------------------------------------------------------*/
		$('#m_objects_div *[show_tooltip=true]').each(function() {
			
			var id = $(this).attr('obj_id');
			var obj = ws.m.objects.objects.get(id);
			
			// ws.tt
			ws.tt.show(this, {
				header: obj.name,
				headerExt: obj.groupName,
				content: function() {
				
					var content = '';
					
					$.ajax({
				        url: 'scripts/modules/objects/get_content.gsp' + ws.c.ur,
				        data: 'id=' + id,
				        async: false,
				        dataType: 'text',
				        success: function(data){
							content = data;
				        }
				    });
					
					return content;
				},
				width: ws.m.objects.tooltipWidth
			});
		});
		
		/* =hover
		-----------------------------------------------------------------------*/
		$('#m_objects_div *[hover=true]').each(function() {
			
			var id = $(this).attr('obj_id');
			var obj = ws.m.objects.objects.get(id);
			
			if ($.browser.msie) {
				
				var curObj = $('#m_objects_object_' + id + ' stroke');
				var strokeWidth = curObj.attr('weight');
				var newStrokeWidth = strokeWidth ? strokeWidth * 2 : 4;
				
				newStrokeWidth = newStrokeWidth < 2 ? 2 : newStrokeWidth;
				
				if (obj.type == 'point' || obj.type == 'line' || obj.type == 'polygon') {
					$(this).mouseover(function() {
						curObj.attr('weight', newStrokeWidth);
					}).mouseout(function() {
						curObj.attr('weight', strokeWidth);
					});
				}
			} else {
				
				var strokeWidth = parseInt(this.getAttribute('stroke-width'));
				var newStrokeWidth = strokeWidth ? strokeWidth * 2 : 4;
				
				newStrokeWidth = newStrokeWidth < 2 ? 2 : newStrokeWidth;
				
				if (obj.type == 'point' || obj.type == 'line' || obj.type == 'polygon') {
					var strokeWidth = parseInt(this.getAttribute('stroke-width'));
					
					$(this).mouseover(function() {
						this.setAttribute('stroke-width', newStrokeWidth + 'px');
					}).mouseout(function() {
						this.setAttribute('stroke-width', strokeWidth + 'px');
					});
				}
			}
		});
		
		/* =highlight after zoomto
		-----------------------------------------------------------------------*/
		if (ws.m.objects.highlightObjectId) {
			
			if ($.browser.msie) {
				var obj = $('#m_objects_object_' + ws.m.objects.highlightObjectId);
				ws.m.objects.highlightObjectId = 0;

				obj.css('z-index', '2800');
				
				if (obj) {
					var type = obj.attr('shape_type');
					
					switch (type) {
						case 'image':
							ws.m.objects.animObj.obj = obj;
							
							ws.m.objects.animObj.startWidth = parseInt(obj.css('width').replace(/px/, ''));
							ws.m.objects.animObj.startHeight = parseInt(obj.css('height').replace(/px/, ''));
							ws.m.objects.animObj.curWidth = parseInt(obj.css('width').replace(/px/, ''));
							ws.m.objects.animObj.curHeight = parseInt(obj.css('height').replace(/px/, ''));
							ws.m.objects.animObj.curTop = parseInt(obj.css('top').replace(/px/, ''));
							ws.m.objects.animObj.curLeft = parseInt(obj.css('left').replace(/px/, ''));
							
							setTimeout(ws.m.objects.animateGrow, 1200);
							
							break;
							
						case 'point':
							
							break;
							
						case 'line':
							
							break;
							
						case 'polygon':
							
							break;
					}
				}
				
			} else {
				var obj = document.getElementById('m_objects_object_' + ws.m.objects.highlightObjectId);
				ws.m.objects.highlightObjectId = 0;
				
				if (obj) {
					var type = obj.getAttribute('shape_type');
					
					switch (type) {
						case 'image':
							ws.m.objects.animObj.obj = obj;
							
							ws.m.objects.animObj.startWidth = parseInt(obj.getAttribute('width'));
							ws.m.objects.animObj.startHeight = parseInt(obj.getAttribute('height'));
							ws.m.objects.animObj.curWidth = parseInt(obj.getAttribute('width'));
							ws.m.objects.animObj.curHeight = parseInt(obj.getAttribute('height'));
							ws.m.objects.animObj.curTop = parseInt(obj.getAttribute('y'));
							ws.m.objects.animObj.curLeft = parseInt(obj.getAttribute('x'));
							
							setTimeout(ws.m.objects.animateGrow, 1200);
							
							break;
							
						case 'point':
							
							break;
							
						case 'line':
							
							break;
							
						case 'polygon':
							
							break;
					}
				}
			}
		}
	},
	
	/* =animateGrow
	-----------------------------------------------------------------------*/
	animateGrow: function() {
		
		var obj = ws.m.objects.animObj.obj;
		
		ws.m.objects.animObj.curTop--;
		ws.m.objects.animObj.curLeft--;
		ws.m.objects.animObj.curWidth += 2;
		ws.m.objects.animObj.curHeight += 2;
		
		if ($.browser.msie) {
			obj.css('width', ws.m.objects.animObj.curWidth + 'px');
			obj.css('height', ws.m.objects.animObj.curHeight + 'px');
			obj.css('top', ws.m.objects.animObj.curTop + 'px');
			obj.css('left', ws.m.objects.animObj.curLeft + 'px');
		} else {
			obj.setAttribute('width', ws.m.objects.animObj.curWidth + 'px');
			obj.setAttribute('height', ws.m.objects.animObj.curHeight + 'px');
			obj.setAttribute('y', ws.m.objects.animObj.curTop + 'px');
			obj.setAttribute('x', ws.m.objects.animObj.curLeft + 'px');
		}
		
		if (ws.m.objects.animObj.curWidth < ws.m.objects.animObj.startWidth * 2) {
		    ws.m.objects.animObj.timeout = window.setTimeout(ws.m.objects.animateGrow, 40);
		} else {
			window.clearTimeout(ws.m.objects.animObj.timeout);
			ws.m.objects.highlightObjectId = 0;
		}
	},
	
	/* =Returns an object with image attributes
	-----------------------------------------------------------------------*/
	shapeToImage: function(obj) {
		
		// shape
		var coords = $(obj.shape).find('shape').attr('coords').split('@');
		
		// style (chrome bug!)
		var styleObj = $.browser.safari ? new DOMParser().parseFromString(obj.style, "text/xml") : obj.style;
		var style = $(styleObj).find('style');
		
		var src = style.attr('src');
		var width = style.attr('width');
		var height = style.attr('height');
		
		// default
		src = src ? src : obj.groupObjectDefaultImg;
		width = width ? width : 30;
		height = height ? height : 30;
		
		return {point: new ws.cl.Point(coords[0], coords[1]), src: src, width: width, height: height};
	},
	
	/* =Returns an object with point attributes
	-----------------------------------------------------------------------*/
	shapeToPoint: function(obj) {
		
		// shape
		var coords = $(obj.shape).find('shape').attr('coords').split('@');
		
		// style (chrome bug!)
		var styleObj = $.browser.safari ? new DOMParser().parseFromString(obj.style, "text/xml") : obj.style;
		var style = $(styleObj).find('style');
		
		var r = style.attr('r');
		var strokeColor = style.attr('stroke-color');
		var strokeWidth = style.attr('stroke-width');
		var strokeOpacity = style.attr('stroke-opacity');
		var fillColor = style.attr('fill-color');
		var fillOpacity = style.attr('fill-opacity');
		
		// default
		r = r ? r : ws.m.objects.defStyle.r;
		strokeColor = strokeColor ? strokeColor : ws.m.objects.defStyle.strokeColor;
		strokeWidth = strokeWidth ? strokeWidth : ws.m.objects.defStyle.strokeWidth;
		strokeOpacity = strokeOpacity ? strokeOpacity : ws.m.objects.defStyle.strokeOpacity;
		fillColor = fillColor ? fillColor : ws.m.objects.defStyle.fillColor;
		fillOpacity = fillOpacity ? fillOpacity : ws.m.objects.defStyle.fillOpacity;
		
		return {point: new ws.cl.Point(coords[0], coords[1]), r: r, strokeColor: strokeColor, strokeWidth: strokeWidth, strokeOpacity: strokeOpacity, fillColor: fillColor, fillOpacity: fillOpacity};
	},
	
	/* =Returns an object with line attributes
	-----------------------------------------------------------------------*/
	shapeToLine: function(obj) {
		
		// shape
		var coords = $(obj.shape).find('shape').attr('coords').split(';');
		var points = [];
		
		for (i = 0; i < coords.length; i++) {
			var coord = coords[i].split('@');
			points.push(new ws.cl.Point(coord[0], coord[1]));
		}
		
		// style (chrome bug!)
		var styleObj = $.browser.safari ? new DOMParser().parseFromString(obj.style, "text/xml") : obj.style;
		var style = $(styleObj).find('style');
		
		var strokeColor = style.attr('stroke-color');
		var strokeWidth = style.attr('stroke-width');
		var strokeOpacity = style.attr('stroke-opacity');
		
		// default
		strokeColor = strokeColor ? strokeColor : ws.m.objects.defStyle.strokeColor;
		strokeWidth = strokeWidth ? strokeWidth : ws.m.objects.defStyle.strokeWidthLine;
		strokeOpacity = strokeOpacity ? strokeOpacity : ws.m.objects.defStyle.strokeOpacity;
		
		return {points: points, strokeColor: strokeColor, strokeWidth: strokeWidth, strokeOpacity: strokeOpacity};
	},
	
	/* =Returns an object with polygon attributes
	-----------------------------------------------------------------------*/
	shapeToPolygon: function(obj) {
		
		// shape
		var coords = $(obj.shape).find('shape').attr('coords').split(';');
		var points = [];
		
		for (i = 0; i < coords.length; i++) {
			var coord = coords[i].split('@');
			points.push(new ws.cl.Point(coord[0], coord[1]));
		}
		
		// style (chrome bug!)
		var styleObj = $.browser.safari ? new DOMParser().parseFromString(obj.style, "text/xml") : obj.style;
		var style = $(styleObj).find('style');
		
		var strokeColor = style.attr('stroke-color');
		var strokeWidth = style.attr('stroke-width');
		var strokeOpacity = style.attr('stroke-opacity');
		var fillColor = style.attr('fill-color');
		var fillOpacity = style.attr('fill-opacity');
		
		// default
		strokeColor = strokeColor ? strokeColor : ws.m.objects.defStyle.strokeColor;
		strokeWidth = strokeWidth ? strokeWidth : ws.m.objects.defStyle.strokeWidth;
		strokeOpacity = strokeOpacity ? strokeOpacity : ws.m.objects.defStyle.strokeOpacity;
		fillColor = fillColor ? fillColor : ws.m.objects.defStyle.fillColor;
		fillOpacity = fillOpacity ? fillOpacity : ws.m.objects.defStyle.fillOpacity;
		
		return {points: points, strokeColor: strokeColor, strokeWidth: strokeWidth, strokeOpacity: strokeOpacity, fillColor: fillColor, fillOpacity: fillOpacity};
	},
	
	/* =Zoom to the bounding box of a specific object (first, get the objects within this group from group tree)
	-----------------------------------------------------------------------*/
	zoomto: function(id, group, type, minx, miny, maxx, maxy) {
		
		var newCenter = new ws.cl.Point((minx + maxx) / 2, (miny + maxy) / 2);
		
		if (minx && miny && maxx && maxy) {
			
			// object to scale, object will be scaled after getMap() and objects has been loaded
			ws.m.objects.highlightObjectId = id;
			
			// add/set choosen group
			$('#m_objects_tree input[value=' + group + ']').attr('checked', 'checked');
			
			// get objects from tree
			ws.m.objects.get({group: group, show: false, async: false});
			
			if (type == 'point' || type == 'image') {
				ws.map.zoomto(newCenter.x, newCenter.y, ws.m.objects.zoomtoScale);
			} else {
				var widthPixel = $('#map_div').width();				
				ws.map.scale = (maxy - miny) / widthPixel;

				ws.map.center.x = newCenter.x;
				ws.map.center.y = newCenter.y;
				
				ws.map.load();
			}
			
		} else {
			alert(ws.m.objects.l.noCoords.replace(/\{0\}/, id)); 
		}
	},
	
	/* =Calculate the width and height of an image
	-----------------------------------------------------------------------*/
	getImageSize: function(scaleNumber) {
		
		var widthHeight = 0;
		
		if (scaleNumber >= ws.m.objects.imgMinScale) {
			widthHeight = ws.m.objects.imgMinSize;
		} else if (scaleNumber <= ws.m.objects.imgMaxScale) {
			widthHeight = ws.m.objects.imgMaxSize;
		} else {
			var diffImgScale = ws.m.objects.imgMinScale - ws.m.objects.imgMaxScale;
			var realDiffImgScale = scaleNumber - ws.m.objects.imgMaxScale;
			var percent = (realDiffImgScale * 100) / diffImgScale;
			
			var diffImgSize = ws.m.objects.imgMaxSize - ws.m.objects.imgMinSize;
			var imgDiffSize = (diffImgSize * percent) / 100
			
			widthHeight = ws.m.objects.imgMaxSize - imgDiffSize;
		}
		
		return Math.round(widthHeight);
	},
	
	/* =Uncheck all groups in the group tree
	-----------------------------------------------------------------------*/
	uncheckGroups: function() {
		$('#m_objects_tree input:checked').each(function (i) {
			$(this).attr('checked','');
		});
		$('#m_objects_div').empty();
	}
};























