/*
 * 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: 2,
            strokeColor: "#FF0000"
        }
    },

      /**
     * Property: handlerOptions
     * {Object} Specific handler options for the Ruler control
     */
    handlerOptions: {
        style: "ruler",
        layerOptions: {
            styleMap: null
        },
        persist: true
    },

    /**
     * APIProperty: eventListeners
     * {Object} Used to override default event handlers. Object must contain one of the following memebers {"measure": <handler>, "measurepartial": <handler>}
     */
    eventListeners: null,


    /**
     *Property: divImg
     *{div} div where the image generated by the profile will be placed on the window
     */
    imgId: null,

     /**
    * Constructor: UserControlProfile
    *
    * Parameters:
    * options - {Object} Hashtable of extra options to tag onto the control
    */
    initialize: function(options){
        // We do this before the extend so that instances can override
        // className in options.
        this.displayClass =
          this.CLASS_NAME.replace("eGV.", "egv").replace(/\./g, "");

        options = options || [];
        OpenLayers.Util.extend(this, 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);

        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();
        }

        this.map.addControl(this);

    },


  /**
        * Method: handleMeasure
        *
        * Parameters:
        * evt - {Event)}
        */
        handleMeasure: function(evt){
            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=300&HEIGHT=300&BOTTOMCOLOR=white&TOPCOLOR=maroon&LOCALIZERS=1";
            var htmlElem = document.getElementById(this.imgId);
            htmlElem.src = img;
            htmlElem.style.visibility = "visible";
        },

         /**
        * Method: activateProfile
        *
        * Parameters:
        * evt - {Event)}
        */
        activateProfile: function(){
            
        },

    CLASS_NAME: "eGV.Control.User.UserControlProfile"
});
