/*Core Geometry*/
var FLPoint = function(aX, aY) {
	this.x=(aX) ? aX : 0;
	this.y=(aY) ? aY : 0;
}

var FLSize = function(aW,aH) {
	this.width= (aW) ? aW : 0;
	this.height= (aH) ? aH : 0;
}

var FLRect = function(x,y,width,height) {
	this.origin = new FLPoint(x,y);
	this.size = new FLSize(width,height);
}

var FLBounds = function(aLeft,aTop,aRight,aBottom) {
	this.left = (aLeft) ? aLeft : 0;
	this.right = aRight ? aRight : 0;
	this.top = (aTop) ? aTop : 0;
	this.bottom = aBottom ? aBottom : 0;
}

/*Event System */
var FLEvent = function(aType, aHandler,anObject, isBubbled) {
	this.type = aType;
	this.handler = aHandler;
	this.object = anObject;
	this.bubbled = isBubbled;
	this.event;
	this.x = function() {
		return this.event.pageX;
	};
	this.y = function() { return this.event.pageY; };
	this.mouseIsDown;
}

/*Styles*/
var FLColor = function(red,green,blue,alpha) {
	this.red = (red) ? red : 0;
	this.green = (green) ? green : 0;
	this.blue = (blue) ? blue : 0;
	this.alpha = (alpha) ? alpha : 0;
	
	this.RGB = function() { return 'rgb('+this.red+','+this.green+','+this.blue+')'; };
	this.RGBA = function() { return 'rgba('+this.red+','+this.green+','+this.blue+','+this.alpha+')'; };
	this.RGB255 = function() {return'rgb('+(this.red * 255)+'%,'+(this.green * 255)+'%,'+(this.blue * 255)+'%)'; };
	this.RGB100 = function() {return'rgb('+(this.red * 100)+'%,'+(this.green * 100)+'%,'+(this.blue * 100)+'%)';};
	this.RGBA100 = function() {return 'rgba('+(this.red * 100)+'%,'+(this.green * 100)+'%,'+(this.blue * 100)+'%,'+(this.alpha * 100)+'%)'; };
}

var FLBorder = function(aWidth,aType,aColor) {

	this.width = function(aVal) { this.leftWidth=aVal; this.rightWidth=aVal; this.topWidth=aVal; this.bottomWidth=aVal; return this.leftWidth;};
	this.type = function(aVal) { this.lefType=aVal; this.rightType=aVal; this.topType=aVal; this.bottomType=aVal; return this.leftType;};
	this.color = function(aVal) { this.leftColor=aVal; this.rightColor=aVal; this.topColor=aVal; this.bottomCoor=aVal; return this.leftColor;};
	this.width((aWidth) ? aWidth : 0);
	this.type((aType) ? aType : 0);
	this.color((aColor) ? aColor : new FLColor());
	
	this.leftWidth;
	this.leftStyle;
	this.leftColor;
	
	this.righttWidth;
	this.rightStyle;
	this.rightColor;
	
	this.topWidth;
	this.topStyle;
	this.topColor;
	
	this.bottomWidth;
	this.bottomStyle;
	this.bottomColor;
}

var FLOutline = function(aWidth,aType,aColor) {
	this.width = (aWidth) ? aWidth : 0;
	this.type = (aType) ? aType : 0;
	this.color = (aColor) ? aColor : new FLColor();
}

var FLShadow = function(anX,aY, theBlur, theColor, isInset) {
	this.offsetX = (anX) ? anX: 0;
	this.offsetY = (aY) ? aY: 0;
	this.blur = (theBlur) ? theBlur: 0;
	this.inset = (isInset) ? isInset: false;
	this.color =  (theColor) ? theColor: new FLColor(0,0,0,1.0);
}

var FLCurve = function(theLeftTop, theRightTop, theBottomRight, theBottomLeft) {
	this.leftTop = theLeftTop;
	this.rightTop = theRightTop;
	this.rightBottom = theBottomRight;
	this.leftBottom = theBottomLeft;
}

/*Object*/
newClass('FLObject');
	addMethod('init', function() {} );
	
	addMethod('isEqualToClass', function(aClass) {
		if(this.classObject === aClass) return true;
		return false;
	});
	
endClass();

newClass('FLResponse', 'FLObject');
	addVar('_events', []);
	addVar('_event_function', []);
	addVar('_event_names',[]);

	addVar('defaultEventsEnabled', false);
	
	addMethod('init', function() {
		this.superClass().send('init');
		if(this.sel('inputDown')!=-1) {
			send('addEvent', 'inputDown', new FLEvent('mousedown', 'inputDown', this, false));
		}
		if(!send('inputUp')==-1) send('addEvent', new FLEvent('mousedown', 'inputUp', this, false));
		if(!send('inputMove')==-1) send('addEvent', new FLEvent('mousedown', 'inputMove', this, false));
		if(!send('inputOut')==-1) send('addEvent', new FLEvent('mousedown', 'inputOut', this, false));
		if(!send('inputOver')==-1) send('addEvent', new FLEvent('mousedown', 'inputOver', this, false));
		if(!send('keyDown')==-1) send('addEvent', new FLEvent('mousedown', 'keyDown', this, false));
		if(!send('keyUp')==-1) send('addEvent', new FLEvent('mousedown', 'keyUp', this, false));
	});
	
	addMethod('addEvent', function(aName, anEvent) {
		var theObject = this, theElement;
		if(!anEvent.object) anEvent.object = theObject;
		var func = function(e) {
			anEvent.event = e;
			anEvent.inputIsDown = pyro.event.inputDown;
			if(anEvent.handler) theObject.send(anEvent.handler, anEvent);
			if(anEvent.type=='mousedown') pyro.event.inputDown=true;
			
			if(!theObject.get('defaultEventsEnabled')) e.preventDefault();
		}
		if(anEvent.object.get) theElement = anEvent.object.get('element');
		else theElement = anEvent.object;
		
		if(document.addEventListener) theElement.addEventListener(anEvent.type, func, anEvent.bubbled);
		else if(document.attachEvent) theElement.attachEvent(anEvent.type, func);
		
		get('_events').push(anEvent);
		get('_event_names').push(aName);
		get('_event_function').push(func);
	});
	
	addMethod('removeEvent', function(aName) {
		var eventIndex = get('_event_names').indexOf(aName);
		var theElement, theEvent;
		if(eventIndex > -1) {
			theEvent = get('_events')[eventIndex];
			theElement = (theEvent.object.get) ? theEvent.object.get('element') : theEvent.object;
			
			if(document.removeEventListener) theElement.removeEventListener(theEvent.type, get('_event_function')[eventIndex], theEvent.bubbled);
			else if(document.attachEvent) theElement.detachEvent(theEvent.type, get('_event_function')[eventIndex]);

			get('_events').splice(eventIndex,1);
			get('_event_names').splice(eventIndex,1);
			get('_event_function').splice(eventIndex,1);
		}
	});
	
	addMethod('eventWithName', function(aName) {
		return get('_events')[get('_event_names').indexOf(aName)];
	});
	
	addMethod('inputUp', function(theEvent) {
		return -1;
	});
	
	addMethod('inputMove', function(theEvent) {
		return -1;
	});
	
	addMethod('inputOut', function(theEvent) {
		return -1;
	});
	
	addMethod('inputOver', function(theEvent) {
		return -1;
	});
	
	addMethod('keyDown', function(theEvent) {
		return -1;
	});
	
	addMethod('keyUp', function(theEvent) {
		return -1;
	});
