/* ======================
  FlashOrGif
  Copyright (c) Graficke studio VLADO. www.vlado.cz. All rights reserved.
	Jakub Roztocil, jakub@vlado.cz
	23. 5. 2005
========================= */



var flashDetect =  {

	latestFlashVersion 	: 9,
	version 			: null,
	contents 			: new Object(),	
	
	init : function(msg, targetEl)
	{
		var divs, div, version, verArray, minVersion, showNotice;
		
		if (!document.getElementsByTagName || !document.body.removeChild || !document.createElement || !document.body.appendChild)
		{
			return;
		}
		
		
		flashDetect.msg = msg;
		flashDetect.targetEl = targetEl;
		
		/* 
		verze pluginu se ulozi do flashDetect.version
		- pokud 0, plugin neni
		- pokud -1, nepodarilo se zjistit
		*/
		flashDetect.detect();		
		
		divs = document.getElementsByTagName('div');
		
		if (!divs)
		{
			return;
		}
		
		for (var i = 0; i <divs.length; i ++)
		{
			div = divs[i];
			
			if (/flashHolder/.test(div.className))
			{						
				minVersion = -1;
				verArray = /minVersion-([0-9]+)/.exec(div.className)		
				
				if (verArray)
				{
					minVersion = verArray[1];
				}
			
				if (flashDetect.version === 0 || flashDetect.version < minVersion)
				{
					removeFlash(div);
					replaceContent(div);
					showNotice = true;					

	 			
				 	
	 			}
				
				
				}
			}
			
			
		addE(window, 'load', refreshObjects);
		
		if (showNotice && msg)
		{
			addE(window, 'load', createNotice);
		}
		
		// odstrani Flash a nastavi jeho holderu tridu "noFlash"
		function removeFlash(el)
		{
			var flashWrap = el.getElementsByTagName('div')[0];
			el.className += ' noFlash';
			el.removeChild(flashWrap);
		}
		
		// nahradi Flash alternativnim obsahem
		function replaceContent(el)
		{
			if (/replaceContent/.test(el.className))
			{
				if (flashDetect.contents[el.id])
				{
					el.innerHTML = flashDetect.contents[el.id];
				}
			}
		}
		
		// vytvori upozorneni, ze neni Flash naistalovan nebo je stary
		function createNotice()
		{
			var div, place, tmp;
			
			div = document.createElement('div');
			div.appendChild(document.createTextNode(flashDetect.msg));
			div.setAttribute('id', 'notice');		
			
			place = document.body;
			
			if (flashDetect.targetEl)
			{
				tmp = document.getElementById(flashDetect.targetEl);
				if (tmp)
				{
					place = tmp;
				}
			}
			
			place.appendChild(div);	
		}
		
		function refreshObjects(){
				// focus ActiveX objektů (aby se nemusely aktivovat kliknutim)
				var objects = document.getElementsByTagName("object");
				for (var i=0; i<objects.length; i++) {
		 		objects[i].removeAttribute("data");
		 		objects[i].outerHTML = objects[i].outerHTML; 
			}		
		}		
		
		function addE(el, ev, f)
		{
			if (el.addEventListener)
			{
				el.addEventListener(ev, f, false);
			}
			else if (el.attachEvent)
			{
				el.attachEvent('on'+ev, f);
			}
		}
		
	},
	
	
	
	detect : function()
	{
		
		flashDetect.version = getFlashVersion();
		
		
		/* THIS PART IS BASED ON: */
		// Flash Version Detector  v1.2.1
		// documentation: http://www.dithered.com/javascript/flash_detect/index.html
		// license: http://creativecommons.org/licenses/by/1.0/
		// code by Chris Nott (chris[at]dithered[dot]com)
		// with VBScript code from Alastair Hamilton (now somewhat modified)
		
		function isDefined(property) 
		{
		  return (typeof property != 'undefined');
		}
		
		
		function getFlashVersion() 
		{
			var flashVersion = 0;
			var latestFlashVersion = 8;
			var agent = navigator.userAgent.toLowerCase(); 
			
			
			var reallyExistsPluginsArray = false;
			
			try
			{
				// IE vraci tvrdi, ze ma navigator.plugins, byt ho nema, otestujem metodu refresh
				if(navigator.plugins && navigator.plugins.refresh)
				{
					reallyExistsPluginsArray = true;
				}
			}
			catch(e)
			{
				// coz ale v IE nepochopoitelne vyvola vyjimku
			}
			
			if (reallyExistsPluginsArray) 
			{				
				// NS3+, Opera3+, IE5+ Mac (support plugin array):  check for Flash plugin in plugin array
				var flashPlugin = navigator.plugins['Shockwave Flash'];
				if (typeof flashPlugin == 'object') 
				{ 
					 for (var i = flashDetect.latestFlashVersion; i >= 3; i--) 
					 {
						if (flashPlugin.description.indexOf(i + '.') != -1) 
						{
						   flashVersion = i;
						   break;
						}
				 	}
				}				
			}		
			
			// IE4+ Win32:  attempt to create an ActiveX object using VBScript
			else if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4 && agent.indexOf("win")!=-1 && agent.indexOf("16bit")==-1) 
			{
			 var doc = '<scr' + 'ipt language="VBScript"\> \n';
			  doc += 'On Error Resume Next \n';
			  doc += 'Dim obFlash \n';
			  doc += 'For i = ' + latestFlashVersion + ' To 3 Step -1 \n';
			  doc += '   Set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash." & i) \n';
			  doc += '   If IsObject(obFlash) Then \n';
			  doc += '      ieFlashVersion = i \n';
			  doc += '      Exit For \n';
			  doc += '   End If \n';
			  doc += 'Next \n';
			  doc += '</scr' + 'ipt\> \n';
			  document.write(doc);
			}			  
			// WebTV 2.5 supports flash 3
			else if (agent.indexOf("webtv/2.5") != -1)
			{
			   flashVersion = 3;
			}		
			// older WebTV supports flash 2
			else if (agent.indexOf("webtv") != -1)
			{
			   flashVersion = 2;
			}		
			// Can't detect in all other cases
			else 
			{
			  flashVersion = -1 // nepodarilo se zjistit verzi;
			}
		
			// pokud existuje window.ieFlashVersion - dekekovano pomoci VBscriptu (v IE)
			return (window.ieFlashVersion ? window.ieFlashVersion : flashVersion);
			
		}
		
	}
	
}

