/*
    This file is part of ClassBehaviours release 20100930.

    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2010  Maurice van Creij (http://www.classbehaviours.com)

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.
*/

	// CLASSBEHAVIOURS CLASS
	// create the root classbehaviours object if it doesn't already exist
	if(typeof(classBehaviours)=='undefined') classBehaviours = {};

		// CLASSNAME BEHAVIOUR FUNCTIONS
		// create the handlers child object if it doesn't already exist
		if(typeof(classBehaviours.handlers)=='undefined') classBehaviours.handlers = {}

			// Resize an iframe to accomodate the contents
			classBehaviours.handlers.autoSizeIframe = {
				// properties
				name: 'autoSizeIframe',
				// methods
				start: function(node){
					// set the onload event of the frame
					node.onload = this.resize;
					this.resize();
				},
				// events
				resize: function(){
					var asi = classBehaviours.handlers.autoSizeIframe;
					// if the frame is loaded, resize all iframes of this class
					allIframes = document.getElementsByTagName('iframe');
					for(var a=0; a<allIframes.length; a++){
						if(allIframes[a].className.indexOf(asi.name)>-1){
							// get the optional size offset
							var sizeOffset = parseInt(classBehaviours.utilities.getClassParameter(allIframes[a], 'offset', '0'));
							if(window.frames[allIframes[a].name].document.getElementsByTagName('body').length>0){
								if(window.frames[allIframes[a].name].document.getElementsByTagName('body')[0].offsetHeight>0){
									document.getElementById(allIframes[a].id).style.height = (window.frames[allIframes[a].name].document.getElementsByTagName('body')[0].offsetHeight + sizeOffset) + 'px';
								}
							}
						}
					}
				}
			}

	// JQUERY WRAPPER
	if(typeof(jQuery)!='undefined'){
		(function($){
			var methods = {
				init : function(options) {
					return this.each(function(){
						classBehaviours.handlers.autoSizeIframe.start($(this).context);
					});
				}
			};
			$.fn.autoSizeIframe = function(method){
				if(methods[method]) {
					return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
				}else if(typeof method === 'object' || !method){
					return methods.init.apply(this, arguments);
				}else{
					$.error('Method ' +  method + ' does not exist on jQuery.autoSizeIframe');
				}
			};
		})(jQuery);

		// JQUERY EVENTS
		$(document).ready(function() {
			$(".autoSizeIframe").autoSizeIframe();
		});
	}

