/*
	Debug function
*/

var dbg = {
	className: "jsdebugpanel",
	id: "debug",
	version: "2.0",
	
	init: function() {
		
		var body 		= $$("body")[0];
		if(!body) return;
		
		this.element 	= new Element("div", {"class": this.className, id:this.id});
		this.element.setStyle({position:"absolute"});
		body.insert(this.element);

		//title bar
		this.title = new Element("div", {"class": "debugTitle"}).update("<h4>JS Debug</h4>");
		this.element.insert(this.title);

		//widget (button) holder
		this.widgets = new Element("div", {"class": "widgets"});
		this.title.insert(this.widgets);
	
	
		this.content = new Element("div", {"class": "debugContent"}).update("<p>Javascript debugger panel version " + this.version + "</p>");
		this.element.insert(this.content);

		//Check to see if we have cookieLib
		this.cookieLibAvailable = true;
		if(typeof cookieLib == 'undefined' || typeof Scriptaculous == 'undefined'){
			dbg.p('cookieLib OR Scriptaculous not found');
			this.cookieLibAvailable = false;
		}
		if(this.cookieLibAvailable){
			new Draggable('debug',{revert:dbg.storePos, handle:this.title, starteffect:null, reverteffect:null, endeffect:null}); //scriptaculous
			var posData = cookieLib.get("debugposdata");
			if(posData){
				//new Effect.Move(oDebug,{ x: posData.split(",")[0], y: posData.split(",")[1], mode: 'absolute',starteffect:null,reverteffect:null,endeffect:null});
				//dbg.h('posData : ' + posData);
				this.element.setStyle({left:posData.split(",")[0] + "px"});
				this.element.setStyle({top:posData.split(",")[1] + "px"});
			}
		}
		//add clear button
		this.widgets.insert(this.btnCreate('min', this.minim, 'min', 'min'));
		this.widgets.insert(this.btnCreate('clr', this.clr, 'clr', 'clr'));

		//dbg.clr();
	},

	btnCreate: function(id, callback, labelText, className) {
		//create a button
		var btn = new Element("button", {"class": className, "type":"button"}).update(labelText);
		btn.observe("click", callback.bind(this));
		return btn;	
	},

	h: function(str){
		//bold header
		if(this.content) this.content.insert(new Element("h2").update(str));
	},
	p: function(str){
		//paragraph
		if(this.content) this.content.insert(new Element("p").update(str));
	},
	
	s: function(str){
		//string
		if(!this.content) return false; 
		var clen = this.content.childElements().length;
		if(clen > 0)
			this.content.childElements()[clen - 1].innerHTML += str;
		else
			this.content.insert(new Element("p").update(str));
	},
	minim: function(){
		//Minimise
		Element.toggle(this.content);	
	},
	clr: function(){
		//Clear
		this.content.update();	
	},
	storePos: function(){
		//dbg.h('storePos');
		//store current position in a cookie for later recall
		//Keeps the debug panel where you left it!
		if(this.cookieLibAvailable){
			cookieLib.set('debugposdata',Position.cumulativeOffset(this.element).toString());

		}
	}
};

//Event.observe(window, "load", function(){dbg.init();}); //start the func on page load
