/*
 * @require jade/compat/Widget.js
 * @require jade/compat/Application.js
 */
jade.compat.Form=function(a,b){if(a!==void 0)this.elementId=a;jade.compat.Object.call(this,null,b);jade.compat.Application.getInstance().connect("load",this.init,this)};
jade.inherit(jade.compat.Form,jade.compat.Object);jade.compat.Form.prototype.elementId=null;jade.compat.Form.prototype.element=null;jade.compat.Form.prototype.width=0;jade.compat.Form.prototype.height=0;jade.compat.Form.prototype.mainWidget=null;jade.compat.Form.prototype.pageX=null;jade.compat.Form.prototype.pageY=null;jade.compat.Form.prototype.initialized=!1;
jade.compat.Form.prototype.init=function(){if(this.elementId){if(this.element=document.getElementById(this.elementId),!this.element)throw Error("No form element with id "+this.elementId+" found");}else{var a;this.element=document.createElement("div");this.element.style.position="fixed";this.element.style.left=0;this.element.style.top=0;a=document.getElementsByTagName("body")[0];a.appendChild(this.element);if(this.element.offsetLeft||this.element.offsetTop)this.element.style.position="absolute"}this.element.style.overflow=
"hidden";if(this.mainWidget&&(this.element.appendChild(this.mainWidget.getRootElement()),this.mainWidget.getRootElement().offsetParent!=this.element&&this.element.style.position!="absolute"))this.element.style.position="relative";this.element.jadeForm=this;this.element.onmousemove=this.handleMouseMove;this.element.onmousedown=this.handleMouseDown;this.element.onmouseup=this.handleMouseUp;this.element.onmouseout=this.handleMouseOut;this.element.onclick=this.handleClick;this.element.ondblclick=this.handleDblClick;
this.element.onmousewheel=this.handleWheel;this.element.addEventListener&&this.element.addEventListener("DOMMouseScroll",this.handleWheel,!1);this.redraw();jade.compat.Application.getInstance().connect("resize",this.redraw,this);this.initialized=!0};
jade.compat.Form.prototype.handleMouseMove=function(a){var b,c;if(!jade.compat.application.isMouseCaptured())if(this.jadeForm)this.jadeForm.handleMouseMove(a?a:window.event);else if(b=this.getRelativeMouseCoords(a)){c=this.getPageMouseCoords(a);if(this.pageX!=c.x-b.x||this.pageY!=c.y-b.y)this.pageX=c.x-b.x,this.pageY=c.y-b.y;this.mainWidget&&(jade.compat.application.isDragging()?this.mainWidget.dragOver(b.x,b.y,jade.compat.application.getDragSource()):this.mainWidget.mouseMove(b.x,b.y))}};
jade.compat.Form.prototype.handleMouseDown=function(a){var b,c;if(!jade.compat.application.isMouseCaptured()&&!jade.compat.application.isDragging())if(this.jadeForm)this.jadeForm.handleMouseDown(a?a:window.event);else if(!(a.button>1)&&(b=this.getRelativeMouseCoords(a))){if(this.pageX===null&&this.pageY===null)c=this.getPageMouseCoords(a),this.pageX=c.x-b.x,this.pageY=c.y-b.y;this.mainWidget&&this.mainWidget.mouseDown(b.x,b.y,a.altKey||a.ctrlKey||a.shiftKey)}};
jade.compat.Form.prototype.handleMouseUp=function(a){var b,c;if(!jade.compat.application.isMouseCaptured())if(this.jadeForm)this.jadeForm.handleMouseUp(a?a:window.event);else if(!(a.button>1)&&(b=this.getRelativeMouseCoords(a))&&this.mainWidget)jade.compat.application.isDragging()?(c=jade.compat.application.getDragSource(),this.mainWidget.dragDrop(b.x,b.y,c),c.endDrag()):this.mainWidget.mouseUp(b.x,b.y)};
jade.compat.Form.prototype.handleClick=function(a){var b;this.jadeForm?this.jadeForm.handleClick(a?a:window.event):a.button>1||(b=this.getRelativeMouseCoords(a))&&this.mainWidget&&this.mainWidget.click(b.x,b.y)};
jade.compat.Form.prototype.handleWheel=function(a){var b,c;if(!jade.compat.application.isMouseCaptured()&&!jade.compat.application.isDragging())if(this.jadeForm)this.jadeForm.handleWheel(a?a:window.event);else if(b=this.getRelativeMouseCoords(a)){c=0;if(a.wheelDelta)c=a.wheelDelta;else if(a.detail)c=-a.detail;else return;this.mainWidget&&this.mainWidget.mouseWheel(b.x,b.y,c<0?-1:1);a.preventDefault&&a.preventDefault();a.returnValue=!1}};
jade.compat.Form.prototype.handleDblClick=function(a){var b;this.jadeForm?this.jadeForm.handleDblClick(a?a:window.event):a.button>1||(b=this.getRelativeMouseCoords(a))&&this.mainWidget&&this.mainWidget.dblClick(b.x,b.y)};
jade.compat.Form.prototype.handleMouseOut=function(a){var b,c,d,e;!jade.compat.application.isMouseCaptured()&&!jade.compat.application.isDragging()&&(this.jadeForm?this.jadeForm.handleMouseOut(a?a:window.event):(b=this.getPageMouseCoords(a),c=a.target?a.target:a.srcElement,d=b.x-this.pageX,e=b.y-this.pageY,(d<0||e<0||d>=this.width-1||e>=this.height-1)&&this.mainWidget&&this.mainWidget.isMouseInWidget()&&this.mainWidget.mouseLeave()))};
jade.compat.Form.prototype.getRelativeMouseCoords=function(a){var b,c;b={};a.offsetX&&a.layerX?(b.x=a.offsetX,b.y=a.offsetY):(b.x=a.layerX|a.offsetX,b.y=a.layerY|a.offsetY);for(c=a.srcElement||a.target;c!=this.element;){if(c===null)return null;b.x+=c.offsetLeft;b.y+=c.offsetTop;c=c.offsetParent}return b};
jade.compat.Form.prototype.getPageMouseCoords=function(a){var b,c;b={};typeof a.pageY=="number"?(b.x=a.pageX,b.y=a.pageY):(c=jade.compat.Application.getInstance(),b.x=a.clientX+c.getScrollLeft(),b.y=a.clientY+c.getScrollTop());return b};
jade.compat.Form.prototype.redraw=function(){var a,b,c;b=this.width;c=this.height;this.elementId?(this.width=this.element.offsetWidth,this.height=this.element.offsetHeight):(a=jade.compat.Application.getInstance(),this.width=a.getClientWidth(),this.height=a.getClientHeight(),this.element.style.width=this.width+"px",this.element.style.height=this.height+"px");this.mainWidget&&this.mainWidget.setGeometry(0,0,this.width,this.height,jade.ALIGN_NORTHWEST);(b!=this.width||c!=this.height)&&this.sendSignal("resize")};
jade.compat.Form.prototype.setMainWidget=function(a){var b;if(this.mainWidget&&this.initialized)this.element.removeChild(this.mainWidget.getRootElement()),this.mainWidget.form=null;a&&a.getParent()&&a.getParent().removeChild(a);if(this.mainWidget=a)this.mainWidget.form=this;if(this.mainWidget&&this.initialized){b=this.mainWidget.getRootElement();this.element.appendChild(b);if(b.offsetParent&&b.offsetParent.id!=this.elementId)this.element.style.position="relative";this.redraw()}};
jade.compat.Form.prototype.getPagePosition=function(){var a;if(this.pageX===null||this.pageY===null)throw Error("Unable to determine page position of the form");a={};a.x=this.pageX;a.y=this.pageY;return a};
jade.compat.Form.prototype.isInitialized=function(){return this.initialized};
