/* Copyright (c) 2006 Yahoo! Inc. All rights reserved. */

/**
 * @class a YAHOO.util.DDProxy implementation that swaps positions with the 
 * target when dropped
 *
 * @extends YAHOO.util.DDProxy
 * @constructor
 * @param {String} id the id of the linked element
 * @param {String} sGroup the group of related DragDrop items
 */
YAHOO.drow.DDSwap = function(id, sGroup, config) {
    this.swapInit(id, sGroup, config);
};

YAHOO.extend(YAHOO.drow.DDSwap, YAHOO.util.DDProxy);

YAHOO.drow.DDSwap.prototype.swapInit = function(id, sGroup, config) {
    if (!id) { return; }

    this.init(id, sGroup, config);
    this.initFrame();

    /**
     * css style to use when items are not being hovered over.
     */
    this.offClass = "dd_container";

    /**
     * css style to use when hovered over
     */
    this.onClass = "dd_container_on";

    /**
     * cache of the elements we have changed the style so we can restore it
     * later
     */
    this.els = [];

};

YAHOO.drow.DDSwap.prototype.onDragDrop = function(e, id) {
    var dd = YAHOO.util.DDM.getDDById(id);
    this.swap(this.getEl(), dd.getEl());
    this.resetConstraints();
    dd.resetConstraints();
};

YAHOO.drow.DDSwap.prototype.swap = function(el1, el2) {

    // Swap out the position of the two objects.  This only works for absolutely
    // positioned elements.  See for an implementation that 
    // works for relatively positioned elements
    var s1 = el1.style;
    var s2 = el2.style;

    var l = s1.left;
    var t = s1.top;

    s1.left = s2.left;
    s1.top = s2.top;

    s2.left = l;
    s2.top = t;
};

YAHOO.drow.DDSwap.prototype.onDragEnter = function(e, id) {

    // store a ref so we can restore the style later
    this.els[id] = true;

    // set the mouseover style
    var el = YAHOO.util.DDM.getElement(id);
    if (el.className != this.onClass) {
        el.className = this.onClass;
    }
};

YAHOO.drow.DDSwap.prototype.onDragOut = function(e, id) {
    // restore the style
    YAHOO.util.DDM.getElement(id).className = this.offClass;
};

YAHOO.drow.DDSwap.prototype.endDrag = function(e) {
    this.resetStyles();

    /*
        var el = this.getDragEl();
    el.style.visibility = ""; // show the element first
    var position = [100, 100];
    var duration = 0.4;
    var oAnim = new YAHOO.util.Motion( 
           el, { points: { to: position } }, duration, YAHOO.util.Easing.easeOut );

    oAnim.onComplete.subscribe( function() { el.style.visibility = "hidden" } );
*/


};

YAHOO.drow.DDSwap.prototype.resetStyles = function() {
    // restore all element styles
    for (var i in this.els) {
        var el = YAHOO.util.DDM.getElement(i);
        if (el) { el.className = this.offClass; }
    }
};

YAHOO.drow.DDSwap.prototype.onDrag = function(e) { };

YAHOO.drow.DDSwap.prototype.onDragOver = function(e) { };


//-------------------------------------------------------------------------
// Intersect mode
//-------------------------------------------------------------------------

YAHOO.drow.DDSwap_i = function(id, sGroup, strict) {
    this.swapInit(id, sGroup);
};

YAHOO.drow.DDSwap_i.prototype = new YAHOO.drow.DDSwap();

YAHOO.drow.DDSwap_i.prototype.onDragDrop = function(e, dds) {
    var dd = YAHOO.util.DDM.getBestMatch(dds, this.strict);
    this.swap(this.getEl(), dd.getEl());

    this.resetConstraints();
    dd.resetConstraints();
};

YAHOO.drow.DDSwap_i.prototype.onDragOver = function(e, dds) {
 
    this.resetStyles();

    var dd = YAHOO.util.DDM.getBestMatch(dds, this.strict);

    this.els[dd.id] = true;

    // set the mouseover style
    var el = dd.getEl();
    if (el.className != this.onClass) {
        el.className = this.onClass;
    }
 
};

YAHOO.drow.DDSwap_i.prototype.onDragOut = function(e, dds) {
    // restore the style
    for (var i=0; i<dds.length; ++i) {
        dds[i].getEl().className = this.offClass;
    }
};

