////////////////////////////////////////////////////////////////
////////////////// dependencies ////////////////////
////////////////////////////////////////////////////////////////
/**
 * jQuery include
 */
//	document.write('<script type="text/javascript" src="../js/jquery.min.js"></script>');

////////////////////////////////////////////////////////////////
////////////////// class constants ////////////////////
////////////////////////////////////////////////////////////////
/**
 * debug mode
 */
JsFrameBase.prototype.ALLOW_FIREBUG = false;

/**
 * Log constants
 */
JsFrameBase.prototype.TRACE_ERROR = "error";
JsFrameBase.prototype.TRACE_WARNING = "warning";
JsFrameBase.prototype.TRACE_DEBUG = "debug";
JsFrameBase.prototype.nextZIndex = 20000;

/**
 * class name
 */
JsFrameBase.prototype.className = "JsFrameBase";

////////////////////////////////////////////////////////////////
////////////////// JsFrame class ////////////////////
////////////////////////////////////////////////////////////////
/**
 * The Frame component lets you display HTML content in your AS2 or AS3 application.
 * In ActionScript or in Flash with the component inspector, use the location property to specify the URL of an HTML page whose content is displayed in place of the Flash component. 
 * Also you can specify the URL of an object to be placed in <object> and <embed> HTML tags, a plugin detection will occure. 
 * Each action on the Flash component reflects on the HTML frame through the JSFrame class and JSFrame class proxies the frame events to the Flash component.
 * @class	JsFrameBase
 * @author	lex@silex-ria.org
 */
