var False=false
var True=true

var MenuRegister = new Object()
MenuRegister.Groups=new Array()
MenuRegister.isEditing=false

MenuRegister.draggingOBJ=null

MenuRegister.editMenu=new Object()
MenuRegister.editMenu.visible=false
MenuRegister.editMenu.HTML=document.createElement("DIV");
MenuRegister.editMenu.HTML.style.position='absolute';
MenuRegister.editMenu.HTML.style.display='none';
MenuRegister.editMenu.HTML.style.width='200px';

MenuRegister.editMenu.Item=null
MenuRegister.editMenu.TargetItem=null
MenuRegister.editMenu._p=null


function IM_MenuItem(ID,PID,Ord,Code,IsSystem,Image1URL,Image2URL,Topic,Link,LinkTarget,CountOfItems,Selected)
{
    this.ID=ID
    this.PID=PID
    this.Ord=Ord
    this.Code=Code
    this.IsSystem=IsSystem
    this.Image1URL=Image1URL
    this.Image2URL=Image2URL
    this.Topic=Topic
    this.TopicNoHTML=RemoveHTML(Topic)
    this.Link=Link
    this.LinkTarget=LinkTarget
    this.CountOfItems=CountOfItems
    this.Selected=Selected
    this.ChildSelected=false
    this.isRoot=(PID>0)
    
    function RemoveHTML(x)
    {
        var r=x.replace(/<[^>]*>/ig,'')
        return r
    }
}


function IM_JSMenuGroupOBJ(PID,StylePrefix,Type,Direction,Effect)
{
    this._str='Group'
    this.Type=Type
    this.Effect=Effect
    this._parent=null
    this.ID=PID
    this.Items=new Array()
    this.StylePrefix=StylePrefix
    this.Direction=Direction

    
    this.width=0
    this.height=0
    this.x=0
    this.y=0

    this.c_width=0
    this.c_height=0

    this.max_width=0
    this.max_cheight=0
    this.overflow='visible'
    
    this.c_x=0
    this.c_y=0
    this.delta=10
    
    this.Selected=false
    this.ChildSelected=false


    this.over=false
    this.status=0
    this.action=0
    this.isRoot=false
    
    this.HTMLObj=document.createElement("DIV");


    this.HTMLObj.className=StylePrefix + 'Group'
    this.HTMLObj.style.display='none'    

    var _this=this;
    
    this.HTMLObj.onmouseover=on
    this.HTMLObj.onmouseout=off
    
    var RegNum=MenuRegister.Groups.length
    this.RegNum=RegNum
    this.TimerHandler=null
    MenuRegister.Groups[RegNum]=this


    function on(){
        if (MenuRegister.isEditing==false) _this.on()
    }
    function off(){
        if (MenuRegister.isEditing==false) _this.off()
    }

}

IM_JSMenuGroupOBJ.prototype.on = function()
{
    this.over=true
    if (this.HTMLObj==null) return
    if (this.isRoot!=true){
        this.HTMLObj.className=this.StylePrefix + 'GroupOn'
        if (MenuRegister.isEditing==false) this.show(true)
    } else {
        this.HTMLObj.className= this.StylePrefix + 'MainGroupOn'
    }
}

IM_JSMenuGroupOBJ.prototype.off = function()
{
    this.over=false
    if (this.HTMLObj==null) return
    if (this.isRoot!=true){
        this.HTMLObj.className= this.StylePrefix + 'Group'
    } else {
        this.HTMLObj.className= this.StylePrefix + 'MainGroup'
    }
}

IM_JSMenuGroupOBJ.prototype.show = function(q)
{
    if (this.status>=100) return 

    if (MenuRegister.isEditing==true) return

    if (q==true||this.Effect=='')
    {

        if (this.Type=='JS'&&this._parent!=null)
        {

            this.recountMeasures(true)
            this.recountMaxMeasures()

            this.setPosByParent()        
        }

        this.HTMLObj.style.display='block'
        this.status=100
    } else if (q==false||this.Effect=='Slide') {
        if (this.action==1||this.status>=100) return
        this.recountAbsPos(true)

        if (this.action==0)
        {
            this.recountMeasures(true)
            this.recountMaxMeasures()
        }

        if (this.Type=='JS'&&this._parent!=null)
        {
            this.setPosByParent()        
        }



        this.HTMLObj.style.display='block'
        this.HTMLObj.style.overflow='hidden'
        if (this.action==1&&this.status>0) this.status += - this.delta
        this.action=1
        this.showFX()
        this.action=1        
    }
}


IM_JSMenuGroupOBJ.prototype.setPosByParent = function()
{
            this._parent.recountMeasures(true)
            this._parent.recountAbsPos(true)
            
            if (this.isRoot==true){
                this.HTMLObj.style.top=this._parent.y + 'px'
                this.HTMLObj.style.left=this._parent.x + this._parent.width + 'px'
            } else {
                if (this._parent._parent.Direction=='H')
                {
                    this.HTMLObj.style.top=this._parent.y + this._parent.height + 'px'
                    this.HTMLObj.style.left=this._parent.x   + 'px'
                } else {
                    this.HTMLObj.style.top=this._parent.y  + 'px'
                    this.HTMLObj.style.left=this._parent.x + this._parent.width   + 'px'
                }    
            }    
}

IM_JSMenuGroupOBJ.prototype.showFX = function()
{
    if (this.action==-1){ return }
    

    if (this.status<100&&this.status>-this.delta)
    {
        this.status += this.delta
        setTimeout("MenuRegister.Groups[" + this.RegNum + "].showFX()",10)
        
        if (this.Effect=='Slide')
        {
            this.c_height=parseInt(this.max_height * Math.sin(1.57 * this.status / 100 ) )
            if(this.c_height>0) this.HTMLObj.style.height=this.c_height + 'px'
        }    

    } else {
        this.action  = 0
        this.status = 100
        
        if (this.Effect=='Slide')
        {
            this.HTMLObj.style.height='auto'
            this.HTMLObj.style.overflow=this.overflow
            
        }    
    }
}

