MDBSDDSwap = function(id, sGroup, config)
{
    this.swapInit(id, sGroup, config);
};

YAHOO.extend(MDBSDDSwap, YAHOO.util.DDProxy);

MDBSDDSwap.prototype.showFrame = function(iPageX, iPageY)
{
    var el = this.getEl();
    var dragEl = this.getDragEl();
    var s = dragEl.style;

    this._resizeProxy();

    if (this.centerFrame)
    {
        this.setDelta( Math.round(parseInt(s.width,  10)/2), 
                       Math.round(parseInt(s.height, 10)/2) );
    }

    this.setDragElPos(iPageX, iPageY);

    dragEl.innerHTML = this.getEl().parentNode.innerHTML;
    YAHOO.util.Dom.setStyle(dragEl, "visibility", "visible"); 
    YAHOO.util.Dom.setStyle(dragEl, "border", "0");
};

MDBSDDSwap.prototype.swapInit = function(id, sGroup, config)
{
    if (!id) { return; }

    this.init(id, sGroup, config);
    this.initFrame();
    this.logger = this.logger || YAHOO;

    /**
     * css style to use when items are not being hovered over.
     */
    this.offClass = "set_multi";

    /**
     * css style to use when hovered over
     */
    this.onClass = "set_multiOn";

    /**
     * cache of the elements we have changed the style so we can restore it
     * later
     */
    this.els = [];

};

MDBSDDSwap.prototype.onDragDrop = function(e, id)
{
    var dd = YAHOO.util.DDM.getDDById(id);
    this.swap(this.getEl(), dd.getEl());
    this.resetConstraints();
    dd.resetConstraints();
};

MDBSDDSwap.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 title1,title2;
    var elements = YAHOO.util.Dom.getElementsByClassName('mul_imgcon','div',el1);
    if( elements.length > 0 )
	    title1 = elements[0].id;
    elements = YAHOO.util.Dom.getElementsByClassName('mul_imgcon','div',el2);
    if( elements.length > 0 )
	    title2 = elements[0].id;
    setTheme(el2.id,title1,el1.id,title2);
    
    
    var tmp = el1.innerHTML;
    el1.innerHTML = el2.innerHTML;
    el2.innerHTML = tmp;
    
    /*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;*/
};

MDBSDDSwap.prototype.onDragEnter = function(e, id)
{
    this.logger.log(this.id + " dragEnter " + 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;
    }
};

MDBSDDSwap.prototype.onDragOut = function(e, id)
{
    this.logger.log(this.id + " dragOut " + id);

    // restore the style
    YAHOO.util.DDM.getElement(id).className = this.offClass;
};

MDBSDDSwap.prototype.endDrag = function(e)
{
    this.logger.log(this.id + " endDrag");
    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" } );
*/
};

MDBSDDSwap.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; }
    }
};

MDBSDDSwap.prototype.onDrag = function(e) { };

MDBSDDSwap.prototype.onDragOver = function(e) { };


MDBSDDApp = function()
{
    var dd, dd2, dd3, dd4, dd5, dd6;

    function initPointMode()
    {

        unreg();

        YAHOO.util.DDM.mode = YAHOO.util.DDM.POINT;

        dd = new MDBSDDSwap("dragDiv1", "theme");
		dd.setHandleElId("handle");

        dd2 = new MDBSDDSwap("dragDiv2", "theme");
		dd2.setHandleElId("handle");

        dd3 = new MDBSDDSwap("dragDiv3", "theme");
		dd3.setHandleElId("handle");

		dd4 = new MDBSDDSwap("dragDiv4", "theme");
		dd4.setHandleElId("handle");

		dd5 = new MDBSDDSwap("dragDiv5", "theme");
		dd5.setHandleElId("handle");

		dd6 = new MDBSDDSwap("dragDiv6", "theme");
		dd6.setHandleElId("handle");
    }

    function unreg()
    {
        if (dd) dd.unreg();
        if (dd2) dd2.unreg();
        if (dd3) dd3.unreg();
		if (dd4) dd4.unreg();
		if (dd5) dd5.unreg();
		if (dd6) dd6.unreg();
    }

    return {
        init: function()
        {
            initPointMode();
        }
    };
}();


YAHOO.util.Event.addListener(window, "load", MDBSDDApp.init);
var theme_handle = function()
{
}

function setTheme(index1,title1,index2,title2)
{
	var postData = "theme1="+encodeURI(title1);
	postData += "&theme2="+encodeURI(title2);
	postData += "&theme1Index="+index1.substring(7);
	postData += "&theme2Index="+index2.substring(7);

	var callback =
	{
	  success:theme_handle,
	  failure:theme_handle
	};

	YAHOO.util.Connect.initHeader('If-Modified-Since','0');
	YAHOO.util.Connect.initHeader('Cache-Control','no-cache');
	YAHOO.util.Connect.initHeader('Pragma','no-cache');
	YAHOO.util.Connect.asyncRequest('POST', 'PAT/theme.jsp', callback, postData);
	return false;
}