
////////////////////////////////////////////////////////////////////////
// cwong 2008: creates a mini debug console on the bottom right of the screen
//
function debugConsole(_title, _color, _parentObj) {
        
            var _self = this;

            this.isIE = (document.all)?true:false;
            this.INNER_TXT  = (this.isIE)?"innerText":"innerHTML";
            var floatPpty   = (this.isIE)?"styleFloat":"cssFloat";
            
            if(_title) this.title = _title;
            else this.title = 'untitled';
        
            var div = this.createDiv("", this.mainStyle);
                div.id          = "debugObjectDiv";
                div.className   = "debugObjectDiv";
                
            this.id = div.id;
            this.container = div;

            var button = document.createElement("div");
                button.innerHTML = "-";
                button.style.cursor = "pointer";                         
                button.style[floatPpty] = "right";
                button.style.marginLeft= "10px";
                button.onclick = function(){_self.expandCollapse()};
                
            var deleteDiv = document.createElement("div");
                deleteDiv.innerHTML = "x";
                deleteDiv.style.cursor = "pointer";                         
                deleteDiv.style[floatPpty] = "right";
                deleteDiv.style.marginLeft= "10px";
                deleteDiv.onclick = function(){_self.hide()};                
                
            var clear = document.createElement("div");
                clear.innerHTML = "clear";
                clear.style.fontWeight = "normal";      
                clear.style.cursor = "pointer";                          
                clear.style[floatPpty] = "left";
                clear.onclick = function(){_self.clear()};
                
            var selectAll = document.createElement("div");
                selectAll.innerHTML = "copy";
                selectAll.style.fontWeight = "normal";
                selectAll.style.marginRight= "10px";
                selectAll.style.cursor = "pointer";                
                selectAll.style[floatPpty] = "left";
                selectAll.onclick = function(){_self.copy()};
            
            var dummyDiv = document.createElement("div");
                dummyDiv.innerHTML = "<!-- -->";
                dummyDiv.style.clear = "both";

            var titleDiv = document.createElement("div");
                titleDiv.innerHTML = this.title;
                titleDiv.style.marginRight= "10px";
                titleDiv.style[floatPpty] = "left";
                
            var color = (_color)?_color:"#999";

            var h6 = document.createElement("h6");
                h6.style.margin = "0";
                h6.style.backgroundColor = color;
                h6.style.color = "white";
                h6.style.borderBottom = "1px solid black";
                h6.style.padding = "3px";                
                
                
                h6.appendChild(titleDiv);
                h6.appendChild(selectAll);
                h6.appendChild(clear);
                
                h6.appendChild(deleteDiv);                 
                h6.appendChild(button);
 
                h6.appendChild(dummyDiv);
            
            var content = document.createElement("div");
                content.style.overflowY = "scroll";
                content.style.height = "300px";
                content.style.display = "block";

            div.appendChild(h6);
            div.appendChild(content);
            
            this.divContent = content;
            this.expandCollapseButton = button;
        
            var parentObj = (_parentObj)?_parentObj:document.body; //inserts the debug div in the body element by default
                parentObj.appendChild(div);

            //closes the div by default
            this.collapse();
 
}           
debugConsole.prototype.FLAG1 = {backgroundColor:"red",color:"white"};
debugConsole.prototype.FLAG2 = {backgroundColor:"purple",color:"yellow"};
debugConsole.prototype.FLAG3 = "#ffffcc";
debugConsole.prototype.FLAG4 = "#ECFCEC";


debugConsole.prototype.mainStyle = {
    width           : "50%",
    position        : "absolute",
    right           : "0",
    bottom          : "0",
    backgroundColor : "#fcfcfc",
    border          : "1px solid #666",
    filter          : "alpha(opacity=85)"
}
debugConsole.prototype.boxStyle = {
    border          :"1px solid #ccc",
    borderBottom    :"1px solid #999",
    borderRight     :"1px solid #999",    
    backgroundColor :"#f0f0f0",
    margin          :"5px"
}
debugConsole.prototype.dateStyle = {
    backgroundColor :"#ccc",
    color           :"black",
    fontWeight      :"bold",
    textAlign       :"right",
    padding         :"2px"
}

