var xwindow = new __xw_window();

function __xw_window(){
   this.document = new __xw_document();
   this.cursor = new __xw_cursor();
   this.screen = new __xw_screen();
   this.event = __xw_convert_event;
   this.init = __xw_init;
}

function __xw_convert_event(e){
   return new __xw_event(e);
}

function __xw_init(){
   __xw_create_object_array();   
   __xw_checkfocus();
  
   if(!self.document.all)
      self.document.captureEvents(Event.MOUSEMOVE);
	  
   self.document.__xw_mousemove = self.document.onmousemove;
   self.document.onmousemove = __xw_mousemove;
}

function __xw_mousemove(e){   
   var xevent = new __xw_event(e);   
   
   xwindow.cursor.X = xevent.X;
   xwindow.cursor.Y = xevent.Y;
   xwindow.cursor.screenX = xevent.screenX;
   xwindow.cursor.screenY = xevent.screenY;
   
   if(self.document.__xw_mousemove)
      self.document.__xw_mousemove(e);
}

function __xw_screen(){
   this.getTop = __xw_gettop;
   this.getLeft = __xw_getleft;
   this.setTop = __xw_settop;
   this.setLeft = __xw_setleft;
   this.getWidth = __xw_getwidth;
   this.getHeight = __xw_getheight;
}

function __xw_document(){
   this.dhtml = new Array();
   this.getElementById = __xw_getElementById;
   this.hasFocus = "";
   this.lostFocus = "";
   this.focusedElement = __xw_focused_element;
   this.bluredElement = __xw_blured_element;
}

function __xw_focused_element(){
   if(xwindow.document.hasFocus != "")
      return xwindow.document.getElementById(xwindow.document.hasFocus);
   else
      return null;
}

function __xw_blured_element(){  
   if(xwindow.document.lostFocus != "")
      return xwindow.document.getElementById(xwindow.document.lostFocus);
   else
      return null;
}

function __xw_blank(e){}

function __xw_cursor(){
   this.X = 0;
   this.Y = 0;
   this.screenX = 0;
   this.screenY = 0;
}

function __xw_blank(e){}

function __xw_getwidth(){
    if(self.document.all){
        return document.body.clientWidth;
    }
    else if(self.document.layers){
        return window.innerWidth - 18;
    }
    else{
        return null;
    }
}

function __xw_getheight(){
    if(self.document.all){
        return document.body.clientHeight;
    }
    else if(self.document.layers){
        return window.innerHeight - 18;
    }
    else{
        return null;
    }
}

function __xw_gettop(){
   if(self.document.layers){
      return window.pageYOffset;
   }
   else{
      return self.document.body.scrollTop;
   }
}

function __xw_getleft(){
   if(self.document.layers){
      return window.pageXOffset;
   }
   else{
      return self.document.body.scrollLeft;
   }
}

function __xw_settop(t){
   if(self.document.layers){
      window.scrollTo(xwindow.screen.getTop(), t);
   }
   else{
      self.document.body.scrollTop = t;
   }
}

function __xw_setleft(l){
   if(self.document.layers){
      window.scrollTo(l, xwindow.screen.getLeft());
   }
   else{
      self.document.body.scrollTop = l;
   }
}

function __xw_getElementById(n, d){
   var p, i, x;  
   if(!d) 
      d = self.document; 
   if((p = n.indexOf("?")) > 0 && parent.frames.length){
      d = parent.frames[n.substring(p+1)].document; 
	  n = n.substring(0, p);
   }
   if(!(x = d[n]) && d.all) 
      x = d.all[n]; 
   for(i = 0; !x && i < d.forms.length; i++) 
      x = d.forms[i][n];
   for(i = 0; !x && d.layers && i < d.layers.length; i++) 
      x = __xw_getElementById(n, d.layers[i].document);
   if(!x && document.getElementById) 
      x = document.getElementById(n); 
   return x;
}

function __xw_checkfocus(){
   var d = self.document;
   for(var i = 0; i < d.forms.length; i++){
      var formObj = d.forms[i];
	  for(var x = 0; x < formObj.elements.length; x++){
	     formObj.elements[x].__xw_focus = formObj.elements[x].onfocus;
		 formObj.elements[x].onfocus = __xw_isfocused;
		 formObj.elements[x].__xw_blur = formObj.elements[x].onblur;
	     formObj.elements[x].onblur = __xw_notfocused;
	  }
   }
}

