/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */




UserControlProfile = OpenLayers.Class(OpenLayers.Control.Measure, {

    /**
     * Property: type
     * {String} The type of <OpenLayers.Control> -- When added to a
     *     <Control.Panel>, 'type' is used by the panel to determine how to
     *     handle our events.
     */
    type: OpenLayers.Control.TYPE_TOOL,

      /**
     * APIProperty: symbolizer
     * {Object} Symbolizer to be used by the handler. This can be overriden providing a suitable object in the options
     */
    symbolizer: {
        "Point": {
            pointRadius: 4,
            graphicName: "cross",
            fillColor: "white",
            fillOpacity: 1,
            strokeWidth: 1,
            strokeColor: "#000000"
        },
        "Line": {
            strokeWidth: 3,
            strokeColor: "#85963d"
        }
    },

      /**
     * Property: handlerOptions
     * {Object} Specific handler options for the Ruler control
     */
    handlerOptions: {
        style: "ruler",
        layerOptions: {
            styleMap: null
        },
        persist: true
    },

    /**
    * APIProperty: the div assigned to the control
    * (div Object)
    */
    div: null,


    /**
     * APIProperty: eventListeners
     * {Object} Used to override default event handlers. Object must contain one of the following memebers {"measure": <handler>, "measurepartial": <handler>}
     */
    eventListeners: null,


    /**
     *Property: imgId
     *{String} id for the image created by the control
     */
    imgId: null,

    /**
     *  Property: activated
     *  {Boolean}
     */
    activated:false,


     /**
    * Constructor: UserControlProfile
    *
    * Parameters:
    * options - {Object} Hashtable of extra options to tag onto the control
    */
    initialize: function(div, options){
        //Set container
        this.div = document.getElementById(div);
        if (!this.div) {
            eGV.Control.prototype.initialize.apply(this);
            return;
        }

        OpenLayers.Control.Measure.prototype.EVENT_TYPES =
            eGV.Control.prototype.EVENT_TYPES.concat(
            OpenLayers.Control.Measure.prototype.EVENT_TYPES
        );

        options = options || [];

        //Create style with the current symbolizer
        var style = new OpenLayers.Style();
        style.addRules([
            new OpenLayers.Rule({symbolizer: this.symbolizer})
        ]);
        var styleMap = new OpenLayers.StyleMap({"ruler": style});
        this.handlerOptions.layerOptions.styleMap = styleMap;

        //Create handler
        var handler = OpenLayers.Handler.Path;

        var newArguments = [handler].concat(options);
        OpenLayers.Control.Measure.prototype.initialize.apply(this, newArguments);

        if (!options.displayClass)
            this.displayClass =
                this.CLASS_NAME.replace("eGV.", "egv").replace(/\./g, "");

        this.events.on({
            "activate": this.activateProfile/*,
            "deactivate": this.deactivateRuler*/
        });

        //Register custom events if provided
        if (this.eventListeners instanceof Object){
            this.events.on(this.eventListeners);
        } else {
            this.events.on({
                "measure": this.handleMeasure,
                "measurepartial": this.handlePartialMeasure
            });
        }

        //Set map
        // If none provided in the custom options, default map is used
        // Some frameworks like mootools implement a map method for arrays. If so, we override it
        if (!this.map || typeof(this.map) == "function") {
            this.map = eGV.getMap();
        }

    },
    /**
    * Method: handleClick - actions to be performed when the tool is called
    *
    * Parameters:
    * evt - {Event)}
    */
    handleClick: function(evt){
        this.events.triggerEvent("actionstart");
        this.drawInterface();
    },

    /**
    * Method: drawInterface
    * Draws the interface for interacting with the control in the corresponding div
    *
    */
    drawInterface: function(){
        this.events.triggerEvent("actionstart");
        if(this.div == null) return;

		while(this.div.firstChild) this.div.removeChild(this.div.firstChild);

        var divMain = document.createElement("div");
        divMain.className = "divProfilesClass";

        divMain.appendChild(document.createTextNode("Aquesta eina permet obtenir el perfil d'altimetria de la línia formada pels punts que l'usuari ha marcat en el mapa. Per fer-la servir, seguiu els següents passos:"));
        divMain.appendChild(document.createElement("br"));

        var list = document.createElement("ol");
        var item = document.createElement("li");

        item.appendChild(document.createTextNode("Feu click, almenys, sobre dos punts del mapa"));
        list.appendChild(item);

        item = document.createElement("li");
        item.appendChild(document.createTextNode("Quan hagueu decidit finalitzar la línia de punts, feu doble click"));
        list.appendChild(item);

        divMain.appendChild(list);

        this.div.appendChild(divMain);
    },

  /**
    * Method: handleMeasure
    *
    * Parameters:
    * evt - {Event)}
    */
    handleMeasure: function(evt){
        this.events.triggerEvent("actionstart");
        var arrayPoints = [];
        var points = evt.geometry.components;
        for(var i = 0, len = points.length; i < len; i++){
            arrayPoints.push(points[i].x);
            arrayPoints.push(points[i].y);
        }

        var img = serverUrl+"/geoz/geoz.php?REQUEST=getimage&POINTS="+arrayPoints.toString()+"&STEP=100&WIDTH=590&HEIGHT=125&BOTTOMCOLOR=green&TOPCOLOR=maroon&LOCALIZERS=0&FLOOR=1";

        if(this.div == null) return;
		while(this.div.firstChild) this.div.removeChild(this.div.firstChild);

        var divMain = document.createElement("div");
        divMain.className = "divProfilesClass";

        //divMain.appendChild(document.createTextNode("Resultat de l'eina d'altimetria"));
        //divMain.appendChild(document.createElement("br"));

        

        var imgElem = document.createElement("img");
        imgElem.src = img;

        var divLeft = document.createElement('div');
        divLeft.className = 'profileLeft';
        var divRight = document.createElement('div');
        divRight.className = 'profileRight';
        
        
        divLeft.appendChild(imgElem);
        
        divRight.appendChild(document.createTextNode(locStrings["_profile_export_title"]));
        divRight.appendChild(document.createElement('br'));
        divRight.appendChild(document.createElement('br'));
        
        var expImg = document.createElement('a');
        expImg.href = serverUrl+"/geoz/geoz.php?REQUEST=getimage&POINTS="+arrayPoints.toString()+"&STEP=100&WIDTH=1024&HEIGHT=480&BOTTOMCOLOR=green&TOPCOLOR=maroon&LOCALIZERS=0&FLOOR=1";
        expImg.target = '_blank';
        var expImgIcon = document.createElement('img');
        expImgIcon.src = 'img/fileTree/page_white_picture.png';
        divRight.appendChild(expImgIcon);
        expImg.appendChild(document.createTextNode(locStrings["_profile_export_img"]));
        divRight.appendChild(expImg);
        
        divRight.appendChild(document.createElement('br'));
        
        
        var expCsv = document.createElement('a');
        expCsv.href = serverUrl+"/tremp/php/profile.php?POINTS="+arrayPoints.toString();
        var expCsvIcon = document.createElement('img');
        expCsvIcon.src = 'img/fileTree/page_white_excel.png';
        divRight.appendChild(expCsvIcon);
        expCsv.appendChild(document.createTextNode(locStrings["_profile_export_csv"]));
        divRight.appendChild(expCsv);

        divMain.appendChild(divLeft);
        divMain.appendChild(divRight);


        this.div.appendChild(divMain);

    },

     /**
    * Method: handlePartialMeasure
    *
    * Parameters:
    * evt - {Event)}
    */
    handlePartialMeasure: function(evt){
        if(evt.measure == 0){
            this.events.triggerEvent("actionstart");
            this.drawInterface();
        }
    },

     /**
    * Method: activateProfile
    *
    * Parameters:
    * evt - {Event)}
    */
    activateProfile: function(){
        this.events.triggerEvent("actionstart");
        this.drawInterface();
    },

    CLASS_NAME: "eGV.Control.User.UserControlProfile"
});



