
// ///////////////////////////////////////////////////////////////////////////////////// //
// @DATE: 	14/11/2008																	 //
// @AUTHOR: David Hund - http://www.valuedstandards.com									 //
// 																						 //
// This script should be run directly when the Document/DOM is loaded ..				 //
// .. most JS libs (such as Prototype or jQuery) have handy functions for this			 //
// .. such as document.ready(function);													 //
// ///////////////////////////////////////////////////////////////////////////////////// //
// This script picks up all iFrames with classname: CLASSNAME							 //
// It resizes the iFrame when the framed document is reloaed (& it's height 			 //
// .. changes																			 //

// To remain JS Library Agnostic, the script uses the following scripts: 				 //
// getElementsByClassName() from: robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/  //
// addEvent from: http://www.dustindiaz.com/rock-solid-addevent/						 //
// ///////////////////////////////////////////////////////////////////////////////////// //

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/	
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

// ///////////////////////////////////////////////////////////////////////////////////// //

// addEvent from Dustin Diaz:
// http://www.dustindiaz.com/rock-solid-addevent/
var addEvent = function() {if (window.addEventListener) {return function(el, type, fn) {el.addEventListener(type, fn, false);};} else if (window.attachEvent) {return function(el, type, fn) {var f = function() {fn.call(el, window.event);};el.attachEvent('on' + type, f);};}}();

// ///////////////////////////////////////////////////////////////////////////////////// //
// addLoadEvent from Simon Willison:
// http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {var oldonload = window.onload;if (typeof window.onload != 'function') {window.onload = func;} else {window.onload = function() {if (oldonload) {oldonload();}func();}}}

// ///////////////////////////////////////////////////////////////////////////////////// //
// THE ACTUAL SCRIPTS																	 //
// ///////////////////////////////////////////////////////////////////////////////////// //
function resizeIframe(theIframe){
	// Get the 'body' el of the current iFrame
	var frameBody = frames[theIframe.name].document.body;
        if(frameBody.offsetHeight < 150){frameBody = frames[theIframe.name].document.documentElement;}
	var y = frameBody.scrollHeight > frameBody.offsetHeight ? frameBody.scrollHeight : frameBody.offsetHeight;
	y += 10; // Somehow we need just 1 extra px to remove scrollbars. I thought 10 should be good t0o...
	theIframe.style.height = y + "px";
}

function loadIframeResizer(){
	// This can be modified, if needed here...
	var iFrameClassName = "resizableIFrame";
	var allIframes = getElementsByClassName(iFrameClassName);
	if(allIframes.length > 0){
		// We have at least one iFrame with className *iFrameClassName* on the page!
		// Assign the Change Event handler to all iframes
		for(var i = 0; i < allIframes.length; i++){
			// resize iFrame now (onload) ..
			resizeIframe(allIframes[i]);
 			// .. and when iFrame document get's reloaded
			addEvent(allIframes[i], 'load', function(anevent){resizeIframe(this);});
		}
	}
}


// ///////////////////////////////////////////////////////////////////////////////////// //
// INITIALIZING																			 //
// ///////////////////////////////////////////////////////////////////////////////////// //
// This sets it all up, when the document is loaded.
//addLoadEvent(loadIframeResizer);
// Use YAHOO onDOMReady here because apparently it conflicts with addLoadEvent...
YAHOO.util.Event.onDOMReady(loadIframeResizer);
