/* @ requires
newElement.js
misc.js
*/

var Debug = {
  blnDrag : false,
  divOuter : '',
  divContent : '',
  initWidth : 300,
  initHeight : 120,
  
  setCoOrds : function(ev){
    var self = this;
    if (ev.button<=1){
      var pleft=self.divOuter.offsetLeft;
      var ptop=self.divOuter.offsetTop;
      
      var xcoor=ev.clientX;
      var ycoor=ev.clientY;
      
      document.onmousemove=function(e){
        var ev = e || window.event;
        if(self.blnDrag){
          var l = pleft+(ev.clientX-xcoor);
          var t = ptop+(ev.clientY-ycoor);
          self.divOuter.style.left = (l + 'px');
          self.divOuter.style.top = (t + 'px');
        }
      };
    }
  },
  
  construct : function(){
    var self=this, clrButton, closeButton, captionTable, tableDebug;
    
    with(clrButton = new Button()){
      with(style){
        height='20px';
        fontWeight='bold';
        fontSize='10px';
        fontFamily='verdana';
        color='#000000';
      }
    innerHTML = 'CLR';
    title = 'Clear Results';
    }
    
    clrButton.onclick = function(){
      misc.setInnerText(self.divContent, '');
    }
    
    with(closeButton=new Button()){
      with(style){
        height='20px';
        width='20px';
        fontWeight='bold';
        fontSize='10px';
        fontFamily='verdana';
        color='#000000';
      }
      innerHTML = 'X';
      title = 'Close';
    }
    
    closeButton.onclick = function(){
      misc.removeElement(self.divOuter);
      self.divOuter = '';
    }
    
    with(captionTable=new Table(1,2)){
      with(style){
        width = '100%';
        cursor = 'default';
      }
      border = '0';
      cellSpacing= '0';
      cellPadding= '0';
      
      with(rows[0]){
        style.height = '22px';
        with(cells[0]){
          innerHTML = '<div style="color:#FFFFFF; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold">&nbsp;JS Report</div>';
        }
        
        with(cells[1]){
          dir = 'rtl';
          appendChild(closeButton);
          appendChild(clrButton);
        }
      }
    }
    
    captionTable.onmousedown = function(e){
      this.style.cursor = 'move';
      var ev = e || window.event;
      self.blnDrag = true;
      self.setCoOrds(ev);
    };
    
    captionTable.onmouseup = function(e){
      this.style.cursor = 'default';
      self.blnDrag = false;
      document.onmousemove = '';
    };
    
    with(frame=new Table(3,1)){
      with(style){
        width = '100%';
        height = '100%';
      }
      border = '1px';
      borderColor = '#CCCCCC';
      cellSpacing= '0';
      cellPadding= '0';
      
      with(rows[0]){
        style.backgroundColor = '#006666';
        cells[0].appendChild(captionTable);
      }
      
      with(rows[1]){
        with(cells[0]){
          style.height='auto';
          vAlign = 'top';
          with(self.divContent=new Div()){
            with(style){
              color = '#000000';
              font = 'normal 11px Verdana, Arial, Helvetica, sans-serif';
              border = '1px solid black';
              padding = '2px';
              position = 'static';
              overflow = 'auto';
              background = '#EBE9ED';
              height = self.initHeight +'px';
            }
          }
          appendChild(self.divContent);
        }
      }
      
      with(rows[2]){
        style.backgroundColor = '#EBE9ED';
        with(cells[0]){
          style.height = '15px';
          var footerTable = new Table(1,2);
          with(footerTable){
            with(style){
              width = '100%';
              border = '0';
              cellSpacing = '0';
              cellPadding = '0';
              
              with(rows[0]){
                with(cells[0]){
                  align = 'left';
                  innerHTML = '<span style="color:#000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px">&nbsp;Done</span>';
                }
                
                with(cells[1]){
                  align = 'right';
                  var divResize = new Div();
                  with(divResize.style){
                    background = 'transparent url(\'imgs/resizeSE.gif\') top right no-repeat';
                    //float = 'right';
                    width = '11px'; /*width of resize image*/
                    height = '20px'; /*height of resize image*/
                    cursor = 'nw-resize';
                    fontSize = '0';
                  }
                  
                  divResize.onmousedown = function(e){
                    var ev = e || window.event;
                    if (ev.button<=1){
                      var applySize = function(obj){
                        self.divContent.style.height = obj.h-52;
                        //self.Content.style.width = (obj.w-(_isIE?0 :10));
                        self.divOuter.style.width = obj.w;
                        applySize = '';
                      };
                      misc.Resize(applySize, self.divOuter, ev, 150, 250);
                    }
                  }
                  appendChild(divResize);
                }
              }
            }
          }
          appendChild(footerTable);
        }
      }
    }
    
    with(self.divOuter=new Div('debugger')){
      with(style){
        width= self.initWidth +'px';
        height='auto';
        backgroundColor='white';
        position='absolute';
        //overflow='hidden';
        cursor='default';
        top = '150px';
        left = ((screen.width-self.initWidth)/2)+'px';
      }
      appendChild(frame);
    }
    document.body.appendChild(self.divOuter);
  },
  
  print : function(txt_AS_String, clear_AS_Boolean){
    var self = this;
    if(!self.divOuter)self.construct();
    var txt = txt_AS_String;
    
    //if(!clear_AS_Boolean)misc.setInnerText(self.divContent, txt);
    //else misc.setInnerText(self.divContent, (misc.getInnerText(self.divContent) +'\n'+ txt));
    
    if(!clear_AS_Boolean)self.divContent.innerHTML = (txt +'<br />');
    else self.divContent.innerHTML += (txt +'<br />');
  },
  
  text : function(txt_AS_String, clear_AS_Boolean){
    var self = this;
    if(!self.divOuter)self.construct();
    var txt = txt_AS_String;
    
    //if(!clear_AS_Boolean)misc.setInnerText(self.divContent, txt);
    //else misc.setInnerText(self.divContent, (misc.getInnerText(self.divContent) +'\n'+ txt));
    
    if(!clear_AS_Boolean){
      misc.setInnerText(self.divContent, txt);
    }
    else misc.setInnerText(self.divContent, misc.getInnerText(self.divContent)+'\n'+txt);
  }
};
