	var idFromBrowseTo = 0;
	var WEBSERVICE_URL = './xml.php?action=store';

	function passToFlash( flashId, varName, varValue )
	{
		var flashObj = null;
		
		flashObj = window.top.document.getElementById( flashId );

		if( flashObj )
		{
			if( typeof( flashObj.SetVariable ) != "undefined" )
			{
				// as2 (also called, but not working for as3)
				flashObj.SetVariable( varName, varValue );
			}

			// as3
			if( 	( typeof( flashObj.openPageDynamic ) != "undefined" ) 
				&&	( varName == "activePageId" ) )
			{
				if( varValue )
				{
					flashObj.openPageDynamic( varValue );
				}
			}
			
			
			
			if( 	( typeof( flashObj.browserWindowWidth ) != "undefined" ) 
					&&	( varName == "windowWidth" ) )
			{
				if( varValue )
				{
					flashObj.browserWindowWidth( varValue );
				}
			}
			
			if( 	( typeof( flashObj.browserWindowHeight ) != "undefined" ) 
					&&	( varName == "windowHeight" ) )
			{
				if( varValue )
				{
					flashObj.browserWindowHeight( varValue );
				}
			}
		}
	}
	
	function SetDimensions()
	{
		passToFlash( "swfobjId", "windowWidth", document.body.clientWidth );
		passToFlash( "swfobjId", "windowHeight", document.body.clientHeight );
	}
	

	function historyHandler()
	{
		this.onAddressChange = function( event )
		{
			if( event && event.path && event.path.length )
			{
				var menuId = getIdFromPath( event.path );
				
				if( menuId.length )
				{
					if( idFromBrowseTo != menuId )
					{
						pushDeeplink( menuId );
					}
				}
				
				idFromBrowseTo = 0;
			}
			
		};
		
		this.olgaAddHistory = function( data )
		{
			
			if ( data && data.length && ( data != "0" ) )
			{
				var currentId = getIdFromPath( SWFAddress.getValue() );
				if( currentId != data )
				{
					idFromBrowseTo = data;
					SWFAddress.setValue( data );
				}
				
				var menuId = data;

				// add to statistics tracker
				if( typeof( trackStatistics ) == "function" ) 
					trackStatistics( menuId );
			}
		};
		
		
	};

	function pushDeeplink( id )
	{
		passToFlash( "swfobjId", "activePageId", id );
	}

	function getIdFromPath( path )
	{
		var menuId		= "";
		var firstChar	= path.substr( 0, 1 );
		if( ( firstChar == "/" ) && ( path.length > 1 ) )
		{
			menuId = path.substr( 1 );
		}
		else
		{
			if( path != "/" )
				menuId = path;
		}
		return( menuId )
	}
	
	/**
	 * @return string The header of the XML-request
	 */
	function generateRequestHeader( action )
	{
		var requestHead = '\<\?xml version="1.0" encoding="UTF-8" standalone="yes" \?\><db>';
		requestHead += '<action>'+action+'</action>';
		return requestHead;
	}

	/**
	 * @return string The footer of the XML-request
	 */
	function generateRequestFooter()
	{
		var requestFooter = '</db>';
		return requestFooter;
	}
	
	/**
	 * Issues a xml request to the webservice
	 * @param action
	 * @param requestString
	 * @return
	 */
	function doXmlRequest( action, requestString )
	{
		var requestSent = false;
		
		if( requestString.length > 0 )
		{
			var completeRequestString = generateRequestHeader( action ) + requestString;
			completeRequestString += generateRequestFooter();
			
			requestSent = true;
			
			var webserviceRequest = new Ajax.Request(WEBSERVICE_URL, {
				method: 'post',
				contentType: 'text/xml',
				postBody: completeRequestString,
				onSuccess: function(transport) {
						parseXML(transport.responseXML);
					},
				onFailure: function(transport) {
						// alert("Something went wrong processing the webservice request");
					}
				});
		}
		return( requestSent );
	}
	
	/**
	 * Parses response from webservice
	 * @param responseXML
	 * @return
	 */
	function parseXML( responseXML )
	{
		if( responseXML && responseXML.documentElement && responseXML.documentElement.getElementsByTagName("m") )
		{
			var menuNode = responseXML.documentElement.getElementsByTagName("m");
			var menuId = menuNode[0].getAttribute("id");
			if( menuId > 0 )
			{
				menuData[menuId] = new Array();
				menuData[menuId]["m_title"]			= responseXML.documentElement.getElementsByTagName('m_title')[0].firstChild.data;
				menuData[menuId]["m_pretty_url"]	= responseXML.documentElement.getElementsByTagName('m_pretty_url')[0].firstChild.data;
				storeStatistics( menuData[menuId] );
			}
		}
		
	}
	
	/**
	 * Gets menu-data from webservice and tracks the pageview for this menu id 
	 * @param menuId
	 * @return
	 */
	function retrieveMenuDataAndStoreStatistics( menuId )
	{
		var requestString = '';
		
		requestString += '<m id="' + menuId + '"></m>';
		doXmlRequest( "getMenuUrl", requestString );
	}
	
	var menuData = new Array();
	
	/**
	 * Handles request for registration of a pageview
	 * @param data	The menu id of the page to be tracked
	 * @return
	 */
	function handleStatistics( data )
	{
		var menuId = data;
		
		if( menuId > 0 )
		{
			if( menuData[menuId] )
			{
				storeStatistics( menuData[menuId] );
			}
			else
			{
				retrieveMenuDataAndStoreStatistics( menuId );
			}
		}		
	}
	
	/**
	 * Call Googles page tracker
	 * @param menuItemData
	 * @return
	 */
	function storeStatistics( menuItemData )
	{
		try
		{ 
			var pageTracker = _gat._getTracker( trackCode );

			SWFAddress.setTitle( menuItemData["m_title"] );
			pageTracker._trackPageview( menuItemData["m_pretty_url"] );
		} 
		catch(err)
		{
		}	
	}
	
	Event.observe(window, "load", function(event)
	{
		if( window.document.location.hash.length )
			menuId = getIdFromPath( window.document.location.hash.replace( "#", "" ) );
		
		document.historyHandler = new historyHandler();
		// SWFAddress.addEventListener( SWFAddressEvent.EXTERNAL_CHANGE, document.historyHandler.onAddressChange );
		// IE(8) needs SWFAddressEvent.CHANGE...
		SWFAddress.addEventListener( SWFAddressEvent.CHANGE, document.historyHandler.onAddressChange );
		LoadFlash();
	});

	Event.observe(window, "resize", function(event) {SetDimensions();});
