
/* from prototypejs.org */
if (typeof(Prototype) == "undefined") {
  $A = function(iterable) {
    if (!iterable) return [];
    var results = [];
    for (var i = 0; i < iterable.length; i++)
      results.push(iterable[i]);
    return results;
  }

  Function.prototype.bind = function() {
    var __method = this, args = $A(arguments), object = args.shift();
    return function() {
      return __method.apply(object, args.concat($A(arguments)));
    }
  }

  Function.prototype.bindAsEventListener = function() {
    var __method = this, args = $A(arguments), object = args.shift();
    return function(event) {
      return __method.apply(object, [event || window.event].concat(args));
    }
  }
}


function CM4allIFrameResizer() {
	this.INTERVAL	= 300;
	this.TIMEOUT	= 5000;
	this.browser	= this.getBrowser();
	this.prepare();
}

CM4allIFrameResizer.prototype = {

	prepare : function() {
		this.id = this.jdecode(this.parseUrlParameter(document.location.href, "iframeId", ""));
		if (!this.id)
			return;
		
		var homepageUrl = this.jdecode(this.parseUrlParameter(document.location.href, "homepageUrl", ""));
		
		this.resizeHtml = homepageUrl + "include/iframe_resize.html";

		var start = this.start.bindAsEventListener(this);
		if (window.addEventListener) {
			window.addEventListener("load", start, false);
		} else {
			window.attachEvent("onload", start);
		}
	},

	start : function() {
		if (this.startTime)
			return;
		
		this.startTime	= new Date();
		this.iframe		= this.createIframe();
		this.interval	= setInterval(this.onInterval.bind(this), this.INTERVAL);
	},

	stop : function() {
		if (this.interval) {
			clearInterval(this.interval);
			this.interval = null;
		}
	},

	createIframe : function() {
		var ifr = document.createElement("iframe");
		ifr.style.position = "absolute";
		ifr.style.width = "1px";
		ifr.style.height = "1px";
		ifr.style.left = "1px";
		ifr.style.top = "1px";
		ifr.style.visibility = "hidden";
		ifr.style.zIndex = "-1";
		document.body.appendChild(ifr);
		return ifr;
	},

	onInterval : function() {
		var newHeight = this.getBodyHeight();
		
		var now = new Date();
		var diffTime = now.valueOf() - this.startTime.valueOf();
		
		var timeout = diffTime > this.TIMEOUT;
		if (this.lastHeight
		 && (newHeight.height == this.lastHeight || newHeight.height - newHeight.scrollBarWidth == this.lastHeight)
		 || timeout) {
			this.stop();
			return;
		}
		
		this.resize(newHeight.height);
	},

	resize : function(height) {
		if (height) {
			this.lastHeight = height;
			var target = this.resizeHtml + "?id=" + this.jencode(this.id) + "&height=" + height;
			
			this.iframe.src = target;
		}
	},

	getBrowser : function() {
		var userAgent = navigator.userAgent.toLowerCase();
		var browsers = [
			["msie", "IE"],
			["safari", "Safari"],
			["firefox", "Firefox"],
			["konqueror", "Konqueror"],
			["opera", "Opera"],
			["gecko", "Gecko"]				// Keep Gecko at last position, because it appears also e.g. in Safari.
		];

		var B = {};
		for (var i = 0; i < browsers.length; i ++) {
			if (userAgent.indexOf(browsers[i][0]) != -1) {
				B[browsers[i][1]] = true;
				break;
			}
		}

		if (document.compatMode == "CSS1Compat") {
			B.CSS1Compat = true;
		} else {
			B.BackCompat = true;
		}
		return B;
	},

	getBodyHeight : function() {
		var h = 0;
		var B = this.browser;
		if (B.IE || B.Konqueror || B.Opera) {
			h = (B.CSS1Compat ? document.documentElement.scrollHeight : document.body.scrollHeight);
		} else {
			// (B.Firefox || B.Safari || B.Gecko)
			h = document.documentElement.offsetHeight;
		}
		
		var scrollBarWidth = this.getScrollBarWidth();
		
		return { 'height': h + scrollBarWidth, 'scrollBarWidth': scrollBarWidth };
	},
	
	getScrollBarWidth : function() {
  		document.body.style.overflow = 'hidden'; 
		var width = document.body.clientWidth;
		
		document.body.style.overflow = 'scroll'; 
		
		width -= document.body.clientWidth; 
		
		if ( ! width) 
			width = document.body.offsetWidth - document.body.clientWidth;
			
		document.body.style.overflow = ''; 
		
		return width; 
	},

	parseUrlParameter : function(url, param, deflt) {
		var p = url.indexOf("?");
		if (p == -1)
			return deflt;
		
		var query = url.substring(p + 1);
		var params = query.split("&");
		for (var i = 0; i < params.length; i ++) {
			var pair = params[i].split("=");
			if (pair[0] == param) {
				return pair[1];
			}
		}
		return deflt;
	},

	jencode : function(s) {
		var re1 = /\+/g;
		var re2 = /%20/g;
		var re3 = /'/g;
		s = encodeURIComponent(s);
		return s.replace(re1, "%2B").replace(re2, "+").replace(re3, "%27");
	},

	jdecode : function(s) {
		var re = /\+/g;
		s = s.replace(re, "%20");
		return decodeURIComponent(s);
	}
};

new CM4allIFrameResizer();