endClass();

var FLAnimationLinear='linear', FLAnimationSmooth='smooth',FLAnimationEaseIn='easeIn', FLAnimationEaseOut='easeOut';

var FLBasicAnimation = function(theTarget, theProp, theTargetValue, theDuration, theStartSel, theEndSel) {
	this.target=theTarget;
	this.property=theProp;
	this.endValue=theTargetValue;
	this.duration=(theDuration) ? theDuration : 1;
	this.type=FLAnimationSmooth;
	this.startSelector=theStartSel;
	this.endSelector=theEndSel;
}

var FLKeyFrameAnimation = function(theTarget, theProp, theTargetValues, theDurations, theTypes, theStartSel, theEndSel) {
	this.target=theTarget;
	this.property=theProp;
	this.keyFrames= (theTargetValues) ? theTargetValues : [];
	this.durations=(theDurations) ? theDurations :[];
	this.types=(theTypes) ? theTypes :[]
	this.startSelector=theStartSel;
	this.endSelector=theEndSel;
	this.keyFrame=0;
	
	if(this.keyFrames.length >0) {
		if(this.durations.length ==0) for(var i=0; i < this.keyFrames.length;i++) this.durations[i]=1.0;
		if(this.types.length ==0) for(var i=0; i < this.keyFrames.length;i++) this.types[i]=FLAnimationLinear;
	}
}

var FLTextAnimation = function(theTarget, theProp, theTargetValue, theDuration, appendTheText, theStartSel, theEndSel) {
	this.target=theTarget;
	this.property=theProp;
	this.endValue=theTargetValue;
	this.duration=(theDuration) ? theDuration : 1;
	this.startSelector=theStartSel;
	this.endSelector=theEndSel;
	this.charIndex=0;
	this.appendText=(appendTheText)?true:false;
}

newClass('FLAnimationServer','FLObject');
	addVar('animations', [], kPrivate);
	addVar('isRunning', false);
	addVar('stopped', false, kProperty, 'readonly');
	addVar('framesPerSecond', 60);
	
	addMethod('addAnimation', function(anAnimation) {
		var theAnimations = $('animations'), added=false;
		
		/*
		for(var i=0; i < theAnimations.length; i++) {
			if(theAnimations[i].target === theAnimations[i].target) {
				if(theAnimations[i].property == anAnimation.property) {
					theAnimations[i] = anAnimation;
					added=true;
					break;
				}
			}
		}
		*/
		if(!added) theAnimations.push(anAnimation);
		
		if(!$('isRunning')) send('start');
	});
	
	addMethod('start', function() {
		var theAnimations = $('animations'), fps=1000/$('framesPerSecond'), current_time;
		var aniServ=this;
		$('isRunning', true);
		$('stopped', false);
		
		function _process_animations() {
			if(aniServ.get('isRunning')) {
				current_time = new Date().getTime();
				
				for(var i in theAnimations) {
				
					if(theAnimations[i].keyFrames) {
						theAnimations[i].endValue = theAnimations[i].keyFrames[theAnimations[i].keyFrame];
						theAnimations[i].duration = theAnimations[i].durations[theAnimations[i].keyFrame];
					}
					if(!theAnimations[i].beginTime) { 
						theAnimations[i].beginTime=current_time;
						theAnimations[i].startValue = theAnimations[i].target.get(theAnimations[i].property);
						if(typeof(theAnimations[i].endValue) == 'string') {
							theAnimations[i].isString=true;
							theAnimations[i].current_value = (theAnimations[i].appendText) ? theAnimations[i].target.get(theAnimations[i].property) : '';
						}
						theAnimations[i].isObject=(typeof(theAnimations[i].endValue) == 'object') ? true : false;
						if(theAnimations[i].startSelector) {
							if(typeof(theAnimations[i].startSelector) == 'function') {
								theAnimations[i].startSelector.call(theAnimations[i].target, theAnimations[i]);
							} else {
								theAnimations[i].target.send(theAnimations[i].startSelector, ani);
							}
						}
					}
					if(!theAnimations[i].types) {
						if(theAnimations[i].isString) {
							animateString(theAnimations[i]);
						} else if(theAnimations[i].type == FLAnimationLinear) {
							animateLinear(theAnimations[i]);
						} else if(theAnimations[i].type == FLAnimationSmooth) {
							animateSmooth(theAnimations[i]);
						} else {
							theAnimations.splice(theAnimations.indexOf(ani), 1);
						}
					} else {
					
						if(theAnimations[i].types[theAnimations[i].keyFrame] == FLAnimationLinear) {
							animateLinear(theAnimations[i]);
						} else if(theAnimations[i].types[theAnimations[i].keyFrame] == FLAnimationSmooth) {
							animateSmooth(theAnimations[i]);
						} else {
							theAnimations.splice(theAnimations.indexOf(theAnimations[i]), 1); //corrupted types
						}
					}
				}
				if(theAnimations.length > 0) {
					window.setTimeout(_process_animations, fps);
				} else {
					aniServ.set('isRunning', false);
				}
			}
		}
		_process_animations();
		
		function animateLinear(ani) {		
			var percent = (current_time-ani.beginTime) / (ani.duration*1000), current_value;
			
			if(percent>=1.0) {
				ani.target.set(ani.property,  ani.endValue);
				if(!ani.keyFrames) {
					theAnimations.splice(theAnimations.indexOf(ani), 1);
				} else {
					ani.keyFrame++;
					ani.beginTime=undefined;
					if(ani.keyFrame >=ani.keyFrames.length) theAnimations.splice(theAnimations.indexOf(ani), 1);
				}
				
				if(ani.endSelector) {
					if(typeof(ani.endSelector) == 'function') ani.endSelector.call(ani.target, ani);
					else ani.target.send(ani.endSelector, ani);
				}
			} else {
				if(!ani.isObject) {
					 current_value = (ani.startValue < ani.endValue) ? ani.startValue + ((ani.endValue-ani.startValue) * percent) : ani.startValue - ((ani.startValue-ani.endValue)*percent);
				} else {
					current_value= {};
					function proc_obj(targetObj, startObj, endObj) {
						for (var i in endObj) {
							if(typeof(endObj[i]) == 'object') {
								current_value[i]= {};
								proc_obj(current_value[i], startObj[i], endObj[i]);
							} else if (typeof(endObj[i]) == 'function') {
								current_value[i] = endObj[i];
							} else {
								targetObj[i] = (startObj[i] < endObj[i]) ? startObj[i] + ((endObj[i]-startObj[i]) * percent) : startObj[i] - ((startObj[i]-endObj[i])*percent);
							}
						}
					}
					proc_obj(current_value, ani.startValue, ani.endValue);
				
				}
				ani.target.set(ani.property,  current_value);
			}
				
		}
		
		function animateSmooth(ani) {
			var percent = (current_time-ani.beginTime) / (ani.duration*1000), current_value;
			
			if(percent>=1.0) {
				ani.target.set(ani.property,  ani.endValue);
				
				if(!ani.keyFrames) {
					theAnimations.splice(theAnimations.indexOf(ani), 1);
				} else {
					ani.keyFrame++;
					ani.beginTime=undefined;
					if(ani.keyFrame >=ani.keyFrames.length) theAnimations.splice(theAnimations.indexOf(ani), 1);
				}
				
				if(ani.endSelector) {
					if(typeof(ani.endSelector) == 'function') ani.endSelector.call(ani.target, ani);
					else ani.target.send(ani.endSelector, ani);
				}
			
			} else {
				if(percent <= 0.5) {
					percent=percent*2;
					percent = 1*Math.pow(percent, 2);
					percent=percent/2;
				} else {
					percent=1.0 - ((percent-0.5)*2);
					percent = ((-1*Math.pow(percent, 2))+1);
					percent=(percent/2)+0.5;
				}
					
				
				if(!ani.isObject) {
					 current_value = (ani.startValue < ani.endValue) ? ani.startValue + ((ani.endValue-ani.startValue) * percent) : ani.startValue - ((ani.startValue-ani.endValue)*percent);
				} else {
					current_value= {};
					function proc_obj(targetObj, startObj, endObj) {
						for (var i in endObj) {
							if(typeof(endObj[i]) == 'object') {
								current_value[i]= {};
								proc_obj(current_value[i], startObj[i], endObj[i]);
							} else if (typeof(endObj[i]) == 'function') {
								current_value[i] = endObj[i];
							} else {
								targetObj[i] = (startObj[i] < endObj[i]) ? startObj[i] + ((endObj[i]-startObj[i]) * percent) : startObj[i] - ((startObj[i]-endObj[i])*percent);
							}
						}
					}
					proc_obj(current_value, ani.startValue, ani.endValue);
				
				}
				ani.target.set(ani.property,  current_value);
				
			}
		}
		
		function animateString(ani) {		
			var percent = (current_time-ani.beginTime) / (ani.duration*1000);
			var charEndIndex;
			
			if(percent>=1.0) {
				ani.target.set(ani.property,  ani.endValue);
				theAnimations.splice(theAnimations.indexOf(ani), 1);
				if(ani.endSelector) {
					if(typeof(ani.endSelector) == 'function') ani.endSelector.call(ani.target, ani);
					else ani.target.send(ani.endSelector, ani);
				}
			} else {
				charEndIndex = ani.endValue.length * percent;
				if(charEndIndex > ani.charIndex) {
					ani.current_value += ani.endValue.substring(ani.charIndex, charEndIndex);
					ani.target.set(ani.property,  ani.current_value);
					ani.charIndex = charEndIndex;
				}
			}
		}
	});
	
	addMethod('stop', function() {
		$('isRunning', false);
		$('stopped', true);
	});
