
red.prototype.dragsort = {
   
	dragobj : null,
	
	targetobj : null,
	
	targetobj_rect : null,
	
	container : null,
	
	container_rect : null,
	
	allecontainer : new Array(),
	
    init : function(container,anderecontainer_) {
        if(!red.prototype.is_object(container)){
			var container = red.prototype.dom.byid(container);
		}
		
		var anderecontainer = new Array();
		if(red.prototype.is_array(anderecontainer_)){
			for(var i=0;i<anderecontainer_.length;i++){
				if(!red.prototype.is_object(anderecontainer_[i])){
					anderecontainer[i] = red.prototype.dom.byid(anderecontainer_[i]);
				}
				else{
					anderecontainer[i] = anderecontainer_[i];
				}
			}
		}
		
		var elemente = red.prototype.dom.childs(container);
		
		red.prototype.dragsort.container		= container;
		red.prototype.dragsort.container_rect 	= red.prototype.rect.get(container);
		
		red.prototype.event.add(container,'mousedown',function(){return false;});
		red.prototype.event.add(container,'mouseover',red.prototype.dragsort.setcontainer);
		red.prototype.event.add(container,'mouseout',red.prototype.dragsort.delcontainer);
		
		
		container.inputfelder = new Array();
		
		for(var i=0;i<elemente.length;i++){
		
			var size = red.prototype.css.getsizexy(elemente[i]);			
			
			elemente[i].style.border = '1px solid black';
			red.prototype.event.add(elemente[i],'mousedown',red.prototype.dragsort.start);
			red.prototype.event.add(elemente[i],'mouseover',red.prototype.dragsort.settarget);
			red.prototype.event.add(elemente[i],'mouseout',red.prototype.dragsort.deltarget);
			red.prototype.css.set(elemente[i],";cursor:move;",false);
			
			elemente[i].container 		= container;
			elemente[i].anderecontainer = anderecontainer;
			
			var id = elemente[i].id.replace(/^dragsort_/g,'');
			red.prototype.dom.byid('divs').innerHTML += id+' - '+elemente[i].innerHTML+'<br>';
		}
		
		red.prototype.dragsort.allecontainer[red.prototype.dragsort.allecontainer.length] = container;
		
		var container_input = red.prototype.dom.element('div');
    },
	
	start : function(e){
		e = red.prototype.dragsort.fixE(e);
		if ((e.button && e.button != 1) || (e.which && e.which != 1)){
			red.prototype.dragsort.end();
			return;
		}
		
		red.prototype.css.set(document.body,";cursor:move;",false);
		
		red.prototype.event.add(document,'mousemove',red.prototype.dragsort.Drag);
		red.prototype.event.add(document,'mouseup',red.prototype.dragsort.end);
		
		red.prototype.dragsort.dragobj = this;
		
		this.style.display = 'none';
		
		red.prototype.dom.byid('drag').innerHTML = this.innerHTML+" START";
	
	},

	Drag: function(e){
		e = red.prototype.dragsort.fixE(e);
		if ((e.button && e.button != 1) || (e.which && e.which != 1)){
			red.prototype.dragsort.end();
			return;
		}

		/* mouseup außerhalb von window */
		if ((red.prototype.dragsort.dragobj != null) && !((e.button & 1)||(e.which & 1))){
			red.prototype.dragsort.end();
			return;
		}		
		
		var xy = red.prototype.mouse.getxy(e);
		red.prototype.dom.byid('position').innerHTML = "x: "+xy[0]+', y: '+xy[1];
		red.prototype.dom.byid('drag').innerHTML = red.prototype.dragsort.dragobj.innerHTML+" DRAG";
		
		if (document.selection){document.selection.empty();} 
		else if(window.getSelection){window.getSelection().removeAllRanges();}		
		
		if(red.prototype.dragsort.dragobj!=null){
		
			if(!red.prototype.dragsort.is_drag_valid()){
				return;
			}
		
			if(red.prototype.dragsort.targetobj!=null){
				var target_rect = red.prototype.dragsort.targetobj_rect;
				if(xy[1] < target_rect.top+(target_rect.height/2)){
					red.prototype.dragsort.targetobj.style.borderTop = '1px solid red';
					red.prototype.dragsort.targetobj.style.borderBottom = '1px solid black';
				}
				else{
					red.prototype.dragsort.targetobj.style.borderBottom = '1px solid red';
					red.prototype.dragsort.targetobj.style.borderTop = '1px solid black';
				}
			}
			else if(red.prototype.dragsort.container!=null){
				if(red.prototype.dragsort.container.lastChild!=null&&red.prototype.dragsort.container.lastChild.style){
					red.prototype.dragsort.container.lastChild.style.borderBottom = '1px solid red';
				}
			}
		}
	},
	
	end: function(e){
		//e = red.prototype.dragsort.fixE(e);

		red.prototype.css.set(document.body,";cursor:default;",false);		
		
		var xy = red.prototype.mouse.getxy(e);
		
		red.prototype.event.remove(document,'mousemove',red.prototype.dragsort.Drag);
		red.prototype.event.remove(document,'mouseup',red.prototype.dragsort.end);

		if(red.prototype.dragsort.dragobj!=null){
			red.prototype.dragsort.dragobj.style.display = '';
			red.prototype.dragsort.dragobj.style.borderBottom = '1px solid black';
			red.prototype.dragsort.dragobj.style.borderTop = '1px solid black';
			
			red.prototype.dom.byid('drag').innerHTML = red.prototype.dragsort.dragobj.innerHTML+" ENDE";
		
			if(!red.prototype.dragsort.is_drag_valid()){
				return;
			}
		
			if(red.prototype.dragsort.targetobj!=null){
				var target_rect = red.prototype.dragsort.targetobj_rect;
				if(xy[1] < target_rect.top+(target_rect.height/2)){
					red.prototype.dom.insertBefore(red.prototype.dragsort.dragobj,red.prototype.dragsort.targetobj);
				}
				else{
					red.prototype.dom.insertAfter(red.prototype.dragsort.dragobj,red.prototype.dragsort.targetobj);
				}
			}
			else if(red.prototype.dragsort.container!=null){
				if(red.prototype.dragsort.container.lastChild!=null&&red.prototype.dragsort.container.lastChild.style){
					red.prototype.dragsort.container.lastChild.style.borderBottom = '1px solid black';
				}				
				red.prototype.dom.insert(red.prototype.dragsort.dragobj,red.prototype.dragsort.container);
			}
			red.prototype.dragsort.dragobj = null;
		}
		
	},
	
	is_drag_valid : function(){
		if(red.prototype.dragsort.container!=null && red.prototype.dragsort.dragobj.container==red.prototype.dragsort.container){
			return true;
		}
		else if(red.prototype.dragsort.container!=null){
			for(var i=0;i<red.prototype.dragsort.dragobj.anderecontainer.length;i++){
				if(red.prototype.dragsort.dragobj.anderecontainer[i]==red.prototype.dragsort.container){
					return true;
				}
			}
		}
		return false;
	},
	
	settarget : function(e){
		if(red.prototype.dragsort.dragobj==this){return;}
		red.prototype.dragsort.targetobj = this;
		red.prototype.dragsort.targetobj_rect = red.prototype.rect.get(this);
		red.prototype.dom.byid('drop').innerHTML = this.innerHTML+' SET';
	},
	
	deltarget : function(e){
		if(red.prototype.dragsort.dragobj==this){return;}
		red.prototype.dragsort.targetobj.style.border = '1px solid black';
		red.prototype.dragsort.targetobj = null;
		red.prototype.dom.byid('drop').innerHTML = this.innerHTML+' DEL';
	},
	
	setcontainer : function(e){
		red.prototype.dragsort.container = this;
		red.prototype.dom.byid('container').innerHTML = 'Container SET';		
	},
	
	delcontainer : function(e){
		if(red.prototype.dragsort.container.lastChild&&red.prototype.dragsort.container.lastChild.style){
			if(red.prototype.dragsort.container!=null){
				red.prototype.dragsort.container.lastChild.style.borderBottom = '1px solid black';
			}
		}
		red.prototype.dragsort.container = null;
		red.prototype.dom.byid('container').innerHTML = 'Container DEL';
	},
	
	fixE: function(e) {
        if (typeof e == 'undefined') e = window.event;
        if (typeof e.elemX == 'undefined') e.elemX = e.offsetX;
        if (typeof e.elemY == 'undefined') e.elemY = e.offsetY;
        return e;
    }
	
};