function __xw_isfocused(e){
   if(this.__xw_focus)
      this.__xw_focus(e);
   xwindow.document.hasFocus = this.name;   
}

function __xw_notfocused(e){
   if(this.__xw_blur)
      this.__xw_blur(e);
   xwindow.document.hasFocus = "";
   xwindow.document.lostFocus = this.name;
}

function __xw_event(e){
   if(self.document.all){ 
      this.X = event.clientX + document.body.scrollLeft;
      this.Y = event.clientY + document.body.scrollTop;
	  this.type = event.type;
	  this.keyCode = event.keyCode;
	  this.element = event.srcElement;
	  this.shiftKey = 0;
	  this.altKey = 0;
	  this.ctrlKey = 0;
	  if(event.shiftKey == 1)
	     this.shiftKey = 1;
	  if(event.altKey == 1)
	     this.altKey = 1;
	  if(event.ctrlKey == 1)
	     this.ctrlKey = 1;  
	  if(event.button == 1)
	     this.button = 1;
	  else
	     this.button = 2;
   } 
   else{  
      this.X = e.pageX;
      this.Y = e.pageY;
	  this.type = e.type;
	  this.keyCode = 0;
	  if(e.which > 3)
	     this.keyCode = e.which;
	  this.element = e.target;
	  this.shiftKey = 0;
	  this.ctrKey = 0;
	  this.altKey = 0;
	  if(e.modifier == "SHIFT")
	     this.shiftKey = 1;
	  if(e.modifier == "CTRL")
	     this.ctrKey = 1;
	  if(e.modifier == "ALT")
	     this.altKey = 1;
		 
	  if(e.which == 1)
	     this.button = 1;
	  else
	     this.button = 2;
   } 
   if(this.X < 0)
      this.X = 0;
   if(this.Y < 0)
      this.Y = 0;
}

function __xw_create_object_array(){
    var div_tags
    var span_tags
    var css_tags

    if(self.document.getElementById){
        div_tags = self.document.getElementsByTagName("div")

        for (var counter = 0; counter < div_tags.length; counter++){
            current_object = div_tags[counter]
            object_css = current_object.style
            object_id = current_object.id
            if(object_id){
                xwindow.document.dhtml[object_id] = new __xw_dhtml_object(current_object,
                                                            object_css, 
                                                            object_id)
            }
        }
        
        span_tags = self.document.getElementsByTagName("span")
        
        for (var counter = 0; counter < span_tags.length; counter++){
            current_object = span_tags[counter]
            object_css = current_object.style
            object_id = current_object.id
            if(object_id){
                xwindow.document.dhtml[object_id] = new __xw_dhtml_object(current_object,
                                                            object_css, 
                                                            object_id)
            }
        }
    }
    
    else if(self.document.all){
        div_tags = self.document.all.tags("div")
        for (var counter = 0; counter < div_tags.length; counter++){
            current_object = div_tags[counter]
            object_css = current_object.style
            object_id = current_object.id
            if(object_id){
                xwindow.document.dhtml[object_id] = new __xw_dhtml_object(current_object,
                                                            object_css, 
                                                            object_id)
            }
        }
        span_tags = self.document.all.tags("span")
        for (var counter = 0; counter < span_tags.length; counter++){
            current_object = span_tags[counter]
            object_css = current_object.style
            object_id = current_object.id
            if(object_id){
                xwindow.document.dhtml[object_id] = new __xw_dhtml_object(current_object,
                                                            object_css, 
                                                            object_id)
            }
        }
    }
    else if(self.document.layers){
        css_tags = self.document.layers
        for (var counter = 0; counter < css_tags.length; counter++){
            current_object = css_tags[counter]
            object_css = current_object
            object_id = current_object.id
            if(object_id){
                xwindow.document.dhtml[object_id] = new __xw_dhtml_object(current_object,
                                                            object_css, 
                                                            object_id)
            }
        }
    }
}

