var JadeDebug = {
	_vars: function(){
		this.loaded=0;
		this.count=0;
		this.count_error=0;
		this.count_warn=0;
		this.aMessages = [];
		this.aLines = [];
	},
	_initial: function(){
		Jade.Css.Include(Jade.path+'css/debug.css');
		window.onerror = Jade.Debug.SystemError;
	},
	_auto: function(){
		Jade.Debug._make_container();
		Jade.Debug._make_loaded();
		Jade.Debug.loaded=1;
	},
	_make_container: function(){
		var documentTop = top.document;
		var body = documentTop.getElementsByTagName('body').item(0);
		if(body){
			if(typeof(this.divDebug)=="undefined"){
				var debug = documentTop.createElement('div');
				debug.id = 'jade_debug';
				debug.collapsed=false;
				body.appendChild(debug);
				this.divDebug=debug;
				
				var header = documentTop.createElement('div');
				header.id = 'jade_debug_header';
				debug.appendChild(header);
				this.header=header;
				
				var text = documentTop.createElement('div');
				text.className = 'text';
				header.appendChild(text);
				this.header.text = text;
				
				var close = documentTop.createElement('div');
				close.className = 'buttons';
				close.innerHTML = '<div class="text">Close</div><div class="close">';
				close.onclick=function() { this.parentNode.parentNode.style.display='none'; }
				header.appendChild(close);

				var min = documentTop.createElement('div');
				min.className = 'buttons';
				min.innerHTML = '<div class="text">Hide</div><div class="minimize">';
				min.onclick=function() {
					if(Jade.Debug.divDebug.collapsed){ Jade.Debug.expand(); }
					else{ Jade.Debug.collapse(); }
				}
				header.appendChild(min);
				header.min=min;
				
				var wrapper = documentTop.createElement('div');
				wrapper.className = 'wrapper';
				debug.appendChild(wrapper);
				
				var container = documentTop.createElement('div');
				container.id = 'jade_debug_container';
				wrapper.appendChild(container);
				this.container=container;
			}
		}
		else { alert ("Jade.Debug couldn't found '<body>' tag"); Jade.Debug.state=0; }
	},
	_make_loaded: function(){
		if(Jade.Debug.loaded==0){
			if(typeof(window.console)!="undefined" && typeof(window.console.firebug)!="undefined"){ Jade.Debug.collapse(); }
			if(!DEBUG_MODE){ this.collapse(); this.hide(); }
			for(var i=0; i<Jade.Debug.aMessages.length; i++){
				var element = Jade.Debug.aMessages[i];
				Jade.Debug._add(element.text, element.type, element.id);
			}
		}
	},
	collapse: function(){
		var text = this.header.min.childNodes[0];
		var icon = this.header.min.childNodes[1];
		text.innerHTML = 'Show';
		icon.className = icon.className.replace(/minimize/,'maximize');
		this.divDebug.className += 'collapsed';
		this.divDebug.collapsed=true;
	},
	expand: function(){
		var text = this.header.min.childNodes[0];
		var icon = this.header.min.childNodes[1];
		text.innerHTML = 'Hide';
		icon.className = icon.className.replace(/maximize/,'minimize');
		this.divDebug.className = Jade.Debug.divDebug.className.replace(/collapsed/,'');
		this.divDebug.collapsed=false;
	},
	hide: function(){
		this.divDebug.style.display='none';
	},
	show: function(){
		this.divDebug.style.display='';
	},
	
	Add: function (text,type, id){
		if(typeof(type)=="undefined"){
			type='error'; 
		}
		if(typeof(id)!="undefined" && typeof(this.aLines['C'+id])!="undefined"){
			Jade.Debug._edit('C'+id, text);
		}
		else{

		
			if(typeof(id)=="undefined"){ id=this.count; }
			else{ id='C'+id; }
			if(Jade.Debug.loaded==0){ Jade.Debug.aMessages.push({'text': text, 'type': type, 'id': id}); }
			else{ Jade.Debug._add(text, type, id); }
			
			this.count++;
			if(type=='error'){ this.count_error++; }
			if(type=='warn'){ this.count_warn++; }
		}

		return id;
	},
	_add: function(text, type, id){
		var line;
		if(type=='debug'){
			var strLen = text.length;
			var strPos = text.indexOf('\n');
			if(strPos!=-1){
				var strTitle = text.substring(0,strPos);
				var strBody = text.substring(strPos+1);
				this._add_debug(strTitle, strBody, id);
			}
			else{
				this._add_single(text,'debug', id);
			}
		}
		else{
			this._add_single(text, type, id);
		}
		if((type=='error' || type =='warn') && !DEBUG_MODE){ this.show(); this.expand(); }
		
		this.header.text.innerHTML = '<b>'+(this.count-1)+'</b> messages';
		this.header.text.innerHTML += (this.count_warn>0?('&nbsp;&nbsp;::&nbsp;&nbsp;<span class="warn"><b>You have '+this.count_warn+'</b> warnings</span>'):'');
		this.header.text.innerHTML += (this.count_error>0?('&nbsp;&nbsp;::&nbsp;&nbsp;<span class="error"><b>'+this.count_error+'</b> errors has occurred in the script on this page</span>'):'')
	},
	_add_single: function(text, type, id){
		var container = Jade.Debug.container;	
		
		var line = top.document.createElement('div');
		line.debug_id=id;
		line.className = 'line '+type;
		container.appendChild(line);
		
		if(type!='debug'){
			var icon = top.document.createElement('div');
			icon.className = 'icon';
			line.appendChild(icon);
		}
		
		var texto = top.document.createElement('div');
		texto.className = 'text';
		texto.innerHTML = text;
		line.appendChild(texto);
		
		line.oText=texto;

		this.aLines[id] = line;
	},
	_add_debug: function(title, text, id){
		var container = Jade.Debug.container;
		
		var line = top.document.createElement('div');
		line.debug_id=id;
		line.className = 'line collapsed';
		line.collapsed=true;
		container.appendChild(line);
		
		var arrow = top.document.createElement('div');
		arrow.className = 'arrow maximize';
		arrow.onclick=function(){
			var parent = this.parentNode;
			if(parent.collapsed){
				parent.className = parent.className.replace(/ collapsed/,'');
				this.className = this.className.replace(/maximize/,'minimize');
			}
			else{
				parent.className += ' collapsed';
				this.className = this.className.replace(/minimize/,'maximize');
			}
			parent.collapsed=!parent.collapsed;
		}
		line.appendChild(arrow);

		var divTitle = top.document.createElement('div');
		divTitle.className = 'title';
		divTitle.innerHTML = title;
		line.appendChild(divTitle);
		
		var divText = top.document.createElement('div');
		divText.className = 'content';
		divText.innerHTML = text;
		line.appendChild(divText);
		
		line.oTitle = divTitle;
		line.oText = divText;
		
		this.aLines[id] = line;
	},
	
	Edit: function(id, text){
		this._edit('C'+id, text);
	},
	_edit: function(id, text){
		if(Jade.Debug.loaded==0){
			for(var i=0; i<Jade.Debug.aMessages.length; i++){
				if(Jade.Debug.aMessages[i].id==id){ Jade.Debug.aMessages[i].text = text; }
			}
		}
		else{
			if(typeof(this.aLines[id])!="undefined"){ this.aLines[id].oText.innerHTML = text; }
		}
	},
	
	Title: function(id, title){
		if(this.loaded==0){
			for(var i=0; i<this.aMessages.length; i++){
				if(this.aMessages[i].id==id){ this.aMessages[i].text = title + this.aMessages[i].text.substring(this.aMessages[i].text.indexOf('\n')); }
			}
		}
		else{
			if(typeof(this.aLines[id])!="undefined"){ this.aLines[id].oTitle.innerHTML = title; }
		}
	},
	Content: function(id, text){
		if(Jade.Debug.loaded==0){
			for(var i=0; i<Jade.Debug.aMessages.length; i++){ 
				if(Jade.Debug.aMessages[i].id==id){ this.aMessages[i].text = this.aMessages[i].text.substring(0, this.aMessages[i].text.indexOf('\n')+1)+text; }
			}
		}
		else{
			if(typeof(this.aLines[id])!="undefined"){ this.aLines[id].oText.innerHTML = text; }
		}
	},
	AddContent: function(id, text){
		if(Jade.Debug.loaded==0){
			for(var i=0; i<Jade.Debug.aMessages.length; i++){
				if(Jade.Debug.aMessages[i].id==id){ Jade.Debug.aMessages[i].text += text; }
			}
		}
		else{
			if(typeof(this.aLines[id])!="undefined"){ this.aLines[id].oText.innerHTML += text; }
		}
	},
	
	SystemError: function(msg,url,line,chr){
		var text = '<b>Error: </b>'+msg;
		text += '<span class="right"><b>'+url+'</b>';
		text += (typeof(line)!="undefined"?' on line <b>'+line+'</b>':'');
		text += (typeof(chr)!="undefined"?' at char <b>'+chr+'</b>':'');
		text += '</span>';
		
		Jade.Debug.Add(text,'error');
		if(DEBUG_MODE){ return false; }
		else{ return true };
	}
}
Jade.LoadLibrary('Debug',JadeDebug);
Jade.Debug.Add('<b>Browser agent: </b> '+navigator.userAgent+(navigator.vendor!='' && navigator.vendor=='undefined'?' <b>Vendor:</b> '+navigator.vendor:'')+' <b>Platform: </b> '+navigator.platform,'info');