/**
 * This class manages an absolutely-positioned rectangle in Gigapan Explorer
 * application.  It will have Stack widgets as children.  Note that the background
 * color for the dom object managed by this widget defaults to <code>transparent</code>.
 * <br><br>
 * <a href="mailto:ehudsons@andrew.cmu.edu">Ellen Hudson-Snyder</a>, 
 * <a href="mailto:evedar@andrew.cmu.edu">Elvin Vedar</a>,
 * <a href="mailto:cbalz@andrew.cmu.edu">Christopher M. Balz</a>.
 * <br><br>CVS Version Info:<br>
 *  $Id: AbstractStack.js,v 1.19 2005/12/27 01:05:21 cbalz Exp $  
 * <br><br>
 * @inherits-from <code>AbstractWidget</code>
 * @object-prop <code>number</code>  <code>intLeftPx</code> The left position of this stack
 * @object-prop <code>number</code>  <code>intTopPx</code> The top position of this stack
 * @object-prop <code>Widget</code>  <code>wgtImageLayer</code> The <code>ImageLayer</code> managed by this class. 
 *                                   This property is temporary and will be replaced by the collection below.
 * @object-prop <code>Array</code>  <code>arrImageLayers</code> The collection of <code>ImageLayer</code>s managed 
 *                                  by objects of this class. Later, this may be changed to be a linked list.
 * @object-prop <code>string</code>  <code>strBackgroundColor</code>  The background color of the stack.  This may 
 *                                   be set for testing purposes by invoking <code>setRandomBackgroundColor</code>.
 * @object-prop <code>boolean</code>  <code>booTurnOnRandomBackgroundColors</code>  The stack will draw itself with
 *                                    a randomly colored background if this value is set to <code>true</code>.  This
 *                                    should generally be set to the value of <code>self.gloScope.booDebugMode</code>.
 * @author Team GigaToasted (Fall-2005-CMU-NASA/Google-Practicum Subteam) 
 * @version 1.0
 * <br><br>
 */ 


// Set the prototype chain for efficient inheritance:
AbstractStack.prototype = new AbstractWidget();

/**
 * Create an  <code>AbstractStack</code> object.
 * @param objParent object  The parent, if any, of this object.
 */
function AbstractStack(objParent) {
    this.superC("abstractStack", objParent); // Complete the inheritance.

    this.intLeftPx = 0;
    this.intTopPx = 0;
    this.intHeightPx = 0;
    this.intWidthPx = 0;
    this.strBackgroundColor = "transparent";
    this.booTurnOnRandomBackgroundColors = self.gloScope.booDebugMode;

    // Methods
    this.superC = AbstractStack_superConstructor;
    this.superSuperC = AbstractStack.prototype.superC;

    this.setLeftPx = AbstractStack_setLeftPx;
    this.getLeftPx = AbstractStack_getLeftPx;
    this.setTopPx = AbstractStack_setTopPx;
    this.getTopPx = AbstractStack_getTopPx;
    this.setImageLayer = AbstractStack_setImageLayer; // Temporary until upgrade to collection of images.
    this.getImageLayer = AbstractStack_getImageLayer; // Temporary until upgrade to collection of images.
    this.setImageSources = AbstractStack_setImageSources;
    this.receiveEvent = AbstractStack_receiveEvent;
    this.getHtml = AbstractStack_getHtml;
    
    this.setRandomBackgroundColor = AbstractStack_setRandomBackgroundColor;
}


function AbstractStack_superConstructor(strId, objParent) {
    this.superSuperC(strId, objParent);
    this.wgtImageLayer = new ImageLayer(this); // To be replaced by collection below.
    this.arrImageLayers = [ this.wgtImageLayer ];
}


/**
 * Set the image url properties of all images in the <code>Stack</code> objects
 * collectin. 
 * @param pArrImgSrc The array of image source urls.
 */
function AbstractStack_setImageSources(pArrImgSrcs) {
    var intL = pArrImgSrcs.length, i=0;
    
    for (; i < intL; i++) {
        this.arrImageLayers[i].setImageSrc(pArrImgSrcs[i]);
    }
}


/**
 * Set the intLeftPx value in the <code>AbstractStack</code> widget and its child widgets.    
 * @param pIntLeftPx The LeftPx value to be set in this <code>Stack</code>
 */
function AbstractStack_setLeftPx(pIntLeftPx) {
    this.intLeftPx = pIntLeftPx;  
}


/**
 * Get the left pixel value in the <code>AbstractStack</code> widget and its child widgets.    
 * @return pIntLeftPx The LeftPx value to in this <code>Stack</code>
 */
