AWP.Popup = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, {
	initialize:function(id, lonlat, contentSize, contentHTML, closeBox, closeBoxCallback) {
		OpenLayers.Popup.AnchoredBubble.prototype.initialize.apply(this, arguments);
    },
	destroy: function() {

        this.id = null;
        this.lonlat = null;
        this.size = null;
        this.contentHTML = null;
        
        this.backgroundColor = null;
        this.opacity = null;
        this.border = null;
        
        if (this.closeOnMove && this.map) {
            this.map.events.unregister("movestart", this, this.hide);
        }

        this.events.destroy();
        this.events = null;
        
        if (this.closeDiv) {
            OpenLayers.Event.stopObservingElement(this.closeDiv); 
            this.groupDiv.removeChild(this.closeDiv);
        }
        this.closeDiv = null;
        
        this.div.removeChild(this.groupDiv.parentNode);
        this.groupDiv = null;

        if (this.map != null) {
            this.map.removePopup(this);
        }
        this.map = null;
        this.div = null;
        
        this.autoSize = null;
        this.minSize = null;
        this.maxSize = null;
        this.padding = null;
        this.panMapIfOutOfView = null;
    },
	setRicoCorners:function() {
		var corners = this.getCornersToRound(this.relativePosition);
		var options = {
			corners: corners,
			color: this.backgroundColor,
			bgColor: "transparent",
			blend: false,
			border: 'red'
		};

        if (!this.rounded) {
            AWP.Corner.round(this.div, options);
            this.rounded = true;
        }
    },
	getContentDivPadding: function() {

        //use cached value if we have it
        var contentDivPadding = this._contentDivPadding;
        if (!contentDivPadding) {
			/**
			 * this.div.parentNode may be not null but document fragment
			 */
        	if (this.div.parentNode == null || this.div.parentNode.nodeType == 11) {
	        	//make the div invisible and add it to the page        
	            //this.div.style.display = "none";
	            document.body.appendChild(this.div);
	    	}
	            
            //read the padding settings from css, put them in an OL.Bounds        
            contentDivPadding = new OpenLayers.Bounds(
                OpenLayers.Element.getStyle(this.contentDiv, "padding-left"),
                OpenLayers.Element.getStyle(this.contentDiv, "padding-bottom"),
                OpenLayers.Element.getStyle(this.contentDiv, "padding-right"),
                OpenLayers.Element.getStyle(this.contentDiv, "padding-top")
            );
    
            //cache the value
            this._contentDivPadding = contentDivPadding;

            if (this.div.parentNode == document.body) {
	            //remove the div from the page and make it visible again
	            document.body.removeChild(this.div);
	            this.div.style.display = "";
            }
        }
        return contentDivPadding;
    },
    setSize: function(contentSize) {
        this.size = contentSize.clone(); 
        
        // if our contentDiv has a css 'padding' set on it by a stylesheet, we 
        //  must add that to the desired "size". 
        var contentDivPadding = this.getContentDivPadding();
		//alert('contentDivPadding: ' + contentDivPadding)
        var wPadding = contentDivPadding.left + contentDivPadding.right;
        var hPadding = contentDivPadding.top + contentDivPadding.bottom;
		//alert('wPadding,hPadding: ' + [wPadding,hPadding])
        // take into account the popup's 'padding' property
        this.fixPadding();
        wPadding += this.padding.left + this.padding.right;
        hPadding += this.padding.top + this.padding.bottom;
		//alert('fixPadding: ' + this.padding)
		//alert('wPadding,hPadding: ' + [wPadding,hPadding])
        // make extra space for the close div
        if (this.closeDiv) {
            var closeDivWidth = parseInt(this.closeDiv.style.width);
			//alert('closeDivWidth: ' + closeDivWidth)
            wPadding += closeDivWidth + contentDivPadding.right;
			//alert('wPadding,hPadding: ' + [wPadding,hPadding])
        }
		       
        //increase size of the main popup div to take into account the 
        // users's desired padding and close div.        
        this.size.w += wPadding;
        this.size.h += hPadding;
   		//alert('this.size:' + this.size);
        //now if our browser is IE, we need to actually make the contents 
        // div itself bigger to take its own padding into effect. this makes 
        // me want to shoot someone, but so it goes.
       /* if (OpenLayers.Util.getBrowserName() == "msie") {
            this.contentSize.w += 
                contentDivPadding.left + contentDivPadding.right;
            this.contentSize.h += 
                contentDivPadding.bottom + contentDivPadding.top;alert('this.contentSize:' + this.contentSize)
        }*/
		
        if (this.div != null) {
            this.div.style.width = this.size.w + "px";
            this.div.style.height = this.size.h + "px";
        }
		//alert('contentSize: ' + contentSize)
        if (this.contentDiv != null){
            this.contentDiv.style.width = contentSize.w + "px";
            this.contentDiv.style.height = contentSize.h + "px";
        }
    },
	setOpacity:function(opacity) { 
		if (opacity != undefined) {
			this.opacity = opacity; 
		}
		
		if (this.div != null) {
			// for Mozilla and Safari
			this.div.style.opacity = this.opacity;
			
			// for IE
			ua = navigator.userAgent.toLowerCase();
    		if (ua.indexOf("msie 8.0") == -1 ) {
				this.div.style.filter = 'alpha(opacity=' + this.opacity*100 + ')';
    		}
		}
	},  
	show: function() {
		OpenLayers.Popup.prototype.show.apply(this, arguments);
		this.map.setPopupToTop(this);
	},
	calculateNewPx:function(px) {
        var newPx = px.offset(this.anchor.offset);
        
        if (!this.size) this.setSize(this.contentSize);
        
        //use contentSize if size is not already set
        var size = this.size || this.contentSize;

        var top = (this.relativePosition.charAt(0) == 't');
        newPx.y += (top) ? -size.h - 10 : this.anchor.size.h + 10;
        
        var left = (this.relativePosition.charAt(1) == 'l');
        newPx.x += (left) ? -size.w - 10 : this.anchor.size.w + 10;

        return newPx;   
    }
});
