//debug 1.3


//funzioni principali
//-> poplog(msg);		Lancia un div "popup" con il testo passato alla funzione, asincrono, si possono accodare pių richieste 
//-> 


var messaggi = new Array();
var ttl = 3000; //vita popup in millisecondi

//classe container
if($.browser.msie && $.browser.version<7) //IE6 non supporta position:fixed
	var style_container = " position:absolute; top:50px; right:50px; width:320px; overflow:hidden; ";
else //altri browser sė
	var style_container = " position:fixed; top:50px; right:50px; width:320px; overflow:hidden; ";


//classe css popup
var nome_classe = ".msg-popup";
var style_classe = "border:1px solid red; margin:0 0 3px 0; background-color:#000; color:#fff; width:300px; font-size:8pt; padding:5px; ";

//classe object selector
var style_obj_selector = "border:1px solid #ccc; margin:0 0 3px 0; background-color:#333; color:#fff; width:300px; font-size:8pt; padding:5px; ";


function aggiungi_classe()
{
	if (!document.styleSheets) return;
	var thecss = new Array();
	var styleSheet = document.styleSheets[0];
	
	if (document.styleSheets[0].cssRules)  // Standards Compliant
        {
	   		thecss = document.styleSheets[0].cssRules;
        }
	else
        {         
        	thecss = document.styleSheets[0].rules;  // IE 
        }
	
	if(styleSheet.insertRule){ //FF e forse altri
		styleSheet.insertRule(nome_classe + " { " + style_classe + " }",thecss.length);
	}else if(styleSheet.addRule){ //IE e CHROME e forse altri
		styleSheet.addRule(nome_classe, style_classe);
	}else{
			//boh, il browser non supporta le due funzioni precedenti
	}
	
}


function popup_container_init(){
	$("body").append("<div id='popup-container' style='"+ style_container +"'></div>")
}


function poplog(messaggio){
		var t_creazione = new Date();
		
		$("#popup-container").prepend("<div class='msg-popup' id='msg-1' rel='"+ t_creazione.getTime() +"'>"+messaggio+"</div>");
		
		return true;
}

function poplog_txt(messaggio){
		var t_creazione = new Date();
		
		//messaggio.replace(/</g,"&lt;");
		//messaggio.replace(/>/g,"&gt;");
		
		rel_timer = t_creazione.getTime();
		
		$("#popup-container").prepend("<div class='msg-popup' rel='"+ rel_timer +"'></div>");
		$(".msg-popup[rel="+rel_timer+"]").text(messaggio);
		
		return true;
}


function popup_destroyer(){
	
	var t_now = new Date();
	
	$(".msg-popup").each(
						 
		function( intIndex ){
			
			if( t_now.getTime()- $(this).attr("rel") > ttl ){
				$(this).fadeOut("slow",function(){$(this).remove()});
				;
				
			}
			
		}				

	);
	
	if( $(".msg-popup").length == 0 ){
		//se non ci sono pių messaggi faccio qualcosa?
	}
	
}


function object_selector(){
	
	$("#popup-container").prepend("<div id='object_display' style='"+style_obj_selector+"'>obj</div>");
	
	$("*").mouseenter(function(){
		
		$("*").css("border","0px");
		$(this).css("border","1px dotted red");
		
		//attributes
		//"<br>Attributes: " +  this.attributes.length + this.attributes[0].name + this.attributes[1].value
		
		$("#object_display").html( this.tagName.toString() + "<br>ID: " + this.id.toString() + "<br>Class: " + this.className.toString() + "<br>Attributes: ");
		
    }).mouseleave(function(){
      	$(this).css("border","0px");
    });

}



//auto inizializzo il debug js
$(document).ready(function() {
						   
	aggiungi_classe();
	popup_container_init();
	
	t1 = setInterval("popup_destroyer()",1000);
	
	
	var isCtrl = false;

	$(document).keyup(function (e) {
		if(e.which == 17) isCtrl=false;
	}).keydown(function (e) {
		if(e.which == 17) isCtrl=true;
		if(e.which == 79 && isCtrl == true) {
			//CTRL + M
			object_selector();
			return false;
		}
	});
	
	
	$("#popup-container").dblclick(function(e){
		$("#popup-container").prepend('<span id="dragTab" style="background-color:#cc0000; color:#fff; font-size:8pt; padding:2px; cursor:pointer;">sposta usando le frecce</span>');
		//alert(e.pageX + " " + e.pageY)
		
		$(window).keydown(function(event){
			switch (event.keyCode) {
				case 37:
					 $("#popup-container").animate({"left": "-=100px"}, "fast");
					break;
				
				case 38:
					$("#popup-container").animate({"top": "-=100px"}, "fast");
					break;
				
				case 39:
					 $("#popup-container").animate({"left": "+=100px"}, "fast");
					break;
				
				case 40:
					$("#popup-container").animate({"top": "+=100px"}, "fast");
					break;
				
			}
		});

		
	
		
	});
	
});