endClass();

newGlobal('defaultAnimationServer',newObject('FLAnimationServer'));

newClass('FLElement', 'FLResponse');
	var
	FLCoorLeft='left',
	FLCoorRight= 'right',
	FLCoorTop='top',
	FLCoorBottom='bottom',
	FLScaleNone=0,
	FLScaleWidth=1,
	FLScaleHeight=2,
	FLScaleFit=3,
	FLScaleFull=4,
	FLScaleCenter=5,
	FLScaleCenterX=6,
	FLScaleCenterY=7;
	

	addVar('element');
	addVar('_parent');
	
	addVar('unit', 'px');
	addVar('unitChanged', false);
	addVar('frame', new FLRect());
	addVar('curve');
	addVar('border');
	addVar('outline', new FLBounds());
	addVar('margin', new FLBounds());
	addVar('padding', new FLBounds());
	addVar('color', null);
	addVar('shadow', null);
	addVar('clip', new FLBounds());
	addVar('cursor', 'default');
	addVar('display', 'inline');
	addVar('overflow', 'visible');
	addVar('position', 'static');
	addVar('visible', true);
	addVar('zIndex', 'auto');
	addVar('defaultEventsEnabled', false);
	
	addVar('scaleMode', 'none');
	addVar('_xcoord', FLCoorLeft);
	addVar('_ycoord', FLCoorTop);
	addVar('autoResizeWidth', false);
	addVar('autoResizeHeight', false);
	addVar('lockBounds', new FLBounds(false, false, false, false));
	
	addVar('draggable', false);
	addVar('dragKeepInFrame', false);
	addVar('dragX', true);
	addVar('dragY', true);
	addVar('_drag_x_stop', 0);
	addVar('_drag_y_stop', 0);
	addVar('_drag_x', 0);
	addVar('_drag_y', 0);
	

	addProperty('element');
	addProperty('draggable');
	addProperty('unit');
	addProperty('frame');
	addProperty('curve');
	addProperty('border');
	addProperty('outline');
	addProperty('margin');
	addProperty('padding');
	addProperty('color');
	addProperty('shadow');
	addProperty('clip');
	addProperty('cursor');
	addProperty('display');
	addProperty('overflow');
	addProperty('zIndex');
	addProperty('position');
	addProperty('visible');
	
	addProperty('autoResizeWidth');
	addProperty('autoResizeHeight');
	addProperty('lockBounds');
	
	addProperty('defaultEventsEnabled');
	
	addMethod('init', function() {
		this.superClass().send('init');
	} );
	addMethod('initWithFrame', function(aFrame) {
		send('init');
		set('frame', aFrame);
	});
	addMethod('initWithFrameScale', function(aFrame, aScaleMode, x,y) {
		send('init');
		$('frame', aFrame);
		send('scaleWithOffset', aScaleMode, x,y);
	});
	addMethod('initWithFullScale', function() {
		send('init');
		send('scaleWithOffset', FLScaleFull, 0,0);
	});
	addMethod('initWithFrameCenter', function(aFrame) {
		send('init');
		$('frame', aFrame);
		send('scaleWithOffset', FLScaleCenter, 0,0);
	});
	
	addMethod('initWithFrameCenterX', function(aFrame) {
		send('init');
		$('frame', aFrame);
		send('scaleWithOffset', FLScaleCenterX, 0,0);
	});
	
	addMethod('initWithFrameCenterY', function(aFrame) {
		send('init');
		$('frame', aFrame);
		send('scaleWithOffset', FLScaleCenterY, 0,0);
	});
	
	addMethod('setCoordinates', function(x,y) {
		$('_xcoord', x);
		$('_ycoord', y);
	});
	
	addMethod('frame', function(aValue) {
		var theFrame=aValue, theBounds,xc,yc,_style;
		if(exists(aValue)) {
			$('frame', aValue);
			theBounds = $('lockBounds');
			_style = get('element').style;
			
			xc = $('_xcoord');
			yc = $('_ycoord');
			
			if(xc=='left') {
				if(!theBounds.right) _style.setProperty('left', theFrame.origin.x +'px',null);
				else if(!theBounds.left && theBounds.right) _style.setProperty('right', theFrame.size.width + theFrame.origin.x +'px',null);
				else _style.setProperty('left', send('convertLeftToPercent', theFrame.origin.x) +'px',null);
			} else {
				if(!theBounds.left) _style.setProperty('right', theFrame.origin.x +'px',null);
				else if(!theBounds.right && theBounds.left) _style.setProperty('left', theFrame.size.width + theFrame.origin.x +'px',null);
				else _style.setProperty('right', send('convertLeftToPercent', theFrame.size.width - theFrame.origin.x) +'px',null);
			}
			
			if(yc=='top') {
				if(!theBounds.bottom) _style.setProperty('top', theFrame.origin.y +'px',null);
				else if(!theBounds.top && theBounds.bottom) _style.setProperty('bottom', theFrame.size.height + theFrame.origin.y +'px',null);
				else _style.setProperty('top', send('convertLeftToPercent', theFrame.origin.y) +'px',null);
			} else {
				if(!theBounds.bottom) _style.setProperty('bottom', theFrame.origin.y +'px',null);
				else if(!theBounds.top && theBounds.bottom) _style.setProperty('left', theFrame.size.height + theFrame.origin.y +'px',null);
				else _style.setProperty('bottom', send('convertLeftToPercent', theFrame.size.height - theFrame.origin.y) +'px',null);
			}
			
			if(!theBounds.left && !theBounds.right)
			_style.setProperty('width', ($('autoResizeWidth')==true) ? send('convertWidthToPercent', theFrame.size.width) + '%' : theFrame.size.width+'px', null);
			else
			_style.setProperty('width', send('convertWidthToPercent', theFrame.size.width) + '%', null);
				
			if(!theBounds.top && !theBounds.bottom)
			_style.setProperty('height', ($('autoResizeHeight')) ? send('convertHeightToPercent', theFrame.size.height) + '%' : theFrame.size.height+'px', null);
			else
			_style.setProperty('height', send('convertHeightToPercent', theFrame.size.height) + '%', null);

		} else {
			return $('frame');
		}
	});
	
	addMethod('border', function(aValue) {
		var node, unit;
		if(exists(aValue)) {
			unit = get('unit');
			node = get('element');
			
			node.style.borderLeftWidth = aValue.leftWidth;
			node.style.borderLeftStyle = aValue.leftStyle;
			node.style.borderLeftColor = aValue.leftColor.RGB100();
			node.style.borderRightWidth = aValue.leftWidth;
			node.style.borderRightStyle = aValue.leftStyle;
			node.style.borderRightColor = aValue.leftColor.RGB100();
			node.style.borderTopWidth = aValue.leftWidth;
			node.style.borderTopStyle = aValue.leftStyle;
			node.style.borderTpoColor = aValue.leftColor.RGB100();
			node.style.borderBottomWidth = aValue.leftWidth;
			node.style.borderBottomStyle = aValue.leftStyle;
			node.style.borderBottomColor = aValue.leftColor.RGB100();
			
			$('border', aValue);
		} else {
			return $('border');
		}
	});
	
	addMethod('outline', function(aValue) {
		var node, unit;
		if(exists(aValue)) {
			unit = get('unit');
			node = get('element');

			node.style.outlineWidth = aValue.width;
			node.style.outlineStyle = aValue.style ;
			node.style.outlineColor= aValue.color.RGB100();
			
			$('outline', aValue);
		} else {
			return $('outline');
		}
	});
	
	addMethod('margin', function(aValue) {
		var node, unit;
		if(exists(aValue)) {
			unit = get('unit');
			node = get('element');

			node.style.marginLeft = aValue.left;
			node.style.marginRight = aValue.right;
			node.style.marginTop = aValue.top;
			node.style.marginBottom = aValue.bottom;
			
			$('margin', aValue);
		} else {
			return $('margin');
		}
	});
	
	addMethod('padding', function(aValue) {
		var node, unit;
		if(exists(aValue)) {
			unit = get('unit');
			node = get('element');

			node.style.paddingLeft = aValue.left;
			node.style.paddingRight = aValue.right;
			node.style.paddingTop = aValue.top;
			node.style.paddingBottom = aValue.bottom;
			
			$('padding', aValue);
		} else {
			return $('padding');
		}
	});
	
	addMethod('color', function(aValue) {
		var node;
		if(exists(aValue)) {
			node = $('element');

			node.style.backgroundColor = aValue.RGB100();
			node.style.opacity = aValue.alpha;
			
			$('color', aValue);
		} else {
			return $('color');
		}
	});
	
	addMethod('shadow', function(aValue) {
		var shadowString, propName;
		if(exists(aValue)) {
		
			if(pyro.browser.isWebkit && pyro.browser.version >= 522.11) { 
				propName='-webkit-box-shadow';
			} else if(pyro.browser.isGecko && pyro.browser.version >= 3.5){ 
				propName='-moz-box-shadow';
			} else if(pyro.browser.isOpera && pyro.browser.version >= 10.5){
				propName='box-shadow';
			}
			
			if(propName) {
				if(!pyro.browser.isIE) {
					shadowString = (aValue.onset) ? 'onset ' : '';
					shadowString += aValue.offsetX + $('unit');
					shadowString += ' '+aValue.offsetY + $('unit');
					shadowString += ' '+aValue.blur + $('unit');
					shadowString += ' '+aValue.color.RGBA();
					$('element').style.setProperty(propName, shadowString, null);
				} else {
					shadowString = 'offX=' +aValue.offsetX +',';
					shadowString += 'offY=' +aValue.offsetY +',';
					shadowString += 'color=' +aValue.color.HEX();
					$('element').style.filter = 'progid:DXImageTransform.Microsoft.DropShadow('+shadowString+')';
				}
			
			} else if(!$('border')) {
			}
			
			$('shadow', aValue);
		} else {
			return $('shadow');
			
		}
		
	});

	addMethod('curve', function(aValue) {
		var shadowString, propName;
		if(exists(aValue)) {
			var node = $('element');

			if(pyro.browser.isWebkit && pyro.browser.version >= 522.11) {
				node.style.setProperty('-webkit-border-top-left-radius', aValue.leftTop + $('unit'), null);
				node.style.setProperty('-webkit-border-top-right-radius', aValue.rightTop + $('unit'), null);
				node.style.setProperty('-webkit-border-bottom-right-radius', aValue.rightBottom + $('unit'), null);
				node.style.setProperty('-webkit-border-bottom-left-radius', aValue.leftBottom + $('unit'), null);
			} else if(pyro.browser.isGecko && pyro.browser.version >= 3.5) {
				node.style.setProperty('-moz-border-radius-topleft', aValue.leftTop + $('unit'), null);
				node.style.setProperty('-moz-border-radius-topright', aValue.rightTop + $('unit'), null);
				node.style.setProperty('-moz-border-radius-bottomright', aValue.rightBottom + $('unit'), null);
				node.style.setProperty('-moz-border-radius-bottomleft', aValue.leftBottom + $('unit'), null);
			}
			
			$('curve', aValue);
		} else {
			return $('curve');
		}
	});
	
	addMethod('clip', function(aValue) {
		if(exists(aValue)) {
			get('element').style.clip = 'rect('+aValue.top+'px'+','+aValue.right+'px'+','+aValue.bottom+'px'+','+aValue.left+'px'+')';
			set('clip', aValue);
		} else {
			return get('clip');
		}
	});
	
	addMethod('cursor', function(aValue) {
		if(exists(aValue)) {
			get('element').style.cursor = aValue;
			$('cursor', aValue);
		} else {
			return $('cursor');
		}
	});
	
	addMethod('display', function(aValue) {
		if(exists(aValue)) {
			get('element').style.display = aValue;
			$('display', aValue);
		} else {
			return $('display');
		}
	});
	
	addMethod('overflow', function(aValue) {
		if(exists(aValue)) {
			get('element').style.overflow = aValue;
			$('overflow', aValue);
		} else {
			return $('overflow');
		}
	});
	
	addMethod('zIndex', function(aValue) {
		if(exists(aValue)) {
			get('element').style.zIndex = aValue;
			$('zIndex', aValue);
		} else {
			return $('zIndex');
		}
	});
	
	addMethod('position', function(aValue) {
		if(exists(aValue)) {
			get('element').style.position = aValue;
			$('position', aValue);
		} else {
			return $('position');
		}
	});
	
	addMethod('visible', function(aValue) {
		if(exists(aValue)) {
			get('element').style.visibility = aValue.toString();
			$('visible', aValue);
		} else {
			return $('visible');
		}
	});
	
	addMethod('defaultEventsEnabled', function(aValue) {
		if(exists(aValue)) {
			if($('element')) {
				if(aValue==false) {
					$('element').onmousedown=function(e) { e.preventDefault(); return false; };
					$('element').oncontextmenu=function(e) {e.preventDefault(); return false; };
				}
			}
			$('defaultEventsEnabled', aValue);
		
		} else {
			return $('defaultEventsEnabled');
		}
	});
	addMethod('element', function(aValue) {
		if(exists(aValue)) {
			$('element', aValue);
			if(get('defaultEventsEnabled')==false) set('defaultEventsEnabled', false) ;
		} else {
			return $('element');
		}
	});
	
	addMethod('draggable', function(aValue) {
		if(exists(aValue)) {
			if(aValue) {
				send('addEvent','_dragMouseDown', new FLEvent('mousedown','_dragMouseDown', this));
			} else {
				send('removeEvent','_dragMouseDown');
			}
			$('draggable', aValue);
		} else {
			return $('draggable');
		}
	});
	
	addMethod('_dragMouseDown', function(theEvent) {
		if(!theEvent.inputIsDown) {
			var theElement = get('element');
			send('addEvent','_dragMouseUp', new FLEvent('mouseup','_dragMouseUp',document));
			send('addEvent', '_dragMouseDrag', new FLEvent('mousemove','_dragMouseMove',document));
			set('_drag_x',theEvent.x());
			set('_drag_y',theEvent.y());
		}
	});
	
	addMethod('_dragMouseUp', function(theEvent) {
		send('removeEvent', '_dragMouseDrag');
		send('removeEvent', '_dragMouseUp');
	});
	
	addMethod('_dragMouseMove', function(theEvent) {
		var theElement = get('element');
		var theFrame = $('frame');
		var x=theEvent.x(),y=theEvent.y();
		var left, top;
		if(theElement) {
			if($('_drag_x_stop')!=0) {
				if($('_drag_x_stop')==-1) { if(x >= $('_drag_x')) $('_drag_x_stop', 0) }
				else if($('_drag_x_stop')==1) { if(x <= $('_drag_x')) $('_drag_x_stop', 0); }
			} else {
				if($('dragX')) theFrame.origin.x = x + parseFloat(theElement.style.left) - get('_drag_x');
				$('_drag_x',x);
			}
			
			if($('_drag_y_stop')!=0) {
				if($('_drag_y_stop')==-1) { if(y >= $('_drag_y')) $('_drag_y_stop', 0); }
				else if($('_drag_y_stop')==1) { if(y <= $('_drag_y')) $('_drag_y_stop', 0); }
			} else {
				if($('dragY')) theFrame.origin.y = y + parseFloat(theElement.style.top) - get('_drag_y');
		 		$('_drag_y', y);
			}
			
			if(theFrame.origin.x < 0) {
				theFrame.origin.x=0;
				$('_drag_x_stop', -1);
			} else if(theFrame.origin.x > $('element').parentNode.clientWidth - theFrame.size.width) {
				theFrame.origin.x=$('element').parentNode.clientWidth - theFrame.size.width;
				$('_drag_x_stop', 1);
			}
			
			if(theFrame.origin.y < 0) {
				theFrame.origin.y=0;
				$('_drag_y_stop', -1);
			} else if(theFrame.origin.y > $('element').parentNode.clientHeight - theFrame.size.height) {
				theFrame.origin.y=$('element').parentNode.clientHeight - theFrame.size.height;
				$('_drag_y_stop', 1);
			}
			set('frame',theFrame);
		}
	});
	
	addMethod('autoResizeWidth', function(aBOOL) {
		if(exists(aBOOL)) {
			$('autoResizeWidth', aBOOL);
			send('frame', $('frame'));
		} else {
			return $('autoResizeWidth');
		}
	});
	
	addMethod('autoResizeHeight', function(aBOOL) {
		if(exists(aBOOL)) {
			$('autoResizeHeight', aBOOL);
			send('frame', $('frame'));
		} else {
			return $('autoResizeHeight');
		}
	});
	
	addMethod('lockBounds', function(aValue) {
		if(exists(aValue)) {
			$('lockBounds', aValue);
			send('frame', $('frame'));
		} else {
			return $('lockBounds');
		}
	});
	
	addMethod('centerWithOffset', function(offsetX, offsetY) {
		var theFrame = $('frame'), parent= ($('element').parentNode) ? $('element').parentNode : document.body;
		theFrame.origin.x = (parent.clientWidth/2 - theFrame.size.width/2) + offsetX;
		theFrame.origin.y = (parent.clientHeight/2 - theFrame.size.height/2) + offsetY;
		set('frame', theFrame);
	});
	
	addMethod('centerXWithOffset', function(offsetX, offsetY) {
		var theFrame = $('frame'), parent= ($('element').parentNode) ? $('element').parentNode : document.body;
		theFrame.origin.x = (parent.clientWidth/2 - theFrame.size.width/2) + offsetX;
		set('frame', theFrame);
	});
	
	addMethod('centerYWithOffset', function(offsetX, offsetY) {
		var theFrame = $('frame'), parent= ($('element').parentNode) ? $('element').parentNode : document.body;
		theFrame.origin.y = (parent.clientHeight/2 - theFrame.size.height/2) + offsetY;
		set('frame', theFrame);
	});
	
	addMethod('scaleWithOffset', function(theScaleMode, offsetX, offsetY) {
		
		if(theScaleMode==FLScaleCenterX) send('centerXWithOffset', 0, 0)
		else if(theScaleMode==FLScaleCenterY) send('centerYWithOffset', 0, 0)
		else send('centerWithOffset', 0, 0);
		
		
		var theFrame = $('frame'), parent= ($('element').parentNode) ? $('element').parentNode : document.body;
		
		if(theScaleMode==FLScaleWidth) {
			theFrame.origin.x += offsetX;
			theFrame.origin.y += offsetY;
			theFrame.size.width = parent.clientWidth;
			$('frame', theFrame);
			set('autoResizeWidth', true);
		} else if(theScaleMode==FLScaleHeight) {
			theFrame.origin.y = offsetY;
			theFrame.origin.x += offsetX;
			theFrame.size.height = parent.clientHeight;
			$('autoResizeHeight', true);
			set('frame', theFrame);
		} else if(theScaleMode==FLScaleFit) {
			var ratio;
			theFrame.origin.y = offsetY;
			theFrame.origin.x = offsetX;
			if(theFrame.size.height > theFrame.size.width) {
				ratio = theFrame.size.height / theFrame.size.width;
				theFrame.size.height = parent.clientHeight;
				theFrame.size.width = theFrame.size.height * ratio;
			} else {
				ratio = theFrame.size.width / theFrame.size.height;
				theFrame.size.width = parent.clientWidth;
				theFrame.size.height = theFrame.size.width * ratio;
			}
			$('autoResizeHeight', true);
			$('autoResizeWidth', true);
			set('frame', theFrame);

		} else if(theScaleMode==FLScaleFull) {
			theFrame.origin.y = offsetY;
			theFrame.origin.x = offsetX;
			theFrame.size.height = parent.clientHeight;
			theFrame.size.width = parent.clientWidth;
			$('autoResizeHeight', true);
			$('autoResizeWidth', true);
			set('frame', theFrame);
		} else if(theScaleMode==FLScaleCenter) {
			theFrame.origin.y += offsetY;
			theFrame.origin.x += offsetX;
			$('lockBounds', new FLBounds(1,0,0,0));
			$('margin', new FLBounds('auto','auto','auto','auto'));
			set('frame', theFrame);
		} else if(theScaleMode==FLScaleCenterX) {
			theFrame.origin.y += offsetY;
			$('lockBounds', new FLBounds(1,0,0,0));
			$('margin', new FLBounds('auto',0,'auto',0));
			set('frame', theFrame);
		} else if(theScaleMode==FLScaleCenterY) {
			theFrame.origin.x += offsetX;
			$('lockBounds', new FLBounds(9,1,0,0));
			$('margin', new FLBounds(0,'auto',0, 'auto'));
			set('frame', theFrame);
		} else if(theScaleMode==FLScaleNone) {
			$('margin', new FLBounds(0,0,0,0));
			$('lockBounds', new FLBounds(0,0,0,0));
			theFrame.origin.x = send('convertLeftToPixel');
			theFrame.origin.y = send('convertTopToPixel');
			theFrame.size.width = send('convertWidthToPixel');
			theFrame.size.height = send('convertHeightToPixel');
			$('autoResizeHeight', false);
			$('autoResizeWidth', false);
			set('frame', theFrame);
		}
	});
	
	addMethod('convertLeftToPercent', function() {
		var parent = ($('element').parentNode) ? $('element').parentNode : document.body;
		return ($('frame').origin.x / parent.clientWidth) * 100;
	});
	addMethod('convertTopToPercent', function() {
		var parent = ($('element').parentNode) ? $('element').parentNode : document.body;
		return ($('frame').origin.y / parent.clientHeight) * 100;
	});
	addMethod('convertWidthToPercent', function() {
		var parent = ($('element').parentNode) ? $('element').parentNode : document.body;
		return ($('frame').size.width / parent.clientWidth) * 100;
	});
	addMethod('convertHeightToPercent', function() {
		var parent = ($('element').parentNode) ? $('element').parentNode : document.body;
		return ($('frame').size.height / parent.clientHeight) * 100;
	});
	
	addMethod('convertLeftToPixel', function() {
		var parent = ($('element').parentNode) ? $('element').parentNode : document.body;
		return ($('frame').origin.x / 100) * parent.clientWidth;
	});
	addMethod('convertTopToPixel', function() {
		var parent = ($('element').parentNode) ? $('element').parentNode : document.body;
		return ($('frame').origin.y / 100) * parent.clientHeight;
	});
	addMethod('convertWidthToPixel', function() {
		var parent = ($('element').parentNode) ? $('element').parentNode : document.body;
		return ($('frame').size.width / 100) * parent.clientWidth;
	});
	addMethod('convertHeightToPixel', function() {
		var parent = ($('element').parentNode) ? $('element').parentNode : document.body;
		return ($('frame').size.height / 100) * parent.clientHeight;
	});
	
	
	addMethod('animateValue', function(varName, newValue, theDuration, startSel, endSel) {
		var anim = new FLBasicAnimation(this,varName,newValue,theDuration,startSel,endSel);
		global('defaultAnimationServer').send('addAnimation', anim);
	});
	
	addMethod('keyFrameValue', function(varName, keyFrames, theDurations, theTypes, startSel, endSel) {
		var anim = new FLKeyFrameAnimation(this,varName,keyFrames,theDurations,theTypes,startSel,endSel);
		global('defaultAnimationServer').send('addAnimation', anim);
	});
	
