/**
 * Setup for the search engine optimized flash website.
 * @author Holger Stitz (http://www.holgerstitz.de)
 */

// Location of the target file from document root with trailing slash.
var directory = "/";

// The target file for the redirects and the flash holder.
var targetFile = "";

// The index file of a directory. Usually it's something like index.html or index.php.
var indexFile = "";

// If file extension is defined, erase it in SWFAddress value. This value should corresponding with extension of the indexFile. Leave it empty to include the file extension into the SWFAddress value.
var fileExtension = ".php";

// Id of the html container, that is replaced by the flash file.
var idcontent = "flashcontent";

// Required flash version
var flashVersion = "9.0.0";

// Configure the SWFObject embed
function runSWFObject(flashvars, params, attributes)
{
	swfobject.embedSWF("Loader.swf", idcontent, "100%", "100%", flashVersion, "expressInstall.swf", flashvars, params, attributes);
}


//////////////////////////////////////////////////////////
//														//
//   It is not necessary to edit the following lines!   //
//													    //
//////////////////////////////////////////////////////////

//test, if flash can be used
if (swfobject.hasFlashPlayerVersion(flashVersion))
{

	//get the document root
	var documentRoot = location.protocol + "//"+ location.host + directory;

	//get relative path from entrypoint (document root)
	var relativeUrl = location.href.replace(documentRoot, "");

	//remove any fragment
	relativeUrl = relativeUrl.replace("#"+SWFAddress.getValue(), "");
	relativeUrl = (relativeUrl.lastIndexOf("#") == relativeUrl.length-1) ? relativeUrl.substring(0, relativeUrl.indexOf("#")) : relativeUrl;

	//if is root directory, then set relative url to index file
	if(relativeUrl == "" && SWFAddress.getValue() == "/")
	{
		relativeUrl = indexFile;
	}
	//else if is any other directory without index file, add it
	else if(relativeUrl.lastIndexOf("/") == relativeUrl.length-1)
	{
		relativeUrl += indexFile;
	}

	//if show targetFile in url, always redirect it. also by index it self
	if(relativeUrl != targetFile || SWFAddress.getValue() == "/")
	{
		//console.log("REDIRECT: "+ documentRoot + targetFile + "#/"+relativeUrl.replace(fileExtension, "")+"/");
		SWFAddress.href(documentRoot + targetFile + "#/"+relativeUrl.replace(fileExtension, ""));
	}
	

	//the following lines where used by the index file, after redirect
	
	/**
	* Generates an URL from the SWFAddress value
	* @see ExternalInterface call from flash
	*/
	function getURLBySWFAddress(swfaddress)
	{
		//parse SWFAddress value for query string. Remove first slash
		var rawUrl = swfaddress.substring(1, swfaddress.length);
		//cut trailing slash in url
		if(rawUrl.lastIndexOf("/") == rawUrl.length-1) rawUrl = rawUrl.substring(0, rawUrl.length-1);
		//returns the file extension or undefined
		function getFileExtension(filename) { return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined; }
		//if no file extension given, add the defined fileextension
		rawUrl += (getFileExtension(rawUrl) == undefined && rawUrl != "") ? fileExtension : "";
		return (documentRoot + rawUrl);
	}
	
	/**
	* Generates an SWFAddress value from the URL
	* @see ExternalInterface call from flash
	*/
	function getSWFAddressByURL(url)
	{
		return swfaddress = url.replace(documentRoot, "").replace(fileExtension, "");
	}

	//setup the flash vars, params and attributes
	var flashvars = {};
	var params = { allowScriptAccess: "always", allowNetworking: "always", allowFullscreen: "true" };
	var attributes = { id:"wormlandflash" };
	attributes.id = idcontent;

	//embed the swf file via SWFObject
	runSWFObject(flashvars, params, attributes);
	//swfmacmousewheel.registerObject(attributes.id)
	/*
	document.writeln('\n	<style type="text/css">');
	document.writeln('	<!--');
	document.writeln('		html {');
	document.writeln('			overflow: hidden;');
	document.writeln('		}');
	document.writeln('	-->');
	document.writeln('	</style>');
	*/
}
