/*

concept, design and production by Netvertising AG, Zurich

NetvClientDetect 


REVISION HISTORY

---
Version 2.2  //remind to update 'detector_version' in function!
---
- bugfix ie if no flash installed "playerVersion 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.2';
	
	var cookiecheck=false; //check for cookies or not
	var DEBUGOUT="";
	var flashVersion;
	var params;
	
	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(params.badbrowsers[n][m]==this.clientInfo[m]){
						++c;
					}
				}
				if(c==0){isbadbrowser=true;}
			}
		}
		
		var tmpparams=new Array();
		for(var all in params.flashparams){
			tmpparams.push(all+"="+params.flashparams[all]);
		}
		var flashparams="?";
		if(tmpparams.length>0){
			flashparams+=tmpparams.join("&");
		}
		flashparams+="&_stageWidth="+params.width+"&_stageHeight="+params.height;
		
		var protocol_=(window.location.toString().indexOf("https")==0)? "https": "http";

		var html='';
		if(!isbadbrowser && this.clientInfo['flash_ver'] >= params.minversion) {
			html+='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="'+protocol_+'://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" ';
			html+=(isset(params.id))? 'id="'+params.id+'" ': '';
			//html+=(isset(params.id))? 'name="'+params.id+'" ': '';
			html+=(isset(params.width))? 'width="'+params.width+'" ': '';
			html+=(isset(params.height))? 'height="'+params.height+'" ': '';
			html+=(isset(params.align))? 'align="'+params.align+'"': '';
			html+='>\n';
			html+='<param name=movie value="'+params.src+flashparams+'" />\n';
			html+=(isset(params.menu))? '<param name="menu" value="'+params.menu+'" />\n': '';
			html+=(isset(params.salign))? '<param name="salign" value="'+params.salign+'" />\n': '';
			html+=(isset(params.scale))? '<param name="scale" value="'+params.scale+'" />\n': '';
			html+=(isset(params.quality))? '<param name="quality" value="'+params.quality+'" />\n': '';
			html+=(isset(params.bgcolor))? '<param name="bgcolor" value="'+params.bgcolor+'" />\n': '';
			html+=(isset(params.play))? '<param name="play" value="'+params.play+'" />\n': '';
			html+=(isset(params.loop))? '<param name="loop" value="'+params.loop+'" />\n': '';
			html+=(isset(params.wmode))? '<param name="wmode" value="'+params.wmode+'" />\n': '';
			html+=(isset(params.allowScriptAccess))? '<param name="allowScriptAccess" value="'+params.allowScriptAccess+'" />\n': '';
			html+=(isset(params.base))? '<param name="base" value="'+params.base+'" />\n': '';
			html+=(isset(params.allowFullScreen))? '<param name="allowFullScreen" value="'+params.allowFullScreen+'" />\n': '';

			html+='<embed src="'+params.src+flashparams+'" ';
			html+=(isset(params.id))? 'name="'+params.id+'" ': '';
			html+=(isset(params.menu))? 'menu="'+params.menu+'" ': '';
			html+=(isset(params.width))? 'width="'+params.width+'" ': '';
			html+=(isset(params.height))? 'height="'+params.height+'" ': '';
			html+=(isset(params.quality))? 'quality="'+params.quality+'" ': '';
			html+=(isset(params.scale))? 'scale="'+params.scale+'" ': '';
			html+=(isset(params.align))? 'align="'+params.align+'" ': '';
			html+=(isset(params.salign))? 'salign="'+params.salign+'" ': '';
			html+=(isset(params.bgcolor))? 'bgcolor="'+params.bgcolor+'" ': '';
			html+=(isset(params.play))? 'play="'+params.play+'" ': '';
			html+=(isset(params.loop))? 'loop="'+params.loop+'" ': '';
			html+=(isset(params.wmode))? 'wmode="'+params.wmode+'" ': '';
			html+=(isset(params.allowScriptAccess))? 'allowScriptAccess="'+params.allowScriptAccess+'" ': '';
			html+=(isset(params.base))? 'base="'+params.base+'" ': '';
			html+=(isset(params.allowFullScreen))? 'allowFullScreen="'+params.allowFullScreen+'" ': '';
			html+='pluginspage="'+protocol_+'://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"';
			html+='></embed>';
			html+='</object>\n';
			if(params.focusonload==1){
				window.onload=function(){
					if(typeof(document.embeds[params.id])!="undefined"){
						//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="plugin not installed (flash "+params.minversion+")";
		}
		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: "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"
			},
		]
		
		
		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){
			var flashVersion = -1;
			var flashVersion_DONTKNOW = -2;
			// NS3+, Opera3+, IE5+ Mac, firefox,safari (support plugin array):  check for Flash plugin in plugin array
			if (navigator.plugins != null && navigator.plugins.length > 0){
				var flashPlugin = navigator.plugins['Shockwave Flash'];
				if (typeof flashPlugin == 'object'){
					for (var i=15; i>0; i--){
						if (flashPlugin.description.indexOf(i+'.') != -1){
							flashVersion = i;
						}
					}
				}
			}
			// IE4+ Win32:  attempt to create an ActiveX object using VBScript
			else if (clientinfo['br_name']=='Explorer' && clientinfo['br_ver'] >= 4 && clientinfo['os_name']=='Windows'){
				//vb function for active-x
				/* document.write('<scr' + 'ipt language="VBScript"\> \n');
				document.write('Function VBGetSwfVer(i) \n');
				document.write('on error resume next \n');
				document.write('Dim swControl, swVersion \n');
				document.write('swVersion = 0 \n');
				
				document.write('set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(i)) \n');
				document.write('if (IsObject(swControl)) then \n');
				document.write('swVersion = swControl.GetVariable("$version") \n');
				document.write('end if \n');
				document.write('VBGetSwfVer = swVersion \n');
				document.write('End Function \n');
				document.write('</scr' + 'ipt\> \n');
				var versionStr=0;
				for (var i=15;i>0;i--){
					versionStr = VBGetSwfVer(i);
				}
				if(versionStr!=0 && versionStr!=-1){
					var tempArray = versionStr.split(" ");
					var tempString = tempArray[1];
					var versionArray = tempString .split(",");
					flashVersion=parseInt(versionArray[0]);
				}
				else {
					flashVersion = flashVersion_DONTKNOW;
				} */
				
				
				
				// modified from google swfobject
				UNDEF = "undefined";
				OBJECT = "object";
				SHOCKWAVE_FLASH = "Shockwave Flash";
				SHOCKWAVE_FLASH_AX = "ShockwaveFlash.ShockwaveFlash";
				FLASH_MIME_TYPE = "application/x-shockwave-flash";
				EXPRESS_INSTALL_ID = "SWFObjectExprInst";
				if (typeof window.ActiveXObject != UNDEF) {
					var a = null, fp6Crash = false;
					try {
						a = new ActiveXObject(SHOCKWAVE_FLASH_AX + ".7");
					} catch(e) {
						try { 
							a = new ActiveXObject(SHOCKWAVE_FLASH_AX + ".6");
							flashVersion = 6;
							a.AllowScriptAccess = "always";	 // Introduced in fp6.0.47
						} catch(e) {
							if (flashVersion == 6) {
								fp6Crash = true;
							}
						}
						if (!fp6Crash) {
							try {
								a = new ActiveXObject(SHOCKWAVE_FLASH_AX);
							}
							catch(e) {
							}
						}
					}
					
					if (!fp6Crash && a) { // a will return null when ActiveX is disabled
						try {
							d = a.GetVariable("$version");	// Will crash fp6.0.21/23/29
							if (d) {
								try {
									flashVersion = d.split(" ")[1].split(",")[0];
								} catch(e) {
									flashVersion = flashVersion_DONTKNOW;
								}
								//flashVersion = [parseInt(d[0], 10), parseInt(d[1], 10), parseInt(d[2], 10)];
								if(versionStr < 1){
									flashVersion = flashVersion_DONTKNOW;
								} 
							}
						}
						catch(e) {}
					}
				}
				// end modified from google swfobject
			} else {
				flashVersion = flashVersion_DONTKNOW;
			}
			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_ver = getFlashVersion(this);
		this.screen_width = screen.width;
		this.screen_height = screen.height;
		
		if(cookiecheck){
			if(typeof(navigator.cookieEnabled)=="undefined"){
				alert(1);
				document.cookie="testcookie=1";
				this.cookies_enabled=(document.cookie.indexOf("testcookie")<0)? false : true;
			}
			else{
				this.cookies_enabled=(navigator.cookieEnabled)? true : false;
			}
		}
		
	}

	
	
	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
	
}