var moveMyObject =
{
    obj:null,
	
	init:function(obj)
    {
		obj.oldOnMouseDown = obj.onmousedown;
		obj.onmousedown = moveMyObject.start;
		
        if(isNaN(parseInt(obj.style.left,10))) obj.style.left="0px";
        if(isNaN(parseInt(obj.style.top,10))) obj.style.top="0px";


        obj.onDragStart=new Function();
        obj.onDragEnd=new Function();
        obj.onDrag=new Function()

    }

    ,start:function(evt)
    {
        var obj=moveMyObject.obj=this;
        if (obj.clonat)
        {
			obj.createObjectClone()
			obj = moveMyObject.obj = obj.objClona;
			if (obj.objClonat.objDeMutat == null)
			{
				obj.style.left = offsetLeft(obj.objClonat)+'px';
				obj.style.top = offsetTop(obj.objClonat)+'px';
			}
			else
			{
				obj.style.left = offsetLeft(obj.objClonat.objDeMutat)+'px';
				obj.style.top = offsetTop(obj.objClonat.objDeMutat)+'px';
			}
		}
		else
		{
			obj = moveMyObject.obj = obj.objDeMutat;
		}

        evt=moveMyObject.fixE(evt);

        if(evt.button==2||evt.button==3)
        {
            return true;
        }

        obj.lastMouseX = evt.clientX;
        obj.lastMouseY = evt.clientY;
        document.onmousemove = moveMyObject.drag;
        document.onmouseup = moveMyObject.end;
        obj.isDragged = false;
        return false;
    }
    
    ,drag:function(evt)
    {
        evt = moveMyObject.fixE(evt);
        var obj = moveMyObject.obj;
        var newLeft, newTop, top, left, mouseY, mouseX;

        mouseY = evt.clientY;
        mouseX = evt.clientX;
        top = parseInt(obj.style.top,10);
        left = parseInt(obj.style.left,10);
        newLeft = left + mouseX - obj.lastMouseX;
        newTop = top + mouseY - obj.lastMouseY;
        obj.style.left = newLeft + "px";
        obj.style.top = newTop + "px";

        obj.lastMouseX = mouseX;
        obj.lastMouseY = mouseY;
        if (!obj.isDragged)
        {
			if (obj.clona)  document.body.appendChild(obj);
			var iTop=parseInt(obj.style.top,10);
        	var iLeft=parseInt(obj.style.left,10);
			obj.onDragStart(iLeft, iTop, evt.clientX, evt.clientY);
        	obj.isDragged = true;
		}
        obj.onDrag(newLeft, newTop, evt.clientX, evt.clientY);

        return false
    }

    ,end:function()
    {
        document.onmousemove=null;
        document.onmouseup=null;
        obj = moveMyObject.obj;

        if (obj.isDragged)
			obj.onDragEnd(parseInt(obj.style.left,10),parseInt(obj.style.top,10));
        if ((obj.clona)&&(obj.isDragged))
        {
			if (obj.cloneEndEfect)
				obj.cloneEndEfect();
			else
	        	//obj.objClonat.parentNode.removeChild(obj);
	        	document.body.removeChild(obj);
	    }
	    else
            obj.isDragged = false;
        moveMyObject.obj=null
    }

    ,fixE:function(evt)
    {
        if(typeof evt=="undefined") evt=window.event;
        if(typeof evt.layerX=="undefined") evt.layerX=evt.offsetX;
        if(typeof evt.layerY=="undefined") evt.layerY=evt.offsetY;
        return evt;
    }
}

function setMoveFunctions(wObj)
{
	if (wObj == null) return;
	wObj.onDragStart = function(iLeft,iTop)
	{
		if (this.clonat)
		{
			this.style.left = iLeft;
			this.style.top = iTop;
		}
		this.oldOpacity = (typeof this.style.opacity == "undefined")? 100: this.style.opacity;
		this.style.opacity = 0.7;
		this.style.filter="alpha(opacity=70)";
		this.dragged=false
	};

	wObj.onDrag=function(iLeft,iTop)
	{
		this.dragged=true
	}
	;
	wObj.onDragEnd=function(iLeft,iTop)
	{
		if (!this.clona)
		{
			this.style.opacity = this.oldOpacity;
			this.style.filter="alpha(opacity="+this.oldOpacity*100+")";
		}
		if(this.dragged)
		{
		}
		else
		{
		}
		this.dragged = false;
	}
}

