/**
 * 
 *	SWFForce Resize
 * 	@author : Nicolas Bush
*	@company : Megalo(s)
*	@version 1.4
*	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, maxWidth, maxHeight, align)
{
	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.maxW = maxWidth == null ? 0 : maxWidth;
	this.maxH = maxHeight == null ? 0 : maxHeight;
	this.align = align == null ? SWFForceSize.C : align;
	
	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, maxWidth, maxHeight, align){
	_ctrCallFromStatic = true;
	_swfForceSizeInstance = new SWFForceSize(idDiv, minWidth, minHeight, sizeUpdatable, maxWidth, maxHeight, align);
	_ctrCallFromStatic = false;
}

SWFForceSize.updateMinSize = function(minWidth, minHeight){
	_swfForceSizeInstance.minW = minWidth;
	_swfForceSizeInstance.minH = minHeight;
	_swfForceSizeInstance.onResizeDiv();
}

SWFForceSize.getWinSize = function() {
	return _swfForceSizeInstance.getWinSize();
}

SWFForceSize.TL = "TL";
SWFForceSize.TR = "TR";
SWFForceSize.BL = "BL";
SWFForceSize.BR = "BR";
SWFForceSize.C = "C";
SWFForceSize.T = "T";
SWFForceSize.B = "B";



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();
		
		//on definit la largeur
		var maxW = false;
		var w;
		if (winSize.width < this.minW){
			w = this.minW + "px";
		}
		else if (this.maxW != 0 && winSize.width > this.maxW){
			w = this.maxW + "px";
			maxW = true;
		}
		else {
			w = "100%";
		}
		
		//on definit la hauteur
		var maxH = false;
		var h;
		if (winSize.height < this.minH){
			h = this.minH + "px";
		}
		else if (this.maxH != 0 && winSize.height > this.maxH){
			h = this.maxH + "px";
			maxH = true;
		}
		else {
			h = "100%";
		}
		
		//on affiche ou non les scrollbars
		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%" && !maxH) ? "scroll" : "hidden";
			//document.body.style.overflowX = w != ("100%" && !maxH)? "scroll" : "hidden";
			
			if(w == '100%') {
				document.body.style.overflowX = 'hidden';
			}
			else {
				document.body.style.overflowX = 'scroll';
			}
			
		}
		
		//on redimmensionne 
		document.getElementById( this.div ).style.width = w;
		document.getElementById( this.div ).style.height = h;
		
		//on replace en x et y
		if (maxW){
			switch (this.align){
				case SWFForceSize.TL:
				case SWFForceSize.BL:
					document.getElementById( this.div ).style.marginLeft = "0px";
					break;
				case SWFForceSize.TR:
				case SWFForceSize.BR:
					document.getElementById( this.div ).style.marginLeft = (winSize.width - this.maxW) + "px";
					break;
				default:
					document.getElementById( this.div ).style.marginLeft = (winSize.width - this.maxW) / 2 + "px";
					break;
			}
		}
		else {
			document.getElementById( this.div ).style.marginLeft = "0px";
		}
		
		if (maxH){
			switch (this.align){
				case SWFForceSize.T:
				case SWFForceSize.TL:
				case SWFForceSize.TR:
					document.getElementById( this.div ).style.marginTop = "0px";
					break;
				case SWFForceSize.BL:
				case SWFForceSize.BR:
				case SWFForceSize.B:
					document.getElementById( this.div ).style.marginTop = (winSize.height - this.maxH) + "px";
					break;
				default:
					document.getElementById( this.div ).style.marginTop = (winSize.height - this.maxH) / 2 + "px";
					break;
			}
		}
		else {
			document.getElementById( this.div ).style.marginTop = "0px";
		}
		
		
	},
	
	getBrowser : function()
	{
		if (navigator.appVersion.indexOf("WebKit") != -1)
			return this.WEBKIT;
		return navigator.appName;
	}
}
