/*
version 2006.10
concept, design and production by Netvertising AG, Zurich 
*/
/*
- do not change this file
- do not put this code in a html-file! must be js-file.
- copy the commented code below into html-file.
- duplicate the function if you have multiple swf's.
- do not forget <noscript> tags in html.
- reqflashVersion is the required flash-version.
- params that are not set are not used.

debug:
- to show debug code as alert box set "?clientdebug=1&debugmode=1"  
- to show debug code in html set "?clientdebug=1&debugmode=2"
- to show debug code in html on predefined position, additionally insert  
		<div id="debugOutDiv" style="position:absolute;z-index:200;left:0px;top:0px; background-color:yellow;"></div>
   right below the <body> tag
*/

// EXAMPLE USAGE:
/*
var f=new function(){
this.reqflashVersion = 7;
this.flashName="test.swf";
this.flashId="myname"; //identification name
this.flashWidth=100;
this.flashHeight=100;
this.flashBgcolor="#FFFFFF";
this.flashScale="noborder"; //noborder, exactfit, noscale
this.flashAlign="top"; //top, bottom, left, right
this.flashSalign="TL"; // TL, TR, BL, BR
this.flashQuality="high"; // low, medium, high, autolow, autohigh
this.flashMenu="false"; //true, false
this.flashPlay=""; //true, false
this.flashLoop=""; //true, false
this.flashWmode="window"; //window,transparent, opaque
//this.altPage="noflash.php"; //url
this.altImg="noflash.gif"; //url, image must have same size as swf
this.altHtml="<div>no flash installed</div>";
this.altFunc="showalt()";
this.moreParams="?sessid=1&lang=de";
this.badBrowsers=new Array(); //use os/application/version combination, e.g. Array("win-ie-7", "win-op", "ie-5", "nn", "mac");
this.flash();
}
*/
////////////////////////////
////////////////////////////

var debug = new Array();
debug['show'] = 1; //1: auto, 2: enforce, 0: nodebug
debug['showMode'] = 1; //1: alert, 2: browser out
debug['showHtml'] = 0; //0: no html code, 1: show html code 
debug['out'] = '';


if (debug['show'] == 1 && window.location.href.lastIndexOf('clientdebug=1') > 0) {
	debug['show'] = 2;
	if (window.location.href.lastIndexOf('debugmode=2') > 0) {
		debug['showMode'] = 2;
	} 
}


Object.prototype.flash=flash;