function testApartine(obj, objTata)
{
	var r = false;
	if (objTata == null) return true;
    while ((!r) && (obj != null))
    {
        if (obj == objTata) r = true;
        obj = obj.parentNode;
    }
    return r;
}

function changeId(objTata)
{
	var i;
	if (objTata.id.length != 0) objTata.id = objTata.id+'_clonat';
	for (i=0;i<objTata.childNodes.length;i++)
	{
		obj = objTata.childNodes[i];
		if (typeof obj.id != "undefined")
			changeId(obj);
	}
}

var fadeTimer;
function fadeEfect()
{
	var opa = this.style.opacity*100;
	var objFading = this;
	fadeTimer = setInterval(
	function()
    {
        if(opa < 1)
        {
            clearInterval(fadeTimer);
            document.body.removeChild(objFading);
            return
        }
        opa-=3;
        objFading.style.opacity = opa/100;
        objFading.style.filter="alpha(opacity="+opa+")";
    },5);
}

var slideTimer
function slideEfect()
{
   var iLeftF, iTopF;
   var speed = 15;
   var objSliding = this;
   if (typeof this.objClonat.objDeMutat == "undefined")
   {
		iLeftF = parseInt(offsetLeft(obj.objClonat.objDeMutat),10);
		iTopF = parseInt(offsetTop(obj.objClonat.objDeMutat),10);
   }
   else
   {
		iLeftF = parseInt(offsetLeft(obj.objClonat),10);
		iTopF = parseInt(offsetTop(obj.objClonat),10);
   }
/*   iLeftF = 0;
   iTopF = 0;*/
   var actulaLeft = parseInt(this.style.left,10);
   var actualTop =  parseInt(this.style.top,10);
   iLeftDif = (actulaLeft - iLeftF)/speed;
   iTopDif = (actualTop - iTopF)/speed;
   slideTimer =  setInterval(
	function()
    {
        if(speed<1)
        {
            clearInterval(slideTimer);
            document.body.removeChild(objSliding);
            return;
        }
        speed--;
        actulaLeft-=iLeftDif;
        actualTop-=iTopDif;
        objSliding.style.left=parseInt(actulaLeft,10)+"px";
        objSliding.style.top=parseInt(actualTop,10)+"px"
    },10)
}

function addDDObject(obj, bClonabil, objDeMutat)
{
    obj.style.cursor = "move";
    obj.setClonabil = function(bClonabil)
	{
		this.clonat = bClonabil;
	}
	obj.setObjDeMutat = function(objDeMutat)
	{
		if (!testApartine(this, objDeMutat))
		{
			alert('atentie obiectul de mutat nu este tata !');
			return;
		}

		this.objDeMutat = objDeMutat;
		if (objDeMutat != null)
			objDeMutat.clona = false;

	}
	obj.setCloneEndEfect = function(cloneEndEfect)
	{
		if (this.objClona)
			this.objClona.cloneEndEfect = cloneEndEfect;
	}
	obj.setCloneClassName = function(className)
	{
		obj.clonaClassName = className;
	}

	obj.createObjectClone = function()
	{
		if (this.objDeMutat == null)
		{
        	wObj = this.cloneNode(true);

	        wObj.style.height = this.clientHeight+'px';
	        wObj.style.width = this.clientWidth+'px';
		}
		else
		{
			wObj = objDeMutat.cloneNode(true);
	        wObj.style.height = this.clientHeight+'px';
	        wObj.style.width = this.clientWidth+'px';
		}
		changeId(wObj);
		wObj.clona = true;
		wObj.objClonat = this;
		this.objClona = wObj;
		setMoveFunctions(wObj);
        if(isNaN(parseInt(wObj.style.left,10))) this.style.left="0px";
        if(isNaN(parseInt(wObj.style.top,10))) this.style.top="0px";
        this.setCloneEndEfect(fadeEfect);
        wObj.style.position='absolute';
        wObj.className = this.clonaClassName;
	}

    obj.clona = false;

	obj.setClonabil(bClonabil);
	obj.setObjDeMutat(objDeMutat);

    moveMyObject.init(obj);

    setMoveFunctions(obj);
    setMoveFunctions(objDeMutat);
}
