// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false
// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var mouse = new mouse_coordinate();

// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    mouse.X = event.clientX + document.body.scrollLeft
    mouse.Y = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    mouse.X = e.pageX
    mouse.Y = e.pageY
  }
  // catch possible negative values in NS4
  if (mouse.X < 0){mouse.X = 0}
  if (mouse.Y < 0){mouse.Y = 0}
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  return true
}

function mouse_coordinate(){
  this.X=0;
  this.Y=0;
}


function doblurpa(obj, str) {
	if(obj.value=="") {
		obj.value=str;
	}
}
function dofocuspa(obj, str) {
	if(obj.value==str) {
		obj.value='';
	} else {
	}
}

//-------------------------------------------------------------------
// isArray(obj)
// Returns true if the object is an array, else false
//-------------------------------------------------------------------
function isArray(obj){
  if(typeof(obj)=="undefined") return false;
  if(typeof(obj.length)=="undefined")return false;
  return true;
}

function treeObject(parent){
  this.line=-1;
  this.parent=parent;
  this.tmp_top=-1;
  this.tmp_box=null;
  this.tmp_text=null;

  function top(){
    if(this.tmp_top==-1){
      this.tmp_top=document.getElementById('row'+this.line).offsetTop;
    }
    return this.tmp_top;
  }

  function box(){
    if(this.tmp_box==null){
      this.tmp_box=document.getElementById('line'+this.line);
    }
    return this.tmp_box;
  }

  function text(){
    if(this.tmp_text==null){
      this.tmp_text=document.getElementById('text'+this.line);
    }
    return this.tmp_text;
  }

  this.box=box;
  this.top=top;
  this.text=text;
}

function miniobj(){
  this.id = '';
  this.pos = -1;
}

function actionObject(uri, target, warning){
  this.uri=uri;
  this.target=target;
  this.warning=warning;
}

