// TPI Sorter Impelemntation.
//
// This YUI component allows us to define a listing of items that are sortable, and 
// exist within a parent.  The init function of the DDApp object sets the ID for the 
// parent, the class of the children, the serialize helper function, and the type of 
// element to be sorted.
YAHOO.namespace('YAHOO.TPI.sorter');

(function() {

var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var DDM = YAHOO.util.DragDropMgr;

YAHOO.TPI.sorter.DDApp = {
	sort_group: '',
	item_class: '',
	serialize_function: '',
	element_type: 'tr',
	
    init: function(id, class_name, func_serialize, element) {
		
		this.sort_group = id;
		this.item_class = class_name;
		this.serialize_function = func_serialize;
		
		if(element != '') {
			this.element_type = element;
		}
		
		new YAHOO.util.DDTarget(this.sort_group);
		
		$('#'+this.sort_group).children('.'.item_class).each(function(i) {
			if(this.id != '' && this.className != 'no_dd') {
				
				var list = new YAHOO.TPI.sorter.DDList(this.id, id, {sort_group: id, item_class: class_name, serialize_function: func_serialize, element_type: element});
			}
		});
    }
};

//////////////////////////////////////////////////////////////////////////////
// custom drag and drop implementation
//////////////////////////////////////////////////////////////////////////////

YAHOO.TPI.sorter.DDList = function(id, sGroup, config) {
	
    YAHOO.TPI.sorter.DDList.superclass.constructor.call(this, id, sGroup, config);
	
    var el = this.getDragEl();
    Dom.setStyle(el, "opacity", 0.67); // The proxy is slightly transparent

    this.goingUp = false;
    this.lastY = 0;
};

YAHOO.extend(YAHOO.TPI.sorter.DDList, YAHOO.util.DDProxy, {
	
    startDrag: function(x, y) {
        // make the proxy look like the source element
        var dragEl = this.getDragEl();
        var clickEl = this.getEl();
		
		if(clickEl.className == 'split_row') {
			
			 Dom.setStyle(dragEl, "visibility", "hidden");
			return;
		}
		
        Dom.setStyle(clickEl, "visibility", "hidden");
		dragEl.innerHTML = clickEl.innerHTML;
		/*$(dragEl).children('tr').each(function(i) {
			$(this).children('td').each(function(j) {
				this.width = '25%';
			 });
		 });*/
		
		Dom.setStyle(dragEl, "position", "absolute");
        Dom.setStyle(dragEl, "width", Dom.getStyle(clickEl, "width"));
		Dom.setStyle(dragEl, "color", Dom.getStyle(clickEl, "color"));
        Dom.setStyle(dragEl, "backgroundColor", Dom.getStyle(clickEl, "backgroundColor"));
        Dom.setStyle(dragEl, "border", "2px solid gray");
    },

    endDrag: function(e) {

        var srcEl = this.getEl();
        var proxy = this.getDragEl();
		
		if(this.config.serialize_function != null) {
			
			var serial = '';
			$('#'+this.config.sort_group).children().each(function(i) {
				if(this.id != '' && this.id != null) {
					serial += this.parentNode.id+'[]='+this.id+'&';
				}
			});
			
			var result = this.config.serialize_function(serial, this.config.sort_group);
		}
		
		// Show the proxy element and animate it to the src element's location
		Dom.setStyle(proxy, "visibility", "");
		var a = new YAHOO.util.Motion( 
			proxy, { 
				points: { 
					to: Dom.getXY(srcEl)
				}
			}, 
			0.2, 
			YAHOO.util.Easing.easeOut 
		)
		var proxyid = proxy.id;
		var thisid = this.id;

		// Hide the proxy and show the source element when finished with the animation
		a.onComplete.subscribe(function() {
				Dom.setStyle(proxyid, "visibility", "hidden");
				Dom.setStyle(thisid, "visibility", "");
			});
		a.animate();
		
		if($('#'+thisid).prevAll('.split_row').attr('id')) {
			$('#'+thisid).children('td:first').children('input:first').attr('checked', false);
		} else {
			$('#'+thisid).children('td:first').children('input:first').attr('checked', 'checked');
		}
    },

    onDragDrop: function(e, id) {
		 
		var srcEl = this.getEl();
		var splitter = false;
		
		if(srcEl.className == 'split_row') {
			
			splitter = true;
		}
		
        // If there is one drop interaction, the li was dropped either on the list,
        // or it was dropped on the current location of the source element.
        if (DDM.interactionInfo.drop.length === 1 && splitter != true) {

            // The position of the cursor at the time of the drop (YAHOO.util.Point)
            var pt = DDM.interactionInfo.point; 

            // The region occupied by the source element at the time of the drop
            var region = DDM.interactionInfo.sourceRegion; 

            // Check to see if we are over the source element's location.  We will
            // append to the bottom of the list once we are sure it was a drop in
            // the negative space (the area of the list without any list items)
            if (!region.intersect(pt)) {
                var destEl = Dom.get(id);
                var destDD = DDM.getDDById(id);
                destEl.appendChild(this.getEl());
                destDD.isEmpty = false;
                DDM.refreshCache();
            }

        }
    },

    onDrag: function(e) {

        // Keep track of the direction of the drag for use during onDragOver
        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 = Dom.get(id);
		
		if(srcEl.className == 'split_row') {
			
			return;
		}
		
        // We are only concerned with list items, we ignore the dragover
        // notifications for the list.
        if (destEl.nodeName.toLowerCase() == this.config.element_type) {
            var orig_p = srcEl.parentNode;
            var p = destEl.parentNode;

            if (this.goingUp) {
                p.insertBefore(srcEl, destEl); // insert above
            } else {
                p.insertBefore(srcEl, destEl.nextSibling); // insert below
            }

            DDM.refreshCache();
        }
    }
});

})(); 

function moveCheckbox(checkbox, section, id) {
	
	if(checkbox.checked == true) {
			
		$('#resort_'+section+'_'+id).insertAfter('#'+section+'_top_row');
	} else {
		
		$('#resort_'+section+'_'+id).insertAfter('#'+section+'_split_row');
	}
}
