
/**
 * JQuery Extension Plugin
 *
 * @date 7/4/2010
 * @version 0.2
 */

(function(jQuery) 
{
    /**
     * Namespace Extension
     */
    namespace = jQuery.namespace = function(name, object) 
    {
        var ns = name.split('.'), 
            obj = window, 
            i, len;

        for (i = 0, len = ns.length; i < len; i++) 
            obj = obj[ns[i]] = obj[ns[i]] || {};
        
        eval(name + " = object");
            
        /*
        // Si existe funcion 'init' la ejecuta, y al terminar activa el evento loaded
        $(document).ready(function() 
        {
            eval
            (
                 "if(typeof " + name + ".init == 'function')" +
                 "{" +
                     name + ".init(); " +
                     "$("+ name + ").trigger('loaded');" +
                 "}"
            );
        });*/
        
        return obj;
    };
    
    
    /** 
     * Include Extension
     * Carga dinamicamente y una sóla vez Javascript y hojas de estilos
     */    
    jQuery.include = function (obj)
    {
        if (obj.css)
        {
            jQuery.include.css(obj.css)
        }

        if (obj.js)
        {
            var func = (obj.func) ? obj.func : "";
            jQuery.include.js(obj.js, func);
        }
    };
    
    jQuery.include.scripts = [];
    jQuery.include.sheets = [];

    /**
     * Javascript Include Extension
     * Carga un script dinamicamente.
     * 
     * La diferencia con getScript es que sólo hace la peticion Ajax 
     * una sóla vez y ejecuta siempre la funcion pasada por parametro.
     * 
     * @param string path La ruta al fichero javascript
     * @param string fn Funcion a ejecutar tras la carga del javascript
     * 
     * @example $.import.js("gui/editor.js", "GUI.Editor.init()");
     */
    jQuery.include.js = function (path, fn)
    {
        var loaded = (jQuery.inArray(path, jQuery.include.scripts) > -1);
        
        // Se pone en la cola de ejecución de jQuery la carga del script
        if (!loaded)
        {
            jQuery.getScript(path, function() 
            {
                if(fn) eval(fn);
            });
            
            jQuery.include.scripts.push(path);
        }
        else
            if(fn) eval(fn);
        
    };
    
    
    /**
     * Style Sheet Include Extension
     * Carga una hoja de estilos dinamicamente.
     * 
     * @param string path La ruta al fichero con la hoja de estilos
     * 
     * @example $.import("gui/editor.js");
     */
    jQuery.include.css = function (path)
    {
        var loaded = (jQuery.inArray(path, jQuery.include.sheets) > -1);
        if (loaded) return;
        
        jQuery.include.sheets.push(path);
        
        // Se pone en la cola de ejecución de jQuery la carga del script
        $("<link>", 
        {
            href: path,
            type:"text/css",
            rel:"stylesheet"
        })
        .appendTo("head");
    };

    
    jQuery.fn.extend
    ({
         /**
          * Delivers easily an HTML Form via Ajax and POST method.
          *
          * @param {string} callback A function to be executed whenever the data is loaded successfully.
          * @example A callback function, like in jQuery ajax function:
          *	    function (data, text_status)
          *	    {
          *		// 'data' could be xmlDoc, jsonObj, html, text, etc...
          *		// textStatus can be one of:
          *		//	"timeout"
          *		//	"error"
          *		//	"notmodified"
          *		//	"success"
          *		//	"parsererror"
          *	    }
          *
          *	@param {string} data_type Type of data to be returned to callback function:
          *	    "xml", "html", "script", "json", "jsonp", or "text".
          *	@see jQuery.ajax()
          */
          send: function(callback, data_type)
          {
             // 'this' should be a form
             $.ajax
             ({
                type:	'post',
                url:	$(this).attr('action'),
                data:	$(this).serialize(),
                success: function(response)
                {
                   callback.call(this, response, "success");
                },

                error: function(http_request, text_status, error_thrown)
                {
                   callback.call(this, {}, text_status);
                },
                dataType: data_type
             });
          }
       });
    
})(jQuery);



