/*!	as3as3communication v0.1 <http://www.webkinz.com> 
	all rights reserved, copyright 2010
*/

	/**
		1). replace names in swfDivNames_as to real tag names (now "[player0]", "[player1]")
		2). add in html in body tag:
		<body bgcolor="#f3ffe8"  onload="pageInit();">
		3). change tag.y probably is not a good idea, there will be a scroller
		( may be width=1, y=padeHeight)
		
		note: swfId == div tag name
		
		note order of div tags are important: if callback in main div tag is not set, there will be a problem in
		sendSwfIdToAll() - it will stopped at first call back and will not send call back to as3div and there will be no
		swf id in bridge -> calls will be returned to originator as if external calls (!)
	*/
	var swfDivNames_a = new Array( "mainSite", "as3core");
	var jsReady = false;
	var allSwfReady = false;
	var swfReadyCount = 0;
	var swfTop_o = new Object();
	var hiddenY= "5000px";	

	
	/** called by the onload event of the <body> tag */
	function pageInit() 
	{
		jsReady = true;
		storeTopPositionForAllSwf();
		//document.forms["form1"].output.value += "\n" + "JavaScript is ready.\n";
	}
	
	
	/** called from actionscript to check if the page has initialized and JavaScript is available */
	function isReady() 
	{
		return { ready:jsReady };
	}
	
	/**  */
	function thisMovie( movieName ) 
	{
		// If in IE use window
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			// If document works (firefox) then use it, if it doesn't (chrome, opera, safari) use window
			if (document[movieName]) {
				return document[movieName];
			} else {
				return window[movieName];
			}
		}
	}
	
	/** called from actionscript to communicate with other actionscript content
	 * send to all exept 'swfId' (myself)
	 * return object
	 */
	function sendToJavaScript( swfId, requestObject ) 
	{
		var responseObject; 

		//document.forms["form1"].output.value += "ActionScript("+ swfId +") requestObject: "+ requestObject +")\n";

		var imax = swfDivNames_a.length;
		
		for (var i=0; i<imax; i++) {
			if ( swfId != swfDivNames_a[ i ] ) {
				responseObject = thisMovie( swfDivNames_a[ i ] ).sendToActionScript( requestObject );
				
				//document.forms["form1"].output.value += "ActionScript("+ swfDivNames_a[ i ] +") responseObject: " + responseObject +",  >responseObject.success: "+responseObject.success +"\n";
			}
		}
		return responseObject;
	}

	
	/** used to hide-show content, to keep initial position */
	function  storeTopPositionForAllSwf()
	{
		var imax = swfDivNames_a.length;
		for (var i=0; i<imax; i++) {
			swfTop_o[ swfDivNames_a[i] ] = document.getElementById( swfDivNames_a[i] ).style.top;
			//document.forms["form1"].output.value += " swfTop_o["+ swfDivNames_a[i] +"]: " +  swfTop_o[ swfDivNames_a[i] ]+"\n";
		}
	}
	/** used to hide-show content */
	function getTopPositionById( swfId)
	{
		return swfTop_o[ swfId ];
	}

	
	
	/** note: there was style.visibility problem in IE: swf in hidden div stops receiving calls
		options: use style.width or style.top
 	*/
	/** called from actionscript: show my div tag */
	function showASContent( swfId ) 
	{
		//document.forms["form1"].output.value += "ActionScript says: showASContent(" + swfId + ")\n";
		//document.getElementById( swfId ).style.visibility = "visible";
		//document.getElementById( swfId ).style.width = 600;
		document.getElementById( swfId ).style.top = getTopPositionById( swfId);
		return true; //indicates call come through
	}
	
	/** called from actionscript: hide my div tag, usage: as3 hides itself with close() button */
	function hideASContent( swfId ) 
	{
		//document.forms["form1"].output.value += "ActionScript says: hideASContent(" + swfId + ")\n";
		//document.getElementById( swfId ).style.visibility = "hidden";
		//document.getElementById( swfId ).style.width = 0; 
		document.getElementById( swfId ).style.top = hiddenY;
		return true; //indicates call come through
	}
	
	/** called from actionscript: show other div tag, usage: as1 loads as3 game */ 
	function showOtherASContent( swfId ) 
	{
		var success = true;
		
		var imax = swfDivNames_a.length;
		for (var i=0; i<imax; i++) {
			if ( swfId != swfDivNames_a[ i ] ) {
				success = success && showASContent( swfDivNames_a[ i ] );
			}
		}
		
		return success;
	}
	
	/** called from actionscript: hide other content */
	function hideOtherASContent( swfId ) 
	{
		var success = true;
		
		var imax = swfDivNames_a.length;
		for (var i=0; i<imax; i++) {
			if ( swfId != swfDivNames_a[ i ] ) {
				success = success && hideASContent( swfDivNames_a[ i ] );
			}
		}
		
		return success;
	}

	
	
	/* 
	 * called from actionscript:
	 *		a). notify that all actionscripts movie are ready
	 *		b). identify swfId's,
	 * each of swf will identify their tag name automatically
	 * (each of 'swfId' will call function to identify itself)
	 */
	function sendSwfIdToAll( timeStamp )
	{
		//1. count ready actionscripts movie
		swfReadyCount = swfReadyCount + 1;
		if ( swfReadyCount == swfDivNames_a.length ) {
			//document.forms["form1"].output.value += "All Swf ready, count= " + swfReadyCount + "\n";
			allSwfReady  = true;
			
		}
		
		//2. send flag "allSwfReady"; send timestamp (by timestamp it will identify himself)
		var successFlag = true;
		var imax = swfDivNames_a.length;
		for (var i=0; i<imax; i++) {
			successFlag = successFlag && thisMovie( swfDivNames_a[ i ] ).receiveSwfId( swfDivNames_a[ i ] , timeStamp, allSwfReady );
		}

		//
		return successFlag;
	}
	/** used to reposition and resize the div when changing views / layouts in the AS3 version of the site **/
	function updateAS3DivProperties( swfId, pDivLeft, pDivTop, pDivWidth, pDivHeight )
	{
	 	previousYPos = pDivTop;
		document.getElementById("as3core").style.top = pDivTop;
		document.getElementById("as3core").style.left = pDivLeft;
		//document.getElementById("as3coreDiv").style.width = pDivWidth;
		//document.getElementById("as3coreDiv").style.height = pDivHeight;
		if (navigator.appName.indexOf("Microsoft") != -1) {
			var divTop = document.getElementById("as3core").style.top;
			var actualTop = divTop.substring(0, divTop.length - 2)
			document.getElementById("as3core").style.top = (actualTop - 55) + "px";
		}
		return true; //indicates call come through
	}