IM_JSMenuGroupOBJ.prototype.hide = function(q,h)
{

    if (MenuRegister.isEditing==true) return

    if (this.Type=='JS') {
        if (this.over==true)
        {
            return false
        } else if (this._parent!=null&&this._parent.over==true)
        {
            return false
        } else {
            for (var i=0;i<this.Items.length;i++)
            {
                if (this.Items[i].Group.HTMLObj!=null&&this.Items[i].Group.status>0&&this.Items[i].Group.over==false){
                 this.Items[i].Group.hide(true,true)
                }
            }
        }
    }

    if (q==true||this.Effect=='')
    {
        this.HTMLObj.style.display='none'
        this.status=0


        if (this.Type=='JS'&&h!=true)
        {
            if (this._parent!=null){
                if (this._parent._parent!=null&&this._parent._parent.isRoot==false){
                   this._parent._parent.hide(true)
                }
            }
        }


    } else if (q==false||this.Effect=='Slide') {
        if (this.status==0)
        {
            
            if (this.Type=='JS'&&h!=true)
            {
                if (this._parent!=null){
                    if (this._parent._parent!=null&&this._parent._parent.isRoot==false){
                        this._parent._parent.hide(false)
                    }   
                }
            }
            
            return false
        }
        if (this.action==-1) return
        this.recountAbsPos(true)
        if (this.action==0)
        {
            this.recountMeasures(true)
            this.recountMaxMeasures()
        }
        this.HTMLObj.style.overflow='hidden'
        if (this.action==1&&this.status<100) this.status += this.delta
        this.action=-1
        this.hideFX()
        this.action=-1
    }
}

IM_JSMenuGroupOBJ.prototype.hideFX = function()
{
    if (this.action==1){ return }
    
    if (this.status>this.delta&&this.status<100 + this.delta)
    {
        this.status += - this.delta
        setTimeout("MenuRegister.Groups[" + this.RegNum + "].hideFX()",10)
        if (this.Effect=='Slide')
        {
            this.c_height=parseInt(this.max_height * Math.sin( 1.57 * this.status / 100 ) )
            if(this.c_height>0) this.HTMLObj.style.height=this.c_height + 'px'
        }    

    } else {
        this.action  = 0
        this.status = 0

        if (this.Effect=='Slide')
        {
            this.HTMLObj.style.display='none'
            this.HTMLObj.style.overflow=this.overflow
            this.HTMLObj.style.height='auto'
        }
        if (this.Type=='JS')
        {
            if (this._parent!=null){
                if (this._parent._parent!=null&&this._parent._parent.isRoot==false){
                    this._parent._parent.hide(false)
                }
            }
        }
    }
}



IM_JSMenuGroupOBJ.prototype.toggle = function(q)
{
    if (this.status==0)
    {
        this.show(q)
    } else if (this.status==100) {
        this.hide(q)
    }
     
}

IM_JSMenuGroupOBJ.prototype.recountAbsPos = function(isHidden)
{
    this.x=findPosX(this.HTMLObj)
    this.y=findPosY(this.HTMLObj)

    function findPosX(obj)
        {
	        var curleft = 0;
        	if (obj.offsetParent)
   	        {
		        while (obj.offsetParent)
		        {
			        curleft += obj.offsetLeft
			        obj = obj.offsetParent;
		        }
	        }
	        else if (obj.x)
		        curleft += obj.x;
	        return curleft;
        }

        function findPosY(obj)
        {
	        var curtop = 0;
        	if (obj.offsetParent)
	        {
        		while (obj.offsetParent)
        		{
			        curtop += obj.offsetTop
        			obj = obj.offsetParent;
		        }
	        }
	        else if (obj.y)
        		curtop += obj.y;
        	return curtop;
        }


}

IM_JSMenuGroupOBJ.prototype.recountMeasures = function(isHidden)
{
        var _disp=this.HTMLObj.style.display
        this.HTMLObj.style.display='block'
        this.width=this.HTMLObj.offsetWidth
        this.height=this.HTMLObj.offsetHeight
        this.HTMLObj.style.display=_disp
}

IM_JSMenuGroupOBJ.prototype.recountMaxMeasures = function()
{
        this.max_width=this.width
        this.max_height=this.height
}


IM_JSMenuGroupOBJ.prototype.Open2Seleted = function(q)
{
    for (var i=0;i<this.Items.length;i++)
    {
        if (this.Items[i].Item.Selected==true||this.Items[i].Item.ChildSelected==true){
            this.Items[i].expand(q);
            if (this.Items[i].Group.HTMLObj!=null)
            {
                this.Items[i].Group.Open2Seleted(q)
            }
            break;
        }
    }
     
}



IM_JSMenuGroupOBJ.prototype.newStatusOpen=function()
{
    var r
    if (this.status==100) r=false
    if (this.status==0) r=true
    if (this.action==1) r=false
    if (this.action==-1) r=true
    return r
    
}


IM_JSMenuGroupOBJ.prototype.Add = function()
{
    var _G=this
    var _this=this
    var newItem=new IM_MenuItem(0,this.ID,1,'',false,'','','','','',0,false)    
    

    if (_G.HTMLObj!=null)
    {
        var ii=_G.HTMLObj.childNodes.length
        for (var i=0;i<ii;i++){
            _G.HTMLObj.removeChild(_G.HTMLObj.childNodes[0])
        }
    } else {

        _G.HTMLObj=document.createElement("DIV");
        _G.HTMLObj.className=this.StylePrefix + 'Group'
        _G.HTMLObj.style.display='none'    
        _G.status=0
        _G.action=0
        if (_this._parent!=null)
        {
                _this._parent.ParentGroupElement.appendChild(_G.HTMLObj)
        } else {
                alert('No root')
                return
                //_G.HTMLObj.appendChild(_G.HTMLObj)
        }
        _G.HTMLObj.onmouseover=on
        _G.HTMLObj.onmouseout=off
    }


    var num=-1
    var newOrd=0
    newItem.Ord=0

    newMItem=new IM_JSMenuItemOBJ(newItem,0,0,_this.StylePrefix,_this.Type,_this.editable)

    newMItem.Group.Direction=_this.Direction
    newMItem.Group.Effect=_this.Effect
    newMItem.Group.editable=_this.editable

    var newLevel=0
    if (this._parent!=null) newLevel = this._parent.Level + 1
    newMItem.Level=newLevel

    newMItem._parent=_this
    newMItem.Group.HTMLObj=null
    _G.Items.splice(num+1,0,newMItem)

    var ii=_G.Items.length
    
    for (var i=0;i<ii;i++)
    {
        _G.Items[i].Num=i
        if (i>num) _G.Items[i].Item.Ord++;
    }
    

    _G.getHTML()


    if (_G._parent!=null&&(!(_G._parent>-1)))
    {
        _G._parent.setIMGS(true)
        _G._parent.expand(true)    
    }

    newMItem.editon()


    function on(){
        if (MenuRegister.isEditing==false) _this.on()
    }
    function off(){
        if (MenuRegister.isEditing==false) _this.off()
    }


}