endClass();

newClass('FLText', 'FLObject');
	addVar('element');
	addVar('value','');
	addProperty('value');
	
	addMethod('init', function() {
		this.superClass().send('init');
		set('element', document.createTextNode(''));
	});
	
	addMethod('initWithValue', function(aValue) {
		send('init');
		set('value',aValue);
	});
	
	addMethod('value', function(aValue) {
		if(exists(aValue)) {
			$('element').nodeValue = aValue;
		} else {
			return $('element').nodeValue;
		}
		$('value','');
	});
endClass();

newClass('FLImage','FLElement');
	addVar('opacity', 1.0);
	
	addProperty('opacity');

	addMethod('init', function() {
		set('element',document.createElement('img'));
		this.superClass().send('init');
		set('position','absolute');
		get('element').style.display = 'none';
	});
	
	addMethod('initWithURL', function(aURL) {
		send('init');
		send('imageWithURL', aURL);
	});
	
	addMethod('URL', function(aValue) {
		if(exists(aValue)) {
			$('element').src = aValue;
			$('element').style.display = (aValue == '') ? 'none' : $('display');
		} else {
			return $('element').src;
		}
	});
	
	addMethod('opacity', function(aValue) {
		if(exists(aValue)) {
			$('element').style.opacity = aValue;
			$('opacity', aValue);
		} else {
			return $('opacity');
		}
	});
		
	addMethod('imageWithURL', function(aURL) {
		send('URL', aURL);
		var theFrame = get('frame');
		theFrame.size.width = '100%';
		theFrame.size.height = '100%';
		set('frame', theFrame);
	});

