/**
 * @Ajax Debug
 * Plugin jQuery para debugar requisições Ajax
 * @author Jaydson Gomes
 * @version 1.0
 * @date 11/09/09
 */
 
jQuery.fn.extend({

	/**
	 * Ajax Debug
	 */
	AjaxDebug: function(settings){
		/**
		 * Propriedades Default
		 */
		var defaults = {
			XHR : null,
			nativeConsole : true,
			containerId : "",
			debugObject : new Array()
		}
		
		/**
		 * Extende as configurações
		 */
		settings = $.extend(defaults, settings);
			/**
			 * Para cada XHR passada
			 */			 
			return this.each(function(){
				settings.XHR = this;
				//console.log(this.getAllResponseHeaders())
				//console.log(this.getResponseHeader())
				for(i in this){
					switch(i){
						case "status":
						case "readyState":
						case "responseText":
						case "statusText":
							settings.debugObject[i]=settings.XHR[i];
						break;
						case "getAllResponseHeaders":
							settings.debugObject[i]=this.getAllResponseHeaders();
						break;
					}
				}
				
				/**
				 * Se não é necessário exibir o console nativo, passa o conteúdo para o elemento definido
				 */
				if(!settings.nativeConsole){
					document.getElementById(settings.containerId).innerHTML += createList();
				}else{
					/**
					 * Verifica se já existe o console, e adiciona o conteúdo
					 */
					if(document.getElementById("jquery_ajax_console")){
						document.getElementById("jquery_ajax_console").innerHTML += createConsoleList();
						addResize();
					}else{
						/**
						 * Invoca o método para criar o console
						 */
						createConsole();
					}
					
					/**
					 * Adiciona evento para fechar o console
					 */
					$("#jquery_ajax_console_tab").bind("click",function(){
						$("#jquery_ajax_console").remove();
					});
				}
			});
			
		/**
		 * Cria o elemento HTML que será o console
		 */
		function createConsole(){
			var console = document.createElement("DIV");
			console.id = "jquery_ajax_console";
			console.style.borderTop = "inset 1px gray";
			console.style.position = "absolute";
			console.style.bottom = "0px";
			console.style.left = "0px";
			console.style.width = "100%";
			console.style.height = "150px";
			console.style.overflow = "auto";
			console.style.background = "#D4D0C8";
			
			var bar = document.createElement("DIV");
			bar.style.position = "fixed";
			bar.style.width = "100%";
			bar.id = "jquery_ajax_console_resize";
			bar.style.height = "2px";
			bar.style.background = "#EEEEEE";
			bar.style.cursor = "s-resize";
			console.appendChild(bar);
			
			var tab = document.createElement("DIV");
			tab.id = "jquery_ajax_console_tab";
			tab.style.position = "absolute";
			tab.style.zIndex = "99999999";
			tab.style.border = "outset 1px";
			tab.align = "center";
			tab.style.top = "2px";
			tab.style.cursor = "default";
			tab.style.right = "0px";
			tab.style.width = "50px";
			tab.style.fontFamily = "Tahoma";
			tab.style.fontSize = "11px";
			tab.style.heigth = "10px";
			tab.innerHTML = "Off";
			console.appendChild(tab);
			
			document.body.appendChild(console);
			console.innerHTML += createConsoleList();
			addResize();
		}
		
		/**
		 * Adiciona o evento de Resize no Console
		 */
		function addResize(){
            $("#jquery_ajax_console_resize").bind("mousedown",function(e){
                $(document).bind("mousemove",function(e){
                    $("#jquery_ajax_console").css("height",document.body.clientHeight - e.pageY);
                });
                $("#jquery_ajax_console_resize").bind("mouseup",function(){
                    $(document).unbind("mousemove");
                });
            });
        }
		
		/**
		 * Cria o conteúdo que será exibido no Console
		 */
		function createConsoleList(){
			var dt = new Date();
			var content = "<div align='center' style='margin-left:5px;margin-top:5px;font-family:tahoma;font-size:11px;padding:2px;width:200px;background:#F2E9AE;'>"+dt.toGMTString()+"</div><div style='border-bottom:solid 3px #ffffff;padding:5px;font-family:tahoma;font-size:11px'>" +
			"<div style='padding:2px;'>Headers:<span style='color:red'> " + settings.debugObject["getAllResponseHeaders"] + "</span></div>" + 
			"<div style='padding:2px;'>ReadyState:<span style='color:red'> " + settings.debugObject["readyState"] + "</span></div>" +
			"<div style='padding:2px;'>Status: <span style='color:red'>"  + settings.debugObject["status"] + "</span></div>" + 
			"<div style='padding:2px;'>StatusText: <span style='color:red'>"  + settings.debugObject["statusText"] + "</span></div>" + 
			"<div style='padding:2px;'>ResponseText: " + settings.debugObject["responseText"] + "</div></div>";
			
			return content;
		}
		
		/**
		 * Cria o conteúdo que será retornado para o elemento definido em settings
		 */
		function createList(){
			var content = 
			"<div style='padding:2px;border:solid 1px gray'>Headers:<span style='color:red'> " + settings.debugObject["getAllResponseHeaders"] + "</span></div>" + 
			"<div style='padding:5px;border:solid 1px gray'>ReadyState:<span style='color:red'> " + settings.debugObject["readyState"] + "</span></div>" +
			"<div style='padding:5px;border:solid 1px gray'>Status: <span style='color:red'>"  + settings.debugObject["status"] + "</span></div>" + 
			"<div style='padding:5px;border:solid 1px gray'>StatusText: <span style='color:red'>"  + settings.debugObject["statusText"] + "</span></div>" +
			"<div style='padding:5px;border:solid 1px gray'>ResponseText: " + settings.debugObject["responseText"] + "</div>";
			return content;
		}
	}
});


