
// embed.js
// used to create embed code for flash, with maximum awesome o/
// author: Albin Larsson

// these are used for the bitshifting magic
var TYPE_OBJECTPARAMETER 	= 1;
var TYPE_OBJECTHEADER 		= 2;
var TYPE_EMBED  			= 4;

// for debugging purposes
var output = "";
var _DEBUG_ = 0;

// global array to hold the parameters
var parameters = new Array();
var paramNum = 0;


// this function creates a new parameter
function Parameter(paramName, value, objOrEmbed)
{
	this.paramName = paramName;
	this.value = value;
	this.objOrEmbed = objOrEmbed;
}


function wout(text)
{
	if(_DEBUG_ == 1)
	{
		output += text;
	}
	else
	{
		document.write(text);
	}
}


function newLineChar()
{
	if(_DEBUG_ == 1)
	{
		return "\n";
	}
	else
	{
		//return "<br>";
		return "";
	}
}


function writeHeader()
{
	wout("<object ");

	for(var x = 0; x != parameters.length; x++)
	{
		if(parameters[x].objOrEmbed & TYPE_OBJECTHEADER)
		{
			wout( parameters[x].paramName + "=" + "\"" + parameters[x].value + "\" " );
		}
	}

	wout(">" + newLineChar() );
}



function writeObjectParameters()
{

	for(var x = 0; x != parameters.length; x++)
	{
		if(parameters[x].objOrEmbed & TYPE_OBJECTPARAMETER)
		{
			wout( "<param name=" );
			wout( "\"" + parameters[x].paramName + "\"" + " value=" + "\"" + parameters[x].value + "\"" + "/>" + newLineChar() );
		}
	}
	
}



function writeEmbedString()
{
	wout("<embed ");

	for(var x = 0; x != parameters.length; x++)
	{
		if(parameters[x].objOrEmbed & TYPE_EMBED)
		{
			wout( parameters[x].paramName + "=" + "\"" + parameters[x].value + "\" " );
		}
	}

	wout("></embed>" + newLineChar() );	
}



function writeFooter()
{
	wout("</object>" + newLineChar() );
}



function addParameter(paramName, value, objOrEmbed)
{
	parameters[paramNum] = new Parameter(paramName, value, objOrEmbed);
	paramNum++;
}



function embedFlash( widgetCode, parentCode, instanceCode, width, height, background, flashVars )
{

	// flash movie to display
	var movie = 	"http://www.thewidgetfactory.se/load/" + widgetCode + "/" + parentCode + "/" + instanceCode + "/flash.swf?ref=" + window.location.hostname + window.location.pathname;
	var flashName = "TestFlash";

	// width and height ... where do we get these anyway?

	if( width == null ) { width =   "450"; }	
	if( height == null ) { height =   "330"; }	

	//var width =   "450";
	//var height =  "330";

	// variables grabbed from the browser
	var screenWidth = screen.availWidth;
	var screenHeight = screen.availHeight;
	var browserName = navigator.appName;
	var browserVersion = navigator.appVersion;
	var userOperatingSystem = navigator.platform;

	// flash vars
	if( flashVars != null ) {
		flashVars = "ref=" + window.location.hostname + window.location.pathname + "&" + flashVars;
	} else {
		flashVars = "ref=" + window.location.hostname + window.location.pathname;
	}
 

 
 
	// object header 
	addParameter( "id", flashName, TYPE_OBJECTHEADER );
	addParameter( "classid", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000", TYPE_OBJECTHEADER );
	addParameter( "codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0", TYPE_OBJECTHEADER );

	// shared object header + embed
	addParameter( "width",   width,    TYPE_OBJECTHEADER | TYPE_EMBED );
	addParameter( "height",  height,   TYPE_OBJECTHEADER | TYPE_EMBED );
	addParameter( "align",   "middle", TYPE_OBJECTHEADER | TYPE_EMBED );

	// shared object parameter + embed
	addParameter( "allowScriptAccess", "always", TYPE_OBJECTPARAMETER | TYPE_EMBED );
	addParameter( "allowFullScreen",   "true",      TYPE_OBJECTPARAMETER | TYPE_EMBED );
	addParameter( "quality",           "high",       TYPE_OBJECTPARAMETER | TYPE_EMBED );
	//addParameter( "bgcolor",           "#ffffff",    TYPE_OBJECTPARAMETER | TYPE_EMBED );
	//addParameter( "wmode", 	    	"transparent", 	 TYPE_OBJECTPARAMETER | TYPE_EMBED );
	addParameter( "flashVars", 	    flashVars, 	 TYPE_OBJECTPARAMETER | TYPE_EMBED );
	
	if( background == null || background == "transparent" ) {
		addParameter( "wmode", 	    	"transparent", 	 TYPE_OBJECTPARAMETER | TYPE_EMBED );
	} else {
		addParameter( "bgcolor",           background,    TYPE_OBJECTPARAMETER | TYPE_EMBED );
	}


	// object parameters
	addParameter( "movie",	movie,	TYPE_OBJECTPARAMETER );
	
	// embed parameters
	addParameter( "src", movie, TYPE_EMBED );
	addParameter( "name", flashName, TYPE_EMBED );
	addParameter( "pluginspage", "http://www.macromedia.com/go/getflashplayer", TYPE_EMBED );
	addParameter( "type", "application/x-shockwave-flash", TYPE_EMBED );


	writeHeader();
	writeObjectParameters();
	writeEmbedString();
	writeFooter();
	
	if(_DEBUG_ == 1)
	{
		alert(output);
	}
	
}