function flash(){
	this.moreParams=(typeof(this.moreParams)!="undefined")? this.moreParams: "";
	var browser = new getBrowserInfo();
	var isBadBrowser = false;
	
	debug['out'] += "\n\n#### BadBrowsers:\n"
	for(all in this.badBrowsers){
		if (all != "flash") {
			debug['out'] += all+"="+this.badBrowsers[all]+"\n";
		}
	}
	
	if((typeof(this.badBrowsers)!="undefined")? this.moreParams: ""){
		for(var i=0; i<this.badBrowsers.length; i++){
			
			var ar=this.badBrowsers[i].split("-");
			if (ar[2] && browser['os_name']==ar[0] && browser['app_name']==ar[1] && browser['app_ver']==ar[2]) {
				//win-ie-7
				isBadBrowser = true;
			} else if(!ar[2] && browser['os_name']==ar[0] && browser['app_name']==ar[1]) {
				//win-ie
				isBadBrowser = true;
			} else if (!ar[2] && browser['app_name']==ar[0] && browser['app_ver']==ar[1]) {
				//ie-7
				isBadBrowser = true;
			} else if (!ar[2] && !ar[1] && browser['app_name']==ar[0]) {
				//ie
				isBadBrowser = true;
			} else if (!ar[2] && !ar[1] && browser['os_name']==ar[0]) {
				//win
				isBadBrowser = true;
			}
			if (isBadBrowser) {
				debug['out'] += "badBrowserMatch=" + this.badBrowsers[i] + "\n";
				break;
			}
		}
	}
	debug['out'] += "isBadBrowser=" + isBadBrowser +"\n";
	debug['out'] += "#### EndBadBrowser.\n";
	
	var htmltags="";
	if(!isBadBrowser && browser['flash_verno'] >= this.reqflashVersion) {
		htmltags+="<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab' ";
		htmltags+=(typeof(this.flashId)!="undefined")? "id='"+this.flashId+"' ": "";
		htmltags+=(typeof(this.flashId)!="undefined")? "name='"+this.flashId+"' ": "";
		htmltags+=(typeof(this.flashWidth)!="undefined")? "width='"+this.flashWidth+"' ": "";
		htmltags+=(typeof(this.flashHeight)!="undefined")? "height='"+this.flashHeight+"' ": "";
		htmltags+=(typeof(this.flashAlign)!="undefined")? "align='"+this.flashAlign+"'": "";
		htmltags+=">\n";
		htmltags+="<param name=movie value='"+this.flashName+this.moreParams+"'>\n";
		htmltags+=(typeof(this.flashMenu)!="undefined")? "<param name=menu value='"+this.flashMenu+"'>\n": "";
		htmltags+=(typeof(this.flashSalign)!="undefined")? "<param name=salign value='"+this.flashSalign+"'>\n": "";
		htmltags+=(typeof(this.flashScale)!="undefined")? "<param name=scale value='"+this.flashScale+"'>\n": "";
		htmltags+=(typeof(this.flashQuality)!="undefined")? "<param name=quality value='"+this.flashQuality+"'>\n": "";
		htmltags+=(typeof(this.flashBgcolor)!="undefined")? "<param name=bgcolor value='"+this.flashBgcolor+"'>\n": "";
		htmltags+=(typeof(this.flashPlay)!="undefined")? "<param name=play value='"+this.flashPlay+"'>\n": "";
		htmltags+=(typeof(this.flashLoop)!="undefined")? "<param name=loop value='"+this.flashLoop+"'>\n": "";
		htmltags+=(typeof(this.flashWmode)!="undefined")? "<param name=wmode value='"+this.flashWmode+"'>\n": "";

		htmltags+="<embed src='"+this.flashName+this.moreParams+"' ";
		htmltags+=(typeof(this.flashId)!="undefined")? "name='"+this.flashId+"' ": "";
		htmltags+=(typeof(this.flashMenu)!="undefined")? "menu='"+this.flashMenu+"' ": "";
		htmltags+=(typeof(this.flashWidth)!="undefined")? "width='"+this.flashWidth+"' ": "";
		htmltags+=(typeof(this.flashHeight)!="undefined")? "height='"+this.flashHeight+"' ": "";
		htmltags+=(typeof(this.flashQuality)!="undefined")? "quality='"+this.flashQuality+"' ": "";
		htmltags+=(typeof(this.flashScale)!="undefined")? "scale='"+this.flashScale+"' ": "";
		htmltags+=(typeof(this.flashAlign)!="undefined")? "align='"+this.flashAlign+"' ": "";
		htmltags+=(typeof(this.flashSalign)!="undefined")? "salign='"+this.flashSalign+"' ": "";
		htmltags+=(typeof(this.flashBgcolor)!="undefined")? "bgcolor='"+this.flashBgcolor+"' ": "";
		htmltags+=(typeof(this.flashPlay)!="undefined")? "play='"+this.flashPlay+"' ": "";
		htmltags+=(typeof(this.flashLoop)!="undefined")? "loop='"+this.flashLoop+"' ": "";
		htmltags+=(typeof(this.flashWmode)!="undefined")? "wmode='"+this.flashWmode+"' ": "";
		htmltags+="pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash'";
		htmltags+="></embed>";
		htmltags+="</object>";
		document.write();
		
	} else{
		if(this.altPage!="" && typeof(this.altPage)!="undefined"){
			location.href=this.altPage+this.moreParams;
			return;
		}
		else if(this.altImg!="" && typeof(this.altImg)!="undefined"){
			htmltags="<img src='"+this.altImg+"' width='"+this.flashWidth+"' height='"+this.flashHeight+"' border='0' alt=''>";
		}
		else if(this.altHtml!="" && typeof(this.altHtml)!="undefined"){
			htmltags=this.altHtml;
		}
		else if(this.altFunc!="" && typeof(this.altFunc)!="undefined"){
			eval(this.altFunc);
		}
	}
	
	if (debug['showHtml'] == 1 ) {
		debug['out'] += "\n\n#### HtmlOut:\n"
		debug['out'] += htmltags + "\n";
		debug['out'] += "#### EndHtmlOut.\n";
	}
		
	doShowDebugOut()
	
	document.write(htmltags);		

}

