var Debugger = new Class({
	Implements: Options,
	options: {
		height: 200,
		minimized: true,
		remember: true
	},
	initialize: function(debug,profile,query,options){
		if(Browser.Engine.trident4) return;
		this.setOptions(options);
		if(this.options.remember) this.setDefaults();
		this.views = [];
		this.debug = debug;
		this.profile = profile;
		this.query = query;
		
		this.lastHeight = this.options.height;
		this.createDom();
		this.changeView(this.views[0]);
		this.minimized = this.options.minimized;
		
		this.zDebugger.injectInside(document.body);
		this.toolbarHeight = this.toolbar.getSize().y;
		if(this.minimized) this.minimize();
		else this.setLogHeight();
		this.flush();
		this.makeResize();
		this.initKeyToggler();
		this.initDblClickToggler();
	},
	setDefaults: function(){
		var height = Cookie.read('zDebuggerHeight');
		if(height) this.options.height = height.toInt();
		
		var status = Cookie.read('zDebuggerStatus');
		if(status) {
			if(status=='expanded') this.options.minimized = false;
			if(status=='collapsed') this.options.minimized = true;
		}
	},
	toggleDebugger: function(){
		if(!this.minimized) this.minimize();
		else this.restore();
	},
	changeView: function(curView){
		this.views.each(function(view){
			if(view == curView){
				eval("this.btn"+view+".addClass('active');");
				eval("this.log"+view+".removeClass('hide');");
			}
			else {
				eval("this.btn"+view+".removeClass('active');");
				eval("this.log"+view+".addClass('hide');");
			}
		}.bind(this));
		if(this.minimized) this.restore();
	},
	minimize: function(){
		this.zDebugger.setStyle('height',this.toolbarHeight -1).addClass('minimized');
		this.minimized = true;
		this.setLogHeight();
		if(this.options.remember) Cookie.write('zDebuggerStatus','collapsed',{duration: 30, path: '/'});
	},
	restore: function(){
		this.zDebugger.setStyle('height',this.lastHeight).removeClass('minimized');
		this.setLogHeight();
		this.minimized = false;
		this.log.focus();
		if(this.options.remember) Cookie.write('zDebuggerStatus','expanded',{duration:30, path: '/'});
	},
	createDom: function(){
		this.zDebugger = new Element('div',{
			'id' : 'zDebugger',
			'styles' : {
				'height' : this.lastHeight
			}
		});
		var toolbarHtml = '<span class="title">zDebugger</span>';
		this.toolbar = new Element('div',{'class' : 'toolbar'}).set('html',toolbarHtml).injectInside(this.zDebugger);
		this.btnToggle = new Element('a',{
			'class':'btnToggle',
			'title':'toggle (CTRL+Z)',
			'events':{'click':function(e){new Event(e).stop();this.toggleDebugger();}.bindWithEvent(this)}
		}).injectTop(this.toolbar);
		this.log = new Element('div',{'class':'log'}).injectInside(this.zDebugger);
		if(this.debug){
			this.views.push('Debug');
			this.logDebug = new Element('ul',{'class':'debug hide'}).injectInside(this.log);
			this.btnDebug = new Element('a',{
				'class':'btnMenu',
				'events':{'click':function(ev){ev.stop();this.changeView('Debug');}.bindWithEvent(this)}
			}).set('text','Debug').injectInside(this.toolbar);
		}
		if(this.profile){
			this.views.push('Profile');
			this.logProfile = new Element('ul',{'class':'profile hide'}).injectInside(this.log);
			this.btnProfile = new Element('a',{
				'class':'btnMenu',
				'events':{'click':function(ev){ev.stop();this.changeView('Profile');}.bindWithEvent(this)}
			}).set('text','Profile').injectInside(this.toolbar);
		}
		if(this.query){
			this.views.push('Query');
			this.logQuery = new Element('ul',{'class':'profile hide'}).injectInside(this.log);
			this.btnQuery = new Element('a',{
				'class':'btnMenu',
				'events':{'click':function(ev){ev.stop();this.changeView('Query');}.bindWithEvent(this)}
			}).set('text','Query').injectInside(this.toolbar);
		}
	},
	flush: function(){
		if(this.debug){
			var str = '';
			this.debug.each(function(msg){
				for(prop in msg) str+= '<li>' + prop + ': ' + msg[prop] + '</li>';
			});
			this.logDebug.set('html',str);
		}
		if(this.profile){
			var highest = 0;
			var sum = 0;
			var runtime = 0;
			var width = 0;
			var str = '';
			this.profile.each(function(msg){
				runtime = Number($H(msg).getValues()[0]);
				highest = Math.max(highest,runtime);
				sum += runtime;
			});
			this.profile.each(function(msg){
				for(prop in msg) {
					width = Math.round((msg[prop]/highest)*100);
					str+= '<li><div class=\"text\"><div class=\"runtime\">' + msg[prop] + ' sec.</div>' + prop + '</div><div class=\"bar\" style="width:'+width+'%"></div></li>';
				}
			});
			str+= '<li><div class=\"text\"><div class=\"runtime\"><strong>'+Math.round(sum*10000)/10000+' sec.</strong></div><strong>Summe</strong></div>&nbsp;</li>';
			this.logProfile.set('html',str);
		}
		if(this.query){
			var highest = 0;
			var sum = 0;
			var runtime = 0;
			var width = 0;
			var str = '';
			this.query.each(function(queryObj){
				runtime = Number(queryObj.runtime);
				highest = Math.max(highest,runtime);
				sum += runtime;
			});
			this.query.each(function(queryObj){
				width = Math.round((queryObj.runtime/highest)*100);
				str += '<li><div class=\"text\"><div class=\"runtime\">' + queryObj.runtime + ' sec.</div>' + queryObj.id + ': '  + queryObj.sql + '</div><div class=\"bar\" style="width:'+width+'%"></div></li>';
				/*
				for(prop in msg) {
					width = Math.round((msg[prop]/highest)*100);
					str+= '<li><div class=\"text\"><div class=\"runtime\">' + msg[prop] + ' sec.</div>' + prop + '</div><div class=\"bar\" style="width:'+width+'%"></div></li>';
				}
				*/
			});
			str+= '<li><div class=\"text\"><div class=\"runtime\"><strong>'+Math.round(sum*10000)/10000+' sec.</strong></div><strong>Summe</strong></div>&nbsp;</li>';
			this.logQuery.set('html',str);
		}
	},
	setLogHeight: function(){
		var height = Math.max(this.zDebugger.getSize().y - this.toolbarHeight,0);
		this.log.setStyle('height',height);
	},
	makeResize: function(){
		this.zDebugger.makeResizable({
			handle:this.toolbar,
			modifiers: {x:false,y:'height'},
			limit: {y:[19,1000]},
			invert: true,
			onComplete: function(el){
				if(el.getSize().y <= this.toolbarHeight) this.minimize();
				else {
					this.lastHeight = el.getSize().y;
					Cookie.write('zDebuggerHeight',this.lastHeight,{duration:30, path: '/'});
					this.restore();
				}
			}.bind(this)
		});
		this.log.makeResizable({
			handle:this.toolbar,
			modifiers: {x:false,y:'height'},
			invert: true
		});
	},
	initKeyToggler: function(){
		document.addEvent('keydown',function(ev){
			if(ev.control && ev.shift && ev.key == 'z'){
				new Event(ev).stop();
				this.toggleDebugger();
			}
		}.bindWithEvent(this));
	},
	initDblClickToggler: function(){
		this.toolbar.addEvent('dblclick',this.toggleDebugger.bind(this));
	},
	
	clear: function(){
		this.log.empty();
	}
});
