/**
 * $Id: roundedcornersinterface.js 76074 2009-11-19 20:53:10Z kchiu $
 * $Author: kchiu $
 * $Revision: 76074 $
 * $Name$
 * $Date: 2009-11-19 12:53:10 -0800 (Thu, 19 Nov 2009) $
 *
 * @jsRequire DomUtils
 * @jsRequire interfaces.Interface
 * @jsRequire classes.ResourceManager
 *
 *
 * @version    $Revision: 76074 $
 * @author     Philip Snyder <philip@pricegrabber.com>
 * @copyright  Copyright &copy; 2006, Philip Snyder, PriceGrabber.com
 * @see        interfaces.Interface
 */

/**
 * RoundedCornersInterface Constructor / Definition
 *
 * This interface is built on top of the Interface object
 * and is NOT intended to be instantiated directly. See
 * documentation on interfaces.Interface for a complete
 * explanation.
 *
 * @access public
 * @since  v1.1
 * @return RoundedCornersInterface
 */
function RoundedCornersInterface() {
    this.elemId       = null;
    this.cornerStyle  = null;
    this.init         = RoundedCornersInterface_Init;
    this.draw         = RoundedCornersInterface_Draw;
    this.erase        = RoundedCornersInterface_Erase;
    this.cornersDrawn = false;
    this.init();
}

// Setup RoundedCornersInterface prototype chain
RoundedCornersInterface.prototype             = new Interface;
RoundedCornersInterface.prototype.constructor = RoundedCornersInterface;
RoundedCornersInterface.superclass            = Interface.prototype;






/****** BEGIN EDIT SECTION ******/

/**
 * Rounded corners style definitions.
 *
 * This data structure allows for the definition of various different
 * rounded corners "styles" that can be applied by setting the roundedCornersStyle
 * of the object implementing RoundedCornersInterface.
 *
 * Example:
 *
 *    <script language="JavaScript">
 *    RoundedCornersInterface.settings.style = 'Default';
 *    var popup = new RoundedCornersPopup();
 *    </script>
 *
 * You can also define a rounded corner style at any time after
 * inclusion of this file and then reference that style instead if you
 * like.
 *
 * Note: This style definition is site-wide, not object specific. This
 * will be changing in the near future as this interface adopts the
 * same style interface that DropShadowInterface uses.
 *
 * @access public
 * @since  v1.1
 * @todo   Add .roundedCornersStyle to interface (see DropShadowInterface)
 * @var    RoundedCornersInterface.styles   struct
 */
RoundedCornersInterface.styles = {
    Default: {
        images: {
            tr: { src: 'http://i.pgcdn.com/images/balloon/top_right_6x6.gif',    width: 6, height: 6 },
            tl: { src: 'http://i.pgcdn.com/images/balloon/top_left_6x6.gif',     width: 6, height: 6 },
            bl: { src: 'http://i.pgcdn.com/images/balloon/bottom_left_6x6.gif',  width: 6, height: 6 },
            br: { src: 'http://i.pgcdn.com/images/balloon/bottom_right_6x6.gif', width: 6, height: 6 }
        }
    }
};

/**
 * General settings struct.
 *
 * @access public
 * @since  v1.1
 * @var    RoundedCornersInterface.settings  struct
 *
 */
RoundedCornersInterface.settings = { style: 'Default' };

/****** END EDIT SECTION ******/








/**
 * Internal image ids used for keeping track of image resources.
 *
 * !DO NOT MODIFY THIS!
 *
 * @access private
 * @since  v1.1
 * @var    RoundedCornersInterface.imgIds   struct
 */
RoundedCornersInterface.imgIds   = { tr: 0, tl: 1, bl: 2, br: 3 };

/**
 * Initializes an object that implements the RoundedCornersInterface.
 * Call this function prior to any draw() functions.
 *
 * @access public
 * @since  v1.1
 * @return void
 */
function RoundedCornersInterface_Init() {
    var style = RoundedCornersInterface.styles[RoundedCornersInterface.settings.style];
    if (typeof(window.preloadImage) == 'function') {
        window.preloadImage(style.images.tr.src, style.images.tr.width, style.images.tr.height);
        window.preloadImage(style.images.tl.src, style.images.tl.width, style.images.tl.height);
        window.preloadImage(style.images.bl.src, style.images.bl.width, style.images.bl.height);
        window.preloadImage(style.images.br.src, style.images.br.width, style.images.br.height);
    }
} // End RoundedCornersInterface_Init

