/**
 * 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="window" />';
			    htmlcode+='<embed src="'+src+'" quality="high" width="'+width+'" height="'+height+'" name="'+id+'" allowScriptAccess="always" allowFullScreen="true" scale="noscale" bgcolor="' + bgcolor + '" wmode="window" 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;
}


/**
 * Geen dependency meer met Prototype
 */ 

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};