UserControlOpenKML = OpenLayers.Class(eGV.Control, {

    /**
     * Property: type
     * {String} The type of <OpenLayers.Control> -- When added to a
     *     <Control.Panel>, 'type' is used by the panel to determine how to
     *     handle our events.
     */
    type: OpenLayers.Control.TYPE_BUTTON,

    initialize: function(div, options){

        this.div = document.getElementById(div);

        options = options || [];
        eGV.Control.prototype.initialize.apply(this, [options]);

        return true;
    },

    /*
     * Method: trigger
     * Load all the elements in the list.
     */
    trigger: function() {
        //Display the loading effect
        //alert("OpenKML");
        window.location="kml/senders.kml";
    },

    CLASS_NAME: "eGV.Control.User.UserControlKML"
});

UserControlListFiles = OpenLayers.Class(eGV.Control, {

    /**
     * Property: type
     * {String} The type of <OpenLayers.Control> -- When added to a
     *     <Control.Panel>, 'type' is used by the panel to determine how to
     *     handle our events.
     */
    type: OpenLayers.Control.TYPE_BUTTON,

    /**
     * Constructor: UserControlListFiles
     */
	initialize: function(div, options){

        this.div = document.getElementById(div);

        options = options || [];
        eGV.Control.prototype.initialize.apply(this, [options]);

        return true;
    },

    /*
     * Method: trigger
     * Load all the elements in the list.
     */
    trigger: function() {
        //Display the loading effect
        this.events.triggerEvent("actionstart");
        this.drawList();
        
    },


    drawList: function() {
        if(this.div == null) return;

        while(this.div.firstChild) this.div.removeChild(this.div.firstChild);

        var header = document.createElement('h3');
        header.className ='egvControlPrintTitle';
        header.appendChild(document.createTextNode(locStrings["_list_files_title"]));

        var divBody = document.createElement('div');
        divBody.className = 'egvControlPrintBody';

        var divTree = document.createElement('div');
        divTree.className = 'fileTree';
        divTree.id = "fileTree";

        var closeButton = document.createElement('input');
        closeButton.type = 'submit';
        closeButton.id = "linkCloseGPXModalWindow";
        closeButton.value = locStrings["_close_button"];

        OpenLayers.Event.observe(closeButton, "click", OpenLayers.Function.bindAsEventListener(effects.hideModalDiv, {"divId":"divPrint" }))

        divBody.appendChild(divTree);
        divBody.appendChild(document.createElement('br'));
        divBody.appendChild(closeButton);

        this.div.appendChild(header);
        this.div.appendChild(divBody);

        this.events.triggerEvent("actionend");
        
    },

    CLASS_NAME: "eGV.Control.User.UserControlListFiles"
});