function JsFrameBase($id)
{
	////////////////////////////////////////////////////////////////
	//////////////////		attributes		////////////////////
	////////////////////////////////////////////////////////////////
	/**
	 * the id given by createFrame
	 */
	this.idFrame;
	/**
	 * static reference to the frame container in the dom
	 */
	this.frameContainer;
	/**
	 * reference to the javascript frame object (div or iframe)
	 */
	this.frame;
	
	/**
	 * [private] true if the div has to be redrawn
	 */
	this.isHtmlTextDirty = false;

	////////////////////////////////////////////////////////////////
	////////////////// 	setter & getter	//////////////////
	////////////////////////////////////////////////////////////////
	/**
	 * [private] if true then use iframe, or use div otherwise
	 * when this value changes, toggle to iFrame if a div was already instanciated
	 * depends on the use of htmlText (div) or location (iframe)
	 */
	this._useIframe = true;
	this.getUseIframe =	function ()
	{
		return this._useIframe;
	}
	this.setUseIframe = function ($val)
	{
		if (this.frame && this._useIframe != $val)
		{
			// we need to remove the existing frame and put a div instead of a frame or vice versa
			this.exitDom();
			this._useIframe = $val;
			this.enterDom();
		}
		else
			this._useIframe = $val;
	}

	/**
	 * The URL to be displayed.
	 */
	this._location = "";
	this.getLocation = function ()
	{
		return this._location;
	}
	this.setLocation = function ($val)
	{
		if ($val && $val != "" && $val != this._location)
		{
			this.trace("location set to "+$val);
			// force the content to redraw
			this.isHtmlTextDirty = true;
			// unset the htmlText property
			this._htmlText = "";
			// store the new location
			this._location = $val;
			// toggle to iFrame if needed
			this.setUseIframe(true);					
			// apply changes
			this.applyFrameProperties();
		}
	}
	/**
	 * html code of the content of the frame.
	 */
	this._htmlText = "";
	this.getHtmlText = function ()
	{
		return this._htmlText;
	}
	this.setHtmlText = function ($val)
	{
		if ($val && $val != "" && $val != this._htmlText)
		{
			this.trace("htmlText set to "+$val);
			// force the content to redraw
			this.isHtmlTextDirty = true;
			// store the new htmlText
			this._htmlText = $val;
			// unset the location property
			this._location = "";
			// toggle to DIV if needed
			this.setUseIframe(false);
			// apply changes
			this.applyFrameProperties();
		}
	}
	/**
		position and dimentions attributes
	 */
	/**
	 * x position
	 */
	this._x = 0;
	this.getX = function ()
	{
		return this._x;
	}
	this.setX = function ($val)
	{
		this._x = $val;
		this.applyFrameProperties();
	}
	/**
	 * y position
	 */
	this._y = 0;
	this.getY = function ()
	{
		return this._y;
	}
	this.setY = function ($val)
	{
		this._y = $val;
		this.applyFrameProperties();
	}
	/**
	 * width
	 */
	this._width = 100;
	this.getWidth = function ()
	{
		return this._width;
	}
	this.setWidth = function ($val)
	{
		this._width = $val;
		this.applyFrameProperties();
	}
	/**
	 * height
	 */
	this._height = 100;
	this.getHeight = function ()
	{
		return this._height;
	}
	this.setHeight = function ($val)
	{
		this._height = $val;
		this.applyFrameProperties();
	}

	////////////////////////////////////////////////////////////////
	////////////////// 	methods	//////////////////
	////////////////////////////////////////////////////////////////
	/**
	 * @constructor
	 */
	this.JsFrameBase = function($id)
	{
		this.trace("constructor - id: "+this.idFrame);
		
		// store id
		this.idFrame = $id;
	}
	/**
	 * force to redraw the content
	 */
	this.redraw = function ()
	{
		this.isHtmlTextDirty = true;
		this.applyFrameProperties();
	}
	/**
	 * log function (abstraction layer)
	 */
	this.trace = function($obj,$level)
	{
		if (this.ALLOW_FIREBUG == true && typeof console != "undefined") 
		{
			try 
			{
				switch($level)
				{
					case this.TRACE_ERROR:
						console.error(this.className+" - "+this.idFrame+": ",$obj);
						break;
					case this.TRACE_WARNING:
						console.warning(this.className+" - "+this.idFrame+": ",$obj);
						break;
					default:
						console.log(this.className+" - "+this.idFrame+": ",$obj);
				}
			}
			catch(e)
			{
			}
		}
		else
		{
			// 				alert(this.className+" - "+this.idFrame+": ",$obj);
		}
	}
	/**
	 * [private] creates the iframe or div object in dom. 
	 */
	this.enterDom = function()
	{
		if (!this.frame)
		{
			this.trace("enterDom "+this.idFrame+" - useIframe = "+this.getUseIframe());

			// if the frame container doesn't exist, then create it and store it int the static variable frameContainer
			if (!$('#frameContainer').length)
				$('body').append('<div style="z-index:'+(this.nextZIndex++)+'" id="frameContainer"></div>'); // jb modif

			JsFrameBase.frameContainer = $('#frameContainer');

			// create the frame	
			var $frame;
			if (this.getUseIframe() == true)
				$frame = $('<iframe style="background-color:transparent; border-width:0" id="'+this.idFrame+'">');
			else
				$frame = $('<div style="z-index: '+(this.nextZIndex++)+'; border: 0px; padding-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px;" id="'+this.idFrame+'" >');
				
				//alert(this.nextZIndex);

			$('#frameContainer').append($frame);
			
			// store the frame
			this.frame = $frame[0];
		
			// fill in the frame after it has been created
			var $refToThis = this;
			setTimeout(
				function()
				{
					//$refToThis.
					$refToThis.applyFrameProperties();
				}
			, 1 );
		}
		else
		{
			throw(new Error("enterDom - The frame is allready in the DOM: "+$id));
		}
	}
	/**
	 * [private] apply properties of JSFrame object to the javascript frame
	 */
	this.applyFrameProperties = function()
	{
		if (this.frame)
		{
			//this.trace("applyFrameProperties "+this.frame.id);
			
			// apply styles
			if (this.frame.style.position != "absolute")
				this.frame.style.position = "absolute";

			//if (this.frame.style.background-color != "transparent")
				//this.frame.style.background-color = "transparent";
				
			//if (this.frame.style.border-width != "0")
			//	this.frame.style.border-width = "0";

			// apply position and size
			//if (this.frame.offsetTop == undefined) this.frame.offsetTop = 0;
			if (this.frame.style.top != this.getY()+"px")
				this.frame.style.top = this.getY();// - this.frame.offsetTop;
				
			//this.trace(this.frame.offsetTop);

			//if (this.frame.offsetLeft == undefined) this.frame.offsetLeft = 0;
			if (this.frame.style.left != this.getX()+"px")
				this.frame.style.left = this.getX();// - this.frame.offsetLeft;
			
			if (this.frame.style.width != this.getWidth()+"px")
				this.frame.style.width = this.getWidth();
			
			if(this.frame.style.height != this.getHeight()+"px")
				this.frame.style.height = this.getHeight();
			
			// apply content
			// iframe case
			if (this.getUseIframe() == true)
			{
				if (this.isHtmlTextDirty && this.frame.src != this.getLocation())
				{
					this.frame.src = this.getLocation();
					this.isHtmlTextDirty = false;
				}
			}
			// div case
			else
			{
				if (this.isHtmlTextDirty)
				{
					this.frame.innerHTML = this.getHtmlText();
					this.isHtmlTextDirty = false;
				}
			}
		}
	}
	
	/**
	 * [private] removes the iframe or div object from dom
	 */
	this.exitDom = function ()
	{
		if (this.frame)
		{
			this.trace("exitDom "+this.idFrame);
			// remove frame
			$("#"+this.idFrame).remove();
			$("#FramedPlayerComponentAS2Communication-0").remove(); // jb modif (correction bug affichage rectangle blanc IE)
			
			// unset the frame object
			delete this.frame;
			this.frame = undefined;
		}
		else
		{
			this.trace("exitDom error: was not in the dom", this.TRACE_WARNING);
		}
	}
	/**
	 * unload / clean
	 */
	this.unload = function ()
	{
		// exit the dom
		this.exitDom();
	}
	
	////////////////////////////////////////////////////////////////
	/////////////////// call constructor /////////////////////
	////////////////////////////////////////////////////////////////
	this.JsFrameBase($id);
}

