function DIALOG_DRAGGER(dialog) {
    this.dialog = dialog;
    
    this.resizeCornerSize = 16;
	/* If set to small, we will hide scrollbars of content window and
	resizing will not be operable any more */
    this.minimumWidth = 60;
    this.minimumHeight = 15;
    
    this.MouseMoveBind = this.MouseMove.bindAsEventListener( this );
    this.MouseOutBind = this.MouseOut.bindAsEventListener( this );
    this.MaximizeBind = this.Maximize.bindAsEventListener( this );
    this.ResizeBind = this.Resize.bindAsEventListener( this );
    this.MoveBind = this.Move.bindAsEventListener( this );
    
    this.TrackBind = this.Track.bindAsEventListener( this );
    this.DoneBind = this.Done.bindAsEventListener( this );
    
    this.label_displayed = true;
    
    this.in_resize = false;
    this.in_move = false;
    this.maximized = false;
    
    this.is_moved = false;
    this.is_resized = false;
}


DIALOG_DRAGGER.prototype.Setup = function() {
	// IE7 :) needs it to adjust widths of sub-divs
    domSetWidth(this.dialog.node, this.dialog.node.offsetWidth);
    
	// IE6 :) fixing size for iframes
    if (this.dialog.iframe) {
    	this.SaveDimenssions();
	this.Restore();
    }    


    Event.observe(this.dialog.maximize_node, 'click', this.MaximizeBind );
    Event.observe(this.dialog.node, 'mousemove', this.MouseMoveBind );
    Event.observe(this.dialog.node, 'mouseout', this.MouseOutBind );
    Event.observe(this.dialog.node, 'mousedown', this.ResizeBind );
    Event.observe(this.dialog.titlebar, 'mousedown', this.MoveBind );
    
    
    
}

DIALOG_DRAGGER.prototype.Clean = function() {
    Event.stopObserving(this.dialog.maximize_node, 'click', this.MaximizeBind );
    Event.stopObserving(this.dialog.node, 'mousemove', this.MouseMoveBind );
    Event.stopObserving(this.dialog.node, 'mouseout', this.MouseOutBind );
    Event.stopObserving(this.dialog.node, 'mousedown', this.ResizeBind );
    Event.stopObserving(this.dialog.titlebar, 'mousedown', this.MoveBind );
}

DIALOG_DRAGGER.prototype.MouseMove = function(ev) {
    if ((this.in_resize)||(this.in_move)) return;
    
    var target = domGetEventTarget(ev);
    if (target != this.dialog.node) {
	this.resizeDirection = "";
	this.dialog.node.style.cursor = "";
	return;
    }
    
    var width = this.dialog.node.offsetWidth;
    var height = this.dialog.node.offsetHeight;
    
    var offset = domGetEventLayerOffset(ev);
    var xOff = offset[0];
    var yOff = offset[1];
  
    var resizeDirection = ""
    if (yOff < this.resizeCornerSize)
	resizeDirection += "n";
    else if (yOff > (height - this.resizeCornerSize))
	resizeDirection += "s";
    if (xOff < this.resizeCornerSize)
	resizeDirection += "w";
    else if (xOff > (width - this.resizeCornerSize))
	resizeDirection += "e";

    if (resizeDirection) {
	this.resizeDirection = resizeDirection;
	this.dialog.node.style.cursor = resizeDirection + "-resize";
    } else {
	this.resizeDirection = "";
	this.dialog.node.style.cursor = "";
    }
}

DIALOG_DRAGGER.prototype.MouseOut = function(ev) {
/* Causes problems in Opera
    this.dialog.node.style.cursor = "";
*/
}


DIALOG_DRAGGER.prototype.Move = function(ev) {
    var target = domGetEventTarget(ev);
    if ((target != this.dialog.titlebar)&&(target != this.dialog.label)) return;

    Event.observe(document, 'mousemove', this.TrackBind );
    Event.observe(document, 'mouseup', this.DoneBind );

    var node = this.dialog.node;
    var offset = domGetEventPageOffset(ev);
    this.xOffset = parseInt(domGetStyleProp(node, node.style.left, "left") ,10) - offset[0];
    this.yOffset = parseInt(domGetStyleProp(node, node.style.top, "top"), 10) - offset[1];
    
    this.in_move = true;
    
}

DIALOG_DRAGGER.prototype.SaveDimenssions = function() {
    var node = this.dialog.node;
    var mnode = this.dialog.content;

    this.oldLeft = parseInt(domGetStyleProp(node, node.style.left, "left") ,10);
    this.oldTop = parseInt(domGetStyleProp(node, node.style.top, "top"), 10);

	// Obtaining original window size
    var size = domGetNodeSize(node);
    node.style.width = size[0] + "px";
    node.style.height = size[1] + "px";

    var csize = domGetNodeSize(node);
    this.corr_x = csize[0] - size[0];
    this.corr_y = csize[1] - size[1];
    

    this.oldWidth = size[0] - this.corr_x;//parseInt(domGetStyleProp(node, node.style.width, "width"), 10);
    this.oldHeight = size[1] - this.corr_y;//parseInt(domGetStyleProp(node, node.style.height, "height"), 10);

    node.style.width = this.oldWidth + "px";
    node.style.height = this.oldHeight + "px";

	// Obtaining content element size
    size = domGetNodeSize(mnode);

//    mnode.style.width = size[0] + "px";
    mnode.style.height = size[1] + "px";

    var csize = domGetNodeSize(mnode);
//    this.corr_cx = csize[0] - size[0];
    this.corr_cy = csize[1] - size[1];

//    this.cWidth = size[0] - this.corr_cx;
    this.cHeight = size[1] - this.corr_cy;
    
//    mnode.style.width = this.cWidth + "px";
    mnode.style.height = this.cHeight + "px";
}

