//var CURRENT_WINDOW = null;


bhajs.defineClass("BHA.Web.UI.ClientControls", "Window", 
    function (id) {
        this.$id = id;
        this.$classInfo = bhajs.getClassInfo("BHA.Web.UI.ClientControls.Window");
        this.$classInfo.registerObject(this.$id, this);
        
        this.$element = null;
        this.$windowBehavior = null;
        this.$inputTracking = false;
        this.$inputData = null;
        this.$defaultWindowBehavior = null; 
        this.$onMouseDownHandler = null;
//        this.$onMouseMoveHandler = null;
//        this.$onMouseUpHandler = null;
//        this.$onKeyDownHandler = null;
        this.$onMouseDragMoveHandler = null;
        this.$onMouseDragUpHandler = null;
        this.$onMouseDragKeyDownHandler = null;
        
        this.$onMouseDownHandler = this.$classInfo.createInstanceMethodCall(
            this.$id,"handleOnMouseDown", ["e"]);
//        this.$onMouseMoveHandler = this.$classInfo.createInstanceMethodCall(
//            this.$id,"handleOnMouseMove", ["e"]);
//        this.$onMouseUpHandler = this.$classInfo.createInstanceMethodCall(
//            this.$id,"handleOnMouseUp", ["e"]);
//        this.$onKeyDownHandler = this.$classInfo.createInstanceMethodCall(
//            this.$id,"handleOnKeyDown", ["e"]);
                
        this.$onMouseDragMoveHandler = this.$classInfo.createInstanceMethodCall(
            this.$id,"handleOnMouseDragMove", ["e"]);
//        alert(this.$onMouseDragMoveHandler);
            
//        this.$onMouseDragMoveHandler = new Function("e","CURRENT_WINDOW.handleOnMouseDragMove(e);");
//        alert(this.$onMouseDragMoveHandler);
        
        
        this.$onMouseDragUpHandler = this.$classInfo.createInstanceMethodCall(
            this.$id,"handleOnMouseDragUp", ["e"]);
        this.$onMouseDragKeyDownHandler = this.$classInfo.createInstanceMethodCall(
            this.$id,"handleOnMouseDragKeyDown", ["e"]);
            
            
        var nsClientControls = bhajs.findNamespace("BHA.Web.UI.ClientControls");
        this.$inputData = new nsClientControls.WindowInputData(this.$id);
        this.$defaultWindowBehavior = new nsClientControls.WindowBehavior(this.$id); 
        this.$defaultWindowBehavior.cursor = "default";
        
        this.getElement = function() {
            if (this.$element) return this.$element;
            this.$element = document.getElementById(this.$id);
            //alert("Element Set");
            return this.$element;
        }
        
        this.setCursor = function(cursor) {
            this.getElement().style.cursor = cursor;
        }
        
        this.clearWindowBehavior = function()
        {   
            this.stopInputTracking();
            this.setCursor('');
            
            if (!this.$windowBehavior) return;
            
            var element = this.getElement();
            if (element.removeEventListener) {
                element.removeEventListener("mousedown", this.$onMouseDownHandler, false);
//                if (this.$windowBehavior.onMouseMove) {
//                    element.removeEventListener("mousemove", this.$onMouseMoveHandler, false);
//                }
//                if (this.$windowBehavior.onMouseUp) {
//                    element.removeEventListener("mouseup", this.$onMouseUpHandler, false);
//                }
//                if (this.$windowBehavior.onKeyDown) {
//                    element.removeEventListener("keydown", this.$onKeyDownHandler, false);
//                }
            }else if (element.detachEvent) {
                element.detachEvent("onmousedown", this.$onMouseDownHandler);
//                if (this.$windowBehavior.onMouseMove) {
//                    element.detachEvent("onmousemove", this.$onMouseMoveHandler);
//                }
//                if (this.$windowBehavior.onMouseUp) {
//                    element.detachEvent("onmouseup", this.$onMouseUpHandler);
//                }
//                if (this.$windowBehavior.onKeyDown) {
//                    element.detachEvent("onkeydown", this.$onKeyDownHandler);
//                }
            }// end if
            
            this.$windowBehavior = null;
        }
        
        this.setWindowBehavior = function(windowBehavior) {
            //CURRENT_WINDOW = this;
        
            this.clearWindowBehavior();
            this.$windowBehavior = windowBehavior;
            
            if (!this.$windowBehavior) return;
            
            var element = this.getElement();
            element.style.cursor = windowBehavior.cursor;
            
            if (element.addEventListener) {
                element.addEventListener("mousedown", this.$onMouseDownHandler, false);
//                if (this.$windowBehavior.onMouseMove) {
//                    element.addEventListener("mousemove", this.$onMouseMoveHandler, false);
//                }
//                if (this.$windowBehavior.onMouseUp) {
//                    element.addEventListener("mouseup", this.$onMouseUpHandler, false);
//                }
//                if (this.$windowBehavior.onKeyDown) {
//                    element.addEventListener("keydown", this.$onKeyDownHandler, false);
//                }
            }else if (element.attachEvent) {
                element.attachEvent("onmousedown", this.$onMouseDownHandler);
//                if (this.$windowBehavior.onMouseMove) {
//                    element.attachEvent("onmousemove", this.$onMouseMoveHandler);
//                }
//                if (this.$windowBehavior.onMouseUp) {
//                    element.attachEvent("onmouseup", this.$onMouseUpHandler);
//                }
//                if (this.$windowBehavior.onKeyDown) {
//                    element.attachEvent("onkeydown", this.$onKeyDownHandler);
//                }
            }// end if
            
            
        }
        
        this.startInputTracking = function() {
            this.stopInputTracking();
            this.$inputData.reset();
            var element = document;
            if (element.addEventListener) {
                element.addEventListener("mousemove", this.$onMouseDragMoveHandler, false);
                element.addEventListener("mouseup", this.$onMouseDragUpHandler, false);
                element.addEventListener("keydown", this.$onMouseDragKeyDownHandler, false);
            }else if (element.attachEvent) {
                element.attachEvent("onmousemove", this.$onMouseDragMoveHandler);
                element.attachEvent("onmouseup", this.$onMouseDragUpHandler);
                element.attachEvent("onkeydown", this.$onMouseDragKeyDownHandler);
            }
            this.$inputTracking = true;
        }
        
        this.stopInputTracking = function() {
            if (!this.$inputTracking) return;
            this.$inputTracking = false;
            var element = document;
            if (element.removeEventListener) {
                element.removeEventListener("mousemove", this.$onMouseDragMoveHandler, false);
                element.removeEventListener("mouseup", this.$onMouseDragUpHandler, false);
                element.removeEventListener("keydown", this.$onMouseDragKeyDownHandler, false);
            }else if (element.detachEvent) {
                element.detachEvent("onmousemove", this.$onMouseDragMoveHandler);
                element.detachEvent("onmouseup", this.$onMouseDragUpHandler);
                element.detachEvent("onkeydown", this.$onMouseDragKeyDownHandler);
            }
        }
        
        this.handleOnMouseDown = function(e) {
            if (!this.$windowBehavior) return;
            this.startInputTracking();
            
            var element = this.getElement();
            this.$inputData.clientDownX = e.clientX;
            this.$inputData.clientDownY = e.clientY;
            this.$inputData.clientX = e.clientX;
            this.$inputData.clientY = e.clientY;
           
            this.$inputData.downX = e.clientX - element.offsetLeft;
            this.$inputData.downY = e.clientY - element.offsetTop;
            
            if (e.layerX) {
                this.$inputData.X = e.layerX;
                this.$inputData.Y = e.layerY;
            }else if (e.x) {
                this.$inputData.X = e.x;
                this.$inputData.Y = e.y;
            }else {
                this.$inputData.X = 0;
                this.$inputData.Y = 0;
            }
//            this.$inputData.X = e.clientX - element.offsetLeft;
//            this.$inputData.Y = e.clientY - element.offsetTop;
            
            this.$inputData.deltaX = e.clientX - this.$inputData.clientDownX;
            this.$inputData.deltaY = e.clientY - this.$inputData.clientDownY;
            
            this.$inputData.element = element;
            this.$inputData.eventData = e;

            if (this.$windowBehavior.onMouseDown)
            {
                this.$windowBehavior.onMouseDown(this.$inputData);
            }
            this.$inputData.element = null;
            this.$inputData.eventData = null;
            
            if (this.$inputData.cancelInputTracking)
            {
                this.stopInputTracking();
            }
            
        }
        
        this.handleOnMouseMove = function(e) {
            if (!this.$windowBehavior) return;
     
     
            var element = this.getElement();
            
            
            this.$inputData.element = element;
            this.$inputData.eventData = e;
            
            if (this.$windowBehavior.onMouseMove)
            {
                this.$windowBehavior.onMouseMove(this.$inputData);
            }
            
            this.$inputData.element = null;
            this.$inputData.eventData = null;
            
        }
        this.handleOnMouseUp = function(e) {
            if (!this.$windowBehavior) return;
            
            var element = this.getElement();
            
            
            this.$inputData.element = element;
            this.$inputData.eventData = e;
            
            if (this.$windowBehavior.onMouseUp)
            {
                this.$windowBehavior.onMouseUp(this.$inputData);
            }
            this.$inputData.element = null;
            this.$inputData.eventData = null;
        }
        
        this.handleOnKeyDown = function(e) {
            if (!this.$windowBehavior) return;
            
            var element = this.getElement();
            
            
            this.$inputData.element = element;
            this.$inputData.eventData = e;
            
            if (this.$windowBehavior.onKeyDown)
            {
                this.$windowBehavior.onKeyDown(this.$inputData);
            }
            this.$inputData.element = null;
            this.$inputData.eventData = null;
        }
       
        this.handleOnMouseDragMove = function(e) {
            if (!this.$inputTracking) return;
            if (!this.$windowBehavior) return;
            
            var element = this.getElement();
            this.$inputData.clientX = e.clientX;
            this.$inputData.clientY = e.clientY;
           
           
            if (e.layerX) {
                this.$inputData.X = e.layerX;
                this.$inputData.Y = e.layerY;
            }else if (e.x) {
                this.$inputData.X = e.x;
                this.$inputData.Y = e.y;
            }else {
                this.$inputData.X = 0;
                this.$inputData.Y = 0;
            }
//            this.$inputData.X = e.clientX - element.offsetLeft;
//            this.$inputData.Y = e.clientY - element.offsetTop;
            
            this.$inputData.deltaX = e.clientX - this.$inputData.clientDownX;
            this.$inputData.deltaY = e.clientY - this.$inputData.clientDownY;
            
            this.$inputData.element = element;
            this.$inputData.eventData = e;
            
            if (this.$windowBehavior.onMouseDragMove)
            {
                this.$windowBehavior.onMouseDragMove(this.$inputData);
            }
            this.$inputData.element = null;
            this.$inputData.eventData = null;
            
            if (this.$inputData.cancelInputTracking)
            {
                this.stopInputTracking();
            }
        
        }
        
        this.handleOnMouseDragUp = function(e) {
            if (!this.$inputTracking) return;
            this.stopInputTracking();
            if (!this.$windowBehavior) return;
            
            var element = this.getElement();
            this.$inputData.clientX = e.clientX;
            this.$inputData.clientY = e.clientY;
           
            if (e.layerX) {
                this.$inputData.X = e.layerX;
                this.$inputData.Y = e.layerY;
            }else if (e.x) {
                this.$inputData.X = e.x;
                this.$inputData.Y = e.y;
            }else {
                this.$inputData.X = 0;
                this.$inputData.Y = 0;
            }
//            this.$inputData.X = e.clientX - element.offsetLeft;
//            this.$inputData.Y = e.clientY - element.offsetTop;
            
            this.$inputData.deltaX = e.clientX - this.$inputData.clientDownX;
            this.$inputData.deltaY = e.clientY - this.$inputData.clientDownY;
            
            this.$inputData.element = element;
            this.$inputData.eventData = e;
            
            if (this.$windowBehavior.onMouseDragUp)
            {
                this.$windowBehavior.onMouseDragUp(this.$inputData);
            }
            this.$inputData.element = null;
            this.$inputData.eventData = null;

            this.getElement().focus();
        }
        
        this.handleOnMouseDragKeyDown = function(e) {
            if (!this.$inputTracking) return;
            if (!this.$windowBehavior) return;
            
            this.$inputData.keyCode = e.keyCode;
            this.$inputData.eventData = e;
            if (this.$windowBehavior.onMouseDragKeyDown)
            {
                this.$windowBehavior.onMouseDragKeyDown(this.$inputData);
            }
            this.$inputData.eventData = null;
            
            
            if (this.$inputData.cancelInputTracking)
            {
                this.stopInputTracking();
            }
       
        }
        
        this.setWindowBehavior(this.$defaultWindowBehavior);
            
    }
);


