/* ---------------------------------------------------------------
*	Fit Iframe Javascript Library Ver.1.1
*	
*	from 2009, created by edo.
*	http://css-eblog.com/
* --------------------------------------------------------------- */

//automatic setting.( 0=manual, 1=auto )
var auto = 1;

//main function.
function fitIfr() {
	if( !arguments[0] || arguments[0].type === 'load' ) {
		var ifrObjs = document.getElementsByTagName( 'iframe' );
		for( var i=0; i<ifrObjs.length; i++) {
			var ifrObj = ifrObjs.item(i);
			try {
				var mt = getStyle( ifrObj.contentWindow.document.body, 'margin-top' );
				var mb = getStyle( ifrObj.contentWindow.document.body, 'margin-bottom' );
				ifrObj.height = ifrObj.contentWindow.document.body.scrollHeight + Number( mt.match( /[0-9]+/g ) ) + Number( mb.match( /[0-9]+/g ) );
			} catch( e ) {
				/* skip process */
			}
		}
	} else if( arguments[0].charAt(0) == '-' ) {
		var ifrObjs = document.getElementsByTagName( 'iframe' );
		var chkFlg;
		for( var i=0; i<ifrObjs.length; i++) {
			var ifrObj = ifrObjs.item(i);
			chkFlg = false;
			for( var k=0; k<arguments.length; k++ ) {
				if( ifrObj.id == arguments[k].substring( 1, arguments[k].length ) ) {
					if ( arguments[k].charAt( 0 ) == '-' ) chkFlg = true;
				}
			}
			
			if ( !chkFlg ) {
				try {
					var mt = getStyle( ifrObj.contentWindow.document.body, 'margin-top' );
					var mb = getStyle( ifrObj.contentWindow.document.body, 'margin-bottom' );
					ifrObj.height = ifrObj.contentWindow.document.body.scrollHeight + Number( mt.match( /[0-9]+/g ) ) + Number( mb.match( /[0-9]+/g ) );
				} catch( e ) {
					/* skip process */
				}
			}
		}
	} else {
		for( var i=0; i<arguments.length; i++ ) {
			var ifrObj = document.getElementById( arguments[i] ) ? document.getElementById( arguments[i] ) : false;
			if( !ifrObj ) continue;
			try {
				var mt = getStyle( ifrObj.contentWindow.document.body, 'margin-top' );
				var mb = getStyle( ifrObj.contentWindow.document.body, 'margin-bottom' );
				ifrObj.height = ifrObj.contentWindow.document.body.scrollHeight + Number( mt.match( /[0-9]+/g ) ) + Number( mb.match( /[0-9]+/g ) );
			} catch( e ) {
				/* skip process */
			}
		}
	}
}

//camelize.
String.prototype.camelize = function() {
	return this.replace( /-([a-z])/g,
	function( $0, $1 ) { return $1.toUpperCase() } );
}

//decamelize.
String.prototype.deCamelize = function() {
	return this.replace( /[A-Z]/g,
	function( $0 ) { return "-" + $0.toLowerCase() } );
}

function getStyle( ele, property, pseudo ) {
	if( ele.currentStyle ) { //IE or Opera
		if( property.indexOf( '-' ) != -1 ) property = property.camelize();
			return ele.currentStyle[ property ];
	} else if ( getComputedStyle ) { //Mozilla or Opera
		if( property.indexOf( '-' ) == -1 ) property = property.deCamelize();
		return document.defaultView.getComputedStyle( ele, pseudo ).getPropertyValue( property );
	}

	return '';
}

if( auto ) {
	if( window.addEventListener ) window.addEventListener( 'load', fitIfr, false );
	else window.attachEvent( 'onload', fitIfr );
}
