/*///////////////////////////////////////////////////////////////////////////////////////////////
Creates a flashBox(); v1.0 - by Jason Savage*
/////////////////////////////////////////////////////////////////////////////////////////////////

implementation: 
	var movie1 = new flashBox('none.swf',300,400{extraParams});
	
-----------------------------------------------------------------------------------------------
included functions:
	[top level]
	- display(target/optional/)			- writes flash code to page
	- remove()							- removes flash code from page
	
	[editing]
	- edit(name,value) or edit(object) 	- edit flash settings
	- addVars("pet","dog")				- basic way to send a variable in to the flash file
	- setTarget(id of target div) 		- basic way to set the flash display location
	- setNoFlashMessage(message)		- Change content to be displayed if flash wasn't found
	
	[internal]
	- trace(true|false/optional/)		- (false) displays an alert dialog with the HTML code or (true) displays all possable settings for the flashBox
	- buildHTMLcode()					- returns the HTML code for the flashBox
	- showAllSettings()					- returns all possable settings for the flashBox

-----------------------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////////////////
* I developed this code while learning javascript, but I'm sure there are similar flash codes out there. Use this freely if you like it.
	- This Script utilizes "MM_findObj()//v4.01" from Adobe as the $O function
	- This Script utilizes "AC_OETags.js" from Adobe to detect at least Flash Player 8
	
credits: ideas borrowed from swfobject.js (www.deconcept.com), the Prototype Framework (www.prototypejs.org), and Adobe Spry framework (www.adobe.com)
///////////////////////////////////////////////////////////////////////////////////////////////

default: {play:'true', loop:'true', menu:'false', devicefont:'false', quality:'high', wmode:'transparent', align:'middle', scale:'showall',
			bgcolor:'#ffffff', allowFullScreen:'false', allowScriptAccess:'sameDomain', salign:''}	
*/

//class constructor function
function flashBox(lnk,width,height,setupObj){ //v1.0
	//default settings for a flash object
	this.settings = {
		id:'',name:'',width:'',height:'',src:'',movie:'',codebase:'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
		classid:'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',pluginspage:'http://www.macromedia.com/go/getflashplayer',type:"application/x-shockwave-flash",
		play:'true',loop:'true',menu:'false',devicefont:'false',quality:'high',wmode:'transparent',align:'middle',scale:'showall',
		bgcolor:'#ffffff',allowFullScreen:'false',allowScriptAccess:'sameDomain',salign:''
	}
	this.core = {
		flashVars	: {},
		Flash	: (window.DetectFlashVer != null)?window.DetectFlashVer(8, 0, 0):'unavailable',
		noFlash	: '<p align="center">Please install Flash Player to view this presentation.<br /><a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&promoid=BIOW"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" width="88" height="31" border="0" /></a></p>',
		target 	: (setupObj && setupObj.target != null)?setupObj.target:''
	}
	this.settings = _fbx.Apply({movie:lnk, src:lnk, width:width, height:height, id:lnk.split(".")[0].toString().split("/").pop(),name:lnk.split(".")[0].toString().split("/").pop()},this.settings);
	if(setupObj){
		if(setupObj.target) setupObj = _fbx.Remove(setupObj,['target']);
		this.settings = _fbx.Apply(setupObj,this.settings);
	}
}
//[top level]
flashBox.prototype.display = function(trg){
	if(trg != null) this.setTarget(trg);
	var ele = (this.core.target != '')?_fbx.$(this.core.target):false;
	var code = (this.core.Flash || this.core.Flash == 'unavailable')? this.buildHTMLcode() : this.core.noFlash;
	if(ele){ele.innerHTML = code; if(ele.style.display == 'none') ele.style.display = 'block';
	}else{document.write(code);}
}
flashBox.prototype.remove = function(){
	var ele = (this.core.target != '')? _fbx.$(this.core.target) : _fbx.$(this.settings.id).parentNode;
	if(ele){ele.innerHTML = '&nbsp;'; ele.style.display = 'none';}
}

