/*
	based on eberhardt's swfresize: http://www.eberhardt.ca/swfresize/
*/

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

martinetz.SWFResize=function(_elementID, _minWidth, _offset) {
	var element;

	this.element = document.getElementById(_elementID);
	this.maxWidth = this.element.clientWidth;
	this.maxHeight = this.element.clientHeight;
	this.ratio = this.maxWidth / this.maxHeight;

	this.minWidth = _minWidth;
	this.minHeight = Math.ceil(_minWidth / this.ratio);

	this.offset = _offset;

	martinetz.addEvent(window, 'resize', martinetz.delegate(this,this.resize), false);

	/*
	// adblock makes a mess out of the layout... but oh well ...
	var Resizer = this;
	window.onload = function() {
		var blockIt = Resizer.element.getElementsByTagName('a')[0];
		if (blockIt && blockIt.className.indexOf('abp-objtab') != -1) {
			blockIt.parentNode.removeChild(blockIt);
			Resizer.resize();
		}
	}
	*/
	this.resize();
};

martinetz.SWFResize.prototype={
	resize: function() {
		var body = document.getElementsByTagName('body').item(0);
		var _W = body.clientWidth - this.offset * 2;
		var _H = body.clientHeight - this.offset * 2;

		var newWidth;
		var newHeight;
		
		if (_W / this.ratio < _H) {
			newWidth = _W;
			newHeight = Math.ceil(_W / this.ratio);
		} else {
			newWidth = Math.ceil(_H * this.ratio);
			newHeight = _H;
		}

		if (newWidth < this.minWidth || newHeight < this.minHeight) {
			newWidth = this.minWidth;
			newHeight = this.minHeight;
		} else if (newWidth > this.maxWidth || newHeight > this.maxHeight) {
			newWidth = this.maxWidth;
			newHeight = this.maxHeight;
		}

		this.element.style.width = newWidth + 'px';
		this.element.style.height = newHeight + 'px';

		// and stick it to the center
		this.element.style.marginTop = Math.floor((_H + this.offset * 2 - newHeight) / 2) + 'px';
	}
}

var SWFResize = martinetz.SWFResize;

martinetz.delegate = function( that, thatMethod ) {
    return function(e) { return thatMethod.call(that,e); }
}

martinetz.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;
	}
}

