// by Chris Sperry: 3-Nov-2006
// For use with SWFObject by Geoff Stearns
//
// Usage:
//
// -Import this script into the head of your document
// -Set up SWFObject as per the documentation. In the example below, the 'holder' DIV has an id "flashmovie"
// -Change the inline script from:
//
//			var so = new SWFObject("someswf.swf", "mymovie", "300", "300", "8", "#dddddd");
//			so.write("flashmovie");
//
//		-to-
//
//			var so = new SWFObject("someswf.swf", "mymovie", "300", "300", "8", "#dddddd");
//			var soResizer = new SWFObjectResizer("flashmovie",so);
//			so.write(soResizer.getElementId()); //no longer "flashmovie" but auto-generated by SWFObjectResizer
//
// The dimensions of the SWFObject are in pixels (but do not append "px")
//
// To resize the player so the HTML document does not reflow (i.e. floating player)
//
//			soResizer.resize(600,600,true);
//
// or change the last parameter to false to cause the page to reflow (i.e. inline player)
//
// To revert the player to its original position call:
//
//			soResizer.revert()
//
// These methods can also be called from ActionScript:
//
//			ExternalInterface.call("soResizer.resize",600,600,true);
// -and-
//			ExternalInterface.call("soResizer.revert");
//
//
// There is a bug with Opera 9 and Flash Player 9.0.16.0 that slows ExternalInterface calls down to a crawl
// it does not occur using Flash Player 8.0.24.0


Delegate=new Object();
Delegate.create= function(obj, func)
{

	var f = function()
	{
		var target = arguments.callee.target;
		var func1 = arguments.callee.func;
		return func1.apply(target, arguments);
	};

	f.target = obj;
	f.func = func;

	return f;
}






function SWFObjectResizer(flashDivId,so)
{
	if(SWFObjectResizer.count==null)
	{
		SWFObjectResizer.count=0;
	}
	else
	{
		SWFObjectResizer.count++;
	}
	this.instanceCount=SWFObjectResizer.count;
	var uniqStr=this.getUniqueString();
	this.origWidth=this.width=so.getAttribute("width");
	this.origHeight=this.height=so.getAttribute("height");
	so.setAttribute("width","100%")
	so.setAttribute("height","100%")
	var flashDiv=document.getElementById(flashDivId);
	flashDiv.innerHTML='<div id="flashDivDummy'+uniqStr+'"></div><div id="flashDivContainer'+uniqStr+'" style="position:absolute;"><div id="flashDiv'+uniqStr+'" style="width:100%;height:100%;">'+flashDiv.innerHTML+'</div></div>';
	SWFObjectResizer.addEvent(window,"load",Delegate.create(this,this.pageLoad));
	

}
SWFObjectResizer.prototype.getElementId=function()
{
	return "flashDiv"+this.getUniqueString();
}
SWFObjectResizer.addEvent=function(target,eventName,func)
{
	
	if(target.addEventListener)
	{
		//W3C
		target.addEventListener(eventName,func,false);
	}
	else if(target.attachEvent)
	{
		//IE
		eventName="on"+eventName;
		target.attachEvent(eventName,func);
	}
	else
	{
		alert("Failed to add event : "+event);
	}
	
}
SWFObjectResizer.prototype.getUniqueString=function()
{
	if(this.uniqueString==null)
	{
		var time_str=new Date().getTime().toString();
		this.uniqueString=
			"_"+
			(Number(time_str.substr(0,7))).toString(36)+
			(Number(time_str.substr(7))).toString(36)+
			(Math.floor(Math.random()*1679616).toString(36))+
			"_"+
			this.instanceCount.toString(36);
	}
	return this.uniqueString;
}

SWFObjectResizer.prototype.pageLoad=function(ev)
{
	var uniqStr=this.getUniqueString();
	this.containerDiv=document.getElementById("flashDivContainer"+uniqStr);
	this.dummyDiv=document.getElementById("flashDivDummy"+uniqStr);
	this.dummyDiv.style.width=this.width+"px";
	this.dummyDiv.style.height=this.height+"px";
	var pos=SWFObjectResizer.findPos(this.dummyDiv);
	this.containerDiv.style.left=pos[0]+"px";
	this.containerDiv.style.top=pos[1]+"px";
	this.containerDiv.style.width=this.width+"px";
	this.containerDiv.style.height=this.height+"px";
	SWFObjectResizer.addEvent(window,"resize",Delegate.create(this,this.positionSync));
	setInterval(Delegate.create(this,this.positionSync),100);
}
SWFObjectResizer.prototype.positionSync=function()
{
	var pos=SWFObjectResizer.findPos(this.dummyDiv);
	this.containerDiv.style.left=pos[0]+"px";
	this.containerDiv.style.top=pos[1]+"px";
}

SWFObjectResizer.prototype.revert=function()
{
	this.resize(this.origWidth,this.origHeight,false);
}
SWFObjectResizer.prototype.resize=function(width,height,floating)
{
	width = Math.round(width);
	height = Math.round(height);
	//alert("resizing! "+width+" "+height+" "+floating); 
	this.width=width;
	this.height=height;
	if(!floating)
	{
		this.dummyDiv.style.width=this.width+"px";
		this.dummyDiv.style.height=this.height+"px";
		
	}
	this.containerDiv.style.width=this.width+"px";
	this.containerDiv.style.height=this.height+"px";
}
SWFObjectResizer.findPos=function(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}