ie=document.all;
var nn6=document.getElementById&&!document.all;
var isdrag=false;
var x,y;
var dobj;
var objectlist = new Array();
var dragobject = null;
var currX, currY;
var dragging_allowed    = false;
var dragging_blocked    = false;
var possibledroptarget  = null;



/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function str_replace (search, replace, subject)
{
  var result = "";
  var  oldi = 0;
  for (i = subject.indexOf (search)
     ; i > -1
     ; i = subject.indexOf (search, i))
  {
    result += subject.substring (oldi, i);
    result += replace;
    i += search.length;
    oldi = i;
  }
  return result + subject.substring (oldi, subject.length);
} 


/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function getPossibleDroptarget(divname)
{
    dropOBJ                         = document.getElementById(divname);
    this.divname                    = divname;
    this.content                    = dropOBJ.innerHTML;
    this.backgroundcolor            = dropOBJ.style.backgroundColor;
    this.background                 = dropOBJ.style.background;
    this.border                     = dropOBJ.style.border;
    
    dropOBJ.style.backgroundColor   = "eeeeee";
    dropOBJ.innerHTML               = "&nbsp;";
    dropOBJ.style.border            = "1px dotted black";
    return this;
}



/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function setPossibleDroptarget(divname)
{
    dropOBJ                         = document.getElementById(divname);
    dropOBJ.innerHTML               = possibledroptarget.content;
    dropOBJ.style.background        = possibledroptarget.background;
    dropOBJ.style.border            = possibledroptarget.border;
    dropOBJ.style.backgroundColor   = possibledroptarget.backgroundcolor;
}




/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function getObjectToppos(obj)
{
    obj_top = obj.offsetTop;
    while (obj=obj.offsetParent) obj_top+=obj.offsetTop;
    return obj_top;
}


/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function getObjectLeftpos(obj)
{
    obj_left = obj.offsetLeft;
    while (obj=obj.offsetParent) obj_left+=obj.offsetLeft;
    return obj_left;
}


/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function divObject(divname, type, rootitem)
{
    divobj = document.getElementById(divname);

    this.name       = divname;
    this.type       = type;
    this.rootitem   = rootitem;
    this.posx       = getObjectLeftpos(divobj);
    this.posy       = getObjectToppos(divobj);
    this.width      = divobj.offsetWidth;// parseInt(str_replace ("px", "", divobj.style.width));
    this.height     = divobj.offsetHeight; //parseInt(str_replace ("px", "", divobj.style.height));
    //alert(divname+" => "+this.posx+"/"+this.posy+" bis : "+(this.width+this.posx)+"/"+(this.height+this.posy));
    return this;
}


/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function swapListnames(source,target)
{
    objectlist_length = objectlist.length;
    for (ic=0;ic<objectlist_length;ic++)
    {
        if (objectlist[ic].name == source)
        {
            objectlist[ic].name = target;
        }
        else
        {
            if (objectlist[ic].name == target)
            {
                objectlist[ic].name = source;
            }
        }
    }
    initContainer();
}


/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function getDivobjectByName(divname)
{
    objectlistlength = objectlist.length;
    for (io=0;io<objectlistlength;io++)
    {
        if (objectlist[io].name == divname)
        {
            return objectlist[io];
        }
    }
    return null;
}


/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function storeDragobject(divname)
{
    obj                         = document.getElementById(divname);
    objectdata                  = getDivobjectByName(divname);
    this.content                = obj.innerHTML;
    this.name                   = divname;
    this.type                   = objectdata.type;
    this.top                    = obj.style.top;
    this.left                   = obj.style.left;
    this.zindex                 = obj.style.zIndex;
    this.parentobject           = obj.parentNode.id;
    this.backgroundcolor        = obj.style.backgroundColor;
    this.background             = obj.style.background;
    this.border                 = obj.style.border;

    obj.innerHTML               = "&nbsp;";
    obj.style.backgroundColor   = "eeeeee";
    obj.style.opacity           = ".8";
    obj.style.filter            = "alpha(opacity=80)";

    return this;
}


/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function restoreDragobject(divname)
{
    obj                         = document.getElementById(divname);
    obj.innerHTML               = dragobject.content;
    obj.style.backgroundColor   = "transparent";////dragobject.backgroundcolor ? dragobject.backgroundcolor : obj.style.backgroundColor;
    obj.style.background        = dragobject.background ? dragobject.background : obj.style.background;
    obj.style.border            = dragobject.border;
    obj.style.opacity           = "1.0";
    obj.style.filter            = "alpha(opacity=100)";
    return this;
}



/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function cloneObject(divname)
{
    return document.getElementById(divname).cloneNode(true);
}


/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function releaseDragobject()
{
    if (dragobject != null)
    {    
        srcOBJ      = getDivobjectByName(dragobject.name)
        
        dropTarget  = getMouseOverContainer(currX, currY + detectYMovement(), srcOBJ.type);
        
        if (dropTarget != null)
        {
            source  = document.getElementById(dragobject.name);
            target  = document.getElementById(dropTarget);
            
            //store switching in PHP object
            xajax_module_userportlets__switchContainer(dragobject.name, target.id);
            
            setPossibleDroptarget(dragobject.name);
            restoreDragobject(dropTarget);
            
            //swap IDs and resort objectlist
            if (target.id != source.id)
            {
                swap_target     = target.id;
                swap_source     = source.id;
                source.id       = "swap_01";
                target.id       = "swap_02";
                source.id       = swap_target;
                target.id       = swap_source;
                swapListnames(target.id, source.id);
            }
        }
        else
        {
            restoreDragobject(dragobject.name);
        }
        
        //removing tmp-object        
        obj = document.getElementById("swapobject");
        if (obj)
        {
            obj.parentNode.removeChild(obj);
        }
        
        
        //reset opacity from source object
        if (source)
        {
            obj  = document.getElementById(source.id);
            if (obj)
            {
                obj.style.opacity    = "1";
                obj.style.filter     = "alpha(opacity=100)";
            }
        }

        dragobject          = null;
    }
}


