/**
 * @author Falko Zander, www.falko-zander.de
 */

/**
 * @param object Element
 * @param area Element
 */
function cDndWidget(object, area) {
    this.object = object;
    this.area = area;
    
    var THIS = this;
    this.elem = (this.area != null) ? this.area : this.object;
    this.elem.style.cursor = 'move';
    
    this.zIndex = 1;
    
    this.f_save1 = this.elem.onmousedown;
    this.f_save2 = this.elem.onselectstart;
    this.f_save3 = this.elem.ondragstart;
    this.elem.onmousedown = function(e) { THIS.begin_(e, THIS.f_save1); };
    this.elem.onselectstart = function() { return false; }
    this.elem.ondragstart = function() { return false; }
}

/**
 * @return Object
 */
cDndWidget.prototype.destructor = function() {
    this.elem.onmousedown = (typeof this.f_save1 == 'function') ? this.f_save1 : null;
    this.elem.onselectstart = (typeof this.f_save2 == 'function') ? this.f_save2 : null;
    this.elem.ondragstart = (typeof this.f_save3 == 'function') ? this.f_save3 : null;
    this.object = null;
    this.area = null;
    this.elem = null;
    
    return null;
}

/**
 * @param event Event
 * @param old_func Function
 */
cDndWidget.prototype.begin_ = function(event, old_func) {
    if (IE == 1) { 
        event = window.event; 
    } else if (typeof event.preventDefault != 'undefined') {
        event.preventDefault();
    }
    
    this.zIndex = this.object.style.zIndex || 1;
    this.object.style.zIndex = '100';
    
    var pos = getAbsoluteDimensions(this.object);
    this.calcX = event.clientX - pos.offsetLeft - pos.parentsLeft;
    this.calcY = event.clientY - pos.offsetTop - pos.parentsTop;
    
    var THIS = this;
    var f1, f2;
    if (IE == 1) {
        f1 = document.onmousemove;
        f2 = document.onmouseup;
        document.onmousemove = function(e) { THIS.drag_(e, f1); }
        document.onmouseup = function() { THIS.end_(f1, f2); }
    } else {
        f1 = window.onmousemove;
        f2 = window.onmouseup;
        window.onmousemove = function(e) { THIS.drag_(e, f1); }
        window.onmouseup = function() { THIS.end_(f1, f2); }
    }
    
    if (typeof old_func == 'function') old_func();
}

/**
 * @param event Event
 * @param old_func Function
 */
cDndWidget.prototype.drag_ = function(event, old_func) {
    if (IE == 1) { event = window.event; }
    
    this.object.style.left = event.clientX - this.calcX + 'px';
    this.object.style.top = event.clientY - this.calcY + 'px';
    
    if (typeof old_func == 'function') old_func();
}

/**
 * @param old_func1 Function
 * @param old_func2 Function
 */
cDndWidget.prototype.end_ = function(old_func1, old_func2) {
    if (IE == 1) {
        document.onmousemove = (typeof old_func1 == 'function') ? old_func1 : null;
        document.onmouseup = (typeof old_func2 == 'function') ? old_func2 : null;
    } else {
        window.onmousemove = (typeof old_func1 == 'function') ? old_func1 : null;
        window.onmouseup = (typeof old_func2 == 'function') ? old_func2 : null;
    }
    
    var pos = getAbsoluteDimensions(this.elem);
    if (pos.offsetLeft + pos.offsetWidth < 10) {
        this.object.style.left = 10 - pos.offsetWidth + 'px'; 
    }
    if (pos.offsetTop + pos.offsetHeight < 10) {
        this.object.style.top = 10 - pos.offsetHeight + 'px';
    }
    
    this.object.style.zIndex = this.zIndex;
    
    if (typeof old_func2 == 'function') old_func2();
}
