/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 * full text of the license. */


/**
 * @requires OpenLayers/Control.js
 * @requires OpenLayers/Feature/Vector.js
 */

/**
 * Class: OpenLayers.Control.AvinetFind
 * Draws features on a vector layer when active.
 *
 * Inherits from:
 *  - <OpenLayers.Control>
 */
OpenLayers.Control.AvinetDrawFeature = OpenLayers.Class(OpenLayers.Control, {
    
    /**
     * Property: layer
     * {<OpenLayers.Layer.Vector>}
     */
    layer: null,

    /**
     * Property: callbacks
     * {Object} The functions that are sent to the handler for callback
     */
    callbacks: null,
    
    /**
     * APIProperty: featureAdded
     * {Function} Called after each feature is added
     */
    featureAdded: function() {},

    /**
     * APIProperty: handlerOptions
     * {Object} Used to set non-default properties on the control's handler
     */
    handlerOptions: null,

    /**
     * APIProperty: drawMode
     * {Object} Used to seperate between different actions
     */
    drawMode: null,
    
    /**
     * APIProperty: goToURL
     * {Object} Used to send geometry to spesified URL as a URL parameter
     */
    goToURL: null,    
    
    /**
     * Constructor: OpenLayers.Control.DrawFeature
     * 
     * Parameters:
     * layer - {<OpenLayers.Layer.Vector>} 
     * handler - {<OpenLayers.Handler>} 
     * options - {Object} 
     */
    initialize: function(layer, handler, options) {
        OpenLayers.Control.prototype.initialize.apply(this, [options]);
        this.callbacks = OpenLayers.Util.extend({done: this.drawFeature},
                                                this.callbacks);
        this.layer = layer;
        this.handler = new handler(this, this.callbacks, this.handlerOptions);
    },

    /**
     * Method: drawFeature
     */
    drawFeature: function(geometry) {
    	//Fjernar alle andre teikna objekt
    	if (this.layer.features.length > 0) {
    		this.layer.destroyFeatures();
    	}
    	   	
    	var wkt = new OpenLayers.Format.WKT();
    	var features = wkt.read(geometry);    	
		var bounds;
		if(features) {
			if(features.constructor != Array) {
				features = [features];
			}
			for(var i=0; i<features.length; ++i) {
				if (!bounds) {
					bounds = features[i].geometry.getBounds();
				} else {
					bounds.extend(features[i].geometry.getBounds());
				}

			}

			var feature = new OpenLayers.Feature.Vector(geometry);
			this.layer.addFeatures([feature]);
			this.featureAdded(feature);

			//alert(bounds.left + ' ' + bounds.bottom + ' ' + bounds.right + ' ' + bounds.top);
		} else {
			alert('Bad WKT');
		}
		
		if (queryString('digAppType') == 'POINT') {		
      try {
        var frame = parent.window.document.getElementById('skjema');
        var frameDocument = frame.contentWindow ? frame.contentWindow.document: frame.contentDocument;
        frameDocument.getElementById('x_utm33').value = Math.round(bounds.left);
        frameDocument.getElementById('y_utm33').value = Math.round(bounds.bottom);
      } catch (error) {
          alert(error);
      }						
		}
    },

    CLASS_NAME: "OpenLayers.Control.AvinetDrawFeature"
});

