/**
 * Removes the iframe dynamic features in Extra Columns.
 * Fixed permanently in 1.3.3, but this file an be used to reset it in 1.3.2 and earlier.
 *
 * http://mittmediadev.adeprimo.se/core/ticket/5440#comment:1
 */
ExternalContent = {
	slcts : ".externalContent > iframe",
	wrappers : null,
	/**
	 * init method to trig and hook on JS enhancements.
	 */
	init : function() {
		this.resize();
		Event.observe(window,'resize',function(){
			ExternalContent.resize();
		});
	},
	/**
	 * resize all chosed IFrames to the height of the viewport. Is called by init() and on RESIZE event.
	 */
	resize : function() {
		for(var i=0, max=this.wrappers.length; i<max; i++) {
			var ifrm = this.wrappers[i];
			var isFixed = (ifrm.up('.externalContent').hasClassName('fixed') || ifrm.height!=null);
			if (!isFixed) {
				this.setSize(ifrm);
			}
		}
	},
	/**
	 * Updates the height of an iFrame if the iFrames's height is lower than the available viewport height.
	 * @param {Element} ifrm	the iframe Element ref.
	 */
	setSize : function(ifrm) {
		var y; // the height to be calculated in pixels.
		var max = 0; // max value if SOP.
		
		// is there any (min-)HEIGHT set on iframe?
		var heightSet = !(isNaN(parseInt(ifrm.height))) || !(isNaN(parseInt(ifrm.getStyle('height'))));
		
		// is part of the iframe visible? NOT USED RIGHT NOW
		//isPartlyVisible = (posTop-vscroll) < vp && (posTop-vscroll) >= 0;
		
		// has the page scrolled down?
		var vscroll = document.viewport.getScrollOffsets().top;
		
		// where is the iframe positioned?
		var posTop = ifrm.cumulativeOffset().top;
		
		// how much viewport does we have?
		var vp = document.viewport.getHeight();
		
		// if the iframe follows SOP, calculate a max-height.
		try{
			var doc = (ifrm.contentWindow.document) ? ifrm.contentWindow.document : ifrm.contentDocument;
			max = doc.getElementsByTagName('body').offsetHeight;
		}catch(e){
		}
		
		// return if the iframe is in the extra column.
		if( ifrm.up('.extraColumn') ) return;
		
		// default value: the same height as the  viewpot
		y = vp;
		
		// if HEIGHT is set and the calculated height is less than HEIGHT,
		// let the HEIGHT become min-height!
		if(heightSet && y <= ifrm.height) {
			y = !(isNaN(parseInt(ifrm.height))) ? ifrm.height : parseInt(ifrm.getStyle('height'));
		}
		
		// if max is set, reduce height if to high.
		if(max > 0 && y > max) {
			y = max;
		}
		
		ifrm.setStyle({height:y+'px'});
	},
	/**
	 * Called by the invoke method to see if there are any external contents on the page.
	 */
	exists : function() {
		this.wrappers = Settings.scope.select(this.slcts);
		return this.wrappers.length > 0;
	}
}