/**
 * Various development Tools.
 * @author NOSE
 *
 * @requires jquery.js 
 * 
 * @option devStyleKeyCode 		Key code to trigger the development style.
 * @option devStyleCSS 			Path to the develop style.
 * @option devStyleActive 		True to initially active.
 * @option xrayKeyCode 			Key code to trigger xray.
 * @option xraySourceJS 		Path to the xray source.
 * @option xrayActive 			True to initially active.
 *
 * @option DEBUG                set DEBUG to false somewhere after this 
 *                              script is loaded to turn debugging off!
 * 
 * @version 0.2					develop version			
 */
 
var DEBUG = true;

jQuery.fn.devTools = function(op) {	
	// defaults
	var defaults =  {	
			devStyleKeyCode:49,
			devStyleCSS: "../style/css/dev/develop.css",
			devStyleActive:false,
			xrayKeyCode:50,
			xraySourceJS: "../js/lib/jquery/jquery.devtools.xray.js",
			xrayActive:false
	};
	jQuery.extend(defaults, op);
	
	// event key pressed
    jQuery(document).bind('keypress', function(event) {
         handleKeyPressed(event);
    });
    
    // initialize
    if (defaults.devStyleActive) {
    	defaults.devStyleActive = false;
    	switchDevelopStyle();
    }
	if (defaults.xrayActive) {
		initializeXRAY();
	}	
	
	
	/**
	* Handles key events.
	*/
	function handleKeyPressed(e) {
	   // only if DEBUG is true
       if (!window.DEBUG) return;
	   // code
	   var code = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
	   // 
	   // develop style
	   if (code == defaults.devStyleKeyCode) {
	   	  	switchDevelopStyle();
	   }
	   // xray
	   if (code == defaults.xrayKeyCode) {
	     	initializeXRAY();
	   }

	};
	
	/*
	 * Switches the develop css in/out.
	 */
	function switchDevelopStyle() {	
		if (defaults.devStyleActive) {
			$('link[@rel*=style]').each(function(i) {
                if (this.getAttribute('title') == "devCSS") {
                    this.disabled = ! this.disabled;
                }
            });
	    } else {
		    var c = document.createElement('link');
            c.type = 'text/css';
            c.rel = 'stylesheet';
            c.title = 'devCSS';
            c.href = defaults.devStyleCSS;
            jQuery('head')[0].appendChild(c);
		    defaults.devStyleActive = true;
	 		
		}
	 }
	 /*
	 * Initializes xray.
	 */
	 function initializeXRAY() {	
		// append script
	 	var xrScript = document.createElement("SCRIPT");
	 	xrScript.setAttribute("language","JavaScript");
	 	xrScript.setAttribute("src",defaults.xraySourceJS);
	 	document.body.appendChild(xrScript);
	 }	

  
    // return
    return this;
};

//jQuery(document).devTools();