/*-----------------------------------------------------------------------
User JavaScript File

version: 	4.0
author:		sebastian kupke
email:		sebastian.kupke@baral.de
website:	http://www.baral.de
-----------------------------------------------------------------------*/


/* =iMap
	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 getMap is not set to true, nothing will happen.
		You should always set getMap to true.
	Example:
		iMap({
			service: 0,
			scale: 5000,
			x: 3450000,
			y: 5400000,
			getMap: true
		});
	Options:
		x|rw					number|string					3450000|'3450000'						The x coordinate of the center
		y|hw					number|string					5400000|'5400000'						The y coordinate of the center
		scale					number|string					2500|'2500'								The scalefactor
		service					number|string					0|'<myservice>'							The id or running number of the service
		minScale				number|string					100000|'100000'							The minimal available scalefactor
		maxScale				number|string					100|'100'								The maximal available scalefactor
		minx					number|string					3450000|'3450000'						The minimal x coordinate
		miny					number|string					5400000|'5400000'						The minimal y coordinate
		maxx					number|string					3451000|'3451000'						The maximal x coordinate
		maxy					number|string					5401000|'5401000'						The maximal y coordinate
		pin						boolean|number|string			true|1|'true'							Shows the pin. If pinX and pinY is not set, it will be centered to the map center
		pinX					number|string					3450000|'3450000'						Sets the pin to the x coordinate
		pinY					number|string					5400000|'5400000'						Sets the pin to the y coordinate
		pinImg					string							'mypin.gif'								Sets the pin image
		pinImgWidth				number|string					25|'25'									Sets the pin image width
		pinImgHeight			number|string					25|'25'									Sets the pin image height
		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
		getMap					boolean|number|string			true|1|'true'							Request a map
-----------------------------------------------------------------------*/
function iMap(options) {

	// if x or rw is set
	if (options.x || options.rw) {
		options.x = options.x || options.rw;
		
		if (options.x.constructor == String) {
			center.x = parseFloat(options.x);
		} else if (options.x.constructor == Number) {
			center.x = options.x;
		} else {
			alert("x or rw is not a number or a string!");
		}
	}
	
	// if y or hw is set
	if (options.y || options.hw) {
		options.y = options.y || options.hw;
		
		if (options.y.constructor == String) {
			center.y = parseFloat(options.y);
		} else if (options.y.constructor == Number) {
			center.y = options.y;
		} else {
			alert("y or hw is not a number or a string!");
		}
	}
	
	// if scale is set
	if (options.scale) {
		if (options.scale.constructor == String) {
			setScaleFactor(parseInt(options.scale));
		} else if (options.scale.constructor == Number) {
			setScaleFactor(options.scale);
		} else {
			alert("scale is not a number or a string!");
		}
	}
	
	// if param service is set
	if (options.service) {
		if (options.service.constructor == String) {
			currentService = options.service;
		} else if (options.service.constructor == Number) {
			var i = 0;
			services.each(function(service) {
				if (options.service == i) {
					currentService = service;
				}
	
				i++;
			});
		} else {
			alert("service is not a number or a string!");
		}
	}
	
	// if minScale is set
	if (options.minScale) {
		if (options.minScale.constructor == String) {
			if (options.minScale.length > 0) {
				minScale = parseInt(options.minScale);
			}
		} else if (options.minScale.constructor == Number) {
			minScale = options.minScale;
		} else {
			alert("minScale is not a number or a string!");
		}
	}
	
	// if maxScale is set
	if (options.maxScale) {
		if (options.maxScale.constructor == String) {
			if (options.maxScale.length > 0) {
				maxScale = parseInt(options.maxScale);
			}
		} else if (options.maxScale.constructor == Number) {
			maxScale = options.maxScale;
		} else {
			alert("maxScale is not a number or a string!");
		}
	}
	
	// if minx is set
	if (options.minx) {
		if (options.minx.constructor == String) {
			if (options.minx.length > 0) {
				boundaryExtent.minx = parseInt(options.minx);
			}
		} else if (options.minx.constructor == Number) {
			boundaryExtent.minx = options.minx;
		} else {
			alert("minx is not a number or a string!");
		}
	}
	
	// if miny is set
	if (options.miny) {
		if (options.miny.constructor == String) {
			if (options.miny.length > 0) {
				boundaryExtent.miny = parseInt(options.miny);
			}
		} else if (options.miny.constructor == Number) {
			boundaryExtent.miny = options.miny;
		} else {
			alert("miny is not a number or a string!");
		}
	}
	
	// if maxx is set
	if (options.maxx) {
		if (options.maxx.constructor == String) {
			if (options.maxx.length > 0) {
				boundaryExtent.maxx = parseInt(options.maxx);
			}
		} else if (options.maxx.constructor == Number) {
			boundaryExtent.maxx = options.maxx;
		} else {
			alert("maxx is not a number or a string!");
		}
	}
	
	// if maxy is set
	if (options.maxy) {
		if (options.maxy.constructor == String) {
			if (options.maxy.length > 0) {
				boundaryExtent.maxy = parseInt(options.maxy);
			}
		} else if (options.maxy.constructor == Number) {
			boundaryExtent.maxy = options.maxy;
		} else {
			alert("maxy is not a number or a string!");
		}
	}
	
	// if pin is set
	if (options.pin) {
		
		if (!options.pinX) {
			userPin.point.x = center.x;
		}
		
		if (!options.pinY) {
			userPin.point.y = center.y;
		}
		
		if (!$.browser.msie) {
			$('#user_pin').effect('pulsate',{times: 2});
		}
	}
	
	// if pinX is set
	if (options.pinX) {
		if (options.pinX.constructor == String) {
			userPin.point.x = parseInt(options.pinX);
		} else if (options.pinX.constructor == Number) {
			userPin.point.x = options.pinX;
		} else {
			alert("pinX is not a number or a string!");
		}
	}
	
	// if pinImgWidth is set
	if (options.pinY) {
		if (options.pinY.constructor == String) {
			userPin.point.y = parseInt(options.pinY);
		} else if (options.pinY.constructor == Number) {
			userPin.point.y = options.pinY;
		} else {
			alert("pinY is not a number or a string!");
		}
	}
	
	// if pinImg is set
	if (options.pinImg) {
		if (options.pinImg.constructor == String) {
			userPin.img = options.pinImg;
		} else {
			alert("pinImg is not a string!");
		}
	}
	
	// if pinImgWidth is set
	if (options.pinImgWidth) {
		if (options.pinImgWidth.constructor == String) {
			userPin.imgWidth = parseInt(options.pinImgWidth);
		} else if (options.pinImgWidth.constructor == Number) {
			userPin.imgWidth = options.pinImgWidth;
		} else {
			alert("pinImgWidth is not a number or a string!");
		}
	}
	
	// if pinImgHeight is set
	if (options.pinImgHeight) {
		if (options.pinImgHeight.constructor == String) {
			userPin.imgHeight = parseInt(options.pinImgHeight);
		} else if (options.pinImgHeight.constructor == Number) {
			userPin.imgHeight = options.pinImgWidth;
		} else {
			alert("pinImgHeight is not a number or a string!");
		}
	}
	
	// if pinTooltipHeader is set
	if (options.pinTooltipHeader) {
		if (options.pinTooltipHeader.constructor == String) {
			userPin.tooltipHeader = options.pinTooltipHeader;
		} else {
			alert("pinTooltipHeader is not a string!");
		}
	}
	
	// if pinTooltipContent is set
	if (options.pinTooltipContent) {
		if (options.pinTooltipContent.constructor == String) {
			userPin.tooltipContent = options.pinTooltipContent;
		} else {
			alert("pinTooltipContent is not a string!");
		}
	}
	
	// if getMap is set and is true
	if (options.getMap) {
		getMap();
	}
}