IM_JSMenuGroupOBJ.prototype.getHTML=function(OnlyOneLevel)
{

    var total=this.Items.length

    if (total==0&&this.isRoot!=true)
    {

        if (OnlyOneLevel){
         if (this._parent!=null){
            this._parent.ParentGroupElement.removeChild(this.HTMLObj)
            
         }
        }
        this.status=0
        this.action=0
        this.HTMLObj=null
        
        return
    }

    if (this.editable==true&&this.isRoot==true&&total==0)
    {
        this.HeadHTMLObj=document.createElement("DIV");
        this.HeadHTMLObj.style.width='100%'
        this.HeadHTMLObj.style.cursor='hand'
        var _this=this
        this.HeadHTMLObj.onclick=Add
        var H=document.createElement("DIV");        

        H.className=this.StylePrefix + 'ItemHead'
        H.innerHTML='Main'
        this.HeadHTMLObj.appendChild(H)
        this.HTMLObj.appendChild(this.HeadHTMLObj)
        return
    }
    

    

   if (this.isRoot&&this.Direction=='H')
   {
            this.Tb=document.createElement("TABLE");    
            this.Tb.setAttribute("border","0");
            this.Tb.setAttribute("cellPadding","0");
            this.Tb.setAttribute("cellSpacing","0");
            this.Tb.setAttribute("width","100%");

            this.Tbb=document.createElement("TBODY");    
            this.Tb.appendChild(this.Tbb)


            this.Tr=document.createElement("TR");
            this.Tbb.appendChild(this.Tr)
            //this.inTb=new Array()
        
            this.HTMLObj.appendChild(this.Tb)
   }



    for (var i=0;i<total;i++)
    {
       
        if (OnlyOneLevel!=true) this.Items[i].Group.getHTML()

        if (this.Items[i].Group.HTMLObj!=null){
            this.Items[i].ParentGroupElement.appendChild(this.Items[i].Group.HTMLObj)
        }


        this.Items[i].setIMGS(OnlyOneLevel)

        if (this.isRoot&&this.Direction=='H')
        {
                var inTd=document.createElement("TD");
                this.Tr.appendChild(inTd)
                //inTd.innerHTML='(' + i + ')'
            
                inTd.appendChild(this.Items[i].HTMLObj)
            
        } else {
            this.HTMLObj.appendChild(this.Items[i].HTMLObj)
        }
        
    }

   if (this.Type=='JS'&&this.HTMLObj!=null&&this.isRoot==false)
   {
        this.HTMLObj.style.position='absolute'
   }

    function Add()
    {
        _this.Add()
    }

}