endClass();

/*View*/
newClass('FLView', 'FLElement');

	addVar('_canvas');
	addVar('image');
	addVar('superView');
	addVar('subviews',[]);
	
	addProperty('image');
	addProperty('subviews', 'readonly');
	
	addMethod('init', function() {
		this.superClass().send('init');
		set('element',document.createElement('div'));
		set('position','absolute');
		set('overflow','hidden');
		if(!send('drawInRect') == -1) {
			var newCanvas = document.createElement('canvas');
			newCanvas.style.width='100%';
			newCanvas.style.height='100%';
			newCanvas.style.zIndex=0;
			$('element').appendChild(newCanvas);
			$('_canvas', newCanvas);
		}
	});
	
	addMethod('addSubview', function(aView) {
		aView.set('zIndex',$('subviews').push(aView)+100);
		get('element').appendChild(aView.get('element'));
		aView.set('superView', this);
	});
	
	addMethod('context', function() { 
		var i=0;
		var current_obj = (get('superView')) ? get('superView') : null;
		while(i < 1) {
			if(current_obj) {
				if(current_obj.send('isEqualToClass', classObject('FLContext'))) 
					return current_obj;
				 else 
					current_obj = current_obj.get('superView');
			} else {
				i++;
			}
		}
	});
	
	addMethod('image', function(aValue) {
		if(exists(aValue)) {
			if($('image')) $('element').removeChild($('image').get('element'));
			$('image', aValue);
			if(aValue.get('element')) {
				$('element').appendChild(aValue.get('element'));
				aValue.set('zIndex','1');
				
			}
		} else {
			if(!$('image')) set('image', newObject('FLImage', 'initWithFullScale'));
			return $('image');
		}
	});
	
	addMethod('drawInRect', function(rect) {
		return -1;
	});
	
