/**
    * Flash Player
    */
	 
var requiredFlashVersion = new Object();
// Set up some defaults
requiredFlashVersion.majorVersion = 9;
requiredFlashVersion.minorVersion = 0;
requiredFlashVersion.revision = 0;
 
/*
	Info text to be displayed when the user does not have flash installed and provides a link to get the flash player
 */
function generateGetFlashLink(element) 
	{
		if($(element + " p.flash-required").length == 0) 
			{
				$('<p class="flash-required">Get the latest <a href="http://www.adobe.com/go/getflashplayer">Adobe Flash plugin</a> so you can view video content.</p>').prependTo(element);
			}	
	}

/*
	Object to be instantiated for each flash block in a page.
*/

function FlashObject() {

	// Parameters controlling the flash object
	this.parameters = new Object();

	/* Required attributes, there can be others */
	this.parameters.container = null;
	this.parameters.id = null;
	this.parameters.width = "100%";
	this.parameters.height = "100%";
	this.parameters.swfSrc = null;
	//this.parameters.flashVars = "";
	this.parameters.altContent = null;
	this.parameters.bgColour = "#ffffff";
	
    this.embed = function() {

		
			if(this.parameters.swfSrc == null) {
				//alert('Initialisation error: No flash source found. Check swfSrc.');
				return;
			}
			
			if(this.parameters.container == null) {
				//alert('Initialisation error: No flash container found.');
				return;
			}
 
			var flashObj = createFlashObject(this.parameters);
			
			if ( $.browser.msie && isWin && !$.browser.opera ) {
				// Need to use this method otherwise IE6 seems to crash
				document.getElementById(this.parameters.container).innerHTML = flashObj;
				//$("#"+element).prepend(flashObj);
			} else {
				// Put the object into the DOM
				$("#"+this.parameters.container).prepend(flashObj);
			}
			
   }
}


function createFlashObject(parameters) {

	var flashObject;
	
	/* Params to ignore, use lower case */
	var globalIgnoreParams = ["swfsrc", "reqminorversion", "reqmajorversion", "reqrevision", "altcontent", "container", "height","width", "id"];
	var ignoreIEParams = ["allowscriptaccess"];
	var ignoreParams = ["bgcolour"];

   // Remove original static content
   $(parameters.altContent).empty();
                  
   /* Create object and add attributes/parameters */
	// For IE on windows (cannot do it the DOM way, need to use strings!)
   if ( $.browser.msie && isWin && !$.browser.opera ) {
		
      // Add required object attributes for IE.
		flashObject = '<object ';
		flashObject += 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
		flashObject += 'width="'+parameters.width+'" height="'+parameters.height+'"';
		flashObject += 'id="'+parameters.id+'">"';
		
		// Create the special case movie param
		flashObject += '<param name="movie" value="'+parameters.swfSrc+'" />';
		
		// Embed the remaining parmeters supplied
		for (var i in parameters) 
			{
				if( isInArray(i, ignoreIEParams) | isInArray(i, globalIgnoreParams)) 
					{
						continue;
					}
			
				// Create a param child element and insert into the object
				flashObject += '<param name="'+i+'" value="'+parameters[i]+'" />';
			}	
		
		flashObject += '</object>';
		
		
   } else {
		// And all the others (which use a different embed method), create our object
      flashObject = document.createElement("object");   
		
      // Add attributes
      $(flashObject).attr({ 
			data		: parameters.swfSrc,
			type		: "application/x-shockwave-flash",
			width 		: parameters.width,
			height		: parameters.height,
			id			: parameters.id
		});
				
		for (var i in parameters) {
			// Ignore these attributes. Used for initialisation only
			if( isInArray(i, ignoreParams) | isInArray(i, globalIgnoreParams)) {
					continue;
			}
			
	      // Create a param child element and insert into the object
	      var param = document.createElement("param");  
	      $(param).attr({name: i, value: parameters[i]})
	      $(flashObject).append(param);
		}		

   }
	
	return flashObject;
}

/*
	Check if a string occurs in an array
*/
function isInArray(str, arr) {
	for(var i=0; i<arr.length; i++) {
		if(str.toLowerCase() == arr[i]) {
			return true
		}
	}
	return false;
}

/*
	Run through page and replace any elements targeted for replacement (they have the class 'flash-object' set)
*/
function insertFlash() {
	
   // Create array of flash objects that have not previously been initialised
   var flashInstances = $(".flash-object:not(.flash-ready)").get();
	
	if($(flashInstances).size() > 0) {

	   // Iterate through each to set flash
	   $(flashInstances).each(function(idx, flashInstance){ 
	      
			// Create the new flash object (default values will have been initialised)
	      var flash = new FlashObject();
			
			// Flag this instance as initialised by adding a class
			$(flashInstance).addClass("flash-ready");
	      var objectElem = $("object",flashInstance);

	      // Get all params from object element in the source.
	      var params = $(objectElem).children("param");

			// Add them to the new flash object
	      $(params).each(function(j){
				var param = params[j];
	         var name = $(param).attr("name");
	         var value = $(param).attr("value");
				
				// Populate required version numbers as they are found
				if(name.toLowerCase() == "reqmajorversion") {
					requiredFlashVersion.majorVersion = value;
				} else if(name.toLowerCase() == "reqminorversion") {
					requiredFlashVersion.minorVersion = value;
				} else if(name.toLowerCase() == "reqrevision") {
					requiredFlashVersion.revision = value;
				} else {
					// Pass along and include ready for output
					flash.parameters[name] = value;
				}
	      });
	      
	      // remove the source object
	      $(objectElem).remove();
	      
	      // Additional parameters not in the source
	      flash.parameters.altContent = flashInstance;
	      flash.parameters.id = "flash_object_" + idx
	   
	      // need to check if it has an ID, and create one if it doesn't: TO DO
	      flash.parameters.container = this.id;
	      
			// Replace the static content with new browser specific piece of flash code
	      flash.embed();
			
	   });
	}

}