UserControlListShapes = OpenLayers.Class(eGV.Control, {

    /**
     * Property: type
     * {String} The type of <OpenLayers.Control> -- When added to a
     *     <Control.Panel>, 'type' is used by the panel to determine how to
     *     handle our events.
     */
    type: OpenLayers.Control.TYPE_BUTTON,

    /**
     * Constructor: UserControlListFiles
     */
	initialize: function(div, options){

        this.div = document.getElementById(div);

        options = options || [];
        eGV.Control.prototype.initialize.apply(this, [options]);

        return true;
    },

    /*
     * Method: trigger
     * Load all the elements in the list.
     */
    trigger: function() {
        //Display the loading effect
        this.events.triggerEvent("actionstart");
        this.drawList();

    },


    drawList: function() {
        
        if(this.div == null) return;

        while(this.div.firstChild) this.div.removeChild(this.div.firstChild);

        var header = document.createElement('h3');
        header.className ='egvControlPrintTitle';
        header.appendChild(document.createTextNode(locStrings["_list_shp_title"]));

        var divBody = document.createElement('div');
        divBody.className = 'egvControlPrintBody';

        var divTree = document.createElement('div');
        divTree.className = 'fileTree';
        divTree.id = "fileTree";

        var closeButton = document.createElement('input');
        closeButton.type = 'submit';
        closeButton.id = "linkCloseGPXModalWindow";
        closeButton.value = locStrings["_close_button"];

        OpenLayers.Event.observe(closeButton, "click", OpenLayers.Function.bindAsEventListener(effects.hideModalDiv, {"divId":"divPrint" }))

        divBody.appendChild(divTree);
        divBody.appendChild(document.createElement('br'));
        divBody.appendChild(closeButton);

        this.div.appendChild(header);
        this.div.appendChild(divBody);

        this.events.triggerEvent("actionend");

    },

    CLASS_NAME: "eGV.Control.User.UserControlListShapes"
});