function __xw_dhtml_object (obj, css, id){
    this.obj = obj;
    this.css = css;
    this.id = id;
    this.left = __xw_get_left;
    this.right = __xw_get_right;
    this.top = __xw_get_top;
    this.bottom = __xw_get_bottom;
    this.width = __xw_get_width;
    this.height = __xw_get_height;
    this.visibility = __xw_get_visibility;
    this.zIndex = __xw_get_zIndex;
    this.moveTo = __xw_move_to;
    this.moveBy = __xw_move_by;
    this.setLeft = __xw_set_left;
    this.setTop = __xw_set_top;
    this.setWidth = __xw_set_width;
    this.setHeight = __xw_set_height;
    this.setVisibility = __xw_set_visibility;
    this.setZIndex = __xw_set_zIndex;
    this.moveAbove = __xw_move_above;
    this.moveBelow = __xw_move_below;
    this.setBackgroundColor = __xw_set_backgroundColor;
    this.setBackgroundImage = __xw_set_backgroundImage;
    this.setHtml = __xw_set_html;
    this.getClipTop = __xw_get_clip_top;
    this.getClipRight = __xw_get_clip_right;
    this.getClipBottom = __xw_get_clip_bottom;
    this.getClipLeft = __xw_get_clip_left;
    this.getClipWidth = __xw_get_clip_width;
    this.getClipHeight = __xw_get_clip_height;
    this.resizeClipTo = __xw_resize_clip_to;
    this.resizeClipBy = __xw_resize_clip_by;
}

function __xw_get_left(){
    return parseInt(this.css.left);
}

function __xw_get_right(){
    return this.left() + this.width();
}

function __xw_get_top(){
    return parseInt(this.css.top);
}

function __xw_get_bottom(){
    return this.top() + this.height();
}

function __xw_get_width(){
    if(!self.document.layers){
        if(this.css.width){
            return parseInt(this.css.width);
        }
        else{
           return parseInt(this.obj.offsetWidth);
       }
    }
    else{
        return parseInt(this.obj.document.width);
    }
}

function __xw_get_height(){
    if(!self.document.layers){
        if(this.css.height){
            return parseInt(this.css.height);
        }
        else{
           return parseInt(this.obj.offsetHeight);
       }
    }
    else{
        return parseInt(this.obj.document.height);
    }
}

function __xw_get_visibility(){
    if(!self.document.layers){
        if(this.css.visibility){
            return this.css.visibility;
        }
    }
    else{
        if(this.css.visibility == "show"){
            return "visible";
        }
        if(this.css.visibility == "hide"){
            return "hidden";
        }
    }
    return "inherit";
}

function __xw_get_zIndex(){
    return this.css.zIndex;
}

function __xw_move_to (new_left, new_top){
    this.css.left = new_left;
    this.css.top = new_top;
}

function __xw_move_by (delta_left, delta_top){
    this.css.left = this.left() + parseInt(delta_left);
    this.css.top = this.top() + parseInt(delta_top);
}

function __xw_set_left (new_left){
    this.css.left = new_left;
}

function __xw_set_top (new_top){
    this.css.top = new_top;
}

function __xw_set_width (new_width){
    if(!self.document.layers){
        this.css.width = new_width;
    }
}

function __xw_set_height (new_height){
    if(!self.document.layers){
        this.css.height = new_height;
    }
}

function __xw_set_visibility (new_visibility){
    if(!self.document.layers){
        this.css.visibility = new_visibility;
    }
    else{
        if(new_visibility == "visible"){
            this.css.visibility = "show";
        }
        else if(new_visibility == "hidden"){
            this.css.visibility = "hide";
        }
        else{
            this.css.visibility = "inherit";
        }
    }
}

function __xw_set_zIndex(new_zindex){
    if(new_zindex > 0){
        this.css.zIndex = new_zindex;
    }
    else{
        this.css.zIndex = 0;
    }
}

function __xw_move_above(reference_object){
    this.css.zIndex = reference_object.css.zIndex + 1;
}

function __xw_move_below(reference_object){
    reference_zindex = reference_object.css.zIndex;
    if(reference_zindex > 0){
        this.css.zIndex =  reference_zindex - 1;
    }
    else{
        reference_object.css.zIndex = 1;
        this.css.zIndex = 0;
    }
}