function IM_JSMenuItemOBJ(Item,Num,Level,StylePrefix,Type,editable)
{
    this._str='Item'
    this.Type=Type
    this.Num=Num
    this.Level=Level
    this._parent=null
    this.Item=Item
    this.ID=Item.ID


    this.width=0
    this.height=0
    this.x=0
    this.y=0


    this.Group=new IM_JSMenuGroupOBJ(this.ID,StylePrefix,Type,null,null)
    this.Group._parent=this
    
    this.StylePrefix=StylePrefix

    this.over=false

    

        this.HTMLObj=document.createElement("DIV");
    
        this.Tb=document.createElement("TABLE");
        this.Tb.setAttribute("border","0");
        this.Tb.setAttribute("cellPadding","0");
        this.Tb.setAttribute("cellSpacing","0");
        this.Tb.setAttribute("width","100%");    
        this.HTMLObj.appendChild(this.Tb)



        this.Tr=document.createElement("TR");
        if (this.Type=='Tree')
        {
            this.Td1=document.createElement("TD");
            this.Td1.setAttribute("vAlign","top");
            this.Tr.appendChild(this.Td1)    
    

            this.LeftHTMLObj=document.createElement("IMG");
            this.LeftHTMLObj.className=this.StylePrefix + 'ItemIMG'
            this.Td1.appendChild(this.LeftHTMLObj)
        }


        if (editable==true)
        {
            this.TdEdit=document.createElement("TD");
            this.TdEdit.setAttribute("vAlign","top");
            this.Tr.appendChild(this.TdEdit)    
    
            this.EditIMGHTMLObj=document.createElement("A");
            this.EditIMGHTMLObj.setAttribute("href", "#");
            this.EditIMGHTMLObj.setAttribute("onclick", "return false;");
            this.TdEdit.appendChild(this.EditIMGHTMLObj)
            
            this.EditIMGHTMLObjImg=document.createElement("IMG");
            this.EditIMGHTMLObjImg.width=18
            this.EditIMGHTMLObjImg.setAttribute("border","0");
            this.EditIMGHTMLObjImg.height=18
            this.EditIMGHTMLObjImg.src='Images/JSmenu/Target.gif'
            this.EditIMGHTMLObjImg.style.cursor='move'
            this.EditIMGHTMLObj.appendChild(this.EditIMGHTMLObjImg)
        }


        this.Td2=document.createElement("TD");
        this.Td2.setAttribute("vAlign","top");
        this.Td2.setAttribute("width","100%");    

        this.MDiv=document.createElement("DIV");
        this.MDiv.style.width="100%"
        this.MDiv.style.cursor="hand"


        this.inHTMLObj=document.createElement("DIV");
        this.inHTMLObj.className=this.StylePrefix + 'Item'


        if (this.Type=='JS')
        {

            //this.inHTMLObj.innerHTML=this.Item.Topic

            //this.inHTMLObj.appendChild(this.inRightHTMLObj)

            this.inRightHTMLObj=document.createElement("IMG");
            this.inRightHTMLObj.align='right'
            this.inRightHTMLObj.className=this.StylePrefix + 'ItemIMG'
            this.inHTMLObj.appendChild(this.inRightHTMLObj)


            this.inTopicText=document.createElement('SPAN');
            this.inTopicText.innerHTML=this.Item.Topic
            this.inHTMLObj.appendChild(this.inTopicText)


            this.editEL=document.createElement("DIV");


            if (editable==true)
            {

                this.editInput=document.createElement("INPUT");
                this.editInput.className=this.StylePrefix + 'MainGroupInput'
                this.editInput.type='Text'
                this.editInput.value=this.Item.Topic
                this.editEL.style.display='none'
                this.editEL.appendChild(this.editInput)
                this.MDiv.appendChild(this.editEL)
            }    




            this.MDiv.appendChild(this.inHTMLObj)


        } else {
            this.inHTMLObj.innerHTML=this.Item.Topic
            
            if (editable==true)
            {
                this.editEL=document.createElement("DIV");
                this.editInput=document.createElement("INPUT");
                this.editInput.className=this.StylePrefix +'MainGroupInput'
                this.editInput.type='Text'
                this.editInput.value=this.Item.Topic
                this.editEL.style.display='none'
                this.editEL.appendChild(this.editInput)
                this.MDiv.appendChild(this.editEL)

            }

            this.MDiv.appendChild(this.inHTMLObj)
        }

        var _this=this;

        this.MDiv.onmouseover=on
        this.MDiv.onmouseout=off    
        this.MDiv.onclick=click

        if (editable==true)
        {
            this.MDiv.oncontextmenu=edit
            this.editInput.onkeydown=inputpress
            this.editInput.change=inputpress
            this.editInput.onblur=editoff

            this.EditIMGHTMLObj.onclick=EditMenuShow
            //this.EditIMGHTMLObj.onblur=EditMenuShow
            this.EditIMGHTMLObj.oncontextmenu=EditMenuShow
            this.EditIMGHTMLObj.ondragstart=ondragstart
            this.EditIMGHTMLObj.ondragend=ondragend
            

            this.MDiv.ondragenter=ondragenter
            this.MDiv.ondragleave=ondragleave
            this.MDiv.ondrop=ondrop
            this.MDiv.ondragover=cb
            this.MDiv.ondragstart=cb

            if (this.Type=='JS')
            {
                this.inRightHTMLObj.ondragenter=ondragenter
            }
            setCBS(this.MDiv)

            //window.event.cancelBubble = true;

        }
        this.Td2.appendChild(this.MDiv)    
        this.Tr.appendChild(this.Td2)
    
    

        if (this.Type=='JS')
        {
        }


        this.Tbb=document.createElement("TBODY");
        this.Tbb.appendChild(this.Tr)    
        this.Tb.appendChild(this.Tbb)    

        this.ParentGroupElement=this.Td2


    function setCBS(o)
    {
            for (var i=0;i<o.childNodes.length;i++)
            {
                
                o.childNodes[i].ondragenter=cbc
                o.childNodes[i].ondragleave=cbc
                o.childNodes[i].ondragover=cbc
                o.childNodes[i].ondragstart=cbc
            }
        

    }



    function cb(){
                window.event.cancelBubble = true;
                return false
    }

    function cbc(){
                window.event.cancelBubble = true;
    }


    function ondragenter(){
        if (MenuRegister.isEditing==false) _this.dragover()
        return false
    }

    function ondrop(){
        if (MenuRegister.isEditing==false) _this.drop(window.event.ctrlKey)
        return false
    }


    function ondragleave(){
        if (MenuRegister.isEditing==false) _this.dragout()
        return false
    }
    function ondragend(){
        if (MenuRegister.isEditing==false) _this.dragend()
        return false
    }

    function ondragstart(){
        if (MenuRegister.isEditing==false) _this.dragstart()
    }

    function on(){
        if (MenuRegister.isEditing==false) _this.on()
    }

    function edit(){
        return _this.editon()
    }

    function editoff(){
        _this.editoff()
        off()
    }


    function off(){
        if (MenuRegister.isEditing==false) _this.off()
    }

    function click(){
        if (MenuRegister.isEditing==false)  _this.click(false)
    }


    function inputpress(e)
    {
        if (!e) e=event
        
        var  keyCode= e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
        return _this.inputpress(keyCode)
    } 
    function EditMenuShow()
    {
        _this.EditMenuShow()
        return false
    }
}

