/*
	@project:	SWFsize @ www.chargedweb.com/swfsize/
	@version:	1.1
	@author:	(c)2010 Julius Loa | jloa@chargedweb.com
	
	licence:	CC 3.0 @ http://creativecommons.org/licenses/by-sa/3.0/
	
	o---[changes]---------------------------------------------------------------
	
	[v.1.1]
		- improved performance
		- method setWidth() renamed to setSWFWidth()
		- method setHeight() renamed to setSWFHeight()
		- method getWidth() renamed to getSWFWidth()
		- method getHeight() renamed to getSWFHeight()
	[v.1.0]
		- added setWidth() method
		- added setHeight() method
		- added getWidth() method
		- added getHeight() method
		- added getWindowWidth() method
		- added getWindowHeight() method
		- added getLeftX() method
		- added getRightX() method
		- added getTopY() method
		- added getBottomY() method
		- added setScrollX() method
		- added getScrollX() method
		- added setScrollY() method
		- added getScrollY() method
		@FIXME:	[not for IE] have to deduct the scrollbars width/height @see desciption below
		
	o---------------------------------------------------------------------------
	
	o---[example]---------------------------------------------------------------
	
	A small util for runtime size changing within the swf via ExternalInterface;
	
	html usage (with swfobject):
	
	<script type="text/javascript" language="javascript" src="include/js/swfobject.js"></script>
	<script type="text/javascript" language="javascript" src="include/js/swfsize.js"></script>
	<script type="text/javascript">
	var attributes = {};
	attributes.id = "swfID";
	attributes.name = "swfID";
	
	var flashvars = {};
	
	var params = {};
	params.menu = "false";
	params.bgcolor = "#ffffff";
	params.quality = "high";
	params.allowFullScreen = "true";
	params.allowScriptAccess = "always";
	swfobject.embedSWF("mysite.swf", "flashContent", "550", "400", "9.0.0","include/swf/expressInstall.swf", flashvars, params, attributes);
	
	// now all you need to do is pass the attributes.id to SWFSize
	swfsize.id = attributes.id;
	</script>
	
	AS3 usage: @see the com.chargedweb.swfsize.SWFSize class	
	
	o---------------------------------------------------------------------------
*/