////////////////////////////////////////////////////////////////
////////////////// class constants ////////////////////
////////////////////////////////////////////////////////////////
/**
 * class name
 */
//JsFrame.prototype.className = "JsFrame";

////////////////////////////////////////////////////////////////
/////////////////	global functions		////////////////
////////////////////////////////////////////////////////////////
/**
 * window resize and swf scale mode
 */
function initJsFrameResize($resizeSwfId)
{
	//alert("initJsFrameResize "+$resizeSwfId);
	/*
	if (typeof console != "undefined") 
	{
		try 
		{
			console.warning("initJsFrameResize "+$resizeSwfId);
		}
		catch(e)
		{
		}
	}
	*/
	JsFrameBase.resizeSwfId = $resizeSwfId;
}
function onFrameResize()
{
	//alert("onFrameResize "+JsFrameBase.resizeSwfId+" - "+$("#"+JsFrameBase.resizeSwfId).width());
	if (JsFrameBase.resizeSwfId)
		$("#"+JsFrameBase.resizeSwfId)[0].onFrameResize($("#"+JsFrameBase.resizeSwfId).width(),$("#"+JsFrameBase.resizeSwfId).height(),$(window).width(),$(window).height());
}
// init right now
$(window).resize( onFrameResize );
// init later
$(document).ready(
	function()
	{
		$(window).resize( onFrameResize );
	}
);
/**
 * Creates a JSFrame object named "id" and store it in a global object
 * Called by ExternalInterface
 */
function addJsFrame ($id)
{
	// workaround IE bug JsFrame.jsFrames undefined
	//if (!JsFrame.jsFrames) alert("pas de jsframe "+$id);
	if (!JsFrameBase.jsFrames) JsFrameBase.jsFrames = new Array();

	if (JsFrameBase.jsFrames[$id])
	{
		//console.error("The frame id was allready defined: "+$id);
		throw(new Error("addJsFrame - The frame id was allready defined: "+$id));
	}
	JsFrameBase.jsFrames[$id] = new JsFrameBase($id);
	return JsFrameBase.jsFrames[$id];
}
/**
 * @return 	The frame created with this id
 * Called by ExternalInterface
 */
function getJsFrame ($id)
{
	return JsFrameBase.jsFrames[$id];
}
/**
 * Delete the frame created with this id
 * Called by ExternalInterface
 */
function removeJsFrame ($id)
{
	JsFrameBase.jsFrames[$id].unload();
	delete JsFrameBase.jsFrames[$id];
}
/*
function embedFlashPlugin($id, $urlMedia, $newId, $w, $h)
{
	alert("embedFlashPlugin "+$id+", "+$urlMedia+", "+$newId+", "+$w+", "+$h);
	var so = new SWFObject($urlMedia, $newId, $w, $h, "8", "#64696D"); 
	so.addParam("wmode", "transparent"); so.addParam("AllowScriptAccess","always"); 
	so.write($idFrame);
}
*/