/*

concept, design and production by Netvertising AG, Zurich

NetvClientDetect 


REVISION HISTORY
---
Version 2.7.1  // 
---
- bug-fix: fullscreen parameter
- added NetvClientDetect.clientInfo['flash_version']  (array with version parts, i.e. [10, 1, 102])

---
Version 2.7  //
---
- added Flash sub-version detection (e.g. 9.0.115 instead of only 9)
- added Chrome detection
- added FlashVars attribute for Flash Player 6+

---
Version 2.6  //bug-fixing
---

---
Version 2.5  //support badbrowsers version compare
---
- added badbrowsers special compare for older browsers e.g. {os_name:'Mac',br_name:'Safari',br_ver:{compare:'<=',value:'419'}}

---
Version 2.4  //support os iPad, iPhone, iPod
---
- get this params to flash "_stageWidth" and "_stageHeight"

---
Version 2.3  //remind to update 'detector_version' in function!
---
- get this params to flash "_stageWidth" and "_stageHeight"

---
Version 2.2  //remind to update 'detector_version' in function!
---
- bugfix ie if no flash installed "flashVersion undefined"

---
Version 2.1  //remind to update 'detector_version' in function!
---
- added detector_version

---
Version 2.0  //remind to update 'detector_version' in function!
---
ajax support:
- xx=function() for all functions, 
- new getFlashVersion for ie (modified from google swfobject)
- removed closing object slash object open tag in function showflash()

usage 
<div id="swf-container" style=""><!-- swf gets here by innerHTML --></div>
<script type="text/javascript">
 	var F=new NetvClientDetect();
	swfembed = F.showflash({
	 	[...]
		mode:'return' // write, return
	)}
	$('swf-container').innerHTML = swfembed;
</script>
--


*/
function NetvClientDetect(){
	
	var detector_version = '2.7.1';
	
	var UNDEF = "undefined";
	var	OBJECT = "object";
	var	SHOCKWAVE_FLASH = "Shockwave Flash";
	var	SHOCKWAVE_FLASH_AX = "ShockwaveFlash.ShockwaveFlash";
	var	FLASH_MIME_TYPE = "application/x-shockwave-flash";
	var DEBUGOUT = "";
	
	var cookiecheck = false; //check for cookies or not
	var params;
	var plugin = false;
	var swfVersion;
	var flashVersion = [0,0,0];
	
	this.clientInfo=new clientInfo_();
	this.clientInfo.detector_version = detector_version;
	
	this.showflash=function(params){
		this.params = params;
		var isbadbrowser=false;
		if(isset(params.badbrowsers)){
			for(var n in params.badbrowsers){
				var c=0;
				for(var m in params.badbrowsers[n]){
					--c;
					if(typeof(params.badbrowsers[n][m])=='object'){
						if(eval(this.clientInfo[m]+' '+params.badbrowsers[n][m].compare+' '+params.badbrowsers[n][m].value)){
							++c;
						}
					}
					else{
						if(this.clientInfo[m]==params.badbrowsers[n][m]){
							++c;
						}
					}
				}
				if(c==0){isbadbrowser=true;}
			}
		}
		
		var tmpparams=new Array();
		for (var flashparam in params.flashparams)
		{
			tmpparams.push(escape(flashparam) + "=" + escape(params.flashparams[flashparam]));
		}
		tmpparams.push(escape("_stageWidth") + "=" + params.width);
		tmpparams.push(escape("_stageHeight") + "=" + params.height);
		
		var flashparams = "";
		if (tmpparams.length>0)
		{
			flashparams += tmpparams.join("&");
		}
		
		var protocol_=(window.location.toString().indexOf("https")==0)? "https": "http";

		var html='';
		
		
		/* get minimal required Flash Player version */
		
		params.minversion = (String(params.minversion).replace(/,/g,"."));
		swfVersion = String(params.minversion).split(".",3);
		// fill in missing 0's
		for (var i=0; i<3; i++)
		{
			if (swfVersion[i] == undefined)
			{
				swfVersion[i] = 0;
			}
		}
		
		/* get installed Flash Player version */
		flashVersion = this.clientInfo['flash_version'];
		
		if (!isbadbrowser && matchVersions())
		{
			if (flashVersion[0] < 6)
			{
				// before Flash Player 6, there was no FlashVars param -> pass vars by appending to url
				params.src += ("?" + flashparams);
			}
			
			var objectString = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="' + protocol_ + '://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="' + (isset(params.width) ? String(params.width) : 0) + '" height="' + (isset(params.height) ? String(params.height) : 0) + '" id="' + (isset(params.id) ? params.id : "") + '" name="' + (isset(params.id) ? params.id : "") + '"><param name="movie" value="' + params.src + '" \/>';
			var objectStringEnd = '<\/object>';
			var embedString = '<embed src="' + params.src + '" width="' + (isset(params.width) ? String(params.width) : 0) + '" height="' + (isset(params.height) ? String(params.height) : 0) + '" id="' + (isset(params.id) ? params.id : "") + '" name="' + (isset(params.id) ? params.id : "") + '"';
			var embedStringEnd = ' type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" \/>';
			
			for (var attr in params)
			{
				attr_lc = String(attr).toLowerCase();
				
				// check if attribute is allowed
				if (
					attr_lc == "play" ||
					attr_lc == "loop" ||
					attr_lc == "menu" ||
					attr_lc == "quality" ||
					attr_lc == "scale" ||
					attr_lc == "align" ||
					attr_lc == "salign" ||
					attr_lc == "wmode" ||
					attr_lc == "bgcolor" ||
					attr_lc == "base" ||
					attr_lc == "border" ||
					attr_lc == "swliveconnect" ||
					attr_lc == "flashvars" ||
					attr_lc == "devicefont" ||
					attr_lc == "allowscriptaccess" ||
					attr_lc == "seamlesstabbing" ||
					attr_lc == "allowfullscreen" ||
					attr_lc == "allownetworking" ||
					attr_lc == "fullscreenaspectratio"
				)
				{
					// console.info(attr_lc+": "+params[attr]);
					if (params[attr])
					{
						objectString += '<param name="' + attr_lc + '" value="' + params[attr] + '" \/>';
						embedString  += ' ' + attr_lc + '="' + params[attr] + '"';
					}
				}
			}
			
			if (flashVersion[0] >= 6)
			{
				// before Flash Player 6, there was no FlashVars param -> pass vars by appending to url
				objectString += '<param name="flashvars" value="' + flashparams + '" \/>';
				embedString  += ' flashvars="' + flashparams + '"';
			}
			
			html += (objectString + embedString + embedStringEnd + objectStringEnd);
			
			if (params.focusonload==1)
			{
				window.onload=function()
				{
					if (typeof(document.embeds[params.id]) != UNDEF)
					{
						//document.embeds[params.id].focus();
					}
					else
					{
						document.getElementById(params.id).focus();
					}
				}
			}
		}
		else if (isset(params.alt))
		{
			//show alt
			switch(params.alt.mode){
				case 'redirect':
				location.href=params.alt.value+""+flashparams;
				break;
				case 'image':
				html='<img src="'+params.alt.value+'" width="'+params.width+'" height="'+params.height+'" border="0" alt="" />';
				break;
				case 'html':
				html=params.alt.value;
				break;
				case 'jsfunc': 
				eval(params.alt.value);
				break;
			}
		}
		else
		{
			html = "Flash Player not installed (Version " + params.minversion + " required)";
		}
		DEBUGOUT+=html;
		if(params.mode=='return'){
			return html;
		}
		else{
			document.write(html);
		}
	};
	
	function clientInfo_(){
		var detector_version;
		/*orig by http://www.quirksmode.org/js/detect.html*/
		var dataBrowser= [
			{
				string: navigator.userAgent,
				subString: "Chrome",
				identity: "Chrome"
			},
			{ 	
				string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{	// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 	// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		]
		var dataOS = [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			},
			{
				string: navigator.platform,
				subString: "BSD",
				identity: "BSD"
			},
			{
				string: navigator.platform,
				subString: "iPad",
				identity: "iPad"
			},
			{
				string: navigator.platform,
				subString: "iPhone",
				identity: "iPhone"
			},
			{
				string: navigator.platform,
				subString: "iPod",
				identity: "iPod"
			}
		]
		
		searchString = function(data) {
		for (var i=0;i<data.length;i++){
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
			}
		}
		
		searchVersion = function(dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		}
		
		getFlashVersion = function(clientinfo) {
			// modified: code.google.com/p/swfobject/
			if (typeof navigator.plugins != UNDEF && typeof navigator.plugins[SHOCKWAVE_FLASH] == OBJECT)
			{
				var d = navigator.plugins[SHOCKWAVE_FLASH].description;
				if (d && !(typeof navigator.mimeTypes != UNDEF && navigator.mimeTypes[FLASH_MIME_TYPE] && !navigator.mimeTypes[FLASH_MIME_TYPE].enabledPlugin))  // navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin indicates whether plug-ins are enabled or disabled in Safari 3+
				{
					plugin = true;
					d = d.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
					flashVersion[0] = parseInt(d.replace(/^(.*)\..*$/, "$1"), 10);
					flashVersion[1] = parseInt(d.replace(/^.*\.(.*)\s.*$/, "$1"), 10);
					flashVersion[2] = /[a-zA-Z]/.test(d) ? parseInt(d.replace(/^.*[a-zA-Z]+(.*)$/, "$1"), 10) : 0;
				}
				
				/*
					Detect the Flash Player version for non-Internet Explorer browsers
					- Detecting the plug-in version via the object element is more precise than using the plugins collection item's description:
					  a. Both release and build numbers can be detected
					  b. Avoid wrong descriptions by corrupt installers provided by Adobe
					  c. Avoid wrong descriptions by multiple Flash Player entries in the plugin Array, caused by incorrect browser imports
					- Disadvantage of this method is that it depends on the availability of the DOM, while the plugins collection is immediately available
				*/
				if (plugin)
				{
					var b = document.getElementsByTagName("body")[0];
					var o = document.createElement(OBJECT);
					o.setAttribute("type", FLASH_MIME_TYPE);
					var t = b.appendChild(o);
					if (t) {
						var counter = 0;
						(function(){
							if (typeof t.GetVariable != UNDEF) {
								var d = t.GetVariable("$version");
								if (d) {
									d = d.split(" ")[1].split(",");
									flashVersion = [parseInt(d[0], 10), parseInt(d[1], 10), parseInt(d[2], 10)];
								}
							}
							else if (counter < 10) {
								counter++;
								setTimeout(arguments.callee, 10);
								return;
							}
							b.removeChild(o);
							t = null;
						})();
					}
				}
			}
			else if (typeof window.ActiveXObject != UNDEF) {
				try {
					var a = new ActiveXObject(SHOCKWAVE_FLASH_AX);
					if (a) { // a will return null when ActiveX is disabled
						d = a.GetVariable("$version");
						if (d) {
							d = d.split(" ")[1].split(",");
							flashVersion = [parseInt(d[0], 10), parseInt(d[1], 10), parseInt(d[2], 10)];
						}
					}
				}
				catch(e) {}
			}
			// end modified: code.google.com/p/swfobject/
			
			this.flashVersion = flashVersion;
			return flashVersion;
		} //end getFlashVersion
		
		this.br_name = searchString(dataBrowser) || "unknown";
		this.br_ver_float = searchVersion(navigator.userAgent)
			|| searchVersion(navigator.appVersion)
			|| "unknown";
		this.br_ver=Math.floor(this.br_ver_float);
		this.os_name = searchString(dataOS) || "unknown";
		this.br_lang=(navigator.language)? navigator.language: navigator.browserLanguage;
		this.flash_version = getFlashVersion(this);
		this.flash_ver = parseFloat(this.flash_version[0] + "." + this.flash_version[1]);
		this.screen_width = screen.width;
		this.screen_height = screen.height;
		
		if(cookiecheck){
			if(typeof(navigator.cookieEnabled)=="undefined"){
				document.cookie="testcookie=1";
				this.cookies_enabled=(document.cookie.indexOf("testcookie")<0)? false : true;
			}
			else{
				this.cookies_enabled=(navigator.cookieEnabled)? true : false;
			}
		}
		
	}
	
	function matchVersions() {
		for (var i=0; i<3; i++)
		{
			if (flashVersion[i] > swfVersion[i])
			{
				return true;
			}
			else if (flashVersion[i] < swfVersion[i])
			{
				return false;
			}
		}
		return true;
	}
	
	this.showDebug=function(displaymode){
			DEBUGOUT += '\n\n#### clientInfo ####\n';
			
			
			for(all in this.clientInfo){
					DEBUGOUT += all+"="+this.clientInfo[all]+"\n";
			}
			DEBUGOUT += '#### EndclientInfo ####\n';
			
			if (displaymode=='html') {
				if (document.getElementById('NetvClientDetectDebug') || false) {
					document.getElementById('NetvClientDetectDebug').innerHTML = '<pre>' + doHtmlEncode(DEBUGOUT) + '</pre>';
				} else {
					document.write('<div id="NetvClientDetectDebug" style="position:absolute; top:0px;left:0px;font-size:11px;z-index:200;background-color:orange;color:black;width:auto;"><pre>' + doHtmlEncode(DEBUGOUT) + '</pre></div>');
				}
			} else {
				alert(DEBUGOUT);
			}
	};
	
	//check for debugmode get-params
	if(window.location.href.lastIndexOf('debugmode=html') > 0){
		 var me=this;
		 document.write('<div id="NetvClientDetectDebug" style="position:absolute; top:0px;left:0px;font-size:11px;z-index:200;background-color:orange;color:black;width:auto;"><pre></pre></div>');
		 var t=setTimeout(function(){ me.showDebug('html');return true; }, 1000);

	}
	else if(window.location.href.lastIndexOf('debugmode=alert') > 0){
		 var me=this;
		  t=setTimeout(function(){ me.showDebug('alert') }, 1000);
	}
		
	/////////////////utility functions
	function isset(variable){
		return (typeof(variable)!="undefined");
	}
	function doHtmlEncode(s){
		if(typeof(s) != "string"){
			s = s.toString();
		}
		s = s.replace(/&/g, "&amp;");
		s = s.replace(/</g, "&lt;");
		s = s.replace(/>/g, "&gt;");
		return s;
	}
	/////////////////end utility functions
	
}