swfsize = {};
swfsize.id = "";
swfsize.valueAbs = "px"; swfsize.valueRel = "%";
swfsize.browser = navigator.appName;
swfsize.isIE = (swfsize.browser.indexOf("Microsoft") >= 0);
swfsize.lX = 0; swfsize.rX = 0; swfsize.tY = 0; swfsize.bY = 0; swfsize.wH = 0; swfsize.wW = 0; swfsize.sW = 0; swfsize.sH = 0; swfsize.swfW = 0; swfsize.swfH = 0;  swfsize.scrX = 0; swfsize.scrY = 0;
swfsize.init = function()
{
	if(!this.available()){ this.errorID(); return 0; }
	this.targetSWF().focus();
	window.onresize = this.onWindowResizeHandler;
	window.onscroll = this.onWindowScrollHandler;
	if(window.addEventListener) { document.addEventListener("scroll", this.onWindowScrollHandler, false); }	
	this.onWindowLoadHandler(); return 1;
}
swfsize.updateMetrics = function()
{
	this.swfW = this.getSWFWidth();
	this.swfH = this.getSWFHeight();
	this.scrX = this.getScrollX();
	this.scrY = this.getScrollY();
	this.wW = (this.isIE) ? document.body.clientWidth : window.innerWidth;
	this.wH = (this.isIE) ? document.body.clientHeight : window.innerHeight;
	
	/**
	 * @FIXME: [not for IE] have to deduct the scrollbars width/height (horizontal/vertical bars) from the swfsize.wW, swfsize.wH
	 * so that the swfsize.rX, swfsize.bY be precise; window.pageXOffset; window.pageYOffset
	 */  
	if(!this.isIE)
	{
		this.sW = this.wW - document.body.offsetWidth;
		this.sH = this.wH - document.body.offsetHeight;
		this.wW -= this.sW;
	}
	
	this.lX = document.body.scrollLeft;
	this.rX = this.lX + this.wW;
	this.tY = document.body.scrollTop;
	this.bY = this.tY + this.wH;
}
swfsize.onWindowLoadHandler = function(e)
{
	if(swfsize.available())
	{
		swfsize.updateMetrics(); swfsize.targetSWF().onWindowInit(swfsize.tY+","+swfsize.bY+","+swfsize.lX+","+swfsize.rX+","+swfsize.wW+","+swfsize.wH+","+swfsize.swfW+","+swfsize.swfH+","+swfsize.scrX+","+swfsize.scrY);
	}else{
		swfsize.errorID();
	}
}
swfsize.onWindowResizeHandler = function(e)
{
	swfsize.updateMetrics(); swfsize.targetSWF().onWindowResize(swfsize.tY+","+swfsize.bY+","+swfsize.lX+","+swfsize.rX+","+swfsize.wW+","+swfsize.wH+","+swfsize.swfW+","+swfsize.swfH+","+swfsize.scrX+","+swfsize.scrY);
}
swfsize.onWindowScrollHandler = function(e)
{
	swfsize.updateMetrics(); swfsize.targetSWF().onWindowScroll(swfsize.tY+","+swfsize.bY+","+swfsize.lX+","+swfsize.rX+","+swfsize.wW+","+swfsize.wH+","+swfsize.swfW+","+swfsize.swfH+","+swfsize.scrX+","+swfsize.scrY);
}
swfsize.setSWFWidth = function(value, valueType)
{
	valueType = this.checkValueType(valueType);
	if(this.available())
	{
		this.targetSWF().width = (valueType == this.valueAbs) ? value : value + valueType;
		this.targetSWF().style.width = value + valueType;
		this.onWindowResizeHandler();
	}else{
		this.errorID();
	}
}
swfsize.setSWFHeight = function(value, valueType)
{
	valueType = this.checkValueType(valueType);
	if(this.available())
	{
		this.targetSWF().height = (valueType == this.valueAbs) ? value : value + valueType;
		this.targetSWF().style.height = value + valueType;
		this.onWindowResizeHandler();
	}else{
		this.errorID();
	}
}
swfsize.getSWFWidth = function()
{
	if(this.id != "" && this.targetSWF()) { return this.targetSWF().width; }else{ this.errorID(); return 0; }
}
swfsize.getSWFHeight = function()
{
	if(this.id != "" && this.targetSWF()) { return this.targetSWF().height; }else{ this.errorID(); return 0; }
}
swfsize.setScrollX = function(value){ var oY = (this.isIE) ? this.tY : window.pageYOffset; window.scrollTo(value, oY); this.updateMetrics(); }
swfsize.setScrollY = function(value){ var oX = (this.isIE) ? this.lX : window.pageXOffset; window.scrollTo(oX, value); this.updateMetrics(); }
swfsize.getScrollX = function(){ return (this.isIE) ? this.lX : window.pageXOffset; }
swfsize.getScrollY = function(){ return (this.isIE) ? this.tY : window.pageYOffset; }
swfsize.getWindowWidth = function(){ return this.wW; }
swfsize.getWindowHeight = function(){ return this.wH; }
swfsize.getLeftX = function(){ return this.lX; }
swfsize.getRightX = function(){ return this.rX; }
swfsize.getTopY = function(){ return this.tY; }
swfsize.getBottomY = function(){ return this.bY; }
swfsize.available = function() { return (this.id != "" && this.targetSWF()); }
swfsize.checkValueType = function(value) { value = (!value || value != this.valueAbs && value != this.valueRel) ? this.valueAbs : value; return value; }
swfsize.targetSWF = function() { return (this.isIE) ? window[this.id] : document[this.id]; }
swfsize.errorID = function() { alert("Error: Could not find the SWF container using SWFsize.id = \""+this.id+"\"\nCheck whether the swfsize.id = attributes.id"); }