function AbstractStack_getLeftPx() {
    return this.intLeftPx;
}


/**
 * Set the intTopPx value in the <code>AbstractStack</code> widget and its child widgets.    
 * @param pIntTopPx The TopPx value to be set in this <code>Stack</code>
 */
function AbstractStack_setTopPx(pIntTopPx) {
    this.intTopPx = pIntTopPx;  
}


/**
 * Get the top pixel value in the <code>AbstractStack</code> widget and its child widgets.    
 * @return pIntTopPx The top pixel value to in this <code>Stack</code>
 */
function AbstractStack_getTopPx() {
    return this.intTopPx;
}


/**
 * Set the <code>ImageLayer</code> in the <code>AbstractStack</code> widget and its child widgets.    
 * @param pWgtImageLayer The <code>ImageLayer</code> value to be set in this <code>Stack</code>
 */
function AbstractStack_setImageLayer(pWgtImageLayer) {
    this.wgtImageLayer = pWgtImageLayer;  
}


/**
 * Get the <code>ImageLayer</code> in the <code>AbstractStack</code> widget and its child widgets.    
 * @param pWgtImageLayer The <code>ImageLayer</code> value to be set in this <code>Stack</code>
 */
function AbstractStack_getImageLayer() {
    return this.wgtImageLayer;  
}


/**
 * This method receives any events that this object listens to and 
 * routes them to the proper methods. 
 * @param pStrEventSourceElementId  The i.d. of the document object model from which the event sprang.
 * @param pStrEventType  The event type of the object (e.g., 'onclick', etc).
 * @param pObjEvent  The event object itself.
 */
function AbstractStack_receiveEvent(pStrEventSourceElementId, pStrEventType, pObjEvent) { 
    // To-Do
}


/**
 * Return the HTML of the object managed by the <code>AbstractStack</code> widget and its child widgets.    
 * @param <code>pObjStringBuffer</code> Where the resulting string will go
 */

function AbstractStack_getHtml(pObjStringBuffer) {
    if (this.booTurnOnRandomBackgroundColors) {
        this.setRandomBackgroundColor();
    }

    pObjStringBuffer.append( [ "<div id='", this.strId, "' class='stack' style='top: ", this.intTopPx, "px; left: ",
                               this.intLeftPx, "px; height: ", this.intHeightPx, "px; width: ", this.intWidthPx,
                               "px;  background-color: ", this.strBackgroundColor, "; '>" ] );
    this.wgtImageLayer.getHtml(pObjStringBuffer);
    pObjStringBuffer.append( [ "</div>" ] );
}


/**
 * This method sets the background color of the stack to a random color.  
 * It is used for testing purposes.
 */
function AbstractStack_setRandomBackgroundColor() {


    /**
       = This inner method gets a new color reference in hexadecimal format.
       * It is based with permission on code from  from a script at 
       * The JavaScript Source!! http://javascript.internet.com 
       * Original:  Randall Goya (rgoya@yahoo.com )    
       */
    function AbstractStack_setRandomBackgroundColor_newHex() {	//returns "strRandomHexNum" - a random single digit hexadecimal between 0-15
        var intRawRGB = Math.floor(Math.random()*16), strRandomHexNum;
        
        //Convert to Hexadecimal
        if (intRawRGB==10) {
            strRandomHexNum="a";
        }		
        else if (intRawRGB==11) {
            strRandomHexNum="b";
        }
        else if (intRawRGB==12) {
            strRandomHexNum="c";
        }
        else if (intRawRGB==13) {
            strRandomHexNum="d";
        }
        else if (intRawRGB==14) {
            strRandomHexNum="e";
        }
        else if (intRawRGB==15) {
            strRandomHexNum="f";
        }
        else {
            strRandomHexNum=intRawRGB;
        }

        return strRandomHexNum;
    }


    /**
     = This inner method is based with permission on code from  from a script at 
     * The JavaScript Source!! http://javascript.internet.com 
     * Original:  Randall Goya (rgoya@yahoo.com )    
     */
    function AbstractStack_setRandomBackgroundColor_getColor() {	//returns "color" a full 6 digit hexadecimal html color code
        var intCount=0, strColor="", strRandomHexNum;

        while (intCount < 6) {
            intCount++;       
            strRandomHexNum = AbstractStack_setRandomBackgroundColor_newHex();
            strColor = strColor + strRandomHexNum;
        }

        return strColor;
    }
    
    

    this.strBackgroundColor = "#" + AbstractStack_setRandomBackgroundColor_getColor();

    if (this.getDomRef() != null) {
        this.getDomRef().style.backgroundColor = this.strBackgroundColor;
    }
}
