
var jsReady 		= false;
var inWidth;
var inHeight;
var scrollPos		= 0;
var flashPos		= 0;
var MSIE			= false;
var FF				= false;
var FFOSX			= false;
var OPERA			= false;
var CHROME			= false;
var SAFARI			= false;

var appContainer;
var flashContainer;
var flashObject;
var appContainerId	= "flash";
var appElementId	= "app";
var currHeight		= 800


function trace(args)
{
	alert(args);
}

function start()
{
	appContainer 	= document.getElementById(appContainerId);
//	flashContainer 	= document.getElementById(appElementId);
	flashContainer 	= document.getElementById(appContainerId);
	determineUserAgent();
	getDimensions();
	setHeight(inHeight);
	addEventListener2(window, "resize", handleResize, false);
	addEventListener2(window, "scroll", handleScroll, false);
	jsReady 		= true;

	var b;
	switch(true)
	{
		case MSIE:
			b = "msie";
			break;
		case FF:
			b = "ff";
			break;
		case FFOSX:
			b = "ffosx";
			break;
		case CHROME:
			b = "chrome";
			break;
		case OPERA:
			b = "opera";
			break;
	}
	//alert(b);
}

/**
 * called from flash
 * 
 * @param method
 * @param value
 * @return
 */
function talkToJavaScript(method,value) 
{
	//alert(method + ":" + value);
	switch(method)
	{
		case "getInHeight":			
			flashObject.executeASMethod("getInHeight",inHeight);
			break;
			
		case "setHeight":
			setHeight(value);
			break;

		case "scrollToPos":
			//alert(value);
			scrollToPos(0,value);
			break;

		case "scrollToPos2":
			//alert(value);
			//scrollToPos(value,0);
			setTimeout("scrollToPos2(0," + value + ")", 2000);
			//handleScroll();
			break;
	}
}

function setHeight(value)
{
	currHeight					= value >= inHeight ? value : inHeight;
	appContainer.style.height 	= value >= inHeight ? value + "px" : inHeight + "px" ;
}

function scrollToPos2(x,y)
{
	window.scrollTo(x,y);
}

function scrollToPos(x,y)
{
	if(FFOSX)
	{
		setTimeout("scrollToPos2(" + x + "," + y + ")", 10);
		return;
	}
	
	window.scrollTo(x,y);
	
//	flashObject.executeASMethod("update");
//	handleScroll();	
//	alert("jo!");
//	if(x == 0)
//	{
//		flashPos 					= 0;		
//		flashContainer.style.top 	= "0px";
//		flashObject.executeASMethod("scroll",scrollPos + "|" + inHeight + "|" + flashPos);
//		if(!SAFARI && !CHROME)
//		{
//			flashContainer.style.height	= currHeight + "px";
//		}
//	}
}

/**
 * called from flash
 * @return
 */
function javaScriptReady()
{
//	alert("jsReady called:" + jsReady);	
	flashObject 	= getObject("app");
	appContainer 	= document.getElementById(appContainerId);
	return jsReady;
}

function handleResize(e)
{
	getDimensions();
	if(flashObject != null)
	{
		flashObject.executeASMethod("resize",scrollPos + "|" + inHeight + "|" + flashPos);
	}
}

function handleScroll(e)
{
	scrollPos = document.body.scrollTop;
	if (scrollPos == 0)
	{
	    if (window.pageYOffset)
	    	scrollPos = window.pageYOffset;
	    else
	    	scrollPos = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	 
	if(flashObject != null)
	{	
		var diff;
		//max height of a page is 9000 pixels, so this will do
		if(scrollPos < 3000)
		{
			if(flashContainer.style.top != "0px")
			{
				flashPos 					= 0;			
				flashContainer.style.top 	= "0px";
				//flashContainer.style.left = "0px";
				if(!SAFARI && !CHROME)
				{
					flashContainer.style.height	= currHeight + "px";					
				}
			}
			//for Bluto!!
			flashObject.executeASMethod("scroll",scrollPos + "|" + inHeight + "|" + flashPos);
		}
		else if(scrollPos >= 3000 && scrollPos < 6000)
		{
			if(flashContainer.style.top != "3000px")
			{
				flashPos 					= -3000;// + 120;
				flashContainer.style.top 	= "3000px";
				//flashContainer.style.left = "100px";
				if(!SAFARI && !CHROME)
				{
					flashContainer.style.height	= (currHeight - 3000) + "px";
				}
			}
			flashObject.executeASMethod("scroll",scrollPos + "|" + inHeight + "|" + flashPos);
		}
		else if(scrollPos >= 6000)
		{
			if(flashContainer.style.top != "6000px")
			{
				flashPos 					= -6000;// + 120;
				flashContainer.style.top 	= "6000px";
				//flashContainer.style.left = "200px";
				if(!SAFARI &&!CHROME)
				{
					flashContainer.style.height	= (currHeight - 6000) + "px";
				}
			}
			flashObject.executeASMethod("scroll",scrollPos + "|" + inHeight + "|" + flashPos);
		}
	}
}

//utils

function getObject(objectName) 
{
    if (navigator.appName.indexOf("Microsoft") != -1) 
    {
        return window[objectName];
    }
    else 
    {
        return document[objectName];
    }
}

//get current dimensions of browser and document
function getDimensions()
{
	inWidth 	= MSIE?document.documentElement.clientWidth:window.innerWidth;
	inHeight 	= MSIE?document.documentElement.clientHeight:window.innerHeight;
	
	docWidth 	= MSIE?document.documentElement.scrollWidth:document.body.scrollWidth;
	docHeight 	= MSIE?document.documentElement.scrollHeight:document.body.scrollHeight;
	
	docHeight   = (FF || OPERA)?document.documentElement.scrollHeight:docHeight;
}

function determineUserAgent()
{
	switch(true)
	{
		case (navigator.userAgent.indexOf('Chrome') != -1):
			CHROME		= true;
			break;
		
		case (navigator.userAgent.indexOf('Opera') != -1):
			OPERA		= true;
			break;
		
		case (navigator.userAgent.indexOf('Firefox') != -1 && navigator.userAgent.indexOf("Mac") != -1):
			FFOSX		= true;
			break;
		
		case (navigator.userAgent.indexOf('Firefox') != -1):
			FF			= true;
			break;
		
		case (navigator.userAgent.indexOf('MSIE') != -1):
			MSIE		= true;
			break;
		
		case (navigator.userAgent.indexOf('Safari') != -1):
			SAFARI		= true;
			break;
	}	
}

function addEventListener2(element, event_name, observer, capturing) 
{  
	if(element.addEventListener)
	{
		element.addEventListener(event_name, observer, capturing); 
	}
	else if(element.attachEvent)
	{
		element.attachEvent("on" + event_name, observer); 
	}
}






