package classes.matuto.utils.display 
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	
	/**
	 * ...
	 * @author DefaultUser (Tools -> Custom Arguments...)
	 */
	final public class Align 
	{
		
		public static const NONE 	:String = "none";
		public static const MIN 	:String = "min";
		public static const CENTER 	:String = "center";
		public static const MAX 	:String = "max";
		
		public static function alignTo( fromObj :DisplayObject, toObj :DisplayObject, xAlignFrom :String = null, yAlignFrom :String = null, xAlignTo :String = null, yAlignTo :String = null, _x :Number = 0, _y :Number = 0, pct :Boolean = false ) :void
		{
			
			var toWidth  :String;
			var toHeight :String;
			
			if(toObj is Stage){
				toWidth  = "stageWidth";
				toHeight = "stageHeight";
			}else{
				toWidth	 = "width";
				toHeight = "height";
			}
			
			var fromX 	:Number = getValue(fromObj, xAlignFrom, "width");
			var fromY 	:Number = getValue(fromObj, yAlignFrom, "height");
			var toX		:Number = getValue(toObj, xAlignTo, toWidth);
			var toY		:Number = getValue(toObj, yAlignTo, toHeight);
			
			toX = toObj.x + toX;
			toY = toObj.y + toY;
			
			if(xAlignTo==NONE) toX = fromObj.x;
			if(yAlignTo==NONE) toY = fromObj.y;
			
			fromObj.x = ( toX - fromX ) + _x;
			fromObj.y = ( toY - fromY ) + _y;
			
		}
		
		
		private static function getValue(obj :DisplayObject, align :String, property :String ) :Number
		{
			
			var value :Number;
			
			switch(align){
				case NONE:
					value = 0;
					break;
					
				case MIN:
					value = 0;
					break;
					
				case CENTER:
					value = (obj[property]>>1);
					break;
					
				case MAX:
					value = obj[property];
					break;
					
				default:
					value = 0;
					break;
			}
			
			return value;
			
		}
		
	}
	
}