/**
 * "Draws" the actual rounded corners by creating the necessary dom elements and applying them to
 * the object's element.
 *
 * @access public
 * @since  v1.1
 * @return void
 */
function RoundedCornersInterface_Draw() {
    //window.messageQueue.add( new Message('RoundedCornersInterface_Draw', 'called') );
    if (this.cornersDrawn) { RoundedCornersInterface_Erase.call(this); }
    var elem = document.getElementById(this.elemId);
    if (elem) {
        //if (typeof(writeDebug) == 'function') writeDebug('drawing background...');
        var style = RoundedCornersInterface.styles[RoundedCornersInterface.settings.style];
        /** BACKGROUND **/
        var div                   = document.createElement('div');
        div.id                    = 'roundedCorners_Background';
        div.style.position        = 'absolute';
        div.style.left            = style.images.tl.width+'px';
        div.style.top             = style.images.tl.height+'px';
        var eHeight  = DomUtils.getElementHeight(elem);
        var tlHeight = style.images.tl.height;
        var blHeight = style.images.bl.height;
        //window.messageQueue.add( new Message(funcname(this), 'getting width') );
        var eWidth   = DomUtils.getElementWidth(elem);
        var tlWidth  = style.images.tl.width;
        var blWidth  = style.images.bl.width;
        div.style.height          = (parseInt(eHeight)-tlHeight-blHeight)+'px';
        div.style.width           = (parseInt(eWidth)-tlWidth-blWidth)+'px';
        div.style.backgroundColor = '#ffffff';
        div.style.zIndex          = '-1';
        //if (typeof(writeDebug) == 'function') writeDebug('background zIndex: '+div.style.zIndex);
        elem.appendChild(div);
        elem.style.backgroundColor = 'transparent';
        
        /** EDGES **/
        //if (typeof(writeDebug) == 'function') writeDebug('drawing bottom edge...');
        // Bottom edge
        div                       = document.createElement('div');
        div.id                    = 'roundedCorners_BottomEdge';
        div.style.position        = 'absolute';
        div.style.left            = style.images.bl.width+'px';
        div.style.top             = (parseInt(DomUtils.getElementHeight(elem))-style.images.bl.height)+'px';
        div.style.height          = (style.images.bl.height)+'px'; // -1 accounts for bottom border
        //window.messageQueue.add( new Message(funcname(this), 'getting bottom width') );
        div.style.width           = (parseInt(DomUtils.getElementWidth(elem)) - style.images.bl.width - style.images.br.width)+'px';
        div.style.borderBottom    = '1px solid #999999';
        div.style.backgroundColor = '#ffffff';
        div.style.overflow        = 'hidden';
        elem.appendChild(div);
        //if (typeof(writeDebug) == 'function') writeDebug('drawing top edge...');
        // Top edge
        div                       = document.createElement('div');
        div.id                    = 'roundedCorners_TopEdge';
        div.style.position        = 'absolute';
        div.style.left            = style.images.tl.width+'px';
        div.style.top             = '0px';
        div.style.height          = (style.images.tl.height)+'px'; // -1 accounts for top border
        //window.messageQueue.add( new Message(funcname(this), 'getting top width') );
        div.style.width           = (parseInt(DomUtils.getElementWidth(elem))-style.images.tl.width-style.images.tr.width)+'px';
        div.style.borderTop       = '1px solid #999999';
        div.style.backgroundColor = '#ffffff';
        div.style.overflow        = 'hidden';
        elem.appendChild(div);
        // Left edge
        //if (typeof(writeDebug) == 'function') writeDebug('drawing left edge...');
        div                       = document.createElement('div');
        div.id                    = 'roundedCorners_LeftEdge';
        div.style.position        = 'absolute';
        div.style.left            = '0px';
        div.style.top             = style.images.tl.height+'px';
        div.style.width           = (style.images.tl.width)+'px'; // -1 accounts for left border
        div.style.height          = (parseInt(DomUtils.getElementHeight(elem)) - style.images.tl.height - style.images.bl.height)+'px';
        div.style.borderLeft      = '1px solid #999999';
        div.style.backgroundColor = '#ffffff';
        div.style.overflow        = 'hidden';
        elem.appendChild(div);
        //if (typeof(writeDebug) == 'function') writeDebug('drawing right edge...');
        // Right edge
        div                       = document.createElement('div');
        div.id                    = 'roundedCorners_RightEdge';
        div.style.position        = 'absolute';
        //window.messageQueue.add( new Message(funcname(this), 'getting right width') );
        div.style.left            = (parseInt(DomUtils.getElementWidth(elem))-style.images.tr.width)+'px';
        div.style.top             = style.images.tr.height+'px';
        div.style.width           = (style.images.tr.width)+'px';
        div.style.height          = (parseInt(DomUtils.getElementHeight(elem))-style.images.tr.height-style.images.br.height)+'px';
        div.style.borderRight     = '1px solid #999999';
        div.style.backgroundColor = '#ffffff';
        div.style.overflow        = 'hidden';
        elem.appendChild(div);
        /** CORNERS **/
        //if (typeof(writeDebug) == 'function') writeDebug('drawing top right corner...');
        // Top right
        var img                   = document.createElement('img');
        img.id                    = 'roundedCorners_TopRightCorner';
        img.src                   = style.images.tr.src;
        img.height                = style.images.tr.height;
        img.width                 = style.images.tr.width;
        img.border                = 0;
        img.style.position        = 'absolute';
        //window.messageQueue.add( new Message(funcname(this), 'getting tr width') );
        img.style.left            = parseInt(parseInt(DomUtils.getElementWidth(elem))-(img.width))+'px';
        img.style.top             = '0px';
        elem.appendChild(img);
        //if (typeof(writeDebug) == 'function') writeDebug('drawing top left corner...');
        // Top left
        var img                   = document.createElement('img');
        img.id                    = 'roundedCorners_TopLeftCorner';
        img.src                   = style.images.tl.src;
        img.height                = style.images.tl.height;
        img.width                 = style.images.tl.width;
        img.border                = 0;
        img.style.position        = 'absolute';
        img.style.left            = '0px';
        img.style.top             = '0px';
        elem.appendChild(img);
        //if (typeof(writeDebug) == 'function') writeDebug('drawing bottom left corner...');
        // Bottom left
        var img                   = document.createElement('img');
        img.id                    = 'roundedCorners_BottomLeftCorner';
        img.src                   = style.images.bl.src;
        img.height                = style.images.bl.height;
        img.width                 = style.images.bl.width;
        img.border                = 0;
        img.style.position        = 'absolute';
        img.style.left            = '0px';
        img.style.top             = parseInt(parseInt(DomUtils.getElementHeight(elem))-parseInt(img.height))+'px';
        elem.appendChild(img);
        // Bottom right
        var img                   = document.createElement('img');
        img.id                    = 'roundedCorners_BottomRightCorner';
        img.src                   = style.images.br.src;
        img.height                = style.images.br.height;
        img.width                 = style.images.br.width;
        img.border                = 0;
        img.style.position        = 'absolute';
        //window.messageQueue.add( new Message(funcname(this), 'getting br width') );
        img.style.left            = parseInt(parseInt(DomUtils.getElementWidth(elem))-(img.width))+'px';
        img.style.top             = parseInt(parseInt(DomUtils.getElementHeight(elem))-(img.height))+'px';
        elem.appendChild(img);
        
        this.cornersDrawn = true;
    }
    //window.messageQueue.add( new Message('RoundedCornersInterface_Draw', 'finished') );
} // End RoundedCornersInterface_Draw

