

var sourceURL = './index.html';

var divID = "iframeDiv";
var resizeCount = 0;
var orientation = 'profile';
var callback = null;
var docheight = 0;
var scrollBarWidth;

function $() {
	var elements = [];
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string') {element = document.getElementById(element);}
		if (arguments.length == 1) {return element;}
		elements.push(element);
	}
	return elements;
}

if ( typeof(console) !== "object" ) {
	console = { log:function(){return null;} };
}

checkForScrollBarAndFix = function() {
	var check = $('iframeDiv').clientWidth == 337 || $('iframeDiv').clientWidth == 497;
	if( !getIframeDoc() || !(getIframeDoc()).getElementsByTagName('html')[0]) {return;}
	if(contentDoc) {switch( (getIframeDoc()).getElementsByTagName('html')[0].clientWidth ) {
		case 303:
		case 463:
			$('iframeObj').style.width = $('iframeDiv').style.width = orientation=='landscape'?"497px":"337px";
			break;
		case 320:
		case 480:
			if(check) {$('iframeObj').style.width = $('iframeDiv').style.width = orientation=='landscape'?"497px":"337px";}
			else {$('iframeObj').style.width = $('iframeDiv').style.width = orientation=='landscape'?"480px":"320px";}
			break;
		case 337:
		case 497:
			$('iframeObj').style.width = $('iframeDiv').style.width = orientation=='landscape'?"480px":"320px";
	}}
};

setInterval( checkForScrollBarAndFix, 500 );

addEvent = function(obj,type,fn) {
	if(!obj) {return;}
	if(obj && obj.attachEvent) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn](window.event );};
		obj.attachEvent('on'+type, obj[type+fn]);
	} else {
		try{obj.addEventListener(type,fn,false);}
		finally{obj.addEventListener(type,fn,false);}
	}
};

function findPos(obj) {
	var curleft=0,curtop=0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj == obj.offsetParent);
		return [Math.floor(curleft),curtop];
	}
}

pageLoaded = function() {
	document.getElementById(divID).innerHTML = "<iframe src='javascript:false;document.write(\"\");' " +
		"id='iframeObj' name='iframeObj' frameborder='0' " +
		"marginwidth='0' marginheight='0' class='iframeObj' " +
		"onload='setTimeout(iframeContentLoaded,100);' ></iframe>";
	setTimeout(function() {
			(document.getElementsByTagName('iframe')[0]).src = sourceURL;
		}, 1 );
	addEvent(window,'resize',windowResizeEventHandler);
};

addEvent(window,'load',pageLoaded);

windowResizeEventHandler = function() {
	resizeCount++;
	setTimeout(processResizeEvent,50);
};

function resizeIframe() {
	docheight = getDocHeight();
	(document.getElementsByTagName('iframe')[0]).height = docheight;
	setTimeout( checkForScrollBarAndFix, 100);
}

processResizeEvent = function() {
	checkForScrollBarAndFix();
	if(! --resizeCount ) {
		var el,lt=findPos( $('content') );
		if(orientation == 'landscape') {
			(el=$('iframeDiv')).style.left = (134+lt[0])+"px";
			el.style.top  = (55+lt[1])+"px";
			(el=$('reload')).style.left = (653+lt[0])+"px";
			el.style.top  = (186+lt[1])+"px";
		} else {
			(el=$('iframeDiv')).style.left = (34+lt[0])+"px";
			el.style.top  = (154+lt[1])+"px";
			if(el.style.visibility!='visible') {el.style.visibility = 'visible';}
			(el=$('reload')).style.left = (185+lt[0])+"px";
			if(el.style.position != 'absolute') {el.style.position = 'absolute';}
			el.style.top  = (652+lt[1])+"px";
		}
		resizeIframe();
	}
};

function changeOrientation( ) {
	var el = document.getElementsByTagName('body')[0];
	orientation = el.className = ( el.className === 'landscape' ) ? 'profile' : 'landscape';
	$('iframeObj').style.width = $('iframeDiv').style.width = orientation=='landscape'?"480px":"320px";
	resizeCount++;
	processResizeEvent();
	callback( orientation, getDocHeight() );
	return false;
}

getIframeDoc = function() {
	var tmp = document.getElementsByTagName('iframe')[0];
	try {
		if(tmp.contentWindow != 'undefined' && (tmp.contentWindow).document != 'undefined') {contentDoc = (tmp.contentWindow).document;}
		else if( tmp.contentDocument != 'undefined' ) {contentDoc = tmp.contentDocument;}
	} catch(error) { contentDoc = null; }
	return contentDoc;
};

getIframeWin = function() {
	var tmp = document.getElementsByTagName('iframe')[0];
	try {
		if(tmp.contentWindow != 'undefined' && (tmp.contentWindow).document != 'undefined') {tmp = tmp.contentWindow;}
	} catch(error) { }
	return tmp;
};

iframeContentLoaded = function() {
	var contentDoc = getIframeDoc();
	var win = getIframeWin();
	if(contentDoc) {
		addEvent(contentDoc.getElementsByTagName("body")[0],'click', function(){resizeCount++;processResizeEvent();});
		addEvent(contentDoc.getElementsByTagName("body")[0],'webkitAnimationEnd', function(){resizeCount++;processResizeEvent();});
	}
	if( typeof(win.changeOrientation) === 'function' ) {
		callback = win.changeOrientation;
		callback('profile',getDocHeight());
	}
	resizeCount++;
	setTimeout( processResizeEvent, 10);
};

getDocHeight = function() {
	var contentDoc = getIframeDoc();
	var docHeight = 0;
	if(contentDoc) {
		var db,dde;
		if( (db=contentDoc.body) ) {docHeight = Math.max(db.scrollHeight, db.offsetHeight );}
		if( (dde=contentDoc.documentElement) ) {docHeight = Math.max(docHeight, dde.scrollHeight, dde.offsetHeight );}
	}
	return docHeight;
};