IM_JSMenuItemOBJ.prototype.EditMenuShow = function(p)
{

    var _this=this
    if (MenuRegister.editMenu.visible==true)
    {
       MenuRegister.editMenu.HTML.style.display='none'
       MenuRegister.editMenu.visible=false

        this.editing=false
        MenuRegister.isEditing=false


        if (MenuRegister.editMenu.Item==this) return
       
    }
    
    





    if (MenuRegister.editMenu._p==null)
    {
       document.body.appendChild(MenuRegister.editMenu.HTML)
       MenuRegister.editMenu._p=new Object()

        MenuRegister.editMenu.HTML.className=this._parent.StylePrefix + 'Properties'
    }


    //MenuRegister.editMenu.HTML.innerHTML = '<div>1)-*-*-*</div><div>2)-*-*-*</div><div>3)-*-*-*</div><div>4)-*-*-*</div>'


    var ChildELi=MenuRegister.editMenu.HTML.childNodes.length
    for (var i=0;i<ChildELi;i++)
    {
        MenuRegister.editMenu.HTML.removeChild(MenuRegister.editMenu.HTML.childNodes[0]);
    
    }    
    
    MenuRegister.editMenu.HTML.appendChild(EditItem('Edit','Edit'))
    MenuRegister.editMenu.HTML.appendChild(EditItem('Edit (advanced)','EditA'))


    MenuRegister.editMenu.HTML.appendChild(EditItem('Move before previous','Up'))
    MenuRegister.editMenu.HTML.appendChild(EditItem('Move after following','Down'))


    MenuRegister.editMenu.HTML.appendChild(EditItem('Move indent','Right'))
    MenuRegister.editMenu.HTML.appendChild(EditItem('Move outdent','Left'))


    MenuRegister.editMenu.HTML.appendChild(EditItem('Add','Add'))
    MenuRegister.editMenu.HTML.appendChild(EditItem('Add Sub','AddSub'))

    if(this.Item.CountOfItems > 0){
        MenuRegister.editMenu.HTML.appendChild(EditItem('Delete all articles','DeleteA'))
    } else {
        MenuRegister.editMenu.HTML.appendChild(EditItem('Delete this topic and subtopics','Delete'))
    }


    MenuRegister.editMenu.Item=this

    var x=0
    var y=0
    x=findPosX(this.EditIMGHTMLObj)
    y=findPosY(this.EditIMGHTMLObj)
    var dy=this.EditIMGHTMLObj.offsetHeight
    MenuRegister.editMenu.HTML.style.top=(y + dy) + 'px';
    MenuRegister.editMenu.HTML.style.left=x + 'px';
    MenuRegister.editMenu.HTML.style.display='block'
    MenuRegister.editMenu.visible=true

    this.editing=true
    MenuRegister.isEditing=true


    function findPosX(obj)
        {
	        var curleft = 0;
        	if (obj.offsetParent)
   	        {
		        while (obj.offsetParent)
		        {
			        curleft += obj.offsetLeft
			        obj = obj.offsetParent;
		        }
	        }
	        else if (obj.x)
		        curleft += obj.x;
	        return curleft;
        }

        function findPosY(obj)
        {
	        var curtop = 0;
        	if (obj.offsetParent)
	        {
        		while (obj.offsetParent)
        		{
			        curtop += obj.offsetTop
        			obj = obj.offsetParent;
		        }
	        }
	        else if (obj.y)
        		curtop += obj.y;
        	return curtop;
        }


        function EditItem(Title,Opp)
        {
            
            var MEL=document.createElement("DIV");
            MEL.style.width='100%'
            MEL.style.cursor='hand'

            var EL=document.createElement("DIV");
            EL.innerHTML=Title

            MEL.onmouseover=onmouseover
            MEL.onmouseout=onmouseout
            MEL.onclick=onclick
            EL.className=_this.StylePrefix + 'Item'
            
            MEL.appendChild(EL)
            return MEL
            
            function onmouseover()
            {
                //EL.className=_this.StylePrefix + 'ItemOn'
            }

            function onmouseout()
            {
                //EL.className=_this.StylePrefix + 'Item'
            }

            function onclick()
            {
                doMenu(Opp)
            }

        }

        function doMenu(Opp)
        {
            _this.EditMenuShow()
            switch (Opp) {
                case 'Edit':
                    _this.editon()
                    break;
                case 'EditA':
                    _this.editA()
                    break;
                case 'Up':
                    _this.Up()
                    break;
                case 'Down':
                    _this.Down()
                    break;
                case 'Right':
                    _this.Right()
                    break;
                case 'Left':
                    _this.Left()
                    break;
                case 'Add':
                    _this.Add()
                    break;
                case 'AddSub':
                    _this.Group.Add()
                    break;
                case 'Delete':
                    _this.Delete()
                    break;
                case 'DeleteA':
                    _this.DeleteA()
                    break;
                default :
                    alert(Opp + ' unknown')
                    break;
            }
        }



}

IM_JSMenuItemOBJ.prototype.dragstart = function()
{
    //alert('dragstart')
    this.HTMLObj.style.display='none'

    MenuRegister.draggingOBJ=this

	//window.event.dataTransfer.setData("TEXT",'123456');
	window.event.dataTransfer.effectAllowed = "move";

}

IM_JSMenuItemOBJ.prototype.drop = function(aschild)
{
    if (MenuRegister.draggingOBJ==null) return
    this.off()

    if (this.Type=='JS')
    {
        this.inRightHTMLObj.style.visibility='visible'
    }


    IMMenuForm.elements.IMMAction.value='MoveTopic'
    IMMenuForm.elements.TopicID.value=MenuRegister.draggingOBJ.ID
    if (aschild)
    {
        IMMenuForm.elements.ParentID.value=this.ID
        IMMenuForm.elements.Ord.value=1
    } else {
        IMMenuForm.elements.ParentID.value=this.Item.PID
        IMMenuForm.elements.Ord.value=this.Item.Ord+1
    }
    IMMenuForm.submit()

    MenuRegister.draggingOBJ=null
    return
    //alert('drop as child =' + aschild)
}


IM_JSMenuItemOBJ.prototype.dragend = function()
{
    this.HTMLObj.style.display='block'
    if (MenuRegister.draggingOBJ==null) return

    MenuRegister.draggingOBJ=null
}


IM_JSMenuItemOBJ.prototype.dragover = function()
{
    this._parent.on()
    if (MenuRegister.draggingOBJ==null) return
    this.on()

    this.expand(true)
    if (this.Type=='JS')
    {
        this.inRightHTMLObj.style.visibility='hidden'
    }
    //this.HTMLObj.style.display='block'
}


IM_JSMenuItemOBJ.prototype.dragout = function()
{
    if (MenuRegister.draggingOBJ==null) return
    this.off()

    if (this.Type=='JS')
    {
        this.inRightHTMLObj.style.visibility='visible'
    }



    //this._parent.off()

    //this.collapse(true)
    //alert('dragout')
    //this.HTMLObj.style.display='block'
}

IM_JSMenuItemOBJ.prototype.Right = function()
{
    var num=this.Num
    if (num==0) return
    var _G=this._parent
    


    IMMenuForm.elements.IMMAction.value='MoveTopic'
    IMMenuForm.elements.TopicID.value=this.ID
    IMMenuForm.elements.ParentID.value=_G.Items[num-1].Item.ID
    var O=1
    if (window.event.ctrlKey)
    {
        var ll=_G.Items[num-1].Group.Items.length
        if (ll>0){
            O=_G.Items[num-1].Group.Items[ll-1].Item.Ord + 1
        }    
    }
    
    IMMenuForm.elements.Ord.value=O

    IMMenuForm.submit()



}

