function getFileExtension(file){
	var slashPos = Math.max(0, Math.max(file.lastIndexOf("\\"), file.lastIndexOf("/")));
	var dotPos = -1;
		
	if (-1 != (dotPos = file.indexOf(".", slashPos))){
		return file.substring(dotPos + 1); 
	}
	return "";
}

function videoFail(componentId, versionInstalled){
	document.getElementById(componentId).innerHTML = 
			"<p style=\"font-size: 0.9em; font-family: Arial,Helvetica,sans-serif\">" +
				"This video requires the latest version of " + 
				"<a href=\"http://www.adobe.com/go/getflashplayer\" target=\"_blank\" border=\"0\">Adobe Flash player</a>. " + 
			"</p>" + 
			"<a href=\"http://www.adobe.com/go/getflashplayer\" target=\"_blank\" border=\"0\">" + 
				"<img style=\"border: 0\" src=\"/visage/static/10007/160x41_Get_Flash_Player.jpg\" />" + 
			"</a>";
}

function embedFlashVideo(componentId, mediaFile, config){
	//Default width and height
	var width = 320;
	var height = 240;
	var minFlashVersionMajor = 9;
	var minFlashVersionMinor = 115;
	
	//If width or height not specified, see if parent object has dimensions we can use
	var heightSpecified = config != undefined && config.height != undefined;
	var widthSpecified = config != undefined && config.width != undefined;
	
	var parentComponent;
	if (!heightSpecified || !widthSpecified){
		parentComponent = document.getElementById(componentId);
		
		if (!widthSpecified && parentComponent.style.width != ""){
			width = parentComponent.style.width;
		}
		if (!heightSpecified && parentComponent.style.height != ""){
			height = parentComponent.style.height;
		}
	}
	
	//Initialise variables and set defaults if necessary
	var autoplay = (config == undefined || config.autoplay == undefined) ? true : config.autoplay;
	var thumbnailImage = (config == undefined || config.thumbnailimage == undefined) ? "" : config.thumbnailimage;
	var stillFrameImage = (config == undefined || config.stillframeimage == undefined) ? "" : config.stillframeimage;
	var backgroundColor = (config == undefined || config.bgcolor == undefined) ? "#ffffff" : config.bgcolor;
	var thumbnailTakesPrecedence = (config == undefined || config.thumbnailprecedence == undefined) ? true : config.thumbnailprecedence;
	var minFlashVersionMajor = (config == undefined || config.minflashversionmajor == undefined) ? minFlashVersionMajor : config.minflashversionmajor;
	var minFlashVersionMinor = (config == undefined || config.minflashversionminor == undefined) ? minFlashVersionMinor : config.minflashversionminor;
	
	
	/*
	If no file extension then reset splash image. This typically happens when the link is "pics_orig" + @THUMBNAIL 
	put there is no thumbnail specified
	*/
	if ("" == getFileExtension(thumbnailImage)){
		thumbnailImage = "";
	}	
	if ("" == getFileExtension(stillFrameImage)){
		stillFrameImage = "";
	}
		
	var pl = [];    //Create an array to store the playlist details
    var i = 0;
    
    var isAudio = getFileExtension(mediaFile) == "mp3";
    var isVideo = !isVideo;
    
	/*
      Display a splash image if an image is available and either
        1) file is audio, or
        2) file is video and autoplay is set to false (i.e. show 'splash' image)        
     */    
    /*
     Playlist object will end up looking like this:     
     playlist: ['spash_image.jpg', {url: movie.mpg.flv, autoplay: true/false}]
    */		
     if (thumbnailImage != "" || stillFrameImage != ""){
		if (isAudio || (isVideo && !autoplay)){            
			var image = "";
			
			//If thumbanail image *and* still image available then set according to preference, otherwise use the one entered
			if (thumbnailImage != "" && thumbnailTakesPrecedence){
				image = thumbnailImage;
			}
			else if (stillFrameImage != ""){
				image = stillFrameImage;
			}
		
			pl[i] = image;			
			i++;            
        }       
     }
     else if (isAudio && !heightSpecified && !widthSpecified){
		//If audio and a) no image specifed b) the developer has not set the dimensions, use smaller size
		width = 250;
		height = 24;
	 }
    
    pl[i] = new Object;
    pl[i].url = mediaFile;
    pl[i].autoPlay = autoplay;
    	
	flashembed(componentId, 
        {src:"/visage/static/10007/flowplayer-3.1.2.swf", 
		 width:width, 
		 height:height,
                 wmode: 'transparent',
		 bgcolor:backgroundColor,
		 version: [minFlashVersionMajor, minFlashVersionMinor], 
		 onFail:function() {videoFail(componentId, this.getVersion()) }
		}, 
        {config: 
            { 	playlist: pl,
				initialScale: 'scale', useNativeFullScreen: true,showMenu : false,showFullScreenButton : true,loop : false }
        } 
    );
}