endClass();

/*Context*/
newClass('FLContext','FLElement');
	addVar('contentView');
	addVar('isBody', false);
	addVar('pageFrame');
	addProperty('contentView', 'readonly');
	addProperty('pageFrame', 'readonly');
	
	addMethod('init', function() {
		$('contentView', newObject('FLView','initWithFullScale'));
		if(!get('element')) set('element', document.createElement('div'));
		get('contentView').set('superView',this);
		$('element').appendChild(get('contentView').get('element'));
	});
	
	addMethod('initAsBody', function(mainFunction) {
		if(!document.body) document.body = document.createElement('body');
		set('element', document.body).set('margin', new FLBounds(0,0,0,0)).send('initWithFrameScale', new FLRect(), FLScaleFull);
		mainFunction(this);
	});
	
	addMethod('attachToParent',function(theParent){
		theParent.appendChild(get('element'));
		set('parent',theParent);
	});
	
	addMethod('pageFrame', function() {
		var aFrame;
		var i=0, theElement = $('element');
		
		aFrame = new FLRect(theElement.clientLeft,theElement.clientTop,theElement.clientWidth,theElement.clientHeight);
		
		while(i < 1) {
			theElement = theElement.parentNode;
			if(theElement != document.body) {
				aFrame.origin.x += theElement.clientLeft;
				aFrame.origin.y += theElement.clientTop;
			} else {
			i++;
			}
		}
		
		return aFrame;
	});