IM_JSMenuItemOBJ.prototype.Left = function()
{
    if (this._parent._parent==null) return


    IMMenuForm.elements.IMMAction.value='MoveTopic'
    IMMenuForm.elements.TopicID.value=this.ID
    IMMenuForm.elements.ParentID.value=this._parent._parent.Item.PID
    IMMenuForm.elements.Ord.value=this._parent._parent.Item.Ord+1

    IMMenuForm.submit()


}

IM_JSMenuItemOBJ.prototype.Up = function()
{
    var num=this.Num
    if (num==0) return
    var _G=this._parent
        


    IMMenuForm.elements.IMMAction.value='MoveTopic'
    IMMenuForm.elements.TopicID.value=this.ID
    IMMenuForm.elements.ParentID.value=this.Item.PID
    IMMenuForm.elements.Ord.value=_G.Items[num-1].Item.Ord
    IMMenuForm.submit()
    
    return



    var ii=_G.HTMLObj.childNodes.length
    
    for (var i=0;i<ii;i++){
        _G.HTMLObj.removeChild(_G.HTMLObj.childNodes[0])
    }
    var t=new Object()
    var u=new Object()
    t=_G.Items[num]
    u=_G.Items[num-1]
    tNum=t.Num
    tOrd=t.Ord
    uNum=u.Num
    uOrd=u.Ord


    _G.Items[num]=u
    _G.Items[num].Num=tNum
    _G.Items[num].Ord=tOrd

    _G.Items[num-1]=t
    _G.Items[num-1].Num=uNum
    _G.Items[num-1].Ord=uOrd
    
    _G.getHTML(true)

}

IM_JSMenuItemOBJ.prototype.Down = function()
{

    var num=this.Num
    if (this.islast==true) return
    var _G=this._parent
        


    IMMenuForm.elements.IMMAction.value='MoveTopic'
    IMMenuForm.elements.TopicID.value=this.ID
    IMMenuForm.elements.ParentID.value=this.Item.PID
    IMMenuForm.elements.Ord.value=_G.Items[num+1].Item.Ord + 1
    IMMenuForm.submit()
    
    return


    var ii=_G.HTMLObj.childNodes.length
    
    for (var i=0;i<ii;i++){
        _G.HTMLObj.removeChild(_G.HTMLObj.childNodes[0])
    }
    var t=new Object()
    var u=new Object()
    t=_G.Items[num]
    u=_G.Items[num+1]

    tNum=t.Num
    tOrd=t.Ord
    uNum=u.Num
    uOrd=u.Ord

    _G.Items[num]=u
    _G.Items[num].Num=tNum
    _G.Items[num].Ord=tOrd

    _G.Items[num+1]=t
    _G.Items[num+1].Num=uNum
    _G.Items[num+1].Ord=uOrd
    
        

    _G.getHTML(true)

}


IM_JSMenuItemOBJ.prototype.Add = function()
{
    var _G=this._parent
    var newItem=new IM_MenuItem(0,this.Item.PID,1,'',false,'','','','','',0,false)    

    var ii=_G.HTMLObj.childNodes.length
    
    for (var i=0;i<ii;i++){
        _G.HTMLObj.removeChild(_G.HTMLObj.childNodes[0])
    }



    var num=this.Num
    var newOrd=this.Item.Ord
    newItem.Ord=this.Item.Ord

    newMItem=new IM_JSMenuItemOBJ(newItem,0,this.Level,this.StylePrefix,this.Type,this._parent.editable)

    newMItem._parent=this._parent
    newMItem.Group.HTMLObj=null

    newMItem.Group.Direction=_G.Direction
    newMItem.Group.Effect=_G.Effect
    newMItem.Group.editable=_G.editable

    _G.Items.splice(num+1,0,newMItem)

    var ii=_G.Items.length
    
    for (var i=0;i<ii;i++){
        _G.Items[i].Num=i
        if (i>num) _G.Items[i].Item.Ord++;
    }


    _G.getHTML(true)

    if (_G._parent!=null&&(!(_G._parent>-1)))
    {
				_G._parent.setIMGS(true)
    }
    newMItem.editon()
}

IM_JSMenuItemOBJ.prototype.Delete = function()
{
    var _G=this._parent
    var num=this.Num
    
    
            if (this.ID>0)
            {
                if (confirm('Are You Sure?')!=true) return
                IMMenuForm.elements.IMMAction.value='Delete'
                IMMenuForm.elements.TopicID.value=this.ID
                IMMenuForm.submit()
                return
            }

    _G.Items.splice(num,1)
    
    var ii=_G.HTMLObj.childNodes.length
    
    for (var i=0;i<ii;i++){
        _G.HTMLObj.removeChild(_G.HTMLObj.childNodes[0])
    }

    var ii=_G.Items.length
    
    for (var i=0;i<ii;i++){
        _G.Items[i].Num=i
    }


    _G.getHTML(true)

    if (_G._parent!=null&&(!(_G._parent>-1)))
    {
        _G._parent.setIMGS(true)
    }
}

IM_JSMenuItemOBJ.prototype.DeleteA = function()
{
   
    
            if (this.ID>0&&this.Item.CountOfItems>0)
            {
                if (confirm('Are You Sure?')!=true) return
                IMMenuForm.elements.IMMAction.value='DeleteArticles'
                IMMenuForm.elements.TopicID.value=this.ID
                IMMenuForm.submit()
                return
            }

}


IM_JSMenuItemOBJ.prototype.chTopic = function(v)
{
    this.Item.Topic=v

    if (this.Type=='JS'){
        this.inTopicText.innerHTML=v
    } else {
        this.inHTMLObj.innerHTML=v
    }
    this.Item.TopicNoHTML=RemoveHTML(v)
    
    function RemoveHTML(x)
    {
        var r=x.replace(/<[^>]*>/ig,'')
        return r
    }

}