debugConsole.prototype.expand = function() {
    this.divContent.style.display = "block";
    this.expandCollapseButton.innerHTML = "-";        

}

debugConsole.prototype.collapse = function() {
    this.divContent.style.display = "none";
    this.expandCollapseButton.innerHTML = "+";        
}

debugConsole.prototype.expandCollapse = function() {

    if(this.expandCollapseButton.innerHTML == "-") {
        this.divContent.style.display = "none";
        this.expandCollapseButton.innerHTML = "+";        
    } else {
         this.divContent.style.display = "block";
        this.expandCollapseButton.innerHTML = "-";        
    }
    

}
debugConsole.prototype.createDiv = function(obj,css) {

    var div = document.createElement("div");
        if(typeof obj == "string") div[this.INNER_TXT] = obj;
		else div.appendChild(obj);

    if(css) {
        for(var rule in css) {div.style[rule] = css[rule];}
    }

    return div;
}

debugConsole.prototype.write = function(content, style) {

    var bodyCss = new Object();
    
    if(style) {
        if(typeof style == "string") 
            bodyCss = {backgroundColor:style};
        else if(typeof style == "object") {
            bodyCss = style;
        }
            

    }
    bodyCss["padding"] = "5px";

    var now     = new Date();
    var nowStr  = now.getHours() + ":" + now.getMinutes() + ":" +  now.getSeconds();
    var dtStr   = now.getMonth()+"/"+now.getDate()+"/" +now.getFullYear();

    if(typeof content == "string" && !this.isIE) {
        content = content.replace(/\n/g,"<br/>");
    }

    var div   = this.createDiv("",this.boxStyle);
        div.appendChild(this.createDiv(dtStr + "-" + nowStr,this.dateStyle));
        div.appendChild(this.createDiv(content, bodyCss));


    this.divContent.appendChild(div);
    this.divContent.scrollTop = this.divContent.scrollHeight;

}
debugConsole.prototype.copy = function(content) {
      this.copy_clip(this.divContent[this.INNER_TXT]);
}

debugConsole.prototype.writeLn = function(content) {
    var flag = (flag)?flag:"";

    this.clear();
    this.write(content, flag);
}

debugConsole.prototype.clear = function(content) {
    this.divContent[this.INNER_TXT] = "";
}

debugConsole.prototype.hide = function() {
    var _self = this;
    var id= _self.id;
    
    if(confirm("hide debug console?")) {
        this.container.style.display = "none";
    }
}



// Copyright (C) krikkit - krikkit@gmx.net
// --> http://www.krikkit.net/
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
debugConsole.prototype.copy_clip = function (meintext) {
 if (window.clipboardData) 
   {
   
   // the IE-manier
   window.clipboardData.setData("Text", meintext);
   
   // waarschijnlijk niet de beste manier om Moz/NS te detecteren;
   // het is mij echter onbekend vanaf welke versie dit precies werkt:
   }
   else if (window.netscape) 
   { 
   
   // dit is belangrijk maar staat nergens duidelijk vermeld:
   // you have to sign the code to enable this, or see notes below 
   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
   
   // maak een interface naar het clipboard
   var clip = Components.classes['@mozilla.org/widget/clipboard;1']
                 .createInstance(Components.interfaces.nsIClipboard);
   if (!clip) return;
   
   // maak een transferable
   var trans = Components.classes['@mozilla.org/widget/transferable;1']
                  .createInstance(Components.interfaces.nsITransferable);
   if (!trans) return;
   
   // specificeer wat voor soort data we op willen halen; text in dit geval
   trans.addDataFlavor('text/unicode');
   
   // om de data uit de transferable te halen hebben we 2 nieuwe objecten 
   // nodig om het in op te slaan
   var str = new Object();
   var len = new Object();
   
   var str = Components.classes["@mozilla.org/supports-string;1"]
                .createInstance(Components.interfaces.nsISupportsString);
   
   var copytext=meintext;
   
   str.data=copytext;
   
   trans.setTransferData("text/unicode",str,copytext.length*2);
   
   var clipid=Components.interfaces.nsIClipboard;
   
   if (!clip) return false;
   
   clip.setData(trans,null,clipid.kGlobalClipboard);
   
   }
   alert("Data copied to clipboard.");
   return false;
}