endClass();

newClass('FLControl', 'FLView');

	addVar('enabled', true);
	addVar('value');
	addVar('delegate',this);
	addVar('action');
	
	addProperty('enabled');
	addProperty('value');
	
	addMethod('init', function() {
		this.superClass().send('init');
		set('action', this.sel('valueChanged'));
	});
	
	addMethod('enabled', function(aBOOL) {
		if(exists(aBOOL)) {
			$('enabled', aBOOL);
		} else {
			$('enabled');
		}
	});
	
	addMethod('value', function(aValue) {
		if(exists(aValue)) {
			$('value', aValue);
			if($('delegate')) $('action').call($('delegate'), this);
		} else {
			$('value');
		}
	});
	
endClass();

newClass('FLLabel', 'FLControl');
	addVar('_text');
	addVar('text', '');
	addVar('font', '');
	addVar('fontSize',14);
	addVar('fontColor', new FLColor(0,0,0,1.0));
	addVar('bold', false);
	addVar('italic', false);
	addVar('underline', false);
	addVar('bold', false);
	addVar('textWrap', false);
	addVar('textDirection', 'ltr');
	addVar('textAlign', 'justify');
	addVar('verticalTextAlign', 'baseline');
	addVar('lineHeight', 'normal');
	addVar('letterPadding', 'normal');
	addVar('wordPadding', 'normal');
	
	
	addProperty('text');
	addProperty('fontSize');
	addProperty('textAlign');
	addProperty('verticalTextAlign');
	addProperty('textWrap');
	addProperty('fontColor');
	
	
	addMethod('init', function() {
		this.superClass().send('init');
		$('_text', newObject('FLText'));
		$('element').appendChild($('_text').get('element'));
		$('element').style.fontFamily = $('Lucida Grande');
		$('element').style.fontSize = $('fontSize');
		$('element').style.fontStyle = 'normal';
		$('element').style.fontWeight = 'normal';
		$('element').style.textDecoration = 'none';
		$('element').style.color = $('fontColor').RGB100();
		$('element').style.direction = $('textDirection');
		$('element').style.textAlign = $('textAlign');
		$('element').style.verticalAlign = $('verticalTextAlign');
		$('element').style.whiteSpace = 'pre';
		$('element').style.lineHeighr = 'normal';
		$('element').style.letterSpacing = 'normal';
		$('element').style.wordSpacing = 'normal';
		set('cursor','default');
	});
	
	addMethod('text', function(aValue) {
		if(exists(aValue)) {
			$('_text').set('value', aValue);
			$('text', aValue);
		} else {
			return $('text');
		}
	});
	
	addMethod('fontSize', function(aValue) {
		if(exists(aValue)) {
			$('element').style.fontSize=aValue + 'pt';
			$('fontSize', aValue);
		} else {
			return $('fontSize');
		}
	});
	
	addMethod('textAlign', function(aValue) {
		if(exists(aValue)) {
			$('element').style.textAlign=aValue;
			$('textAlign', aValue);
		} else {
			return $('textAlign');
		}
	});
	
	addMethod('verticalTextAlign', function(aValue) {
		if(exists(aValue)) {
			$('element').style.verticalAlign=aValue;
			$('verticalTextAlign', aValue);
		} else {
			return $('verticalTextAlign');
		}
	});
	
	addMethod('textWrap', function(aValue) {
		if(exists(aValue)) {
			$('element').style.whiteSpace=(aValue) ? 'pre-wrap' : 'pre';
			$('textWrap', aValue);
		} else {
			return $('textWrap');
		}
	});
	
	addMethod('fontColor', function(aValue) {
		if(exists(aValue)) {
			$('element').style.color= aValue.RGB100();
			$('element').style.opacity= aValue.alpha;
			$('fontColor', aValue);
		} else {
			return $('fontColor');
		}
	});
	
	addMethod('animateText', function(newValue, theDuration, appendText, startSel, endSel) {
		var anim = new FLTextAnimation(this,'text',newValue,theDuration,appendText,startSel,endSel);
		global('defaultAnimationServer').send('addAnimation', anim);
	});
	
		
