//$version:  $
// This is part of the ebay JS toolbox library
// All rights reserved.
//
// File:	 ebayFrame.js
// Desc:	 Code library for controlling frames dynamically.
//
// Change Log:
// iiimmddyyn  nnnnn  Description
// ----------  -----  --------------------------------------------------------
// gar082504          Initial version
// jce092704		  Adding more jsdocs
//

/*********************************************************************
* 
* EbayFrame<br>
* Description:<br>
* Currently used for auto resizing iframes<br>
* 
* Browsers supported:<br>
* IE 4+, NS 6+, Layers not supported,<br>
*
* Known issues:<br>
*  Mac IE has problems resizing.<br>
*
* Things to do/improve:<br>
* 1. Seperate into 2 different controls Ebayiframe and Ebayframe<br>
* 
*
* Sample simple implementation:<br>
* to do<br>
*
* Test Page URL: (please store on d-sjc-webdev or clearcase)<br>
* <a href="http://d-sjc-webdev.corp.ebay.com/jstoolbox/ebayframe/favnav_base_implementation.html">http://d-sjc-webdev.corp.ebay.com/jstoolbox/ebayframe/favnav_base_implementation.html</a><br>
*
* @constructor
* @since     1.0
* @param        pEbayDoc	EbayDoc - for ebay dom reference
* @param 		pParent		pParent - for parent object to add to correct location in eBay DOM.
* @param 		pName		Name of instance of this object.
* @param 		pCfg		Config object for each instance on the page. Allows for multiple Frames on the same page.
* @return       nothing		
*/
function EbayFrame( pEbayDoc, pParent, pName, pCfg )
{
	if ( !this.objType )
		this.objType = "EbayFrame";

	this.baseObject = EbayControl;
	this.baseObject( pEbayDoc, pParent, pEbayDoc.htmlDoc, pName );
	this.config = pCfg || ( new EBayConfig( pName ) );
	
	//Methods
	this.bindHTML = EbayFrameBindHTML;
    this.resize = EbayFrameResize;
	//this.beforeResize = null;
    //this.afterResize = null;
}
window.EbayFrame = EbayFrame;

function EbayFrameBindHTML()
{
	//Bind Single frame - name and id of frame need to be the same, EbayGetObject
    //uses ID, but other functions might use name
	if ( typeof( document.getElementById ) != "undefined" )
	{
		this.htmlElement = EbayGetObject( this.ebayDoc.htmlDoc, this.htmlElementName );
		
		// init
		if ( this.config.url )
		{
			this.htmlElement.src = this.config.url;
		}
		
		if ( typeof( this.config.autoHeight ) == "undefined" )
		{
			this.config.autoHeight = true;
		}
	}

    // TODO : bind ebayConfig to iframe object after iframe is loaded
}

function EbayFrameResize()
{
	if ( typeof( this.htmlElement ) == "undefined" )
	{
		return false;
	}
	
	if( this.beforeResize )
	{
		this.beforeResize();
	}
	
	var iframeElement = this.htmlElement;
	var iframeWindow = parent.frames[ iframeElement.name ];
	
	if ( is.safari )
	{
		iframeWindow = iframeElement;
	}
	
	// defaults
	var adjustSize = false;
	var iframeWidth = 0;
	var iframeHeight = 0;
	
	if ( iframeWindow.document.height || is.safari )
	{
		if ( is.safari )
		{
			// This will force Safari to calculate the value of iframeWindow.document.height
			var calc = iframeWindow.document.body.offsetHeight;
		}
	
		// NS 7.1, FF 1.0/Mozilla, Safari
		iframeWidth = iframeWindow.document.width;
		iframeHeight = iframeWindow.document.height + 1;
		adjustSize = true;
	}
	else if ( document.all )
	{
		if ( is.win )
		{
			// IE 6.0, Opera
			iframeWidth = iframeWindow.document.body.scrollWidth;
			iframeHeight = iframeWindow.document.body.scrollHeight;
			adjustSize = true;
		}
		else
		{
			// Mac/IE 5.2, 
			iframeWidth = iframeWindow.document.body.offsetWidth;
			iframeHeight = iframeWindow.document.body.offsetHeight;
			adjustSize = true;
		}
	}
	
	if ( adjustSize )
	{
		if ( this.config.autoWidth )
		{
			iframeElement.style.width = iframeWidth + "px";
		}
		
		if ( this.config.autoHeight )
		{
			iframeElement.style.height = iframeHeight + "px";
		}
	}
	
    if( this.afterResize )
	{
		this.afterResize();
	}
}

