
var _resize_listeners = new Array();
var zIndex = 100;

//function initResize() {
    document.onmouseup = function(e) {
        for (var i = 0; i < _resize_listeners.length; i++) {
            _resize_listeners[i].mouseUp(e);
        }
    };

    document.onmousemove = function(e) {
        for (var i = 0; i < _resize_listeners.length; i++) {
            _resize_listeners[i].mouseMove(e);
        }
    };
//};

Array.prototype.contains = function (element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
};

function getChildByType(type, node, index) {
    var tmp = 0;
    for (var i = 0; i < node.childNodes.length; i++) {
        if (node.childNodes[i].nodeType == type) {
            if (tmp == index) {
                return node.childNodes[i];
            }
            tmp++;
        }
    }
    return null;
}

function Resize(config) {

    this.active = false;
    this.targets = new Array();
	var props = {
		active: false,                   // Global toggle of drag/resize.
		minWidth: 10, minHeight: 10,     // Minimum pixel size of elements.
        maxWidth: 99999, maxHeight: 99999,     // Maximum pixel size of elements.
		lastMouseX: 0, lastMouseY: 0    // Last processed mouse positions.
	};

    for (var p in props) {
        this[p] = (typeof config[p] == 'undefined') ? props[p] : config[p]
    }

    this.addTarget = function(target) {
        if (!this.targets.contains(target)) {
            this.targets.push(target);
            /*var elem = document.getElementById(target);
            if (elem) {
                with(elem) {
                    //style.width = this.minWidth+'px';
                    //style.width = this.minWidth+'px';
                    //style.height = this.minHeight+'px';
                }
            }*/
        }
    };

    this.mouseDown = function(event) {
        //alert("mousedown");
        this.active = true;
        if (!event) {
            event = window.event;
        }
        this.lastMouseX = event.screenX;
        this.lastMouseY = event.screenY;
    };

    this.mouseUp = function(event) {
        //alert("mouseup");
        this.active = false;
        document.body.style.cursor='default';
    };

    this.mouseMove = function(event) {
        //alert("mousemove");
        if (this.active) {
            document.body.style.cursor='move';
            if (!event) {
                event = window.event;
            }
            var mouseX = event.screenX;
            var mouseY = event.screenY;
            for (var index = 0; index < this.targets.length; index++) {
                if (this.targets[index]) {
                    //var elem = document.getElementById(this.targets[index]);
                    var elem = this.targets[index];
                    if (elem) {
                        with (elem) {
                            //alert(style);
                            var oldWidth = 0;
                            var oldHeight = 0;
                            if (style.width && style.height) {
                                oldWidth = parseInt(style.width.match(new RegExp('([0-9]+)([a-zA-Z%]{0,3})')) ? RegExp.$1 : 0);
                                oldHeight = parseInt(style.height.match(new RegExp('([0-9]+)([a-zA-Z%]{0,3})')) ? RegExp.$1 : 0);
                            } else {
                                oldWidth = this.minWidth;
                                oldHeight = this.minHeight;
                            }
                            var newWidth = oldWidth + mouseX-this.lastMouseX;
                            var newHeight = oldHeight + mouseY-this.lastMouseY;

                            if (newWidth > this.minWidth && newWidth < this.maxWidth) {
                                style.width = newWidth + 'px';
                            }

                            if (newHeight > this.minHeight && newHeight < this.maxHeight) {
                                style.height = newHeight + 'px';
                            }
                            //alert("mouseX: " + mouseX+ ", lastMouseX:" + this.lastMouseX + ", oldWidth: " + oldWidth + ", newWidth" + newWidth);
                        }
                    }
                }
                if (this.frame)  {
                    this.increaseZIndex();
                }
            }
            this.lastMouseX = mouseX;
            this.lastMouseY = mouseY;
        }
    };
    
    this.register = function(handle) {
        var obj = this;
        handle.onmousedown = function(e) { obj.mouseDown(e)};
        _resize_listeners.push(obj);
    };
    
    this.setFrame = function(frame) {
        this.frame = frame;
    };

    this.increaseZIndex = function() {
        this.frame.style.zIndex = zIndex++;
    };
};

