// 8< ---[Placement.js]---
var Placement={}
Placement._VERSION_='0.1.2';
Placement.getPosition=	function(element){
		// Gets the position of an element relative to the viewport. 
		// For accurate calculations make sure to use pixel values for margins, borders and padding. 
		// This method only works with visible and invisible elements.
		var __this__=Placement;
		var e=$(element);
		var wasVisible=true;
		if ( e.is(":hidden") )
		{
			wasVisible = false;
			e.show()
		}
		var position=e.offset({"scroll":false, "padding":true, "margin":true});
		if ( (! wasVisible) )
		{
			e.hide()
		}
		return {"x":position.left, "y":position.top}
	}
Placement.getRelativePosition=	function(element){
		// Gets the top and left position of an element relative to its offset parent. 
		// For accurate calculations make sure to use pixel values for margins, borders and padding.
		// This method only works with visible and invisible elements.
		var __this__=Placement;
		var e=$(element);
		var wasVisible=true;
		if ( e.is(":hidden") )
		{
			wasVisible = false;
			e.show()
		}
		var position=e.position();
		if ( (! wasVisible) )
		{
			e.hide()
		}
		return {"x":position.left, "y":position.top}
	}
Placement.setPosition=	function(element, pos){
		var __this__=Placement;
		$(element).css({"position":"absolute", "left":(pos.x + "px"), "top":(pos.y + "px")})
	}
Placement.setArea=	function(element, area){
		var __this__=Placement;
		$(element).css({"position":"absolute", "left":(area.x + "px"), "top":(area.y + "px"), "width":(area.w + "px"), "height":(area.h + "px")})
	}
Placement.getArea=	function(element){
		// returns the area with the x and y relative to the viewport
		var __this__=Placement;
		var e=$(element);
		var position=Placement.getPosition(e);
		var area={"x":position.x, "y":position.y, "w":e.width(), "h":e.height()};
		return area
	}
Placement.getRelativeArea=	function(element){
		// returns the area with the x and y relative to it's parent
		var __this__=Placement;
		var e=$(element);
		var position=Placement.getRelativePosition(e);
		var area={"x":position.x, "y":position.y, "w":e.width(), "h":e.height()};
		return area
	}
Placement.contains=	function(container, area){
		var __this__=Placement;
		var x_in=((area.x >= container.x) && (area.x <= (container.x + container.w)));
		var y_in=((area.y >= container.y) && (area.y <= (container.y + container.h)));
		if ( (x_in && y_in) )
		{
			return true
		}
		else if ( true )
		{
			return false
		}
	}
Placement.getAnchorPosition=	function(element, anchor){
		var __this__=Placement;
		var area=Placement.getArea(element);
		var res=[(area.x + (area.w / 2)), area.y];
		if ( (anchor == "N") )
		{res = [(area.x + (area.w / 2)), area.y];}
		if ( (anchor == "NE") )
		{res = [(area.x + area.w), area.y];}
		if ( (anchor == "E") )
		{res = [(area.x + area.w), (area.y + (area.h / 2))];}
		if ( (anchor == "SE") )
		{res = [(area.x + area.w), (area.y + area.h)];}
		if ( (anchor == "S") )
		{res = [(area.x + (area.w / 2)), (area.y + area.h)];}
		if ( (anchor == "SW") )
		{res = [area.x, (area.y + area.h)];}
		if ( (anchor == "W") )
		{res = [area.x, (area.y + (area.h / 2))];}
		if ( (anchor == "NW") )
		{res = [area.x, area.y];}
		return {"x":res[0], "y":res[1]}
	}
Placement.place=	function(targetElement, targetAnchor, element, elementAnchor, offset, minpos){
		// places the object in the desired position
		var __this__=Placement;
		var position=Placement.estimatePosition(targetElement, targetAnchor, element, elementAnchor, offset, minpos);
		var target_x=position.x;
		var target_y=position.y;
		Placement.setPosition(element, [target_x, target_y])
	}
Placement.estimatePosition=	function(targetElement, targetAnchor, element, elementAnchor, offset, minpos){
		// Calculates the target positon 	
		var __this__=Placement;
		var target_pos=Placement.getAnchorPosition(targetElement, targetAnchor);
		var element_pos=Placement.getPosition(element);
		var element_apos=Placement.getAnchorPosition(element, elementAnchor);
		var anchor_offset={"x":(element_pos.x - element_apos.x), "y":(element_pos.y - element_apos.y)};
		var offset=(offset || {"x":0, "y":0});
		var target_x=((target_pos.x + anchor_offset.x) + offset.x);
		var target_y=((target_pos.y + anchor_offset.y) + offset.y);
		target_x = Math.floor(Math.max(target_x, 0));
		target_y = Math.floor(Math.max(target_y, 0));
		return {"x":target_x, "y":target_y}
	}
Placement.inPlace=	function(source, targetElement, margin){
		// Displays the @source element "in place" of the @target element. This
		// basically just gets the position for the target, and resizes the
		// element so that it fits the target.
		var __this__=Placement;
		var area=Placement.getArea(targetElement);
		if ( margin )
		{
			area.x = (area.x - margin);
			area.y = (area.y - margin);
			area.w = (area.w + (margin * 2));
			area.h = (area.h + (margin * 2));
		}
		Placement.setArea(source, area)
	}
Placement.isAreaOver=	function(area1, area2){
		var __this__=Placement;
		if ( ((((area1.x > area2.x) && (area1.x < (area2.x + area2.w))) || (((area1.x + area1.w) > area2.x) && ((area1.x + area1.w) < (area2.x + area2.w)))) || (((area1.w > area2.w) && (area1.x < area2.x)) && ((area1.x + area1.w) > (area2.x + area2.w)))) )
		{
			return true
		}
		return false
	}
Placement.isOver=	function(element1, element2){
		// returns true if an element is over an another one
		var __this__=Placement;
		var area1=Placement.getArea(element1);
		var area2=Placement.getArea(element2);
		return Placement.isAreaOver(area1, area2)
	}
Placement.init=	function(){
		var __this__=Placement;
	}
Placement.init()

