// ==UserScript==
// @name          ortocultor
// @namespace     http://aitor.name/greasemonkey/
// @description   ortocultor
// @include       http://b5m.gipuzkoa.net/url5000/*
// ==/UserScript==

// ---------------------------------------------------------------------------------------------
var OrtocultorLoader =  {

    ROOT: "http://localhost:8080/",

    //------------------------------------------------------------------------------------------
    initialize: function() {
        this.updateRoot();
        this.loadLibraries();
    },

    updateRoot: function() {
        var scripts = document.getElementsByTagName( "script"),
            me = /ortocultor\.loader\.user\.js/;

        for( var i = 0; i < scripts.length; i++) {
            var script = scripts[ i].src;

            if( script && script.match( me)) {

                this.ROOT = script.replace( me, "");
            }
        }
    },

    loadLibraries: function() {
        var FILES = [ "lib/prototype/prototype.js",
                      "lib/scriptaculous/builder.js",   // orden de carga obtenida de scritaculous.js
                      "lib/scriptaculous/effects.js",
                      "lib/scriptaculous/dragdrop.js",
                      "lib/scriptaculous/controls.js",
                      "lib/scriptaculous/slider.js"
                      //"lib/scriptaculous/scriptaculous.js?load=",
        ];

        for( var i = 0; i < FILES.length; i++) {

            document.write( '<script type="text/javascript" src="' + this.ROOT + FILES[ i] + '"> </script>');
        }
    },

    //------------------------------------------------------------------------------------------
    createElement: function( tag, attributes) {
        var element = document.createElement( tag);

        for( var key in attributes) {
            element.setAttribute( key, attributes[ key]);
        }

        return element;
    },

    addToHead: function( tag, attributes) {

        document.getElementsByTagName( "head")[ 0].appendChild( this.createElement( tag, attributes));
    },

    // ---------------------------------------------------------------------------------------------
    load: function() {

        new Ajax.Request( this.ROOT + "ortocultor.html", {

            method: "get",

            onSuccess: function( xhr) {
                var div = $( this.createElement( "div", { id: "ortocultor-html" }));

                div.update( xhr.responseText);
                $( "MapOutDiv").appendChild( div);

                // metemos el CSS y cargamos el ortocultor
                this.addToHead( "link", { type: "text/css", rel: "stylesheet", href: this.ROOT + "ortocultor.css"});
                this.addToHead( "script", { type: "text/javascript", src: this.ROOT + "ortocultor.js"});
            }.bind( this),

            onFailure: function( xhr) {
                alert( "ERROR (onFailure): carga XHR del ortocultor: status = " + xhr.status + ", statusText = " + xhr.statusText);
            },

            onException: function( xhr, exception) {
                //alert( "ERROR (onException): carga XHR del ortocultor: exception = " + exception.message);
            }
        });
    }
};

// ---------------------------------------------------------------------------------------------
{
    OrtocultorLoader.initialize();

    function ortocultorEventHandler() {
        OrtocultorLoader.load();
    }

    if( window.addEventListener) {
        // W3C
        window.addEventListener( "load", ortocultorEventHandler, false);
    } else {
        // IE
        window.attachEvent( "onload", ortocultorEventHandler);
    }
}

/*
// para que no agobien esas ventanas flotantes
window.setInterval( function() {
    const   s = "display:none;";
    var     e = window.document.getElementById( "float_output");
    
    if( e) {
        e.setAttribute( "style", s);
    }
}, 1000); 
*/