UserControlHelp = OpenLayers.Class(eGV.Control, {

    /**
     * Property: type
     * {String} The type of <OpenLayers.Control> -- When added to a
     *     <Control.Panel>, 'type' is used by the panel to determine how to
     *     handle our events.
     */
    type: OpenLayers.Control.TYPE_BUTTON,

    /**
     * Constructor: UserControlListFiles
     */
	initialize: function(div, options){

        this.div = document.getElementById(div);

        options = options || [];
        eGV.Control.prototype.initialize.apply(this, [options]);

        return true;
    },

    /*
     * Method: trigger
     * Load all the elements in the list.
     */
    trigger: function() {
        //Display the loading effect
        this.events.triggerEvent("actionstart");
        this.drawList();

    },


    drawList: function() {
        if(this.div == null) return;

        while(this.div.firstChild) this.div.removeChild(this.div.firstChild);

        var header = document.createElement('h3');
        header.className ='egvControlPrintTitle';
        header.appendChild(document.createTextNode(locStrings["_help_title"]));

        var divBody = document.createElement('div');
        divBody.className = 'egvControlPrintBody';

        var divMain = document.createElement('div');
        divMain.className = 'slider-wrap';
        divMain.id = "codaSlider";

        var divSlider = document.createElement('div');
        divSlider.className = "csw";
        divSlider.id = "slider1";

        var divPanel = document.createElement('div');
        divPanel.className = "panelContainer";

        var divSlide1 = document.createElement('div');
        divSlide1.className = "panel";
        divSlide1.title = locStrings["_help_tab1_title"];

        var divWrap1 = document.createElement('div');
        divWrap1.className = "wrapper";
        divWrap1.innerHTML = locStrings["_help_tab1_text"];

        divSlide1.appendChild(divWrap1);
        divPanel.appendChild(divSlide1);

        var divSlide2 = document.createElement('div');
        divSlide2.className = "panel";
        divSlide2.title = locStrings["_help_tab2_title"];;

        var divWrap2 = document.createElement('div');
        divWrap2.className = "wrapper";
        divWrap2.innerHTML = locStrings["_help_tab2_text"];

        divSlide2.appendChild(divWrap2);
        divPanel.appendChild(divSlide2);

        var divSlide3 = document.createElement('div');
        divSlide3.className = "panel";
        divSlide3.title = locStrings["_help_tab3_title"];;

        var divWrap3 = document.createElement('div');
        divWrap3.className = "wrapper";
        divWrap3.innerHTML = locStrings["_help_tab3_text"];

        divSlide3.appendChild(divWrap3);
        divPanel.appendChild(divSlide3);

        var divSlide4 = document.createElement('div');
        divSlide4.className = "panel";
        divSlide4.title = locStrings["_help_tab4_title"];;

        var divWrap4 = document.createElement('div');
        divWrap4.className = "wrapper";
        divWrap4.innerHTML = locStrings["_help_tab4_text"];

        divSlide4.appendChild(divWrap4);
        divPanel.appendChild(divSlide4);




        divSlider.appendChild(divPanel);
        divMain.appendChild(divSlider);

        

        var closeButton = document.createElement('input');
        closeButton.type = 'submit';
        closeButton.id = "linkCloseGPXModalWindow";
        closeButton.value = locStrings["_close_button"];

        OpenLayers.Event.observe(closeButton, "click", OpenLayers.Function.bindAsEventListener(effects.hideModalDiv, {"divId":"divPrint" }))

        divBody.appendChild(divMain);
        divBody.appendChild(document.createElement('br'));
        divBody.appendChild(closeButton);

        this.div.appendChild(header);
        this.div.appendChild(divBody);

        this.events.triggerEvent("actionend");

    },

    CLASS_NAME: "eGV.Control.User.UserControlHelp"
});