IM_JSMenuItemOBJ.prototype.inputpress = function(keyCode)
{
    if (keyCode==27){
        
        this.editoff()
        return false
    } else if (keyCode==13){
        var v=this.editInput.value
        if (v!='') 
        {
            this.chTopic(v)
            //this.ID=666
            this.editoff() 
            //this.Item.Topic=v
            if (this.ID>0)
            {
                IMMenuForm.elements.IMMAction.value='SaveTopic'
                IMMenuForm.elements.TopicID.value=this.ID
                IMMenuForm.elements.Topic.value=this.Item.Topic
                IMMenuForm.submit()
            } else {
                IMMenuForm.elements.IMMAction.value='AddTopic'
                var _PID=this.Item.PID
                if (_PID==null) _PID=0
                IMMenuForm.elements.ParentID.value=_PID
                IMMenuForm.elements.Ord.value=this.Item.Ord
                IMMenuForm.elements.Topic.value=this.Item.Topic
                IMMenuForm.submit()

            }



            //alert('done')

            
        } 
        return false
    } else {
        return true
    }    
}

IM_JSMenuItemOBJ.prototype.editon = function()
{
    if (this._parent.editable==false||MenuRegister.isEditing==true) return false
    var w
    this.editing=true
    MenuRegister.isEditing=true
    w=this.MDiv.offsetWidth - 12
    this.inHTMLObj.style.display='none'    

    if (w<30) w=30
    this.editInput.style.width=w  + 'px'
    this.editEL.style.display='block'
    this.editInput.focus()
    return false 
}

IM_JSMenuItemOBJ.prototype.editA = function()
{
    popEditTopic(this.Item.ID,this.Item.PID)
}


IM_JSMenuItemOBJ.prototype.editoff = function()
{
        if (this.ID==0) {
          this.editing=false
          MenuRegister.isEditing=false

         this.Delete()
         return false
        }
        if (MenuRegister.isEditing==false) return false

        this.inHTMLObj.style.display='block'    
        
        this.editEL.style.display='none'
        
        this.editing=false
        MenuRegister.isEditing=false
        this.editInput.value=this.Item.Topic
}


IM_JSMenuItemOBJ.prototype.on = function()
{
    this.over=true
    
    if (this.Group.HTMLObj!=null){
        if (this.Group.over==false) window.top.status =this.Item.TopicNoHTML
    } else {
        window.top.status =this.Item.TopicNoHTML
    }    
    
    var _sfx=''
    if (this.Item.Selected==true) _sfx ='Sel'
   // this.inHTMLObj.className=_sfx + this.StylePrefix + 'ItemOn'

    if (this.Group.HTMLObj!=null){
        if (this.Type!='Tree'&&this.Type!='') this.Group.show(false)
    }
}

IM_JSMenuItemOBJ.prototype.off = function()
{
    this.over=false
    
    window.top.status=''
    if (this.inHTMLObj==null) return
    var _sfx=''
    if (this.Item.Selected==true) _sfx ='Sel'

   // this.inHTMLObj.className=_sfx + this.StylePrefix + 'Item'

    var _RegNum=this.Group.RegNum
    if (MenuRegister.Groups[_RegNum].Type=='JS')
    {
        while (_RegNum!=null)   
        {    
            if (MenuRegister.Groups[_RegNum].HTMLObj!=null){
                if (MenuRegister.Groups[_RegNum].over==false){
                    setTimeout("MenuRegister.Groups[" + _RegNum + "].hide(false)",1000)
                    _RegNum=MenuRegister.Groups[_RegNum]._parent
                    if (_RegNum!=null) _RegNum=_RegNum._parent
                    if (_RegNum!=null) _RegNum=_RegNum.RegNum
                }else{
                    _RegNum=null
                }
            } else {
                _RegNum=null
            }
        }
        
        if (this._parent.isRoot==false)
        {
            _RegNum=this._parent.RegNum
            setTimeout("MenuRegister.Groups[" + _RegNum + "].hide(false)",1000)
        }    


    } else {
        /*
        if (MenuRegister.Groups[_RegNum].HTMLObj!=null){        
            MenuRegister.Groups[_RegNum].hide(false)
        } 
        */   
    }

}

IM_JSMenuItemOBJ.prototype.TreeClick = function()
{
    if (this.Group.HTMLObj==null) return
    this.Toggle(false)
}

IM_JSMenuItemOBJ.prototype.Toggle = function(q)
{
    if (this.Group.HTMLObj==null) return
    if (this.Group.newStatusOpen())
    {
        this.expand(q)
    } else {
        this.collapse(q)
    }    
}

IM_JSMenuItemOBJ.prototype.expand = function(q)
{
    if (this.Group.HTMLObj==null) return
    
    if (this.Type=='Tree')
    {
     if (this._parent.isRoot==true){
        this.LeftHTMLObj.src='Images/JSMenu/RootOpen.gif'
     } else {
        this.LeftHTMLObj.src='Images/JSMenu/Open.gif'
     }
    }
    
    this.Group.show(q)
}

IM_JSMenuItemOBJ.prototype.collapse= function(q)
{
    if (this.Group.HTMLObj==null) return
    if (this.Type=='Tree')
    {
     if (this._parent.isRoot==true){
        this.LeftHTMLObj.src='Images/JSMenu/RootClosed.gif'
     } else {
        this.LeftHTMLObj.src='Images/JSMenu/Closed.gif'
     }
    
    }
    this.Group.hide(q)
}


IM_JSMenuItemOBJ.prototype.recountAbsPos = function(isHidden)
{
    this.x=findPosX(this.HTMLObj)
    this.y=findPosY(this.HTMLObj)

    function findPosX(obj)
        {
	        var curleft = 0;
        	if (obj.offsetParent)
   	        {
		        while (obj.offsetParent)
		        {
			        curleft += obj.offsetLeft
			        obj = obj.offsetParent;
			        if (obj) {
			            if (obj.style.position=='absolute') obj=new Object()
			        }    
		        }
	        }
	        else if (obj.x)
		        curleft += obj.x;
	        return curleft;
        }

        function findPosY(obj)
        {
	        var curtop = 0;
        	if (obj.offsetParent)
	        {
        		while (obj.offsetParent)
        		{
			        curtop += obj.offsetTop
        			obj = obj.offsetParent;
			        if (obj) {
			            if (obj.style.position=='absolute') obj=new Object()
			        }    
		        }
	        }
	        else if (obj.y)
        		curtop += obj.y;
        	return curtop;
        }


}

