/**
 * 
 *	SWFForce Resize
 * 	@author : Nicolas Bush
*	@company : Megalo(s)
*	@version 1.2
*	Fonctionne sous ie8, ie7, ie6, firefox 3 win, firefox 3.5 win, firefox 3.5 mac, Safari 4.0.3 mac, Opera 10.10 win, Chrome 3.0.195.25 win
*
 */
function SWFForceSize( idDiv, minWidth, minHeight, scrollAlwaysVisible)
{
	if (!_ctrCallFromStatic){
		alert("/!\\ Warning: SWFForceSize must be call from static method : SWFForceSize.init()");
	}

	this.div = idDiv;
	this.minW = minWidth;
	this.minH = minHeight;
	this.scrollAlwaysVisible = scrollAlwaysVisible == null ? false : scrollAlwaysVisible;
	
	this.IE = "Microsoft Internet Explorer";
	this.NETSCAPE = "Netscape";
	this.WEBKIT = "WebKit";
	
	var o = this;
	this.addWindowEvent( 'onload', this, this.onLoadDiv );
	this.addWindowEvent( 'onresize', this, this.onResizeDiv );
}

var _swfForceSizeInstance;
var _ctrCallFromStatic = false;


SWFForceSize.init = function(idDiv, minWidth, minHeight, sizeUpdatable){
	_ctrCallFromStatic = true;
	_swfForceSizeInstance = new SWFForceSize(idDiv, minWidth, minHeight, sizeUpdatable);
	_ctrCallFromStatic = false;
}

SWFForceSize.updateMinSize = function(minWidth, minHeight){
	_swfForceSizeInstance.minW = minWidth;
	_swfForceSizeInstance.minH = minHeight;
	_swfForceSizeInstance.onResizeDiv();
}




SWFForceSize.prototype = {
	addWindowEvent: function( eventName, scope, func )
	{
		var oldEvent = window[ eventName ];
		if (typeof window[ eventName ] != 'function') window[ eventName ] = function(){ func.call( scope ); };
		else
		{
			window[ eventName ] = function()
			{ 
				if( oldEvent ) oldEvent();
				func.call( scope );
			}
		}
		
	},

	getWinSize: function()
	{
		var winH, winW;
		var browser = this.getBrowser();
		
		if (browser == this.NETSCAPE) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		} 
		else { // ie // WebKit
			winW = document.body.clientWidth;
			winH = document.body.clientHeight;
		}
		
		return { height: winH, width: winW };
	},
	
	onLoadDiv: function()
	{
		
		if (document.getElementById( this.div ) != null && document.body != null){
			document.getElementById( this.div ).style.width = "100%";
			document.getElementById( this.div ).style.height = "100%";
			document.body.style.margin = "0px";
			document.body.style.height = "100%";
			
			if (this.scrollAlwaysVisible){
				document.body.style.overflowY = "scroll";
			}
		
			
			this.onResizeDiv();
		}
		else {
			var thisObj = this;
			setTimeout(function(){thisObj.onLoadDiv();}, 20);
		}
		
	},
	
	
	onResizeDiv: function()
	{
		var winSize = this.getWinSize();
		var w = winSize.width < this.minW ? this.minW: "100%";
		var h = winSize.height < this.minH ? this.minH: "100%";
		
		//add px to the end of size
		if ( w != "100%"){
			w = w + "px";
		}
		if ( h != "100%"){
			h = h + "px";
		}
		
		if (this.getBrowser() == this.NETSCAPE){
		
			if (this.scrollAlwaysVisible){
				document.body.style.overflowY = "scroll";
			}
			else {
				document.body.style.overflow = "auto";
			}
		}
		else {
			document.body.style.overflowY = this.scrollAlwaysVisible || h != "100%" ? "scroll" : "hidden";
			document.body.style.overflowX = w != "100%" ? "scroll" : "hidden";
		}
		
		document.getElementById( this.div ).style.width = w;
		document.getElementById( this.div ).style.height = h;
		
		
	},
	
	getBrowser : function()
	{
		if (navigator.appVersion.indexOf("WebKit") != -1)
			return this.WEBKIT;
		return navigator.appName;
	}
}