/**
 * "Erases" the rounded corners by removing the dom elements created with RoundedCornersInterface.draw().
 *
 * @access public
 * @since  v0.0.1a
 * @return void
 */
function RoundedCornersInterface_Erase() {
    //if (typeof(writeDebug) == 'function') writeDebug('>> RoundedCornersInterface_Erase() called.', '#000000', '#ffff00');
    var elem = document.getElementById(this.elemId);
    if (elem.hasChildNodes()) {
        //if (typeof(writeDebug) == 'function') writeDebug('elem has child nodes: '+elem.childNodes.length);
        for (var i=elem.childNodes.length; i>0; i--) {
            var nodeId = elem.childNodes[i-1].id;
            var eq = '!=';
            var idPre = 'roundedCorners_';
            if (nodeId && nodeId.substr(0, idPre.length) == idPre) {
                eq = '==';
                //elem.removeChild(elem.childNodes[i-1]);
                DomUtils.removeElement(elem.childNodes[i-1]);
            }
            //if (typeof(writeDebug) == 'function') writeDebug(nodeId.substr(0, idPre.length)+' '+eq+' '+idPre);
        }
    } //else if (typeof(writeDebug) == 'function') writeDebug('no child nodes!');
    this.cornersDrawn = false;
    //if (typeof(writeDebug) == 'function') writeDebug('<< RoundedCornersInterface_Erase() finished.', '#000000', '#ffff00');
} // End RoundedCornersInterface_Erase
