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

    /** @type int */
    slideFactor: 50,

    /** @type Array of Button Divs */
    buttons: null,

	/** @type String path to the logo image */
	imgDirectory: './images/map/',

    /**
     * @constructor
     */
    initialize: function() {
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
        this.position = new OpenLayers.Pixel(OpenLayers.Control.PanZoom.X,
                                             OpenLayers.Control.PanZoom.Y);
    },

    /**
    * @param {OpenLayers.Pixel} px
    * 
    * @returns A reference to the container div for the logo control
    * @type DOMElement
    */
    draw: function(px) {
        // initialize our internal div
        OpenLayers.Control.prototype.draw.apply(this, arguments);
        px = this.position;

        // place the controls
        this.buttons = new Array();

        var logoSize = new OpenLayers.Size(115,35);				

		this._addButton("terrapagesLogo",
						"poweredbytp.png",
						new OpenLayers.Pixel(0,this.map.size.h-logoSize.h-10),
						logoSize);

        return this.div;
    },
    
    /**
     * @param {String} id
     * @param {String} img
     * @param {OpenLayers.Pixel} xy
     * @param {OpenLayers.Size} sz
     * 
     * @returns A Div (an alphaImageDiv, to be precise) that contains the 
     *          image of the button, and has all the proper event handlers
     *          set.
     * @type DOMElement
     */
    _addButton:function(id, img, xy, sz) {
        var imgLocation = OpenLayers.Util.getImagesLocation() + img;
        var btn = OpenLayers.Util.createAlphaImageDiv(
                                    "OpenLayers_Control_LogoControl_" + id, 
                                    xy, sz, imgLocation, "absolute");

        //we want to add the outer div
        this.div.appendChild(btn);

        btn.onmousedown = this.buttonDown.bindAsEventListener(btn);
        btn.action = id;
        btn.map = this.map;
        btn.slideFactor = this.slideFactor;

        //we want to remember/reference the outer div
        this.buttons.push(btn);
        return btn;
    },
    
    /**
     * @param {Event} evt
     */
    buttonDown: function (evt) {
        if (!OpenLayers.Event.isLeftClick(evt)) return;

        switch (this.action) {
            case "terrapagesLogo": 
				window.open('http://terrapages.net', "target");
                break;
        }

        OpenLayers.Event.stop(evt);
    },

    /**
     * 
     */
    destroy: function() {
        OpenLayers.Control.prototype.destroy.apply(this, arguments);
        for(i=0; i<this.buttons.length; i++) {
            this.buttons[i].map = null;
        }
    },
    
    /** @final @type String */
    CLASS_NAME: "OpenLayers.Control.LogoControl"
});
