winH = 100;
flashContentHeight = 600;
prevScroll = -1;

var strUri = String(document.location);
var arrUri = strUri.split('#');
var strPage = arrUri[1];

var swfQueue = new Array();

//var swfId = setInterval(sendSwf, 1000);


function sendSwf()
{
	if(swfQueue.length > 0)
	{
		var nextCall = swfQueue.shift();
		//alert(nextCall.func + ' : ' + nextCall.argument);
		var swf = document.getElementById('mmgsite_swf');
		//console.log(swf + ' : ' + nextCall.func);
		//console.log(typeof swf[nextCall.func]);
		if(typeof swf[nextCall.func] == "function")
		{
			//alert(nextCall.func + ' : ' + nextCall.argument);
			swf[nextCall.func](nextCall.argument);
		}
	}
}

function addToQueue(obj)
{
	swfQueue.push(obj);
}

function getBrowserHeight(recheck)
{
	if(recheck)
	{
		if(window.innerHeight)
		{
			winH = window.innerHeight;
		}
		if (document.body.offsetHeight)
		{
			winH = document.body.offsetHeight;
		}
	}
	return winH;
}

function getScrollXY()
{
	var scrOfX = 0, scrOfY = 0;
	if (typeof( window.pageYOffset ) == 'number' )
	{
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	}
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
	{
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	}
	else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	
	//return [ scrOfX, scrOfY ];
	//alert(scrOfY);
	return scrOfY;
}

function setContentHeight(intNewHeight, type)
{
	//alert(winH);
	//alert(intNewHeight + ' : ' + type);
	
	if(type == 'flash') flashContentHeight = intNewHeight;
	
	var swf = document.getElementById('mmgsite_swf');
	//alert(swf + ' - ' + swf.style.height + ' - ' + intNewHeight + ' - ' + type);
	//var diff = swf.style.height.substr(0, swf.style.height.length-2) - intNewHeight;
	//console.log(diff);
	
	if(swf == null) return;
	
	var browser = getScrollXY() + getBrowserHeight(false);
	if(browser > intNewHeight)
	{
		newHeight = intNewHeight;
		scrollBy(0, intNewHeight - browser);
		//fixIt();
	}
	
	
	oldHeight = Number(swf.style.height.substr(0, swf.style.height.length-2));
	swf.style.height = intNewHeight + 'px';
	
	changeScroll();
	
	
	
	
	//alert(winH);
}

function fixIt()
{
	setTimeout("setContentHeight(" + oldHeight + ")", 50);
	setTimeout("setContentHeight(" + newHeight + ")", 50);
}

onBodyLoad = function()
{
	//alert('onBodyLoad');
	getBrowserHeight(true);
	setContentHeight(Math.max(winH, flashContentHeight), 'browser');
	initialize();
	
	window.onresize = function()
	{
		//alert('onResize');
		
		getBrowserHeight(true);
		setContentHeight(Math.max(winH, flashContentHeight), 'browser');
	}
	
	window.onscroll = function()
	{
		changeScroll();
	}
}

function changeScroll()
{
	var xScroll = getScrollXY();
	var swf = document.getElementById('mmgsite_swf');
	//alert(swf.setScrollPos);
	if(swf.setScrollPos != undefined && xScroll != prevScroll)
	{
		prevScroll = xScroll;
		//alert(xScroll);
		swf.setScrollPos(Number(xScroll));
		//addToQueue({func: 'setScrollPos', argument: String(xScroll)});
	}
}

function onSiteScroll(argDelta)
{
	window.scrollBy(0, -Number(argDelta) * 10)
}

function gotoPage(argPage)
{
	//alert('gotoPage(' + argPage + ')');
	//alert(history.current + ' of ' + history.length);
	//document.location = '#' + argPage;
	//top._history.location = 'history.html?' + argPage;
	dhtmlHistory.add(argPage);
	
	scrollTo(0, 0);
}

/* HISTORY */
function initialize()
{

	// initialize our DHTML history
	dhtmlHistory.initialize();
	
	// subscribe to DHTML history change
	// events
	dhtmlHistory.addListener(historyChange);
	
	// if this is the first time we have
	// loaded the page...
	if (dhtmlHistory.isFirstLoad())
	{
		// start adding history
		//dhtmlHistory.add("home");
	}
}

/** Our callback to receive history 
change events. */
function historyChange(newLocation, historyData)
{
	//alert('change : ' + newLocation);
	
	var swf = document.getElementById('mmgsite_swf');
	if(swf == null) return;
	if(swf.setNewPage != undefined) swf.setNewPage(newLocation);
}