/**
 * Tudock Iframe-Resizer
 * Ändert die Höhe das Parent-Iframe, wenn vorhanden (Cross-Domain)
 * @author: TUDOCK - Pahlmann & Wolf GbR <info@tudock.de> 
 */
if(typeof tudock == 'undefined'){tudock = {};}

tudock.iframeResizer = function(){
	this.construct();
}
tudock.iframeResizer.prototype = {
	iframeHeight: 0,
	link: null,
	splitter: '&',
	getParam: 'height',
	offset: 5,
	construct: function(){
		if(window.name.substr(0,5) == '@http'){
			this.link = window.name.substring(1);
	      if (this.link.split('?').length == 1) {
	          this.splitter = '?';
	      }
			_self = this;
			this.makeDiv();
			this.makeFrame();
			this.syncSize();
		}
	},
	getOffsetTop: function(obj){
		 var offsetTop = 0;
		 while (obj){
		     offsetTop += obj.offsetTop;
		     obj = obj.offsetParent;
		 }
		 return offsetTop;
	},
	makeDiv: function(){
		this.div = new Element('div', {'styles': { 'clear': 'both'}});
		$(document.body).adopt(this.div);
	},
	makeFrame: function(link){
		this.iframe = new Element('iframe', {styles: { width: '1px',height:'1px',top:'-100px',position:'absolute' }});
		$(document.body).adopt(this.iframe);
	},
	updateFrame: function(height){
		 this.iframe.setAttribute("src", this.link + this.splitter + this.getParam + '=' + height);
	},
	syncSize: function(){
		 var newh = 0;
		 if ($(document.body).scrollHeight){
		     newh = $(document.body).scrollHeight;
		     if (this.div){
		         var offset = this.getOffsetTop(this.div);
		         if (offset != 0 && offset < newh){
		             newh = offset + this.offset;
		         }
		     }
		 } else if( $(document.body).height){
		     newh = $(document.body).height;
		 }
		 
		 if (this.iframeHeight != newh){
		         this.iframeHeight = newh;
		         this.updateFrame(newh);
		 }
		 setTimeout('_self.syncSize()', 500);
	}
}
window.addEvent('domready', function() {
    var r = new tudock.iframeResizer();
});

