//declaring the class
var ww_move = Class.create();
//defining the rest of the class implmentation

function droplist(el,func)
{
	var obj=$(el);
	this.el=el;
	this.onDrop=func;
	this.x=0;
	this.y=0;
	this.w=0;
	this.h=0;
}

ww_move.prototype = {
	mv_self: '',
	mv_active_obj: '',
	mv_clone: '',
	mv_gapx: 0,
	mv_gapy: 0,
	mv_x: 0,
	mv_y: 0,
	mv_px: 0,
	mv_py: 0,
	mv_evmoli: '',
	mv_evmuli: '',
	mv_droplist: new Array(),

	mv_options: '',
	mv_drop_first_match: true,
	mv_release: '',
	mv_debug_div: '',
	mv_opacity: '0.75',
	mv_del_after_release: true,
	mv_last_obj: '',
	mv_ondrag: '',
	mv_onselectstart: '',

	initialize: function(options)
	{
		mv_options=options;
		if(options.mv_release) this.mv_release=options.mv_release;
		if(options.mv_del_after_release) this.mv_del_after_release=options.mv_del_after_release;
		if(options.mv_debug_div) this.mv_debug_div=options.mv_debug_div;
		if(options.mv_opacity) this.mv_opacity=options.mv_opacity;
		if(options.mv_drop_first_match) this.mv_drop_first_match=options.mv_drop_first_match;
		this.mv_self=this;
		var obj=this;
		addunLoadEvent(function(){ obj.unload();})
	},

	unload: function()
	{
		var obj=this;

		for ( var i in this )
		{
			delete this[ i ];
		}
	},

	setmove: function(el,el1)
	{
		if(el1)
		{
			setMouseDown(el1,this.clicked);
			var iobj=this;
			var obj=$(el1);
			obj.mover=$(el);
			obj.move_obj=this;
			obj.style.cursor='move';
			obj.ondrag=function(){return false;};
			obj.onselectstart=function(){return false;};
		}
		else
		{
			setMouseDown(el,this.clicked);
			var iobj=this;
			var obj=$(el);
			obj.mover=obj;
			obj.move_obj=this;
			obj.style.cursor='move';
			obj.ondrag=function(){return false;};
			obj.onselectstart=function(){return false;};
		}
		if(this.mv_debug_div) $(this.mv_debug_div).innerHTML="Add Dragelement: "+this.id;
	},

	rmmove: function(el)
	{
		var obj=$(el);
		removeEvent(obj,'mousedown',this.clicked);
		obj.style.cursor='default';
	},

	updatedrop: function()
	{
		var dl;
		for (i=0;i<this.mv_droplist.length;i++)
		{
			dl=this.mv_droplist[i];
			var obj=$(dl.el);
			dl.x=findPosX(obj);
			dl.y=findPosY(obj);
			dl.w=obj.offsetWidth;
			dl.h=obj.offsetHeight;
		}
	},

	setdrop: function(el,func)
	{
		this.mv_droplist.push(new droplist(el,func));
		if(this.mv_debug_div) $(this.mv_debug_div).innerHTML="Add Dropelement: "+$(el).id;
	},

	clicked: function(ev)
	{
		var _clone;
		var mvo=this.move_obj;
		var imov=this.mover;

		mvo.mv_ondrag=document.body.ondrag;
		mvo.mv_onselectstart=document.body.onselectstart;
		document.body.ondrag=function () { return false; };;
		document.body.onselectstart=function () { return false; };;
		mvo.pagepos();
		mvo.getmousepos(ev);
		mvo.add_listener();

		if (ev && ev.preventDefault)
		{
			ev.preventDefault(); 
		} 
		else if (window.event)
		{
			window.event.returnValue = false;
		}
		mvo.mv_gapx=mvo.mv_x-findPosX(imov);
		mvo.mv_gapy=mvo.mv_y-findPosY(imov);
		if(mvo.mv_debug_div) $(mvo.mv_debug_div).innerHTML="down "+imov.id+' '+mvo.mv_gapx+" "+mvo.mv_x+' '+mvo.mv_gapy+" "+mvo.mv_y;

		_clone=imov.cloneNode(true);
		_clone.style.zIndex = '10000';
		_clone.style.position = 'absolute';
		if(mvo.mv_opacity)
		{
			ww_set_opacity(_clone,mvo.mv_opacity);
		}
		setMouseUp(_clone,mvo.released);
		mvo.mv_active_obj=_clone;
		mvo.posdiv();
		mvo.mv_last_obj=imov;
		document.getElementsByTagName("body").item(0).appendChild(_clone);
		return false;
	},

	released: function(ev)
	{
		if(this.mv_active_obj)
		{
			if(arguments.callee) this.mv_evmuli=arguments.callee;
			this.remove_listener();

			this.updatedrop();

			// remove node from node-list
			if(this.mv_del_after_release)
			{
				this.mv_active_obj.parentNode.removeChild(this.mv_active_obj);
			}

			var dl;
			for (i=0;i<this.mv_droplist.length;i++)
			{
				dl=this.mv_droplist[i];
				if(dl.x<=this.mv_x && dl.x+dl.w>=this.mv_x && dl.y<=this.mv_y && dl.y+dl.h>=this.mv_y)
				{
					// drop-entry match
					if(this.mv_debug_div) $(this.mv_debug_div).innerHTML="Dropelement: "+this.mv_active_obj.id+ " to: "+dl.el;
					if(dl.onDrop)
					{
						var dropid=this.mv_active_obj.id;
						setTimeout(function(){dl.onDrop(dl.el,dropid)}, 10);
					}
					if(this.mv_drop_first_match)
					{
						break;
					}
				}
			}
			delete this.mv_active_obj;
			if(this.mv_release)
			{
				var obj=this;
				obj.mv_release();
			}
			document.body.ondrag=this.mv_ondrag;
			document.body.onselectstart=this.mv_onselectstart;
			this.mv_active_obj=null;
			this.mv_last_obj=null;
		}
	},

	posdiv: function(el)
	{
		this.mv_active_obj.style.left=this.mv_x-this.mv_gapx+'px';
		this.mv_active_obj.style.top=this.mv_y-this.mv_gapy+'px';
		if(this.mv_debug_div) $(this.mv_debug_div).innerHTML="pos "+this.mv_gapx+" "+this.mv_x+' '+this.mv_gapy+" "+this.mv_y;
	},
	
	pagepos: function()
	{
		if(window.pageXOffset)
		{
			this.mv_px=window.pageXOffset;
			this.mv_py=window.pageYOffset;
		}
		else if (document.documentElement)
		{
			this.mv_px=(document.body.scrollLeft + document.documentElement.scrollLeft);
			this.mv_py=(document.body.scrollTop + document.documentElement.scrollTop);
		}
		else if (document.body.scrollLeft >= 0)
		{
			this.mv_px=document.body.scrollLeft;
			this.mv_py=document.body.scrollTop;
		}
	},

	add_listener: function()
	{
		var obj=this;
		addEvent(document,'mousemove',function(ev) { obj.mousetracker(ev)});
		addEvent(document,'mouseup',function(ev) { obj.released(ev)});
	},

	remove_listener: function()
	{
		var obj=this;
		removeEvent(document,'mousemove',this.mv_evmoli);
		removeEvent(document,'mouseup',this.mv_evmuli);
	},

	getmousepos: function(ev)
	{
		if (!ev)
		{
			ev = window.event;
		}
		this.mv_x = ev.clientX+this.mv_px;
		this.mv_y = ev.clientY+this.mv_py;
	},

	mousetracker: function(ev)
	{
		this.getmousepos(ev);
		if(this.mv_active_obj)
		{
			if(arguments.callee) this.mv_evmoli=arguments.callee;
			this.posdiv();
			return false;
		}
	}
};


