
OpenLayers.Control.ATAdControl = 
  OpenLayers.Class(OpenLayers.Control, {

	url: "",
	top: 125,
	bottom: 0,
	left: 0,
	right: 200,
	width: 160,
	height: 0,
	maximized: false,
	
    //activeColor: "black",
    contentDiv: null,
    basecontentDiv: null,
    
   	imgLocation: '/atom/lib/AnglingTech/img/',

    datacontentDiv: null,
    minimizeDiv: null,
    maximizeDiv: null,

    initialize: function(options) {
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
    },

    destroy: function() {        
        OpenLayers.Event.stopObservingElement(this.div);
        OpenLayers.Event.stopObservingElement(this.minimizeDiv);
        OpenLayers.Event.stopObservingElement(this.maximizeDiv);
        
        OpenLayers.Control.prototype.destroy.apply(this, arguments);
    },
    
    setMap: function(map) {
        OpenLayers.Control.prototype.setMap.apply(this, arguments);
    },
 
    draw: function() {
        OpenLayers.Control.prototype.draw.apply(this);

        // create layout divs
        this.loadContents();

        // populate div with current info
        this.redraw(this.url);    

        return this.div;
    },

    redraw: function(url) {
    	//this.loadContents();
    	//this.contentDiv.innerHTML = 'Loading...';
    	//this.contentDiv.innerHTML = "<center><img src='"+ this.imgLocation + "ajaxload.gif'></center>";
    	
		OpenLayers.loadURL(url, '', this, function(response) {
				this.contentDiv.innerHTML = response.responseText;				
			},this.minimizeControls
		);    	
		

        return this.div;
    },

    maximizeControl: function(e) {

        //HACK HACK HACK - find a way to auto-size this layerswitcher
        //this.div.style.width = "20em";
        this.div.style.width = this.width + "px";
        
        //if(this.height > 0){
        //	this.div.style.height = this.height + "px";
        //}else{
        	this.div.style.height = "";
        //}
        
        this.div.style.visibility = "";
        this.showControls(false);

		this.maximized = true;

        if (e != null) {
            OpenLayers.Event.stop(e);                                            
        }
    },
    
    minimizeControl: function(e) {

        this.div.style.width = "0px";
        this.div.style.height = "0px";
        this.div.style.visibility = "hidden";

        this.showControls(true);

		this.maximized = false;
		
        if (e != null) {
            OpenLayers.Event.stop(e);                                            
        }
    },

    showControls: function(minimize) {
	
		/*
        this.maximizeDiv.style.display = minimize ? "" : "none";
        this.minimizeDiv.style.display = minimize ? "none" : "";
		*/
        this.contentDiv.style.display = minimize ? "none" : "";
    },
    
    loadContents: function() {
    
        

        //configure main div
        this.div.style.position = "absolute";
        
        if(this.bottom > 0){
        	this.div.style.bottom = this.bottom + "px";        
        }else{
        	this.div.style.top = this.top + "px";
        }
        
        this.div.style.width = this.width + "px";
                        
        if(this.right > 0){
        	this.div.style.right = this.right + "px";
        }

        if(this.left > 0){
        	this.div.style.left = this.left + "px";
        }
       
       /* 
        if(this.height > 0){
        	this.div.style.height = this.height + "px";
        	this.div.style.overflow = "auto";
        }
        */
        
        this.div.style.fontFamily = "sans-serif";
        this.div.style.fontWeight = "bold";
        this.div.style.marginTop = "3px";
        this.div.style.marginLeft = "3px";
        this.div.style.marginBottom = "3px";
        this.div.style.fontSize = "smaller";   
        this.div.style.color = "white";   
        this.div.style.backgroundColor = "transparent";

        // layers list div        
        this.contentDiv = document.createElement("div");
        this.contentDiv.id = this.id + "_contentDiv";
        this.contentDiv.style.paddingTop = "5px";
        this.contentDiv.style.paddingLeft = "5px";
        this.contentDiv.style.paddingBottom = "5px";
        this.contentDiv.style.paddingRight = "5px";
        this.contentDiv.style.backgroundColor = this.activeColor; 
        
        if(this.height > 0){
        	this.contentDiv.style.height = this.height + "px";
        	this.contentDiv.style.overflow = "auto";
        }        
        

		//rmm mods
		//only do this for MSIE!!!!
		if (OpenLayers.Util.getBrowserName() == 'msie'){		
			this.contentDiv.style.width = '100%';
		}
		this.contentDiv.style.fontFamily = 'Arial';
		this.contentDiv.style.fontSize = '10px';
		

        this.baseLbl = document.createElement("div");
        this.baseLbl.innerHTML = "<center><img src='"+ this.imgLocation + "ajaxload.gif'></center>";
        this.baseLbl.style.marginTop = "3px";
        this.baseLbl.style.marginLeft = "3px";
        this.baseLbl.style.marginBottom = "3px";
        
        this.basecontentDiv = document.createElement("div");
        this.basecontentDiv.style.paddingLeft = "10px";
        
        this.datacontentDiv = document.createElement("div");
        this.datacontentDiv.style.paddingLeft = "10px";

        this.contentDiv.appendChild(this.baseLbl);
        this.div.appendChild(this.contentDiv);
		this.div.style.width = '100%';


	/*
        OpenLayers.Rico.Corner.round(this.div, {corners: "tl bl tr br",
                                        bgColor: "transparent",
                                        color: this.activeColor,
                                        blend: false});

        OpenLayers.Rico.Corner.changeOpacity(this.contentDiv, 0.75);
*/
	/*

        var sz = new OpenLayers.Size(18,18);        

        // maximize button div
        var img = this.imgLocation + 'layer-switcher-maximize.png';
        this.maximizeDiv = OpenLayers.Util.createAlphaImageDiv(
                                    "OpenLayers_Control_MaximizeDiv", 
                                    null, 
                                    sz, 
                                    img, 
                                    "absolute");
        this.maximizeDiv.style.top = "5px";
        this.maximizeDiv.style.right = "0px";
        this.maximizeDiv.style.left = "";
        this.maximizeDiv.style.display = "none";
        OpenLayers.Event.observe(this.maximizeDiv, "click", 
            OpenLayers.Function.bindAsEventListener(this.maximizeControl, this)
        );
        
        this.div.appendChild(this.maximizeDiv);

        // minimize button div
        var img = this.imgLocation + 'x.png';
        var sz = new OpenLayers.Size(12,12);        
        this.minimizeDiv = OpenLayers.Util.createAlphaImageDiv(
                                    "OpenLayers_Control_MinimizeDiv", 
                                    null, 
                                    sz, 
                                    img, 
                                    "absolute");
        this.minimizeDiv.style.top = "5px";
        this.minimizeDiv.style.right = "0px";
        this.minimizeDiv.style.left = "";
        this.minimizeDiv.style.display = "none";
        this.minimizeDiv.style.paddingRight = "5px";
        OpenLayers.Event.observe(this.minimizeDiv, "click", 
            OpenLayers.Function.bindAsEventListener(this.minimizeControl, this)
        );
        



        this.div.appendChild(this.minimizeDiv);
        */
        
        this.registerEvents();
    },

	/*
    ignoreEvent: function(evt) {
        OpenLayers.Event.stop(evt);
    },

    mouseDown: function(evt) {
        //this.isMouseDown = true;
        //this.ignoreEvent(evt);
    },

    mouseUp: function(evt) {
       // if (this.isMouseDown) {
       //     this.isMouseDown = false;
       //     this.ignoreEvent(evt);
       // }
    },
	*/
    /** 
     * Method: registerEvents
     * Registers events on the popup.
     *
     * Do this in a separate function so that subclasses can 
     *   choose to override it if they wish to deal differently
     *   with mouse events
     * 
     *   Note in the following handler functions that some special
     *    care is needed to deal correctly with mousing and popups. 
     *   
     *   Because the user might select the zoom-rectangle option and
     *    then drag it over a popup, we need a safe way to allow the
     *    mousemove and mouseup events to pass through the popup when
     *    they are initiated from outside.
     * 
     *   Otherwise, we want to essentially kill the event propagation
     *    for all other events, though we have to do so carefully, 
     *    without disabling basic html functionality, like clicking on 
     *    hyperlinks or drag-selecting text.
     */
     registerEvents:function() {
        this.events = new OpenLayers.Events(this, this.div, null, true);

        this.events.on({
            "mousedown": this.onmousedown,
            "mousemove": this.onmousemove,
            "mouseup": this.onmouseup,
            "click": this.onclick,
            "mouseout": this.onmouseout,
            "dblclick": this.ondblclick,
            scope: this
        });
        
     },

    /** 
     * Method: onmousedown 
     * When mouse goes down within the popup, make a note of
     *   it locally, and then do not propagate the mousedown 
     *   (but do so safely so that user can select text inside)
     * 
     * Parameters:
     * evt - {Event} 
     */
    onmousedown: function (evt) {
        this.mousedown = true;
        OpenLayers.Event.stop(evt, true);
    },

    /** 
     * Method: onmousemove
     * If the drag was started within the popup, then 
     *   do not propagate the mousemove (but do so safely
     *   so that user can select text inside)
     * 
     * Parameters:
     * evt - {Event} 
     */
    onmousemove: function (evt) {
        if (this.mousedown) {
            OpenLayers.Event.stop(evt, true);
        }
    },

    /** 
     * Method: onmouseup
     * When mouse comes up within the popup, after going down 
     *   in it, reset the flag, and then (once again) do not 
     *   propagate the event, but do so safely so that user can 
     *   select text inside
     * 
     * Parameters:
     * evt - {Event} 
     */
    onmouseup: function (evt) {
        if (this.mousedown) {
            this.mousedown = false;
            OpenLayers.Event.stop(evt, true);
        }
    },

    /**
     * Method: onclick
     * Ignore clicks, but allowing default browser handling
     * 
     * Parameters:
     * evt - {Event} 
     */
    onclick: function (evt) {
        OpenLayers.Event.stop(evt, true);
    },

    /** 
     * Method: onmouseout
     * When mouse goes out of the popup set the flag to false so that
     *   if they let go and then drag back in, we won't be confused.
     * 
     * Parameters:
     * evt - {Event} 
     */
    onmouseout: function (evt) {
        this.mousedown = false;
    },
    
    /** 
     * Method: ondblclick
     * Ignore double-clicks, but allowing default browser handling
     * 
     * Parameters:
     * evt - {Event} 
     */
    ondblclick: function (evt) {
        OpenLayers.Event.stop(evt, true);
    },	
    CLASS_NAME: "OpenLayers.Control.ATAdControl"
});
