/*
 * @author Eric
 */

/**
 * @class
 * 
 * @requires OpenLayers/Popup.js
 */
OpenLayers.Popup.AutoHeight = OpenLayers.Class.create();
OpenLayers.Popup.AutoHeight.prototype =
   OpenLayers.Class.inherit( OpenLayers.Popup, {

	/** @type boolean*/
	width: 200,

    /** .
 	* @type String */
    quadrant: null,

	offSetX: 0,
	offSetY: 0,
	
	offSetXForEdge: 0,
	offSetYForEdge: 0,

    /** 
    * @constructor
    * 
    * @param {String} id
    * @param {OpenLayers.LonLat} lonlat
    * @param {OpenLayers.Size} size
    * @param {String} contentHTML
    * @param {integer} width
    * @param {Boolean} closeBox
    */
    initialize:function(id, lonlat, size, contentHTML, width, closeBox) {
        var newArguments = new Array(id, lonlat, size, contentHTML, closeBox);
		if (width != undefined) {
			this.width = width;
		}
        OpenLayers.Popup.prototype.initialize.apply(this, newArguments);

    },
	
	draw: function(px) {
        if (px == null) {
            if ((this.lonlat != null) && (this.map != null)) {
                px = this.map.getLayerPxFromLonLat(this.lonlat);
            }
        }
		
		
		
		// Setting the offset for when the tooltip is on the right
		// hand side of the map so it is not hidden because of the edge
		this.offSetXForEdge = 0;
		this.calculateRelativePosition(this.lonlat);
		if (this.quadrant == 'tr' || this.quadrant =='br'){
			this.offSetXForEdge = (this.width) * -1;
		}
		
		
		
		// Setting the offset for when the tooltip is on the bottom
		// and hidden because of the edge of the map.
		// This is more difficult because the height is not static.
		// maybe more on this later.
		this.offSetYForEdge = 0;
		this.calculateRelativePosition(this.lonlat);
		if (this.quadrant == 'br' || this.quadrant =='bl'){
			this.offSetYForEdge = 0 * -1;
		}
        
        return OpenLayers.Popup.prototype.draw.apply(this, arguments);
    },
	
	setWidth:function(width){
		if (width != undefined) {
            this.width = width; 
        }
	},
	
    setSize:function(size) {
		if (size != undefined) {
            this.size = size; 
			this.width = this.size.x;
        }

        if (this.div != null) {
            this.div.style.width = this.width + "px";
            this.div.style.height = null;
        }
        if (this.contentDiv != null){
            this.contentDiv.style.width = this.width + "px";
            this.contentDiv.style.height = null;
        }
    },
	
	moveTo: function(px) {

        if ((px != null) && (this.div != null)) {
            this.div.style.left = px.x + this.offSetX + this.offSetXForEdge + "px";
            this.div.style.top = px.y + this.offSetY + this.offSetYForEdge + "px";
        }
    },
	
	offset: function(x,y) {
        this.offSetX = x;
		this.offSetY = y;
    },
	
	calculateRelativePosition:function(lonlat) {
                        
        var extent = this.map.getExtent();
        var quadrant = extent.determineQuadrant(lonlat);
        
        this.quadrant = quadrant;
    }, 
    
    CLASS_NAME: "OpenLayers.Popup.AutoHeight"
});