function __xw_set_backgroundColor(new_color){
    if(!self.document.layers){
        this.css.backgroundColor = new_color;
    }
    else{
        this.css.bgColor = new_color;
    }
}

function __xw_set_backgroundImage(new_image){
    if(!self.document.layers){
        this.css.backgroundImage = "url(" + new_image + ")";
    }
    else{
        this.css.background.src = new_image;
    }
}

function __xw_set_html(new_html){
    if(!self.document.layers){
        this.obj.innerHTML = new_html;
    }
    else{
        this.obj.document.open();
        this.obj.document.write(new_html);
        this.obj.document.close();
    }
}

function __xw_get_clip_top(){
    if(!self.document.layers){
        __xw_parse_dom_clip(this);
        return __xw_current_clip.top;
    }
    else{
        return this.css.clip.top;
    }
}

var __xw_current_clip;

function __xw_clip_object(top, right, bottom, left){
    this.top = top;
    this.right = right;
    this.bottom = bottom;
    this.left = left;
}

function __xw_parse_dom_clip(current_object){
    clip_string = current_object.css.clip;
    
    if(clip_string.length > 0){
        var values_string = clip_string.slice(5, clip_string.length - 1);
        var clip_values = values_string.split(" ");
        var clip_top = parseInt(clip_values[0]);
        var clip_right = parseInt(clip_values[1]);
        var clip_bottom = parseInt(clip_values[2]);
        var clip_left = parseInt(clip_values[3]);
    }
    else{
        var clip_top = 0;
        var clip_right = current_object.width();
        var clip_bottom = current_object.height();
        var clip_left = 0;
    }
    __xw_current_clip = new __xw_clip_object(clip_top, clip_right, clip_bottom, clip_left);
}

function __xw_get_clip_right(){
    if(!self.document.layers){
        __xw_parse_dom_clip(this);
        return __xw_current_clip.right;
    }
    else{
        return this.css.clip.right;
    }
}

function __xw_get_clip_bottom(){
    if(!self.document.layers){
        __xw_parse_dom_clip(this);
        return __xw_current_clip.bottom;
    }
    else{
        return this.css.clip.bottom;
    }
}

function __xw_get_clip_left(){
    if(!self.document.layers){
        __xw_parse_dom_clip(this);
        return __xw_current_clip.left;
    }
    else{
        return this.css.clip.left;
    }
}

function __xw_get_clip_width(){
    if(!self.document.layers){
        __xw_parse_dom_clip(this);
        return __xw_current_clip.right - __xw_current_clip.left;
    }
    else{
        return this.css.clip.width;
    }
}

function __xw_get_clip_height(){
    if(!self.document.layers){
        __xw_parse_dom_clip(this);
        return __xw_current_clip.bottom - __xw_current_clip.top;
    }
    else{
        return this.css.clip.height;
    }
}

function __xw_resize_clip_to(new_top, new_right, new_bottom, new_left){
    if(new_top == "auto"){new_top = this.get_clip_top(); }
    if(new_right == "auto"){new_right = this.get_clip_right(); }
    if(new_bottom == "auto"){new_bottom = this.get_clip_bottom(); }
    if(new_left == "auto"){new_left = this.get_clip_left(); }

    if(!self.document.layers){
        this.css.clip = "rect(" + new_top + " " + new_right + " " + 
                                  new_bottom + " " + new_left + ")";
    }
    else{
        this.css.clip.top = new_top;
        this.css.clip.right = new_right;
        this.css.clip.bottom = new_bottom;
        this.css.clip.left = new_left;
    }
}

function __xw_resize_clip_by(delta_top, delta_right, delta_bottom, delta_left){
    var new_top = this.get_clip_top() + delta_top;
    var new_right = this.get_clip_right() + delta_right;
    var new_bottom = this.get_clip_bottom() + delta_bottom;
    var new_left = this.get_clip_left() + delta_left;

    if(!self.document.layers){
        this.css.clip = "rect(" + new_top + " " + new_right + " " + 
                                  new_bottom + " " + new_left + ")";
    }
    else{
        this.css.clip.top = new_top;
        this.css.clip.right = new_right;
        this.css.clip.bottom = new_bottom;
        this.css.clip.left = new_left;
    }
}