UserControlGPX = OpenLayers.Class(eGV.Control, {

    /**
     * Property: type
     * {String} The type of <OpenLayers.Control> -- When added to a
     *     <Control.Panel>, 'type' is used by the panel to determine how to
     *     handle our events.
     */
    type: OpenLayers.Control.TYPE_BUTTON,

    olLayers: null,

    eGVConnections: null,

    eGVLayers: null,


    /**
     * Constructor: UserControlListFiles
     */
	initialize: function(div, options){

        this.div = document.getElementById(div);

        options = options || [];
        eGV.Control.prototype.initialize.apply(this, [options]);

        return true;
    },

    /*
     * Method: trigger
     * Load all the elements in the list.
     */
    trigger: function() {
        //Display the loading effect
        this.drawForm();
        this.events.triggerEvent("actionstart");
    },


    drawForm: function() {
        if(this.div == null) return;

        while(this.div.firstChild) this.div.removeChild(this.div.firstChild);

        var header = document.createElement('h3');
        header.className ='egvControlPrintTitle';
        header.appendChild(document.createTextNode(locStrings["_gpx_title"]));

        var divGPXBody = document.createElement('div');
        divGPXBody.className = "divGPXClass";

        var divGPX = document.createElement('div');
        divGPX.id = "divGPX";
        divGPX.className = "divGPXButton";
        

        //creat the explaning text
        var text1 = document.createElement('span');
        text1.appendChild(document.createTextNode(locStrings["_gpx_text1"]));
        var text2 = document.createElement('span');
        text2.appendChild(document.createTextNode(locStrings["_gpx_text2"]));


        //build the form
        var divGPXForm = document.createElement('div');
        divGPXForm.id ="fileUpload";
        divGPXForm.appendChild(document.createTextNode("You have a problem with your javascript"));

        divGPX.appendChild(divGPXForm);

        var closeButton = document.createElement('input');
        closeButton.type = 'submit';
        closeButton.id = "linkCloseGPXModalWindow";
        //closeButton.appendChild(document.createTextNode(locStrings["_close_button"]));
        closeButton.value = locStrings["_close_button"];

        OpenLayers.Event.observe(closeButton, "click", OpenLayers.Function.bindAsEventListener(effects.hideModalDiv, {"divId":"divPrint" }))

        var divGPXError = document.createElement('div');
        divGPXError.id = "divGPXError";
        divGPXError.className = "divGPXError";

        this.div.appendChild(header);
        divGPXBody.appendChild(text1);
        divGPXBody.appendChild(document.createElement('br'));
        divGPXBody.appendChild(text2);
        divGPXBody.appendChild(document.createElement('br'));
        divGPXBody.appendChild(document.createElement('br'));

        divGPXBody.appendChild(divGPX);
        divGPXBody.appendChild(document.createElement('br'));
        divGPXBody.appendChild(divGPXError);
        divGPXBody.appendChild(document.createElement('br'));
        divGPXBody.appendChild(closeButton);
        
        this.div.appendChild(divGPXBody);
        //this.div.appendChild(document.createElement('br'));
        //this.div.appendChild(gpxFrame);

        this.events.triggerEvent("actionend");


    },

    processResults: function (geoJsonString) {
        
        var geojson_format = new OpenLayers.Format.GeoJSON();
        var geoJson = geojson_format.read(geoJsonString);
        
        var map = eGV.getMap();
        var inside = false;

        //check that the returned BBOX is partially inside the map BBOX
        var bboxFound = false;
        var i = 0;
        while (!bboxFound && i < geoJson.length) {
            if (geoJson[i].bounds) {
                bboxFound = true;
            } else {
                i++;
            }
        }
        if (bboxFound) {
            var bbox = geoJson[i].bounds;
            var mapBbox = map.maxExtent;
            
            
            if (bbox.left >= mapBbox.left && bbox.left <= mapBbox.right) {
                if (bbox.bottom >= mapBbox.bottom && bbox.bottom <= mapBbox.top) {
                    inside = true;
                } else if (bbox.top >= mapBbox.bottom && bbox.top <= mapBbox.top) {
                    inside = true;
                }
            }
            else if (bbox.right >= mapBbox.left && bbox.right <= mapBbox.right) {
                if (bbox.bottom >= mapBbox.bottom && bbox.bottom <= mapBbox.top) {
                    inside = true;
                } else if (bbox.top >= mapBbox.bottom && bbox.top <= mapBbox.top) {
                    inside = true;
                }
            }

        } else {
            return -1;
        }

        if (!inside) {
            return -2;
        } else {
            this.drawProfile(geoJson);

            //add the vector layer to the map
            
            if (!this.olLayers) this.olLayers = new Array();
            if (!this.eGVConnections) this.eGVConnections = new Array();
            if (!this.eGVConnections) this.eGVConnections = new Array();

            //add the olLayer
            this.olLayers.push(new OpenLayers.Layer.Vector());
            var lastOlLayer = this.olLayers[this.olLayers.length-1];
            lastOlLayer.addFeatures(geojson_format.read(geoJsonString));

            //Add the eGV Connection
            this.eGVConnections.push(new eGV.Connection(
                "OpenLayers.Layer.Vector",
                lastOlLayer
            ));

            var title = null;
            if (geoJson[0].attributes) {title = geoJson[0].attributes.name;}
            if (!title) {title = "gpx"+this.eGVConnections.length;}

            var lastEgvConn = this.eGVConnections[this.eGVConnections.length - 1];

            lastEgvConn.addLayer(new eGV.Layer({
                "id" : "gpx"+this.eGVConnections.length,
                "name" : "gpx"+this.eGVConnections.length,
                "title" : title,
                "visible" : true,
                "queryable" : false,
                "showLegend" : false,
                "showInLayerManager": true
            }));

            map.addConnection(lastEgvConn);

            var groupVector = map.getGroup("vector");
            groupVector.addLayer(lastEgvConn.layers[0]);

            //add the layer to layersManager or similar
            map.events.triggerEvent("layeradded",{connection:lastEgvConn});

            //add the control to get the info of the points
            /*
            var optionsSelect = {
               hover: true,
               onSelect: function(feature) {
                   document.getElementById("info").innerHTML = feature.attributes.name;
               }
            };
            var select = new OpenLayers.Control.SelectFeature(vector_layer, optionsSelect);
            map.addControl(select);
            select.activate();
            */

            //center the map in the layer extent
            //map.zoomToExtent(new OpenLayers.Bounds(bbox[0],bbox[1],bbox[2],bbox[3]));
            map.zoomToExtent(new OpenLayers.Bounds(bbox.left,bbox.bottom,bbox.right,bbox.top));
            
            return 0;
            
            
        }

        

    },

    drawProfile: function(geoJson) {

        var lineFound = false;
        var i = 0;

        while (!lineFound && i < geoJson.length) {
            if (geoJson[i].geometry.components[0].CLASS_NAME == "OpenLayers.Geometry.LineString") {
                lineFound = true;
            } else {
                i++;
            }
        }
        if (lineFound) {
            this.events.triggerEvent("actionstart");
            var components = geoJson[i].geometry.components[0].components;
            var points = new Array();

            var maxPoints = 50;

            if (components.length > maxPoints) {
                var jump = Math.round(components.length / maxPoints,0);
                points = new Array();
                var j = 0;
                for (j = 0; j < components.length; j+=jump) {
                    points.push(components[j].x+","+components[j].y);
                }
            }
            
            var html = "<div class='divProfilesClass'>\n\
                    <div class='profileLeft'>\n\
                        <img src='"+serverUrl+"/geoz/geoz.php?REQUEST=getimage&amp;POINTS="+points.join(",")+"&amp;STEP=100&amp;WIDTH=590&amp;HEIGHT=125&amp;BOTTOMCOLOR=green&amp;TOPCOLOR=maroon&amp;LOCALIZERS=0&amp;FLOOR=1'/>\n\
                    </div>\n\
                    <div class='profileRight'>\n\
                        Exportació<br/><br/>\n\
                        <img src='img/fileTree/page_white_picture.png'/>\n\
                        <a href='"+serverUrl+"/geoz/geoz.php?REQUEST=getimage&amp;POINTS="+points.join(",")+"&amp;STEP=100&amp;WIDTH=1024&amp;HEIGHT=480&amp;BOTTOMCOLOR=green&amp;TOPCOLOR=maroon&amp;LOCALIZERS=0&amp;FLOOR=1' target='_blank'>\n\
                            Imatge\n\
                        </a>\n\
                        <br/>\n\
                        <img src='img/fileTree/page_white_excel.png'/>\n\
                        <a href='"+serverUrl+"/tremp/php/profile.php?POINTS="+points.join(",")+">Excel</a>\n\
                    </div>\n\
                </div>";

            //fill the floating div width data about the "cami"
            effects.fillFloatingDiv("divProfile",html);

        }
        
    },



    CLASS_NAME: "eGV.Control.User.UserControlGPX"
});


