(function() {

var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var DDM = YAHOO.util.DragDropMgr;
var DDTargets = [];

Preceptor.util.DDApp = function() {
    return {
        init: function() {
        	DDM.handleOnAvailable = true;
            DDM.mode = 1;
           
        	var boxs = YAHOO.util.Dom.getElementsByClassName( "box" );
		   
		    for( var i = 0; i < boxs.length; i++ )
		    {
		    	new Preceptor.util.DragDropList( boxs[i].id );
		    }
		    
		    var targets = YAHOO.util.Dom.getElementsByClassName( "target" );
		    for( var i = 0; i < targets.length; i++ )
		    {
		    	DDTargets[i] = new YAHOO.util.DDTarget( targets[i].id );
		    }
        }
    };
}();

Preceptor.util.DragDropList = function( id, sGroup, config ) {
	
    if ( id ) {
        this.init(( id ), sGroup, config);
        //this.initTarget( id , sGroup, config );
        this.initFrame();
    }

    var el = this.getDragEl();
    Dom.setStyle(el, "opacity", 0.40);

    this.setPadding(-4);
    this.goingUp = false;
    this.lastY = 0;
};

YAHOO.extend( Preceptor.util.DragDropList , YAHOO.util.DDProxy, {

    startDrag: function(x, y) {

        var dragEl = this.getDragEl();
        var clickEl = this.getEl();
        
        Dom.setStyle( clickEl , "visibility", "hidden" );
		
     	Dom.setStyle( dragEl, "backgroundColor", "white" );
        Dom.setStyle( dragEl, "border", "1px solid black" );
        
        for( var i = 0; i < DDTargets.length; i++ )
		{
			el = DDTargets[i].getEl();
			Dom.setStyle( el , "visibility", "visible" );
		}
        
    },

    endDrag: function(e) {

        var srcEl = this.getEl();
        var proxy = this.getDragEl();
        var proxyid = proxy.id;
        var id = this.id;

		Dom.setStyle( proxyid , "visibility" , "" );
        
        // animate the proxy element to the src element's location
        var a = new YAHOO.util.Motion( 
            proxy, { 
                points: { 
                    to: Dom.getXY(srcEl)
                }
            }, 
            0.3, 
            YAHOO.util.Easing.easeOut 
        )
        
        a.onComplete.subscribe( function() {
            Dom.setStyle( proxyid , "visibility", "visible");
            Dom.setStyle( id , "visibility", "visible" );
        });
            
        a.animate();
        
        for( var i = 0; i < DDTargets.length; i++ )
		{
			el = DDTargets[i].getEl();
			Dom.setStyle( el , "visibility", "visible" );
		}
		
		for( var i = 0; i < conf.length; i++ )
		{
			if( conf[i].saved )
			{
				for( var i = 0; i < conf.length; i++ )
				{
					var divRefer = document.getElementById( conf[i].idRef );
					try{
						var tags = divRefer.getElementsByTagName( conf[i].tag );
					}catch(e){}
					
					var getValues = "";
					for( var j = 0; j < tags.length; j++ )
					{
						getValues += "id["+j+"]=" + tags[j].id + "&position["+j+"]=" + (j+1)+"&";
					}
					
					YAHOO.util.Connect.asyncRequest( "POST" , conf[i].host , {} , getValues );
					//new Preceptor.util.AjaxUpdate( '' , conf[i].host + getValues );
				}		
			}
		}
    },

//	save: function()
//	{

//		for( var i = 0; i < conf.length; i++ )
//		{
//			var divRefer = document.getElementById( conf[i].idRef );
//			var tags = divRefer.getElementsByTagName( conf[i].tag );
		
//			var getValues = "";
//			for( var j = 0; j < tags.length; j++ )
//			{
//				getValues += "id=" + tags[j].id + "&position=" + (j+1)+"&"; 	
//			}
//			YAHOO.util.Connect.asyncRequest( "POST" , conf[i].host , {} , getValues );
			//new Preceptor.util.AjaxUpdate( '' , conf[i].host + getValues , {} ,  );
//		}
//	},
	
    onDragDrop: function(e, id) {
    },

    onDrag: function(e, id) {

        // figure out which direction we are moving
        var y = Event.getPageY(e);

        if (y < this.lastY) {
            this.goingUp = true;
        } else if (y > this.lastY) {
            this.goingUp = false;
        }

        this.lastY = y;
        
    },

    onDragOver: function(e, id) {
    
        var srcEl = this.getEl();
        var destEl;

        if ("string" == typeof id) {
            // POINT mode
            destEl = Dom.get(id);
        } else { 
            // INTERSECT mode
            destEl= DDM.getBestMatch(id).getEl();
        }
        var p = destEl.parentNode;

        if (this.goingUp) {
            p.insertBefore(srcEl, destEl);
        } else {
            p.insertBefore(srcEl, destEl.nextSibling);
        }

        DDM.refreshCache();
    },

    onDragEnter: function(e, id) {
    },

    onDragOut: function(e, id) {
    },

    toString: function() {
        return "DDList " + this.id;
    }

});

})();