// ==============================================================
// FLACCESS v1.0 (c)2004 Sergi Meseguer (http://zigotica.com/)
// Released under Creative Commons ShareAlike license: http://creativecommons.org/licenses/by-sa/2.0/
// Go to http://meddle.dzygn.com/eng/tools/ or http://meddle.dzygn.com/esp/utilidades/ for further info
// ==============================================================

// Set this variable to 1 if you want to alert intermediate steps:
var flaccess_debug;


var flaccess_loaded;
function checkFlash(min){
	var version = 0;
	if(!min) min = 10;
	if (navigator.plugins)  { 
		if(navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var desc = navigator.plugins["Shockwave Flash"].description;
			version = parseInt(desc.substring(16)); 
		}
		else if(navigator.appVersion.indexOf("MSIE")>-1){ 
			// try/catch would be better but then breaks script in ns4:
			// loop by Geoff Stearns (geoff@deconcept.com, http://blog.deconcept.com/)
			result = false;
	   		for(var i = min; i >= 3 && result != true; i--){
	    			execScript('on error resume next: result = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
				version = i;
			}
		}
	}
	return version;
}

function addFlash(postpone,minversion,path,width,height,node) {
	// defining arguments (from arguments array) in object and embed elements:
	var minargs = 6;
	var objpars = '<param name="movie" value="'+path+'">';
	var empars = ' src="'+path+'" ';
	var align = '';
	var flashid = '';
	var salign = '';
	var emflva = '';
	var allowedObjParams = ["menu","play","quality","scale","devicefont","bgcolor","wmode","salign","flashvars","base"];  
	var allowedEmbParams = ["menu","play","quality","scale","devicefont","bgcolor","wmode","base","swliveconnect"]; 
	for(var op=parseInt(arguments.length);op>minargs;op--) {
		var tmp = arguments[op-1].split(":"); var tmpname = tmp[0];var tmpvalue = tmp[1];
		if(allowedObjParams.indexOf(tmpname.toLowerCase()) >-1) { 
			objpars += '<param name="'+tmpname+'" value="'+tmpvalue+'">';
		}
		if(allowedEmbParams.indexOf(tmpname.toLowerCase()) >-1) {
			empars += ' '+tmpname+'="'+tmpvalue+'" ';
		}
		if(tmpname.toLowerCase() == "align") {
			align = ' align="'+tmpvalue+'" ';
		}
		if(tmpname.toLowerCase() == "salign") {
			salign = ' salign="'+tmpvalue+'" ';
		}
		if(tmpname.toLowerCase() == "flashvars") {
			emflva += ' flashvars="'+tmpvalue+'" ';
		}
		if(tmpname.toLowerCase() == "id") {
			empars += ' name="'+tmpvalue+'" ';
			flashid = ' id="'+tmpvalue+'" ';
		}
	}
	
	
	// this will allow to call same arguments when postpone true
	var postvars = '';
	for(var p=parseInt(arguments.length);p>minargs;p--) {
		postvars += ',"'+arguments[p-1]+'"';
	}


	if(checkFlash(minversion) >= minversion){
		if(flaccess_debug==1) alert("minimum flash " + minversion + " is ok, we have version " + checkFlash())
		
		if(postpone==0) {
			// adds flash object before page loads, unless it's ns4 and page is loaded
			
			// flash object:
			var obj = '<object '+flashid+' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+width+'" height="'+height+'" '+align+'>';
			obj += objpars;
			obj += '<embed src="'+path+'" width="'+width+'" height="'+height+'"  '+empars+' type="application/x-shockwave-flash" '+align+' '+salign+' '+emflva+' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';
			obj += '</object>'
			
			if(flaccess_debug==1) alert(obj);
			
			// splits node id for normal browsers
			var col = node.split(",");
			var id = col[0];
			
			if(!document.layers) {
				if(document.getElementById(id)) document.getElementById(id).innerHTML = obj;
				else if(document.all[id]) document.all[id].innerHTML = obj;
				else eval('addFlash(1,minversion,path,width,height,node'+postvars+')');
			}
			else {
				// ns4
				if(flaccess_loaded==1){
					// page fully loaded
					
					// generates node reference:
					var ns4path = "";
					for(var a=col.length;a>0;a--) {
						if(a!=col.length) ns4path += ".document['"+col[a-1]+"']";
						else ns4path += "document['"+col[a-1]+"']";
					}
					eval('o = ' + ns4path);
					
					// writes flash object to layer:
					o.document.open();
					o.document.write(obj); 
					o.document.close();

				}
				else {
					// force postpone
					if(postvars!="") eval('addFlash(1,minversion,path,width,height,node'+postvars+')');
					else addFlash(1,minversion,path,width,height,node);
				}
			}
		}
		
		else{
			// setTimeout makes sure window.flaccess_loaded==1 (needed for ns4 to document.write ok)
			addLoadEvent(function(){window.flaccess_loaded = 1;});
			if(postvars!="") addLoadEvent(function(){setTimeout("addFlash(0,"+minversion+",'"+path+"',"+width+","+height+",'"+node+"'"+postvars+")",300);});
			else addLoadEvent(function(){setTimeout("addFlash(0,"+minversion+",'"+path+"',"+width+","+height+",'"+node+"')",300);});
		}
	}
	else {
		if(flaccess_debug==1) alert("we have version " + checkFlash() + " and we needed flash " + minversion)
	}

}

// code by Simon Willison (http://simon.incutio.com)
function addLoadEvent (fn) {
	var old = window.onload;
	if (typeof window.onload != "function") {
		window.onload = fn;
	}
	else {
		window.onload = function() {
			old();
			fn();
		}
	}
}

// extending Array, by Aaron Boodman (youngpup.net):
Array.prototype.indexOf = function(foo) { 
	for (var i = 0; i < this.length; i++)  
	if (foo == this[i]) return i;  
	return -1;  
}