bhajs.defineClass("BHA.Web.UI.ClientControls", "WindowBehavior", 
    function (id) {
        //alert("new WindowBehavior");
        //alert(id);
        this.$id = id;
        this.$classInfo = bhajs.getClassInfo("BHA.Web.UI.ClientControls.WindowBehavior");
        this.$classInfo.registerObject(this.$id, this);
        
        this.cursor = null;
        this.onMouseDown = null;
        this.onMouseMove = null;
        this.onMouseUp = null;
        this.onKeyDown = null;
        
        this.onMouseDragMove = null;
        this.onMouseDragUp = null;
        this.onMouseDragKeyDown = null;
        
        
        
    }
);

bhajs.defineClass("BHA.Web.UI.ClientControls", "WindowInputData", 
    function (id) {
        this.$id = id;
        this.$classInfo = bhajs.getClassInfo("BHA.Web.UI.ClientControls.WindowInputData");
        this.$classInfo.registerObject(this.$id, this);
        
        this.clientDownX = null;
        this.clientDownY = null;
        this.clientX = null;
        this.clientY = null;
        this.downX = null;
        this.downY = null;
        this.X = null;
        this.Y = null;
        this.deltaX = null;
        this.deltaY = null;
        this.keyCode = null;
        this.element = null;
        this.eventData = null;
        this.cancelInputTracking = false;
        
        this.reset = function() {
            this.clientDownX = null;
            this.clientDownY = null;
            this.clientX = null;
            this.clientY = null;
            this.downX = null;
            this.downY = null;
            this.deltaX = null;
            this.deltaY = null;
            this.X = null;
            this.Y = null;
            this.keyCode = null;
            this.element = null;
            this.eventData = null;
            this.cancelInputTracking = false;
        }
        
        
    }
);


