/*
	homepage:  
		http://www.eberhardt.ca/swfresize/
	
	useage: 
		var sr = new SWFResize('flashcontent', 790, 450, 400);
		
	notes:
		It's a good idea to put this immediately before you put your "new SWFObject()" command, if you are using SWFObject

*/

if(typeof eberhardt=="undefined"){var eberhardt=new Object();}

eberhardt.SWFResize=function(_elementID, _idealWidth, _idealHeight, _minWidth){
	var element;
	var ratio;
	var minWidth;
	if(!document.getElementById){return;}
	this.element = _elementID;
	this.ratio = _idealWidth/_idealHeight;;
	this.minWidth = _minWidth;
	
	eberhardt.addEvent(window, 'resize', eberhardt.delegate(this,this.resize), false);
	
	this.resize();
};

eberhardt.SWFResize.prototype={
	resize:function(){
		docWidth = document.body.clientWidth;
		docHeight = document.body.clientHeight;
		var newFlashWidth = docWidth;
				
		while (newFlashWidth / docHeight > this.ratio){
			newFlashWidth = newFlashWidth - 5;
		}
		
		if (newFlashWidth < this.minWidth){
			newFlashWidth = this.minWidth;
		}
		
		document.getElementById(this.element).style.width = newFlashWidth + "px";
		document.getElementById(this.element).style.height = newFlashWidth / this.ratio + "px";
	}
}

var SWFResize = eberhardt.SWFResize;

// These two functions below are used to handle adding the event to the body onresize.
// They are too generic to actually be considered part of SWFResize, but I didn't want them to conflict with any other names,
// so I added them to "eberhardt".
eberhardt.delegate = function( that, thatMethod )
{
    return function(e) { return thatMethod.call(that,e); }
}

eberhardt.addEvent = function(el, eType, fn, uC) {
	if (el.addEventListener) {
		el.addEventListener(eType, fn, uC);
		return true;
	} else if (el.attachEvent) {
		return el.attachEvent('on' + eType, fn);
	} else {
		el['on' + eType] = fn;
	}
}