IM_JSMenuItemOBJ.prototype.recountMeasures = function(isHidden)
{
    
    if (isHidden==true||true) {
        var _disp=this.HTMLObj.style.display
        this.HTMLObj.style.display='block'
        this.width=this.HTMLObj.offsetWidth
        this.height=this.HTMLObj.offsetHeight
        this.HTMLObj.style.display=_disp
    } else {
        this.width=this.HTMLObj.offsetWidth
        this.height=this.HTMLObj.offsetHeight
    }    
}



IM_JSMenuItemOBJ.prototype.setIMGS= function(OnlyOneLevel)
{
    var _this=this

    var total=this._parent.Items.length
    var islast=(this._parent.Items[total-1]==this)
    
    this.islast=islast


    if (this._parent==null) return


        if (this._parent.Type=='Tree')
        {
            if (this.Group.HTMLObj!=null){
                
                if (this.Level==0){
                    if (this.Group.newStatusOpen())
                    {
                       this.LeftHTMLObj.src='Images/JSMenu/RootClosed.gif'
                    } else {
                       this.LeftHTMLObj.src='Images/JSMenu/RootOpen.gif'
                    }   
                } else {
                    
                    if (this.Group.newStatusOpen())
                    {
                       this.LeftHTMLObj.src='Images/JSMenu/Closed.gif'
                    } else {
                       this.LeftHTMLObj.src='Images/JSMenu/Open.gif'
                    }   
                }    
                this.LeftHTMLObj.onclick=TreeClick
            } else {
                if (this.Level==0){
                    this.LeftHTMLObj.src='Images/JSMenu/Root.gif'
                } else {
                    this.LeftHTMLObj.src='Images/JSMenu/Corner.gif'
                    this.LeftHTMLObj.onclick = null
                }    
            }
            
            
            if (!islast&&this.Level>0){
                this.Td1.style.backgroundImage="url(Images/JSMenu/Vertical.gif)"
            } else {
                this.Td1.style.backgroundImage=""
            }       
        }

        if (this._parent.Type=='JS')
        {
            if (this.Group.HTMLObj!=null){
                this.inRightHTMLObj.src='Images/JSMenu/SubMenu.gif'
            } else {
                this.inRightHTMLObj.src='Images/JSMenu/Spacer.gif'
            }
        }

    function TreeClick(){
        if (MenuRegister.isEditing==false) _this.TreeClick()
    }

}
IM_JSMenuItemOBJ.prototype.click= function(op)
{
        if (this.Item.Link!='')
        {
            var L=this.Item.Link
            L=L.replace(/&amp;/g,'&')
            if (this.Item.LinkTarget.toLowerCase()!='_self')
            {
                window.open(L,this.Item.LinkTarget)
            } else {
                window.location.href=L
            }    
        }
}


function IM_JSMenuOBJ(Items,MainMenu,StylePrefix,Type,Direction,Effect,editable)
{
    if (!Items) return null 
    if (!MainMenu) MainMenu=null
    if (!StylePrefix) StylePrefix=''
    if (!Type) Type=''
    if (Type!=''&&Type!='JS'&&Type!='Tree') Type=''
    if (!Direction) Direction=''
    if (!Effect) Effect=''
    if (!editable) editable=false
    var Group=IM_JSMenuGroupMaker(MainMenu,0,Items,StylePrefix,Type,Direction,Effect,editable)
    Group.isRoot=true
    Group.getHTML()
    if (Group.HTMLObj!=null){
     Group.HTMLObj.className=StylePrefix + 'MainGroup'
     Group.show(true)
     //Group.HTMLObj.onselectstart=DoNone
    }
    return Group
    function DoNone(){
        return false
    }
}

function IM_JSMenuGroupMaker(Item,Level,Items,StylePrefix,Type,Direction,Effect,editable)
{
    var ID=null
    if (Item!=null)
		{
			if (parseInt(Item)>0)
			{
				ID=Item
			} else{
			 ID=Item.ID
			} 
    }
//		if (Direction=='H') Type='JS'
    if (Level>0) Direction=''
    var G=new IM_JSMenuGroupOBJ(ID,StylePrefix,Type,Direction,Effect)
    G._parent=Item
    G.editable=editable
    var c=-1
    
    for(var i=0;i<Items.length;i++)
    {
        if (Items[i].PID==ID || ( !(Items[i].PID>0) && !(ID>0) ) )
        {
            c ++;    
            G.Items[c]=new IM_JSMenuItemOBJ(Items[i],c,Level,StylePrefix,Type,editable)
            G.Items[c]._parent=G
            G.Items[c].Group=IM_JSMenuGroupMaker(G.Items[c],Level + 1,Items,StylePrefix,Type,Direction,Effect,editable)
            if (G.Items[c].Item.Selected==true){
                
                //G.Items[c].StylePrefix = 'Sel' + G.Items[c].StylePrefix
                G.Items[c].inHTMLObj.className='Sel' + G.Items[c].StylePrefix + 'Item'
                
                G.Selected=true;
                G.Items[c].Item
                G.ChildSelected=false;
                var _PID=G.Items[c].Item.PID
                var _G=G._parent
                if (_G!=null) _G=_G._parent
                
                while (_G!=null){
                    _G.ChildSelected=true
                    var founded=false
                    var ii=_G.Items.length-1

                        if (_PID==_G.Items[ii].Item.ID){
                            _G.Items[ii].Item.ChildSelected=true
                            _PID=_G.Items[ii].Item.PID;
                            _G=_G._parent;
                            if (_G!=null) _G=_G._parent;
                            founded=true;
                        }

                    if (founded==false) _G=null
                }
            }
        }
    }
    return G;
}