function doShowDebugOut(){
	if(debug['show'] > 1){
		if (debug['showMode'] == 2) {
			if (document.getElementById('debugOutDiv')) {
				document.getElementById('debugOutDiv').innerHTML = '<pre>' + doHtmlEncode(debug['out']) + '</pre>';
			} else {
				document.write('<div id="debugOutDiv" style="position:absolute; top:0px;left:0px;z-index:100;background-color:orange;color=black;width=100px;"><pre>' + doHtmlEncode(debug['out']) + '</pre></div>');
			}
		} else {
			alert(debug['out']);
		}
	}
}

function doHtmlEncode(s){
	if(typeof(s) != "string"){
		s = s.toString();
	}
	s = s.replace(/</g, "&lt;") ;
	s = s.replace(/>/g, "&gt;") ;
	return s;
}

function getFlashVersion(browser) {
	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 (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 ( browser['app_name']=='ie' && browser['app_verno'] >= 4 && browser['os_name']=='win') {
		//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');
		for (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;
		}
	} else {
		flashVersion = flashVersion_DONTKNOW;
	}
	return flashVersion;
}



function getBrowserInfo() {
	var browser = new Array();
	/*
	* original version by Kristof Lipfert
	* http://www.lipfert-malik.de/webdesign/tutorial/bsp/browser_js_test.html
	*/
	var x = '';
	var os = '';
	
	if(document.ids) {
		x='nc4';
	} else if( document.all && !document.getElementById ) {
		x='ie4';
	} else if( window.opera && !document.createElement ) {
		x='op5';
	} else if( window.opera && window.getComputedStyle )  {
	       if(document.createRange) {
				x='op8';
	        } else if(window.navigate) {
				x='op7.5';
			} else  {
				x='op7.2';
			}
	} else if( window.opera && document.compatMode ) {
		x='op7';
	} else if( window.opera && document.releaseEvents ) {
		x='op6';
	} else if( document.contains && !window.opera ) {
		x='kq3';
	} else if(window.pkcs11&&window.XML) {
		x='ff1.5';
	} else if( window.getSelection && window.atob ) {
		x='nn7';
	} else if( window.getSelection && !document.compatMode ) {
		x='nn6';
	} else if( window.clipboardData && document.compatMode ) {
		x=window.XMLHttpRequest? 'ie7' : 'ie6';

	} else if( window.clipboardData ){
		x='ie5';
	    if( !document.createDocumentFragment ) {
			x+='.5';
		}
	    if( document.doctype && !window.print ) {
			x+='m';
		}
	} else if( document.getElementById && !document.all ) {
		x='op4';
	} else if( document.images && !document.all ) {
		x='nn3';
	} else if(document.clientWidth&&!window.RegExp) {
		x='kq2';
	} else  {
		x='???';
	}
	try {
		os = navigator.platform;
		os = os.toLowerCase();
	} catch (e) {
		os = '???';
	}
	var app = '';
	try {
		app = navigator.userAgent;
		app = app.toLowerCase()
	} catch (e) {
		app = '???';
	}
	x = x.toLowerCase();
	browser['os_name']= os.substring(0,3);
	browser['os_ver'] = os.substring(3);
	browser['os_verno'] = parseFloat(browser['os_ver']);
	browser['os_string'] = os;
	
	browser['app_name'] = x.substring(0,2);
	browser['app_ver'] = x.substring(2);
	browser['app_verno'] = parseFloat(browser['app_ver']);
	browser['app_string'] = app;
	browser['app_id'] = x;
	browser['app_fullname'] = browser['os_name'] + '-' + browser['app_name'] + '-' + browser['app_ver'];
	
	browser['flash_ver'] = getFlashVersion(browser);
	browser['flash_verno'] = parseFloat(getFlashVersion(browser));
	if(debug['show'] > 0){
		var d="#### ClientInfo: \n";
		for(all in browser){
			if (all != "flash") {
				d += all+"="+browser[all]+"\n";
			}
		}
		d += "#### EndClientInfo.\n";
		debug['out'] += d;
	}
	return browser;
}