function tree(){
  this.uri;
  this.colorhilite = "ff8308";
  this.lineoffset=3;
  this.iconsize=20;
  this.rootid='0.0';

  this.id_line = new Array();
  this.allowed = new Array();
  this.actions = new Array();
  this.hook = false;
  this.objects = new Array();
  this.selected;
  this.active;
  this.moving = false;
  this.moveto = new miniobj();
  this.hilited;
  this.last_line = 0;
  
  this.lastmenu = '';

  function addaction(action, uri, target, warning ){
    this.actions[action] = new actionObject(uri, target, warning);
  }

  function addallowed(type, allowedtype){
    if(!isArray(this.allowed[type]))this.allowed[type]=new Array();
    this.allowed[type][allowedtype]=true;

  }

  function add(line, id, pos, parent, type, read, write, bgcolor, remove, name, children){
    this.objects[line]=new treeObject(parent);
    this.objects[line].line=line;
    this.objects[line].id=id;
    this.objects[line].pos=pos;
    this.objects[line].type=type;
    this.objects[line].bgcolor=bgcolor;
    this.objects[line].read=read;
    this.objects[line].write=write;
    this.objects[line].name=name;
    this.objects[line].children=children;

    if(type==-1){
      this.objects[line].isobject=false;
    }
    else{
      this.objects[line].isobject=true;
    }
    if(this.objects[line].isobject){
      this.id_line[id]=line;
    }
    if(typeof(remove)=='boolean'){
      this.objects[line].remove=remove
    }
    else{
      this.objects[line].remove=false;
    }
    this.last_line=line;
  }

  function select(line, evtemp){
    if(!this.moving){
      if(typeof(this.selected)=='object'){
        this.selected.box().style.fontWeight="normal";
      }
      this.selected=this.objects[line];
      if(this.hook){
        drHook(this, this.selected.id, this.selected.type, this.selected.name);
      }
      else{
        this.selected.box().style.fontWeight="bold";

		if(this.lastmenu) window.CMenus[this.lastmenu].hide();
        
        if(this.selected.read && this.selected.write) {
          if(this.selected.remove) this.lastmenu = 'menu'+this.selected.type;
          else this.lastmenu = 'static'+this.selected.type;
        } else {
          if(this.selected.remove) this.lastmenu = 'generic_menu';
          else this.lastmenu = 'static_static';
        }
        CMenuPopUp(this.lastmenu, evtemp);
      }
    }
  }

  function activate(line){
    this.active=this.objects[line];
  }

  function deactivate(){
    //this.active=null;
  }

  function setselected(id){
       this.selected=this.objects[this.id_line[id]];
       this.selected.box().style.fontWeight="bold";
       this.moving=true;
  }

  function cansel(){
    if(typeof(this.last_hilited)=='object'){
      this.last_hilited.box().style.background='#'+this.last_hilited.bgcolor;
    }
    this.selected.box().style.fontWeight="normal";
    this.active=null;
    this.moving=false;
    this.moveto.id='';
    this.moveto.pos=-1;
    
    window.CMenus['movemenu'+(this.selected.children ? 'r':'')].hide();
  }

  function highlight(){
    var atpos=-1;
    if(this.moving && typeof(this.active)=='object'){
      //find out what to higlite, from where you are
      if(this.active.isobject){ //normal line with icon
        var diff = mouse.Y - this.active.top(); //Where in the cell is the mouse cursor? (tempY is absolut pos of mouse, mo_obj.offsetTop is absolut pos of table)
        if(diff <= this.lineoffset && this.active.line > 0){ //over
          this.hilited=this.objects[this.active.line-1];
        }
        else if(diff > (this.iconsize - this.lineoffset) && this.active.line < this.last_line){ //under
          this.hilited=this.objects[this.active.line+1];
        }
        else if(this.active.line <= this.last_line && this.active.line >= 0){
          this.hilited=this.active;
        }
      }
      else if(this.active.line <= this.last_line && this.active.line >= 0){ //line between objets
        this.hilited=this.active;
      }
      //we have now found out what we should hilite, lets find out if we should hihlith
      if(this.last_hilited!=this.hilited){
        //can this object type be placed here?
        var legalpos=true;
        if(this.hilited.isobject){
          test=this.hilited;
        }
        else{
          //test if object is abow, or below itselfe..
          atpos=this.hilited.pos;
          if(this.hilited.line==this.selected.line+1 || this.hilited.line==this.selected.line-1)legalpos=false;
          test=this.objects[this.id_line[this.hilited.parent]]; //if between objects, test against parent
        }

        if(typeof(test)!='object')legalpos=false;

        //testing that i can add this objecttype here
        if(legalpos){
          if(isArray(this.allowed[test.type]) && this.allowed[test.type][this.selected.type]){
            legalpos=true;
          }
          else{
            legalpos=false;
          }
        }

        //testing that none of the parents are the object we are moving to make sure that we dont put the object under itselfe...
        if(legalpos){
          var digg=test;
          while(digg.id!=this.rootid && digg.id!=this.selected.id){
            digg=this.objects[this.id_line[digg.parent]];
          }
          if(digg.id==this.selected.id)legalpos=false;
        }

        if(typeof(this.last_hilited)=='object' && this.last_hilited!=null){
          this.last_hilited.box().style.background='#'+this.last_hilited.bgcolor;
        }

        if(legalpos){
          this.moveto.id=test.id;
          this.moveto.pos=atpos;
          this.hilited.box().style.background='#'+this.colorhilite;
          this.last_hilited=this.hilited;
        }
        else{
          this.moveto.id='';
          this.moveto.pos=-1;
          if(typeof(this.last_hilited)=='object' && this.last_hilited!=null){
            this.last_hilited=null;
          }
        }
      }
    }
  }

  function move(){
    this.moving=true;
    window.CMenus['menu'+this.selected.type].hide();
  }

  function navigate(action, id){
    var legalpos=true;
    rootids=this.rootid.split('.');
    uri=this.uri+'?tree_action='+action+'&tree_id='+id+'&rootobjid='+rootids[0]+'&rootsubid='+rootids[1];
    if(typeof(this.selected)=='object' && this.moving){
      uri=uri+'&tree_selected='+this.selected.id;
    }
    if(this.hook==true){
      //keep the hook status when navigating in tree
      uri=uri+'&tree_hook=true';
    }
    //make sure that the tree stay in tha same position, and don't jump to the top of the page
    if(!typeof(window.pageXOffset)=='undefined'){
      uri=uri+'&tree_XOffset='+window.pageXOffset+'&tree_YOffset='+window.pageYOffset;
    }
    else{
     uri=uri+'&tree_XOffset='+document.body.scrollLeft+'&tree_YOffset='+document.body.scrollTop;
    }
    
    if(action=='close' && typeof(this.selected)=='object'){
      //make sure that you dont close the branch with the selected object
      //might be a problem with remembering the values of the selected object if this is removed.
      digg=this.selected;
      while(digg.id!=this.rootid && digg.id!=id){
        digg=this.objects[this.id_line[digg.parent]];
      }
      if(digg.id!=this.rootid)legalpos=false;
    }
    if(legalpos){
      document.location=uri;
    }
  }

  function scroll(x, y){
    window.scrollTo(x, y);
  }

  function execute(action, type){
    var ok=true;

    if(typeof(this.actions[action].warning)=='string'){
      ok=confirm(this.actions[action].warning);
    }
    if(ok){
      if (typeof(this.actions[action].uri)=='string')uri=this.actions[action].uri;
      else uri=this.uri;
      uri=uri+'?tree_action='+action+'&tree_id='+this.selected.id;
      if(this.moveto.id!='')uri=uri+'&tree_toid='+this.moveto.id;
      if(this.moveto.pos!=-1)uri=uri+'&tree_topos='+this.moveto.pos;
      if(typeof(type)!='undefined')uri=uri+'&tree_type='+type;
      if(typeof(this.actions[action].target)=='string'){
        top.frames[this.actions[action].target].document.location=uri;
      }
      else{
        if(!typeof(window.pageXOffset)=='undefined'){
          uri=uri+'&tree_XOffset='+window.pageXOffset+'&tree_YOffset='+window.pageYOffset;
        }
        else{
         uri=uri+'&tree_XOffset='+document.body.scrollLeft+'&tree_YOffset='+document.body.scrollTop;
        }
        document.location=uri;
      }
    }

  }

  function drHook(tree, id, type, name ) {
  	if( typeof top.mainframe.editframe != "undefined" ) {
      if( typeof top.mainframe.editframe.hookUp != "undefined" ) {
        tree.hook = false;
        top.mainframe.editframe.hookUp( id, name, type );
      }
    }
  }


  function movemenu(evtemp){
    if(this.moveto.id=='' && this.moving){
      this.moving=false;
    }
    if(this.moveto.id!='' && this.moving){
      this.moving=false;
      if(this.selected.write) {
        CMenuPopUp('movemenu' + (this.selected.children ? 'r':''), evtemp);
      } else {
        CMenuPopUp('copymenu', evtemp);
      }
    }
  }

  this.move=move;
  this.add=add;
  this.select=select;
  this.activate=activate;
  this.deactivate=deactivate;
  this.highlight=highlight;
  this.movemenu=movemenu;
  this.execute=execute;
  this.addaction=addaction;
  this.addallowed=addallowed;
  this.cansel=cansel;
  this.setselected=setselected;
  //this.close=close;
  this.navigate=navigate;
  this.scroll=scroll;
}



function init(){
  return new tree();
}
