/**
 * Extends the debug object to use the prototype library - currently add dragable feature.
 *
 * @author David Spurr
 * @version 1.0 6th November 2005
 * @link http://www.defusion.org.uk/code/jsdebug/
 * @licence Creative Commons Attribution-ShareAlike 2.0 License (http://creativecommons.org/licenses/by-sa/2.0/)
 * @see debug.js
 */
if(typeof(Prototype) != 'undefined') {
	
	/**
	 * Over ride the notify method to use prototypes better handling of class names
	 * 
	 * @access public
	 * @return void
	 * @see Debug.notify
	 */
	Debug.notify = function() {
		if(!Element.hasClassName(wrap, 'notify')) Element.addClassName(wrap, 'notify');
	};
	
	/**
	 * Over ride the clearNotification to use prototypes better handling of class names
	 * 
	 * @access public
	 * @return void
	 * @see Debug.clearNotification
	 */
	Debug.clearNotification = function() {
		Element.removeClassName(wrap, 'notify');
	};
	
	/**
	 * Over ride the minimize and restore methods to use prototypes nicer toggle
	 *
	 * @access public
	 * @return void
	 * @see Debug.minimize, Debug.toggle
	 */
	Debug.minimize = Debug.restore = function() {
		Element.toggle(console);
	};
	
	/**
	 * Use the extension method to register the extra prototype stuff
	 *
	 * @access public
	 * @return void
	 * @see Debug.extendConsole
	 */
	Debug.extendConsole = function() {
		// use prototype to fake up the fixed positioning
		Event.observe(window, "scroll", Debug.fakeFixed);
		// add the dragging
		Event.observe(menu, "mousedown", Debug.startDrag);
		Event.observe(document, "mouseup", Debug.endDrag);
		Event.observe(document, "mousemove", Debug.performDrag);
		Element.addClassName(wrap, 'hasDrag');
		Debug.active = false;
		Debug.dragging = false;
		Debug.pointerOffsetX = 0;
		Debug.pointerOffsetY = 0;
		// store the current position (used for the fake fixed positioning in IE)
		var offsets = Position.page(wrap);
		Debug.curPageOffsetY = offsets[1];
	};
	
	/**
	 * Fakes fixed positioning in IE, it's not too pretty but it works
	 *
	 * @access public
	 * @param event
	 * @return void
	 */
	Debug.fakeFixed = function(event) {
		if(typeof(document.media)=='string') {
			var Y = window.pageYOffset|| document.documentElement.scrollTop || document.body.scrollTop || 0;
			wrap.style.top = (Y + Debug.curPageOffsetY) + 'px';
		}
	};
	
	/**
	 * Starts the dragging when user left clicks on the menu
	 *
	 * @access public
	 * @param event
	 * @return void
	 */
	Debug.startDrag = function(event) {
		if(Event.isLeftClick(event)) {
			Debug.active = true;
			// find the position of the pointer relative to the menu
			var pointer = [Event.pointerX(event), Event.pointerY(event)];
			var offsets = Position.cumulativeOffset(menu);
			Debug.pointerOffsetX =  (pointer[0] - offsets[0]);
			Debug.pointerOffsetY =  (pointer[1] - offsets[1]);
			Event.stop(event);
		}
	};
	
	/**
	 * Ends the dragging
	 * 
	 * @access public
	 * @param event
	 * @return void
	 */
	Debug.endDrag = function(event) {
		if(Debug.active && Debug.dragging) Event.stop(event);
    	Debug.active = false;
    	Debug.dragging = false;
		// store the current position (used for the fake fixed positioning in IE)
		var offsets = Position.page(wrap);
		Debug.curPageOffsetY = offsets[1];
	};
	
	/**
	 * Performs the dragging
	 *
	 * @access public
	 * @param event
	 * @return void
	 */
	Debug.performDrag = function(event) {
   		if(Debug.active) {
     		if(!Debug.dragging) Debug.dragging = true;
			else {
				var pointer = [Event.pointerX(event), Event.pointerY(event)];
				var offsets = Position.cumulativeOffset(wrap);
				var targX = pointer[0] - Debug.pointerOffsetX;
				var targY = pointer[1] - Debug.pointerOffsetY
				if(targY < 0) targY = 0;
				if(targX < 0) targX = 0;
				wrap.style.left = targX + 'px';
				wrap.style.top = targY + 'px';
				Event.stop(event);
			}
 		}
	};
}