
/* @author:Damien W J King
   @copyright:Virgin Media
   @lastUpdated:16/7/07
   @description: function for outputting embed/object tags for i.e. issue with active x.
   @attibutes: please call with following - divID,codebase,width,height,id,align,movie,quality,bgcolor,wmode in this order as first 8 params.
   anything after this should be "paramName","paramValue" followed in pairs.
   @Function call example 
   <div id="flashContainer"></div>
	<script language="JavaScript" type="text/javascript">
	embedFlash("flashContainer","http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0","600","440","main","middle","http://www.virgin.net/cards/ecards.swf","high","#339999","Transperant","salign","T","swLiveConnect","TRUE");
	</script>
	<noscript>This page requires Javascript to be enabled and Macromedia Flash to be installed.</noscript>
	
   @Note: Removed some attributes like quality, seem to be causing type mismatch in ie6.
   
   @have added check flash version so you can use supportedVersion var to decided if to display or not.
   also if you have multiple flash containers on one page please name the flashContainer1, flashContainer2 etc and have the alternative non flash content in
   nonFlash1, nonFlash2 etc.  Make sure they all have a class to hide them initally.  Once the check has been done it will show the relevant divs, ie, either flash content or nonflash.
*/


var w3c=(document.getElementById)? true: false;
var ie5=(w3c && document.all)? true : false;
var ns6=(w3c && (navigator.appName=="Netscape"))? true: false;
var saf=(navigator.userAgent.indexOf('Safari')!=-1);

var flashversion = 0;
  
function checkFlashVersion() {

  if (navigator.plugins && navigator.plugins.length) {    
    x = navigator.plugins["Shockwave Flash"];
    if (x) {
      if (x.description) {
        y = x.description;
  			flashversion = y.charAt(y.indexOf('.')-1);
  		}
    } 
    else {
      flashversion = 0;
    } 
    
    if (navigator.plugins["Shockwave Flash 2.0"]) {
  		flashversion = 2;
  	}
  }
  else if (navigator.mimeTypes && navigator.mimeTypes.length)
  {
  	x = navigator.mimeTypes['application/x-shockwave-flash'];
  	if (x && x.enabledPlugin)
  		flashversion = 'unknown';
  	else
  		flashversion = 0;
  }
  else {
    testIEFlashVersion();    
  }
  return flashversion;
}
  
function testIEFlashVersion() {
 for(var i=11; i>0; i--){
  		flashversion = 0;
  		try{
  			var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
  			flashversion = i;
  			return;
  		}
  		catch(e){
  		}
  	}
}



function embedFlash() {
		
	// standard flash attributes
	var classid = "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
	var type = "application/x-shockwave-flash";
	var pluginspage="http://www.macromedia.com/go/getflashplayer"
	var allowScriptAccess = "sameDomain";
		
	// get extra paramaters
	var DivID = arguments[0];
	var codebase = arguments[1];
	var width = arguments[2];
	var height = arguments[3];
	var id = arguments[4];
	var align = arguments[5];
	var movie = arguments[6];
	var quality = arguments[7];
	var bgcolor = arguments[8];
	var wmode = arguments[9];
	
	var obj = document.createElement('object');
	var div = document.getElementById(DivID);
	var img = document.createElement('image');
	
	var flashDetected = isFlashReady();

	if(flashDetected) {
		// append object
		if(!saf) {
			div.appendChild(obj);
		}	
		// set object attributes
		obj.setAttribute('width',width);
		obj.setAttribute('height',height);
		obj.setAttribute('align',align);
		obj.setAttribute('id',id);
		obj.setAttribute('codebase',codebase);
		obj.setAttribute('classid',classid);
		obj.setAttribute('bgcolor',bgcolor);
		obj.setAttribute('wmode',wmode);
		
		// reset params - ie 7 compatable yippy
		obj.movie = movie;
		//obj.quality = quality;
		//obj.allowScriptAccess = allowScriptAccess;
		
		// set embed for mozilla 
		var embed = document.createElement('embed');
		embed.setAttribute('src',movie);
		embed.setAttribute('bgcolor',bgcolor);
		embed.setAttribute('name',id);
		embed.setAttribute('type',type);
		embed.setAttribute('pluginspage',pluginspage);
		embed.setAttribute('width',width);
		embed.setAttribute('height',height);
		embed.setAttribute('wmode',wmode);
		
		// arrays for extra params
		xparams = new Array;
		xembed = new Array;

		if(arguments.length > 10 ) {
			i = 10;
			p = 0;
			while(i<arguments.length) {
				eval('obj.'+arguments[i]+'="'+arguments[i+1]+'";');
				embed.setAttribute(arguments[i],arguments[i+1]);
				i=i+2;
				p++;
			}
		}
		if(ns6)div.appendChild(embed); // added to fix ie jscript error
	} else {
		// no flash installed
		div.innerHTML = '<p class="TxtMed">This page requires Macromedia Flash to be installed</p>';
		div.innerHTML += '<p><a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank"><img src="http://www.virgin.net/pix/getFlash.gif" width="89" height="31" border="0" alt="Download Macromedia Flash"/></a></p>';
	}
}

//Flash Detection

function isFlashReady() {
	 var MM_FlashCanPlay = false;
	 var detected = false; 
     var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
     if(plugin) {
          detected = true;
     } else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.indexOf("Win") != -1)) {				   
		// IE flash detection.
		var detected = ieDetection();

	 }
	 return detected;
}

function ieDetection() {
	for(var i=0; i<10; i++){
		flashVersion = 0;
		try{
			var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
			flashVersion = i;
			return true;
		}
		catch(e){
		}
		
	}
}




// onload check if version supported show alternative content
// check if flash is right version
var supportedVersion = 8;

var totalDivsArray = new Array;
var flashDivsArray = new Array;

function getDivs() {

	flashVersion = checkFlashVersion();
	// check if version supported
	if(flashVersion<supportedVersion) {
		// show all nonFlash
		showFlash = false;
	} else {
		// show all flash
		showFlash = true;
	}
	// get all divs	
	totalDivsArray = document.getElementsByTagName('div');
  	// loop through all divs check for flashContainer and nonFlash show the correct ones
  	for (i=0;i<totalDivsArray.length;i++) {
		flashId = totalDivsArray[i].id;
		var flashDivName = flashId.search(/flashContainer/)
		var nonFlashDivName = flashId.search(/nonFlash/)
		
		if(flashDivName==0) {
			//found flash div
			var flashDiv = document.getElementById(flashId);
			if(showFlash==true) {
				flashDiv.style.display = "block";
			}
		}
		if(nonFlashDivName==0) {
			//found nonflash div
			var nonFlashDiv = document.getElementById(flashId);
			if(showFlash==false) {
				flashDiv.style.display = "none";
				nonFlashDiv.style.display = "block";
			}
		}
  	} 


}

window.onload = function (evt) {
getDivs();
};