/* =iLayerTree
	Parameter:
		options					hash
	Description:
		Method to control the layer tree.
		Method can receive a set of options with a hash.
		All options are optional. If not set, the default is taken.
	Example:
		iLayerTree({
			reload: true
		});
	Options:
		service					number|string					0|'<myservice>'							The id or running number of the service of which the layer tree should be loaded. In the moment it takes only affect if the reload param is set to true.
		reload					boolean|number|string			true|1|'true'							Reloads the layer tree
		getMap					boolean|number|string			true|1|'true'							Request a map
-----------------------------------------------------------------------*/
function iLayerTree(options) {

	// if service is set
	if (options.service) {
		if (options.service.constructor == String) {
			currentService = options.service;
		} else if (options.service.constructor == Number) {
			currentService = options.service.toString();
		} else {
			alert("service is not a number or a string!");
		}
	}
	
	// if reload is set
	if (options.reload) {
		$.ajax({
			url: 'scripts/modules/layerTree/layerTree.gsp',
			data: 'service=' + currentService,
			dataType: 'html',
			success: function(data){
				$('#layerTree').html(data);
			}
		});
	}
	
	// if getMap is set and is true
	if (options.getMap) {
		getMap();
	}
}

/* =iSelectRect
	Parameter:
		options					hash
	Description:
		Method to control the rectangle selection.
		Method can receive a set of options with a hash.
		All options are optional. If not set, the default is taken.
	Example:
		iSelectRect({
			minX: 3460000,
			minY: 5400000,
			maxX: 3461000,
			maxY: 5401000,
			show: true
		});
	Options:
		show					boolean|string					true|'true'								Shows or hides the select rect.
		minX					number|string					3450000|'3450000'						Sets the lower left x coordinate
		minY					number|string					5400000|'5400000'						Sets the lower left y coordinate
		maxX					number|string					3451000|'3451000'						Sets the upper right x coordinate
		maxY					number|string					5401000|'5401000'						Sets the upper right y coordinate
-----------------------------------------------------------------------*/
function iSelectRect(options) {

	// if minX is set
	if (options.minX) {
		if (options.minX.constructor == String) {
			selectRectObj.lowerLeftPoint.x = parseInt(options.minX);
		} else if (options.minX.constructor == Number) {
			selectRectObj.lowerLeftPoint.x = options.minX;
		} else {
			alert("minX is not a number or a string!");
		}
	}
	
	// if minY is set
	if (options.minY) {
		if (options.minY.constructor == String) {
			selectRectObj.lowerLeftPoint.y = parseInt(options.minY);
		} else if (options.minY.constructor == Number) {
			selectRectObj.lowerLeftPoint.y = options.minY;
		} else {
			alert("minY is not a number or a string!");
		}
	}
	
	// if maxX is set
	if (options.maxX) {
		if (options.maxX.constructor == String) {
			selectRectObj.upperRightPoint.x = parseInt(options.maxX);
		} else if (options.maxX.constructor == Number) {
			selectRectObj.upperRightPoint.x = options.maxX;
		} else {
			alert("maxX is not a number or a string!");
		}
	}
	
	// if maxY is set
	if (options.maxY) {
		if (options.maxY.constructor == String) {
			selectRectObj.upperRightPoint.y = parseInt(options.maxY);
		} else if (options.maxY.constructor == Number) {
			selectRectObj.upperRightPoint.y = options.maxY;
		} else {
			alert("maxY is not a number or a string!");
		}
	}
	
	// if show is set and is true
	if (options.show) {
		showSelectRect();
	} else {
		$('#select_rect').hide();
	}
}

