/**
 * Flash Detection Script
 * 
 * This script detects Flash 4 and higher.
 * 
 * Place detect_flash() in your page initialisation to make this script active.
 * To add a flash object to a specific object use:
 * load_flvplayer(obj, src, width, height, majorversion);
 * You can call this function everywere in your code at any time,
 * before or after you called detect_flash(), but it will only work once detect_flash() is called.
 * 
 * JavaScript written by Stefan Thoolen <stefan@netvlies.nl>
 * Flash Detector object written by Maarten Verstraeten <maarten@netvlies.nl>
 */

/**
 * Path to the Flash Detector object
 */
flashdetector_swf='/swf/flashdetector.swf';

/**
 * This Object contains all Flash version information
 * 
 * Recursive print of this object:
 * <code>
 *    flashinfo {
 *      detected    Boolean
 *      ostag       String
 *      version {
 *        major     Int
 *        minor     Int
 *        revision  Int
 *      }
 *    }
 * </code>
 */
flashinfo=new Object;

/**
 * Starts the flash detection
 */
function detect_flash() {
	flashinfo.detected=false;
	// For debugging, to check the non-flash version
	if(document.location.search.search('noflash')>-1) return false;
	// Checks if a detection cookie is set
	var yumyum=document.cookie.split("; ");
	for(var i=0;i<yumyum.length;i++) if(yumyum[i].split("=")[0]=='flashversion') cf(yumyum[i].split('=')[1]);
	// Flash is not previously detected in this session, adding detector
	if(!flashinfo.detected) {
		var div=document.createElement('DIV');
		div.style.position='absolute';
		div.style.left='-100px';
		div.style.top='-100px';
		var htmlcode ='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="1" height="1">';
		    htmlcode+='<param name="movie" value="'+flashdetector_swf+'" />';
		    htmlcode+='<embed src="'+flashdetector_swf+'" width="1" height="1" type="application/x-shockwave-flash" plug inspage="http://www.macromedia.com/go/getflashplayer"></embed>'; 
		    htmlcode+='</object>';
		div.innerHTML=htmlcode;
		document.body.appendChild(div);
	}
}

/**
 * Loads a specific flash source into an object
 * @param	object	obj				The container in which the Flash object must be placed
 * @param	string	src				The source link of the Flash object
 * @param	int		width			The width of the Flash object
 * @param	int		height			The height of the Flash object
 * @param	int		majorversion	If set, the object will be loaded in this flash version and newer
 */
function load_flvplayer(obj, src, width, height, majorversion, id, bgcolor) {
	if(typeof obj=='string') var obj=document.getElementById(obj);
	if(!majorversion) var majorversion=0;
	
	if(!flashinfo.detected) {
		flash_objects[flash_objects.length]=new Object;
		var that=flash_objects[flash_objects.length-1];
		that.container=obj;
		that.src=src;
		that.id=id;
		that.bgcolor=bgcolor;
		that.width=width;
		that.height=height;
		that.majorversion=majorversion;
	} else {
		if(majorversion<=flashinfo.version.major) {	
		/**
		 * 'scale', 'showall'
		 * 'wmode', 'window',
		 */	
			var htmlcode ='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" id="'+id+'" width="'+width+'" height="'+height+'">';
			    htmlcode+='<param name="movie" value="'+src+'" />';
			    htmlcode+='<param name="quality" value="high" />';
			    htmlcode+='<param name="allowScriptAccess" value="always" />';
			    htmlcode+='<param name="allowFullScreen" value="true" />';
			    htmlcode+='<param name="scale" value="noscale" />';
			    htmlcode+='<param name="bgcolor" value="' + bgcolor + '" />'
				htmlcode+='<param name="wmode" value="opaque" />';
			    htmlcode+='<embed src="'+src+'" quality="high" width="'+width+'" height="'+height+'" name="'+id+'" allowScriptAccess="always" allowFullScreen="true" scale="noscale" bgcolor="' + bgcolor + '" wmode="opaque" type="application/x-shockwave-flash"></embed>'; 
			    htmlcode+='</object>';
			obj.innerHTML=htmlcode;
		}
	}
	
}

/**
 * Contains all load_flvplayer() calls until detect_flash() is called.
 * @access private
 */
flash_objects=new Array();

/**
 * Flash Callback Function
 * This function is triggered by flashdetector.swf with a version string
 * @param	string	version			The version string of Flash 
 * @access private
 */
function cf(version) {
	// Recording flash version into the cookies
	if(document.cookie.indexOf('flashversion')<0) document.cookie="flashversion="+version;
	var version=version.split(',');
	flashinfo.detected=true;
	flashinfo.ostag=version[0].split(' ')[0];
	flashinfo.version=new Object;
	flashinfo.version.major=parseInt(version[0].split(' ')[1]);
	flashinfo.version.minor=parseInt(version[1]);
	flashinfo.version.revision=parseInt(version[2]);
	
	if(flash_objects) {
		for(var i=0;i<flash_objects.length;i++) {
			that=flash_objects[i];
			load_flvplayer(that.container, that.src, that.width, that.height, that.majorversion, that.id, that.bgcolor);
		}
	}
	flash_objects=null;
}


function nvs_flv_get(movie_id) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movie_id] : document[movie_id];
}

function close_flv_connection(movie_id) {
	var flash_obj = nvs_flv_get(movie_id);
	
	flash_obj.unload_flv();
}
