/* 
 * FlashDivResizer
 * 
 * @author mjr 070502
 *
 * Create an instance of the FlashDivResizer, passing along a reference to the div that is to be resized.
 * If desired, set a max or min content height with the following public functions:
 * setMinWidth()
 * setMinHeight()
 * setMaxWidth()
 * setMaxHeight()
 * 
 * Calling the update() function will update the div to the proper size and is usually called via the window.onResize handler.
 */
function FlashDivResizer(div_id)
{
	this.flashDiv = null;
	this.flashResizable = false;
	this.flashHolderWidth = 0;
	this.flashHolderHeight = 0;
	this.prevFlashHolderWidth = 0;
	this.prevFlashHolderHeight = 0;
	
	this.minContentWidth = null;
	this.minContentHeight = null;
	
	this.maxContentWidth = null;
	this.maxContentHeight = null;
	
	this.setFlashDiv(div_id);
}

FlashDivResizer.prototype = 
{
	getFlashWidth : function(){
		return Number(this.flashDiv.style.width.substr(-2));
	},

	getFlashHeight : function()
	{
		return Number(this.flashDiv.style.height.substr(-2));
	},

	setFlashWidth : function(newW)
	{
		this.flashDiv.style.width = newW+"px";
	},
	
	setFlashHeight : function(newH)
	{
		this.flashDiv.style.height = newH+"px";		
	},
	
	setMinWidth : function(size)
	{
		this.minContentWidth = size;
	},
	
	setMinHeight : function(size)
	{
		this.minContentHeight = size;	
	},
	
	setMaxWidth : function(size)
	{
		this.maxContentWidth = size;
	},

	setMaxHeight : function(size)
	{
		this.maxContentHeight = size;		
	},
	
	setFlashDiv : function(div_id)
	{
		this.flashDiv = document.getElementById(div_id);
		
		if( this.canResizeFlash() == "1" )
		{
			this.flashResizable = true;
		}else{
			this.flashResizable = false;	
		}
	},
	
	setFlashSize : function(newW, newH)
	{
		this.setFlashWidth(newW);
		this.setFlashHeight(newH);
	},
	
	canResizeFlash : function()
	{
		var ua = navigator.userAgent.toLowerCase();
		var opera = ua.indexOf("opera");
		if( document.getElementById ){
			if(opera == -1) return true;
			else if(parseInt(ua.substr(opera+6, 1)) >= 7) return true;
		}
		return false;
	},
	
	update : function()
	{
		var myWidth = 0;
		var myHeight = 0;
		
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement &&
			  ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
				
		this.prevFlashHolderWidth = this.flashHolderWidth;
		this.prevFlashHolderHeight = this.flashHolderHeight;
		
		this.flashHolderWidth = Math.min(this.maxContentWidth,Math.max(myWidth, this.minContentWidth));			
		this.flashHolderHeight = Math.min(this.maxContentHeight,Math.max(myHeight, this.minContentHeight));
		
		// set flash size
		if(this.flashResizable)
		{
			var windowWidthOffset = this.flashHolderWidth;
			var windowHeightOffset = this.flashHolderHeight;
			
			if( typeof( window.innerWidth ) == 'number' ) {
							
				if(this.flashHolderWidth != this.prevFlashHolderWidth){
					windowWidthOffset -= 1;
				}
				
				if(this.flashHolderHeight != this.prevFlashHolderHeight){
					windowHeightOffset -= 1;
				}
				this.setFlashSize(windowWidthOffset, windowHeightOffset);
				
			}else{
	
				this.setFlashSize(this.flashHolderWidth, this.flashHolderHeight);
			}
		}
		
		//alert("setting flashHolder to " + this.flashHolderWidth + "x" + this.flashHolderHeight + " current: " + myWidth + "x" + myHeight);
	}
}