/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function getMouseOverContainer(xpos,ypos,type)
{
    result = null;
    objectlistlength = objectlist.length;
    
    for (ix=0;ix<objectlistlength;ix++)
    {
        if (xpos >= objectlist[ix].posx && xpos <= (objectlist[ix].posx+objectlist[ix].width) && ypos >= objectlist[ix].posy && ypos <= (objectlist[ix].posy+objectlist[ix].height))
        {
            if (objectlist[ix].type == "big" && (type=="any" || type=="big"))
            {
                return objectlist[ix].name;
            }
            if (objectlist[ix].type == "small" && (type=="any" || type=="small"))
            {
                return objectlist[ix].name;
            }
        }
    }
    return null;
}


/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function detectYMovement()
{
    if (ie)
    {
        return document.body.scrollTop;
    }
    else
    {
        return window.pageYOffset
    }
}



/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function movemouse(e)
{

    if (e)
    {
        currX = e.clientX;
        currY = e.clientY;
    }
    else
    {
        currX = event.clientX;
        currY = event.clientY;
    }
    
  if (isdrag)
  {
  
        if (e)
        {
            dobj.style.left = (e.clientX-175)+"px";
            dobj.style.top  = (e.clientY - 15 + detectYMovement())+"px";
        }
        else
        {
            dobj.style.left = (event.clientX - 395 )+"px";
            dobj.style.top  = (event.clientY - 10 )+"px";
        }
    
    
        if (e)
        {
            overname = getMouseOverContainer(e.clientX, e.clientY + detectYMovement(), dragobject.type);
        }
        else
        {
            overname = getMouseOverContainer(event.clientX, event.clientY, dragobject.type);
        }
    
    
        if (overname)
        {
            if (possibledroptarget == null)
            {
                //mark target
                //dummy                       = document.getElementById(overname);
                possibledroptarget          = new getPossibleDroptarget(overname);
                
                //mark source
                if (dragobject.name != overname)
                {
                    setPossibleDroptarget(dragobject.name);
                }
            }
            else
            {
                if (possibledroptarget.divname != overname && possibledroptarget.content!="")
                {
                    setPossibleDroptarget(possibledroptarget.divname);
                    possibledroptarget  = null;
                    
                    //mark source
                    dummy           = document.getElementById(dragobject.name);
                    dummy.innerHTML = "&nbsp;";
                }
            }
                
        }
        else
        {
            if (possibledroptarget!=null && possibledroptarget.divname != "" && possibledroptarget.content!="")
            {
                setPossibleDroptarget(possibledroptarget.divname);
                possibledroptarget = null;
                
                //mark source
                dummy                       = document.getElementById(dragobject.name);
                dummy.innerHTML             = "&nbsp;";
                dummy.style.backgroundColor = "eeeeee";
            }
            else
            {
                // ?
            }
        }
        
        return false;
  }
}


/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function doDebug(debug)
{
    obj = document.getElementById("DEBUG");
    obj.innerHTML = debug;
}






/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function selectmouse(e)
{                      
  var fobj       = nn6 ? e.target : event.srcElement;

  if (dragging_allowed && !dragging_blocked)
  {
        
  var topelement = nn6 ? "HTML" : "BODY";
  while (fobj.tagName != topelement && fobj.className.indexOf("dragme")==-1)
  {
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
  }                                                             


  if (fobj.className.indexOf("dragme")>=0 && dragging_allowed && !dragging_blocked)
  {
  
    isdrag                  = true;
    sourceOBJ               = document.getElementById(fobj.id);
    dobj                    = cloneObject(fobj.id);
    dobj.id                 = "swapobject";
    dobj.style.position     = "absolute";
    dobj.style.zIndex       = 500;
    
    sourceData = getDivobjectByName(fobj.id); 
    if (e)
    {
        dobj.style.top          = (e.clientY+(sourceData.posy-e.clientY))+"px";
        dobj.style.left         = (e.clientX+(sourceData.posx-e.clientX))+"px";
    }
    else
    {
        dobj.style.top          = (event.clientY-10)+"px";
        dobj.style.left         = (event.clientX-395)+"px";
    }
    
    dobj.style.opacity       = ".4";
    dobj.style.filter        = "alpha(opacity=40)";
    
    //store source-object
    if (dragobject == null)
    {
        dragobject = new storeDragobject(fobj.id);
    }
    
    //create movable clone object    
    document.getElementById(sourceData.rootitem).appendChild(dobj);
    
    
    tx = parseInt(dobj.style.left+0,10);
    ty = parseInt(dobj.style.top+0,10);
    if (e)
    {
        x = e.clientX;
        y = e.clientY;
    }
    else
    {
        x = event.clientX;
        y = event.clientY;
    }
    
    
    source = getMouseOverContainer(x,y, "any");
    
    document.onmousemove=movemouse;
    return false;
  }
  else
  {
    //
  }  
  }
}


/*****************************************************************************************************
    METHOD DESCRIPTION
*****************************************************************************************************/
function stopDrag()
{
    isdrag=false;
    dragging_allowed=false;
    releaseDragobject();
}

document.onmouseup = stopDrag;
document.onmousedown=selectmouse;