//[editing] - Getter/Setters//		
flashBox.prototype.edit = function(name, value){
	if(typeof(name) == 'object' && value == null){ this.settings = _fbx.Apply(name,this.settings);
	}else if(name != null && value != null){ this.settings[name] = value;}
}

flashBox.prototype.addVars = function(name,value){ this.core.flashVars[name] = value;}
flashBox.prototype.setTarget = function(trg){ this.core.target = trg;}
flashBox.prototype.setNoFlashMessage = function(message){this.core.noFlash = message;}

//[internal]
flashBox.prototype.trace = function(opts){
	alert( (opts != null && opts == true)?this.showAllSettings():this.buildHTMLcode() );
}
flashBox.prototype.showAllSettings = function(){
	return _fbx.toArray({codebase : '(default)', pluginspage : '(default)',type : '(default)', width : 'px | %', height : 'px | %', id : '(automatically set to swf file name)',
			src : '(path to swf file)',play : '(pause at start) true | false',loop : 'true | false',menu : '(display menu) true | false',devicefont : 'true | false',
			quality : 'low | medium | high | best',wmode : 'window | opaque | transparent',align : '(HTML alignment) left | middle | right | top | bottom',scale : 'noborder | exactfit | noscale',
			salign : 'lt | rt | lb | rb | l | r | t | b', bgcolor : '(Hexadecimal color code) ',allowScriptAccess : 'sameDomain', allowFullScreen : 'true | false'},'p').join("\n");
}
flashBox.prototype.buildHTMLcode = function(){
	this.settings.src = (this.settings.src.indexOf('?') != -1)?this.settings.src.split("?")[0] + _fbx.toQuery(this.core.flashVars) : this.settings.src + _fbx.toQuery(this.core.flashVars);
	this.settings.movie = this.settings.src;
	//build html code
	if(this.settings.target) this.settings = _fbx.Remove(this.settings,['target']);
	var o = _fbx.Convert(_fbx.Keep(this.settings,['classid','codebase','width','height','id','align']),function(a,b){return a+'="'+b+'" ';}).join("")
	var p = _fbx.Convert(_fbx.Remove(this.settings,['id','name','width','height','src','classid','codebase','pluginspage','type']),function(a,b){return '<param name="'+a+'" value="'+b +'" />';}).join("")
	var e = _fbx.Convert(_fbx.Remove(this.settings,['classid','codebase','movie','id']),function(a,b){return a+'="'+b+'" ';}).join("")

	return '<object ' + o + '>' + p + '<embed ' + e + '/></object>';
}

///[Utilites]/////////////////////////////////////////////////////////////////////////////
_fbx = {
	$ : function(n){
		if(document.getElementById) return document.getElementById(n);
		if(document.all) return document.all[n];
	},
	Apply : function(objA,objB){
		for(x in objA) objB[x] = objA[x]; return objB;
	},
	Remove : function(obj,removeArray){
		newobj={},key=removeArray.join(",");
		for(x in obj) if(key.indexOf(x) == -1) newobj[x] = obj[x];
		return newobj;
	},
	Keep : function(obj,keepArray){
		newobj={}, key=keepArray.join(",");
		for(x in obj) if(key.indexOf(x) != -1) newobj[x] = obj[x];
		return newobj;
	},
	Convert : function(obj,utilityFunction){
		var a = []; for(x in obj) 
		a.push(utilityFunction(x,obj[x])); return a;
	},
	toArray : function(obj){
		if(typeof(obj) != 'object') return "";
		return _fbx.Convert(obj,function(a,b){return a+" : "+b;});
	},
	toQuery : function(obj){
		if(typeof(obj) != 'object') return "";
		var str =  _fbx.Convert(obj,function(a,b){return a+"="+b;}).join("&");
		return (str.length > 0)?("?"+str):"";
	}
}