DIALOG_DRAGGER.prototype.Resize = function(ev) {
    var target = domGetEventTarget(ev);
    if (target != this.dialog.node) return;
    
    var node = this.dialog.node;
    var lnode = this.dialog.titlebar;
    
    Event.observe(document, 'mousemove', this.TrackBind );
    Event.observe(document, 'mouseup', this.DoneBind );

    this.in_resize = true;

	// If max-height not supported set titlebar height
    var tb = this.dialog.titlebar;
    var mh = parseInt(domGetStyleProp(tb, tb.style.maxHeight, "max-height"),10);
    if ((!mh)||((tb.offsetHeight - mh)>10)) {
	domSetWidth(node, node.offsetWidth);
	if (mh>0) domSetHeight(tb, mh);
	else domSetHeight(tb, 21);
    }
    
    if (!this.label_width) {
	this.label_width = this.dialog.label.offsetWidth + this.dialog.label.offsetLeft;
    }

    var offset = domGetEventPageOffset(ev);
    this.xPosition = offset[0];
    this.yPosition = offset[1];
    
    this.SaveDimenssions();    
    
	// Obtaining titlebar size (no correction, since preciese size is not important
    var size = domGetNodeSize(lnode);
    size[1] += this.corr_y;
    if ((size[1] + 10) > this.minimumHeight) this.minimumHeight = size[1] + 10;
}

DIALOG_DRAGGER.prototype.Restore = function(ev) {
    var node = this.dialog.node;
    var mnode = this.dialog.content;

    node.style.left = this.oldLeft + "px";
    node.style.top = this.oldTop + "px";
    node.style.width = this.oldWidth + "px";
    node.style.height = this.oldHeight + "px";
    mnode.style.height = this.cHeight + "px";

    cssSetClass(this.dialog.maximize_node, "maximize");
    this.maximized = false;
}

DIALOG_DRAGGER.prototype.Maximize = function(ev) {
    var node = this.dialog.node;
    var mnode = this.dialog.content;

    if (this.maximized) {
	this.Restore();
    } else {
	this.SaveDimenssions();
	
	node.style.left = 0;
	node.style.top = 0;
	
	var ww = windowGetWidth();
    	var wh = windowGetHeight();

	node.style.width =  (ww - this.corr_x) + "px";
	
	if ((wh - this.corr_y) > this.oldHeight) {
	    node.style.height = (wh - this.corr_y) + "px";
	    mnode.style.height = (this.cHeight + (wh - this.corr_y - this.oldHeight)) + "px";
	}

	cssSetClass(this.dialog.maximize_node, "restore");
	this.maximized = true;
    }
}

DIALOG_DRAGGER.prototype.Track = function(ev) {
    var node = this.dialog.node;
    var mnode = this.dialog.content;

    if (this.in_resize) {
	var north = false;
	var south = false;
	var east  = false;
	var west  = false;
	
	if (this.resizeDirection.charAt(0) == "n") north = true;
	if (this.resizeDirection.charAt(0) == "s") south = true;
	if (this.resizeDirection.charAt(0) == "e" || this.resizeDirection.charAt(1) == "e") east = true;
	if (this.resizeDirection.charAt(0) == "w" || this.resizeDirection.charAt(1) == "w") west = true;

        var offset = domGetEventPageOffset(ev);
	var dx = offset[0] - this.xPosition;
	var dy = offset[1] - this.yPosition;

	if (west) dx = -dx;
	if (north) dy = -dy;

	var w = this.oldWidth  + dx;
	var h = this.oldHeight + dy;
	
	if (w < this.minimumWidth) {
	    w = this.minimumWidth;
	    dx = w - this.oldWidth;
	}
	
	if (h < this.minimumHeight) {
	    h = this.minimumHeight;
	    dy = h - this.oldHeight;
	}
	
	if (east || west) {
	    if (w < this.label_width) {
		if (this.label_displayed) {
		    this.dialog.titlebar.removeChild(this.dialog.label);
		    this.label_displayed = false;
		}
	    } else {
		if (!this.label_displayed) {
		    this.dialog.titlebar.appendChild(this.dialog.label);
		    this.label_displayed = true;
		}
	    }
	    
	    node.style.width = w + "px";
	    if (west) node.style.left = (this.oldLeft - dx) + "px";
	}
	
	if (north || south) {
	    node.style.height = h + "px";
	    if ((this.cHeight + dy) < 0) mnode.style.height = 0 + "px";
	    else mnode.style.height = (this.cHeight  + dy) + "px";
	    if (dy < 0) {
		var size = domGetNodeSize(mnode);
		var rdy = size[1] - this.corr_cy - this.cHeight;
		if (rdy > dy) {
	    	    node.style.height = (this.oldHeight + rdy) + "px";
		}
	    }
	    
	    if (north) node.style.top  = (this.oldTop  - dy) + "px";
	}
	
	this.is_resized = true;
    } else if (this.in_move) {
        var offset = domGetEventPageOffset(ev);
	node.style.left = (offset[0] + this.xOffset) + "px";
	node.style.top  = (offset[1] + this.yOffset) + "px";
	
	this.is_moved = true;
    }
    
    Event.stop(ev);
}

DIALOG_DRAGGER.prototype.Done = function(ev) {
    if (this.in_resize) {
	this.in_resize = false;
    }

    if (this.in_move) {
	this.in_move = false;
    }
    
    Event.stopObserving(document, 'mousemove', this.TrackBind );
    Event.stopObserving(document, 'mouseup', this.DoneBind );

    Event.stop(ev);
}
