/*
 *
    Sintaxis:

        include(file [,{opciones}]);

    Ej: include("path/to/archive");

    Parámetros opcionales

        * cache: true | false -     Si se especifica false se le añade al final del archivo 
        *                           un numero aleatorio para que no se guarde cache en el navegador
        * dom: true | false -       Si se especifica true el archivo se cargará creando un nuevo 
        *                           elemento script o link en el DOM, si es false el archivo se
        *                           cargará con un document.write
        * type: js | css -          este parámetro solo es necesario si el archivo a cargar tiene 
        *                           una extensión diferente a js o css por ej un archivo .php
        * onload: funcion -         en el caso de que se cargue un archivo js via DOM es posible
        *                           especificar una acción al finalizar la carga del archivo.

 *
*/

var include = function( file )
{             
    if( file == "" ) return;
    
    var opt = arguments[1] ? arguments[1] : {};

    //Genera una id para el archivo con el fin de evitar que se cargue 2 veces.
    idfile = file.replace(location.hostname,"");
    idfile = idfile.replace(location.protocol,"");
    idfile = idfile.replace("//","");

    if( document.getElementById( idfile ) )
    {
        return;
    }

    if( typeof opt == "undefined" ) opt = {};
    if( typeof opt.cache == "undefined" ) opt.cache = true;
    if( typeof opt.dom == "undefined" )  opt.dom = false;
    if( typeof opt.type == "undefined" )  opt.type = "";
    if( typeof opt.charset == "undefined" )  opt.charset = "ISO-8859-1";


    if( opt.type != "" )
    {
        var ext = opt.type;
    }
    else
    {   
        var ex = file.split(".");
        var ext = ex.pop();
    }
    if(!opt.cache)
    {
        var random = new Date().getTime().toString();       
        if( file.indexOf("?")!=-1 )
        {
            file = file+"&"+random;
        }
        else
        {
            file = file+"?"+random;
        }
    }

    if(opt.dom)
    {
        var head = document.getElementsByTagName('head').item(0)       
    }


    switch(ext)
    {
        case "css":
            if(!opt.dom) 
            {
                document.write( '<link rel="stylesheet" href="' + file + '" id="' + idfile + '" type="text/css"><\/link>' );
            }
            else
            {
                css = document.createElement('link');
                css.rel  = 'stylesheet';
                css.href = file;
                css.type = 'text/css';
                css.id = idfile;
                head.appendChild(css);            
            }               
            break;
            
        case "js":
            if(!opt.dom)
            {
                document.write('<script type="text/javascript" id="'+idfile+'" src="'+file+' charset=' + opt.charset + '"><\/script>');
            }
            else
            {
                script = document.createElement('script');
                script.src = file;
                script.type = 'text/javascript';
                script.id = idfile;
                head.appendChild(script);

                if(typeof opt.oncomplete!="undefined")
                {
                    //Para IE
                    script.onreadystatechange = function () 
                    {
                        if( script.readyState == 'complete' ) 
                        {
                            if( typeof opt.oncomplete == "function" )
                            {
                                eval( opt.oncomplete() );
                            }
                        }
                    }
                    //Para Firefox
                    script.onload = function () 
                    {
                        if( typeof opt.oncomplete == "function" )
                        {
                            opt.oncomplete();
                        }
                    }
                }             
            }

        break;
    }
}

