﻿/*
 * iFrame Manager
 */
var browser = getBrowserType();
var FrameManager = {
	currentFrameId : '',
	currentFrameHeight : 0,
	lastFrameId : '',
	lastFrameHeight : 0,
	resizeTimerId : null,

    init : function() {
		if (FrameManager.resizeTimerId == null) 
			//FrameManager.resizeFrames();
			if (browser == 'ie7' || browser == 'ie6') { FrameManager.resizeTimerId = window.setInterval(FrameManager.resizeFrames, 75); }
			else { FrameManager.resizeTimerId = window.setInterval(FrameManager.resizeFrames, 500); }
	},

	resizeFrames : function() {
		if (browser == 'ie7' || browser == 'ie6') FrameManager.receiveSizeByQueryString();
		else return; // utilize receiveSizeByMessage instead

		if ((FrameManager.currentFrameId != FrameManager.lastFrameId) || (FrameManager.currentFrameHeight != FrameManager.lastFrameHeight)) {
			var iframe = document.getElementById(FrameManager.currentFrameId.toString());

			if (iframe == null) return;

			iframe.style.height = FrameManager.currentFrameHeight.toString() + "px";

			FrameManager.lastFrameId = FrameManager.currentFrameId;
			FrameManager.lastFrameHeight = FrameManager.currentFrameHeight;

			//clearInterval(FrameManager.resizeTimerId);
			window.location.hash = '';
			// force refresh to fix footer not repositioning
			document.getElementById('wrapper').className = document.getElementById('wrapper').className;
			//
		}
	},

	receiveSizeByQueryString : function() {
		if (window.location.hash.length == 0) return;
		var hashValue = window.location.hash.substring(1);
		
		if ((hashValue == null) || (hashValue.length == 0)) return;
		var pairs = hashValue.split('&');

		if ((pairs != null) && (pairs.length > 0)) {
			for(var i = 0; i < pairs.length; i++) {
				var pair = pairs[i].split('=');

				if ((pair != null) && (pair.length > 0)) {
					if (pair[0] == 'f') {
						if ((pair[1] != null) && (pair[1].length > 0)) {
							
							FrameManager.currentFrameId = pair[1];
						}
					}
					else if (pair[0] == 'h') {
						var height = parseInt(pair[1]);

						if (!isNaN(height)) {
							FrameManager.currentFrameHeight = height;
							FrameManager.currentFrameHeight += 10;
						}
					}
				}
			}
		}
    },

	receiveSizeByMessage : function(e) {
		if (e.origin === "http://cdn.cloudfiles.mosso.com") {
			// split into pairs if more than one - sep by '|'
			if (e.data.indexOf('|') != -1) {
				var aPairs = e.data.split('|');
				for (var i = 0; i < aPairs.length; i++) {
					var aFrame = aPairs[i].split(':');
					document.getElementById(aFrame[0]).style.height = aFrame[1] + "px";
				}
			}
			else {
				var aFrame = e.data.split(':');
				document.getElementById(aFrame[0]).style.height = aFrame[1] + "px";
			}
		}
	},

    registerFrame : function(frame) {
		var currentLocation = location.href;
		var hashIndex = currentLocation.indexOf('#');

		if (hashIndex > -1) {
			currentLocation = currentLocation.substring(0, hashIndex);
		}
		frame.contentWindow.location = frame.src + '?f=' + frame.id + '#' + currentLocation;
	}
};

// init the manager - IE6 and 7 - queryString, else utilize postMessage
if (browser == 'ie7' || browser == 'ie6')
	window.onload = function(event) { window.setTimeout(FrameManager.init, 150); };
else {
	if (window.addEventListener) window.addEventListener("message", FrameManager.receiveSizeByMessage, false);
	else if (window.attachEvent) window.attachEvent("onmessage", FrameManager.receiveSizeByMessage);
}

function registerFrame(obj) { if (browser == 'ie7' || browser == 'ie6') FrameManager.registerFrame(obj); }

function getBrowserType()
{
	userAgent = navigator.userAgent.toLowerCase();
	var browser = '';
	if ( /msie 8.0/.test(userAgent) ) browser='ie8';
	else if ( /msie 7.0/.test(userAgent) ) browser='ie7';
	else if ( /msie 6.0/.test(userAgent) ) browser='ie6';
	else if ( /firefox/.test(userAgent) ) browser='firefox';
	else if ( /chrome/.test(userAgent) ) browser='chrome';
	else if ( /safari/.test(userAgent) ) browser='safari';
	else if ( /opera/.test(userAgent) ) browser='opera';
	else browser = 'unknown';
	return browser;
}