endClass();

newClass('FLButton', 'FLControl');
	addVar('label');
	addVar('toggleMode', 'none');
	addVar('normalImage');
	addVar('mousedownImage');
	addVar('selectedImage');
	addVar('highlightImage');
	
	addVar('_mouse_down', false);
	addVar('_mouse_click', false);
	
	addProperty('label', 'readonly');
	addProperty('toggleMode');
	addProperty('normalImage');
	addProperty('mousedownImage');
	addProperty('selectedImage');
	addProperty('highlightImage');
	
	addMethod('init', function() {
		this.superClass().send('init');
		$('label', newObject('FLLabel', 'initWithFrameScale', new FLRect(0,0,$('frame').size.width,$('frame').size.height),FLScaleFull, 0,3));
		$('label').set('textAlign', 'center');
		$('label').set('verticalTextAlign', 'middle');
		send('addSubview',$('label'));
	//	$('label').send('scaleWithOffset', FLScaleFull);
	});
	
	addMethod('toggleMode', function(aType) {
		if(exists(aType)) {
			$('toggleMode',aType);
			send('updateImage');
		} else {
			return $('toggleMode');
		}
	});
	
	addMethod('normalImage', function(anIMG) {
		if(exists(anIMG)) {
			$('normalImage',anIMG);
			get('image').send('URL', anIMG.send('URL'));
		} else {
			return $('normalImage');
		}
	});
	
	addMethod('mousedownImage', function(anIMG) {
		if(exists(anIMG)) {
			if(!$('mousedownImage')) send('addEvent','mousedown', new FLEvent('mousedown', '_mousedown',this));
			$('mousedownImage',anIMG);
		} else {
			return $('mousedownImage');
		}
	});
	
	addMethod('selectedImage', function(anIMG) {
		if(exists(anIMG)) {
			$('selectedImage',anIMG);
		} else {
			return $('selectedImage');
		}
	});
	
	addMethod('highlightImage', function(anIMG) {
		if(exists(anIMG)) {
			if(!$('highlightImage')) send('addEvent','mouseover', new FLEvent('mouseover', '_mouseover',this));
			if(!$('highlightImage')) send('addEvent','mouseout', new FLEvent('_mouseout',this));
			$('highlightImage',anIMG);
		} else {
			return $('highlightImage');
		}
	});
	
	addMethod('_mousedown', function(theEvent) {
		send('addEvent','mouseup', new FLEvent('mouseup', '_mouseup',document));
		if(!get('highlightImage')) {
			send('addEvent','mouseover', new FLEvent('mouseover', '_mouseover',this));
			send('addEvent','mouseout', new FLEvent('mouseout','_mouseout',this));
		}
		get('image').send('URL',$('mousedownImage').send('URL'));
		$('_mouse_down', true);
		$('_mouse_click', true);
	});
	
	addMethod('_mouseup', function(theEvent) {
		send('removeEvent','mouseup');
		if(!$('highlightImage')) { send('removeEvent','mouseover'); send('removeEvent','mouseout'); }
		get('image').send('URL',$('normalImage').send('URL'));
		
		if($('_mouse_click')) set('value', 1);
	});
	
	addMethod('_mouseover', function(theEvent) {
		if(theEvent.inputIsDown && $('_mouse_down')) {
			get('image').send('URL',$('mousedownImage').send('URL'));
			$('_mouse_click', true);
		} else if(get('highlightImage')) {
			get('image').send('URL',$('highlightImage').send('URL'));
		}
		
	});
	
	addMethod('_mouseout', function(theEvent) {
		if(theEvent.inputIsDown && $('_mouse_down')) {
			get('image').send('URL',$('normalImage').send('URL'));
			$('_mouse_click', false);
		} else if(get('highlightImage')) {
			get('image').send('URL',$('normalImage').send('URL'));
		}	
	});
endClass();

newClass('FLWindow', 'FLView');
	addVar('contentView');
	addVar('title');
	addVar('closeButton');
	addVar('maxButton');
	addVar('resizeButton');
	addVar('hasClose', true);
	addVar('hasMax', true);
	addVar('hasResize', true);
	addVar('contentMargin', 'auto');
	
	addProperty('contentView', 'readonly');
	addProperty('title', 'readonly');
	addProperty('closeButton', 'readonly');
	addProperty('maxButton', 'readonly');
	addProperty('hasClose');
	addProperty('hasMax');
	addProperty('hasResize');
	addProperty('contentMargin');
	
	addMethod('init', function() {
		this.superClass().send('init');
		
		set('draggable', true);
		$('contentView', newObject('FLView', 'initWithFullScale'));
		$('contentView').set('unit', '%');
		$('contentView').set('position', 'relative');
		$('contentView').set('margin', new FLBounds($('contentMargin'),$('contentMargin'),$('contentMargin'),$('contentMargin')));
		send('addSubview', $('contentView'));
		
		set('shadow', new FLShadow(0,5,15, new FLColor(0,0,0,0.5)));
		set('curve', new FLCurve(20,20,20,20));
	});
	
endClass();