UserControlPrint = OpenLayers.Class(eGV.Control.Print, {

    initialize: function(div, options){
        this.div = document.getElementById(div);
        if (!this.div) {
            eGV.Control.prototype.initialize.apply(this);
            return false;
        }

        options = options || [];
        eGV.Control.prototype.initialize.apply(this, [options]);

        if (!options.displayClass)
            this.displayClass =
                this.CLASS_NAME.replace("eGV.", "egv").replace(/\./g, "");

        //OpenLayers.Element.addClass(this.div, this.displayClass);

        //Set map
        this.setMap();

        return true;
    },

    drawPrintForm: function() {

        if(this.div == null) return;

        while(this.div.firstChild) this.div.removeChild(this.div.firstChild);

        var form = document.createElement('form');
        form.action = 'javascript:void(0);';

        var header = document.createElement('h3');
        header.className ='egvControlPrintTitle';
        header.appendChild(document.createTextNode(eGV.Lang.translate("print_title")));

        var body = document.createElement('div');
        body.className = 'egvControlPrintBody';

        //creat the explaning text
        var text1 = document.createTextNode(eGV.Lang.translate("print_text1"));
        var text2 = document.createTextNode(eGV.Lang.translate("print_text2"));

        //create the link to the adobe reader
        var adobeLink = document.createElement("a");
        adobeLink.className = "egvControlPrintAdobeLink";
        adobeLink.href = "http://www.adobe.es/products/acrobat/readermain.html";
        adobeLink.target = "_blank";
        adobeLink.appendChild(document.createTextNode(eGV.Lang.translate("print_adobe")));

        var input = document.createElement('input');
        input.id = 'egvControlPrintInputButton' + this.id;
        input.className = 'egvControlPrintInputButton';
        input.type = 'submit';
        input.value = eGV.Lang.translate("print_create_pdf");

        var loading = document.createElement('div');
        loading.id = 'egvControlPrintDivLoading' + this.id;
        loading.className = 'egvControlPrintDivLoading';

        if(this.isPrinting)
            input.style.display = 'none';
        else
            loading.style.display = 'none';

        var inputClose = document.createElement('input');
        inputClose.type = 'submit';
        inputClose.id = "linkClosePrintModalWindow";
        inputClose.value = locStrings["_close_button"];

        OpenLayers.Event.observe(inputClose, "click", OpenLayers.Function.bindAsEventListener(effects.hideModalDiv, {"divId":"divPrint" }))

        form.appendChild(input);
        this.div.appendChild(header);
        body.appendChild(text1);
        body.appendChild(document.createElement('br'));
        body.appendChild(document.createElement('br'));
        body.appendChild(text2);
        body.appendChild(adobeLink);
        body.appendChild(document.createElement('br'));
        body.appendChild(document.createElement('br'));
        body.appendChild(form);
        body.appendChild(loading);
        body.appendChild(document.createElement('br'));
        body.appendChild(inputClose);

        this.div.appendChild(body);

        OpenLayers.Event.observe(input, "click", OpenLayers.Function.bindAsEventListener(this.doPrint,{"parent":this}) );

        return true;
    },

    CLASS_NAME: "eGV.Control.User.UserPrint"
});

