



// Copyright (c) NimbleCat, 2008
/*

* Parts of this module are patterned closely after code found in container.js
* from the Yahoo User Interface library
* and therefore may be subject to Yahoo's license
* http://developer.yahoo.net/yui/license.txt
*/


/*
* this object implements an iframe which is shown below another element to hide combo boxes
* 
* */
// make sure that the required includes are there
 if (typeof NC == 'undefined') {
       alert("menu.js requires the NC JavaScript framework");
 }

  if (typeof NC.widget == 'undefined') {
       alert("button.js requires the NC JavaScript framework");
 }
 
 /*
  * constructor
  */
  
  NC.widget.IFrameShim = function( id, owner) {
		var fnSuperClass = NC.widget.IFrameShim.superclass.constructor;
		fnSuperClass.call( this, id);
		this.iframe = null;
		this.owner = owner;
		this.id = id;
  }
  
  NC.widget.IFrameShim.IFRAME_SRC = "javascript:false;";
/**
    * Number representing how much the iframe shim should be offset from each 
    * side of an Overlay instance, in pixels.
    * @property YAHOO.widget.Overlay.IFRAME_SRC
    * @default 3
    * @static
    * @final
    * @type Number
    */
    NC.widget.IFrameShim.IFRAME_OFFSET = 3;
  
  
  NC.lang.extend(NC.widget.IFrameShim, NC.widget.Element, 
  	{

	  	init: function() {
            NC.widget.IFrameShim.superclass.init.call(this);
	  	},
	  	
        /**
         * This method is a protected helper, used when constructing the DOM structure for the module 
         * to account for situations which may cause Operation Aborted errors in IE. It should not 
         * be used for general DOM construction.
         * <p>
         * If the parentNode is not document.body, the element is appended as the last element.
         * </p>
         * <p>
         * If the parentNode is document.body the element is added as the first child to help
         * prevent Operation Aborted errors in IE.
         * </p>
         *
         * @param {parentNode} The HTML element to which the element will be added
         * @param {element} The HTML element to be added to parentNode's children
         * @method _addToParent
         * @protected
         */
        _addToParent: function(parentNode, element) {
            if (parentNode === document.body && parentNode.firstChild) {
                parentNode.insertBefore(element, parentNode.firstChild);
            } else {
                parentNode.appendChild(element);
            }
        },

		createIframe: function( ) {

			var oIFrame = this.iframe;
			var oParent = this.owner;
/*
                    oElement = this.element,
                    oParent;
 */
                if (!oIFrame) {
                    oIFrame = document.createElement("iframe");
                    oIFrame.id = this.id;

                    if (this.isSecure) {
                        oIFrame.src = NC.widget.IFrame.IFRAME_SRC;
                    }

                    /*
                        Set the opacity of the <iframe> to 0 so that it 
                        doesn't modify the opacity of any transparent 
                        elements that may be on top of it (like a shadow).
                    */

                    if (NC.env.ua.ie) {
                        oIFrame.style.filter = "alpha(opacity=0)";
                        /*
                             Need to set the "frameBorder" property to 0 
                             supress the default <iframe> border in IE.  
                             Setting the CSS "border" property alone 
                             doesn't supress it.
                        */
                        oIFrame.frameBorder = 0;
                    }
                    else {
                        oIFrame.style.opacity = "0";
                    }

                    oIFrame.style.position = "absolute";
                    oIFrame.style.border = "none";
					/*
                    oIFrame.style.border.style = "solid";
                    oIFrame.style.border.width = "1";
                    oIFrame.style.border.color = "#000";
              		*/
                    oIFrame.style.margin = "0";
                    oIFrame.style.padding = "0";
                    oIFrame.style.display = "none";
 
//                    var parentNode = oParent || document.body;
                    var parentNode = document.body;

                    this._addToParent(parentNode, oIFrame);
                    this.iframe = oIFrame;
                }
		},
		
		setShimIframe: function( shim) {
			this.iframe = shim;
		},

        /**
        * Shows the iframe shim, if it has been enabled.
        * @method showIframe
        */
        showIframe: function () {
            if (this.iframe) {
                this.iframe.style.display = "block";
            }
        },

        /**
        * Hides the iframe shim, if it has been enabled.
        * @method hideIframe
        */
        hideIframe: function () {
            if (this.iframe) {
                this.iframe.style.display = "none";
            }
        },

        /**
        * Syncronizes the size and position of iframe shim to that of its 
        * corresponding Overlay instance.
        * @method syncIframe
        */
        syncIframe: function ( ) {

            var oIFrame = this.iframe,
            	oElement = this.owner,
                nOffset = NC.widget.IFrameShim.IFRAME_OFFSET,
                nDimensionOffset = (nOffset * 2),
                aXY;

            if (oIFrame) {
                // Size <iframe>
				var region = NC.util.Region.getRegion( oElement);
                oIFrame.style.width = (region.right - region.left + nDimensionOffset + "px");
                var height = region.bottom - region.top;
                if (height < 3) { // hack - height takes some time to be set after an ajax update?
                	height = 500;
                }
                oIFrame.style.height = (height + nDimensionOffset + "px");

                // Position <iframe>
                aXY = NC.util.Dom.getAbsoluteXY( oElement);
                if (oElement.style.position == 'absolute') {
				  	aXY[0] = -nOffset;
				  	aXY[1] = -nOffset;
                }
                else {
                	aXY[0] -= nOffset;
                	aXY[1] -= nOffset;
                }
                NC.util.Dom.setAbsoluteXY(oIFrame, aXY, true);
            }
            else {
//            	NC.util.Debug.writeToConsole( "syncIframe: shim iframe is null");
            }
        },

        /**
         * Sets the zindex of the iframe shim, if it exists, based on the zindex of
         * the Overlay element. The zindex of the iframe is set to be one less 
         * than the Overlay element's zindex.
         * 
         * <p>NOTE: This method will not bump up the zindex of the Overlay element
         * to ensure that the iframe shim has a non-negative zindex.
         * If you require the iframe zindex to be 0 or higher, the zindex of 
         * the Overlay element should be set to a value greater than 0, before 
         * this method is called.
         * </p>
         * @method stackIframe
         */
        stackIframe: function ( ) {
            if (this.iframe) {
                var overlayZ = NC.util.Dom.getStyle(this.owner, "zIndex");
                if (!NC.lang.isUndefined(overlayZ) && !isNaN(overlayZ)) {
                    NC.util.Dom.setStyle(this.iframe, "zIndex", (overlayZ - 1));
                }
            }
            else {
//            	NC.util.Debug.writeToConsole( "stackIframe: shim iframe is null");
            }
        },
        
        /*
         * call this function to create the iframe
         */
        createAndConfig: function() {
        	if (this.iframe == null) {
            	NC.util.Debug.writeToConsole( "stackIframe: shim iframe is null");
        	}
        	this.createIframe();
        	this.showIframe();
        	this.syncIframe();
        	this.stackIframe();
        },

        config: function() {
        	this.showIframe();
        	this.syncIframe();
        	this.stackIframe();
        }

	  	
  	});
 