/* =iSelectPoly
	Parameter:
		options					hash
	Description:
		Method to control the polygon selection.
		Method can receive a set of options with a hash.
		All options are optional. If not set, the default is taken.
	Example:
		iSelectPoly({
			points: [3450000,5420000,3451000,5500000,3454000,5320000],
			show: true
		});
	Options:
		show					boolean|string					true|'true'								Shows or hides the select polygon.
		points					array[number]					[3450000,5420000,...]					All even numbers are x values, all odd numbers are y values.
		dragVertexes			boolean|string					true|'true'								True makes the vertexes draggable, if set to false, the vertexes are not draggable
-----------------------------------------------------------------------*/
function iSelectPoly(options) {

	// if points is set
	if (options.points) {
		selectPolygonObj = new Polygon();
		selectPolygonObj.points = new Array();
		for (i = 0; i < options.points.length; i += 2) {
			selectPolygonObj.points.push(new Point(options.points[i],options.points[i + 1]));
		}
	}
	
	// if dragVertexes is set and is true
	if (options.dragVertexes) {
		selectPolygonObj.dragVertexes = true;
	} else {
		selectPolygonObj.dragVertexes = false;
	}
	
	// if show is set and is true
	if (options.show) {
		showSelectPolygon('close');
	} else {
		$('#select_polygon').hide();
	}
}

/* =iSelectCircle
	Parameter:
		options					hash
	Description:
		Method to control the circle selection.
		Method can receive a set of options with a hash.
		All options are optional. If not set, the default is taken.
	Example:
		iSelectCircle({
			centerX: 3460000,
			centerY: 5400000,
			radius: 100,
			show: true
		});
	Options:
		show					boolean|string					true|'true'								Shows or hides the select circle.
		centerX					number|string					3450000|'3450000'						Sets the center x coordinate
		centerY					number|string					5400000|'5400000'						Sets the center y coordinate
		radius					number|string					100|'100'								Sets the radius of the circle
-----------------------------------------------------------------------*/
/*
function iSelectCircle(options) {

	// if centerX is set
	if (options.centerX) {
		if (options.centerX.constructor == String) {
			selectCircleObj.center.x = parseInt(options.centerX);
		} else if (options.centerX.constructor == Number) {
			selectCircleObj.center.x = options.centerX;
		} else {
			alert("centerX is not a number or a string!");
		}
	}
	
	// if centerY is set
	if (options.centerY) {
		if (options.centerY.constructor == String) {
			selectCircleObj.center.y = parseInt(options.centerY);
		} else if (options.centerY.constructor == Number) {
			selectCircleObj.center.y = options.centerY;
		} else {
			alert("centerY is not a number or a string!");
		}
	}
	
	// if radius is set
	if (options.radius) {
		if (options.radius.constructor == String) {
			selectCircleObj.radius = parseInt(options.radius);
		} else if (options.radius.constructor == Number) {
			selectCircleObj.radius = options.radius;
		} else {
			alert("radius is not a number or a string!");
		}
	}
	
	// if show is set and is true
	if (options.show) {
		showSelectCircle();
	} else {
		$('#select_circle').hide();
	}
}
*/







