var requiredVersion = 6;

function isDefined(property) {
  return (typeof property != 'undefined');
}

var flashVersion = 0;
function getFlashVersion() {
	var latestFlashVersion = 15;
   var agent = navigator.userAgent.toLowerCase(); 
	
   // NS3 needs flashVersion to be a local variable
   if (agent.indexOf("mozilla/3") != -1 && agent.indexOf("msie") == -1) {
      flashVersion = 0;
   }
   
	// NS3+, Opera3+, IE5+ Mac (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 = 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 += '      flashVersion = 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 = flashVersion_DONTKNOW;
	}

	return flashVersion;
}

function detectFlash()
{
	if( arguments.length == 1 ) {
		required = parseInt(arguments[0], 10);
		if( isNaN(required) )
			required = requiredVersion;
	} else
		required = requiredVersion;
		
	version = getFlashVersion();	
		
	if(version <= required)
		hasRightVersion = false;
	else
		hasRightVersion = true;
}

flashVersion_DONTKNOW = -1;

function outputFlash(file, width, height, fallbackimage, map)
{
	if(hasRightVersion)
	{
		document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ID=picorswf');
		document.write(' WIDTH=');
		document.write(width);
		document.write (' HEIGHT=');
		document.write(height);
		document.write('>');

		document.write('<PARAM NAME=movie');
		document.write(' VALUE="');
		document.write(file);
		document.write('">');
		document.write(' <PARAM NAME=menu VALUE=false> <PARAM NAME=quality VALUE=high> ');

		document.write('<EMBED src="');
		document.write(file);
		document.write('" menu=false quality=high');
		document.write(' swLiveConnect=FALSE ');
		document.write('WIDTH=');
		document.write(width);
		document.write(' HEIGHT=');
		document.write(height);
		document.write(' TYPE="application/x-shockwave-flash" PLUGINSPAGE="<http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"><http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash&quot;> ');

		document.write('</EMBED>');

		document.write('</OBJECT>');
	}
	else
	{
		document.write('<img src="');
		document.write(fallbackimage);
		document.write('"');
		
		if(map != '')
		{
			document.write('usemap="#map"');
		}
		
		document.write(' border="0">');
		
		if(map != '')
		{
			document.write('<map name="map">');
			document.write(map);
			document.write('</map>');
		}
	}
}

function outputBlendedBar()
{
	if(hasRightVersion)
	{
		document.write('<tr><td colspan=2><img src=\"/common/images/interface/headers/blended_bar_top.gif\" width=\"770\" height=\"5\" border=\"0\"></td></tr>');
	}
}

function outputBottomContent(span)
{
	if(!hasRightVersion)
	{
		document.write("<tr class=\"black\"><td colspan=\"");
		document.write(span);
		document.write("\"><table width=\"770\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#000000\"><tr><td width=\"15\"><img src=\"/common/images/interface/blank.gif\" width=\"15\" height=\"8\"></td><td width=\"108\"><a href=\"/customer_service/index.zsp\" onMouseOver=\"img_act('customer')\" onMouseOut=\"img_inact('customer')\"><img src=\"/common/images/interface/links/customer_service.gif\" width=\"93\" height=\"17\" border=\"0\" name=\"customer\"></a><img src=\"/common/images/interface/links/pixel_white.gif\" width=\"5\" height=\"17\" hspace=\"5\"></td><td width=\"115\"><a href=\"/investor_relations/index.zsp\" onMouseOver=\"img_act('investor')\" onMouseOut=\"img_inact('investor')\"><img src=\"/common/images/interface/links/investor.gif\" width=\"100\" height=\"17\" border=\"0\" name=\"investor\"></a><img src=\"/common/images/interface/links/pixel_white.gif\" width=\"5\" height=\"17\" hspace=\"5\"></td><td width=\"82\"><a href=\"/stewardship/index.zsp\" onMouseOver=\"img_act('stewardship')\" onMouseOut=\"img_inact('stewardship')\"><img src=\"/common/images/interface/links/stewardship.gif\" width=\"67\" height=\"17\" border=\"0\" name=\"stewardship\"></a><img src=\"/common/images/interface/links/pixel_white.gif\" width=\"5\" height=\"17\" hspace=\"5\"></td><td width=\"53\"><a href=\"/npk_science/index.zsp\" onMouseOver=\"img_act('science')\" onMouseOut=\"img_inact('science')\"><img src=\"/common/images/interface/links/science.gif\" width=\"38\" height=\"17\" border=\"0\" name=\"science\"></a><img src=\"/common/images/interface/links/pixel_white.gif\" width=\"5\" height=\"17\" hspace=\"5\"></td><td width=\"60\"><a href=\"/npk_markets/index.zsp\" onMouseOver=\"img_act('markets')\" onMouseOut=\"img_inact('markets')\"><img src=\"/common/images/interface/links/markets.gif\" width=\"45\" height=\"17\" border=\"0\" name=\"markets\"></a><img src=\"/common/images/interface/links/pixel_white.gif\" width=\"5\" height=\"17\" hspace=\"5\"></td><td width=\"123\"><a href=\"/about_potashcorp/index.zsp\" onMouseOver=\"img_act('about')\" onMouseOut=\"img_inact('about')\"><img src=\"/common/images/interface/links/about.gif\" width=\"101\" height=\"17\" border=\"0\" name=\"about\"></a></td><td width=\"214\" align=\"right\"><a href=\"/nitrogen/index.zsp\" onMouseOver=\"nutrient_rollover.src='/common/images/interface/nutrients/square_n_ro.gif'\" onMouseOut=\"nutrient_rollover.src='/common/images/interface/blank.gif'\"><img src=\"/common/images/interface/nutrients/square_n.gif\" width=\"22\" height=\"17\" border=\"0\" name=\"mainnitrogen\"></a><img src=\"/common/images/interface/blank.gif\" width=\"5\" height=\"1\"><a href=\"/phosphate/index.zsp\" onMouseOver=\"nutrient_rollover.src='/common/images/interface/nutrients/square_p_ro.gif'\" onMouseOut=\"nutrient_rollover.src='/common/images/interface/blank.gif'\"><img src=\"/common/images/interface/nutrients/square_p.gif\" width=\"22\" height=\"17\" border=\"0\"></a><img src=\"/common/images/interface/blank.gif\" width=\"5\" height=\"1\"><a href=\"/potassium/index.zsp\" onMouseOver=\"nutrient_rollover.src='/common/images/interface/nutrients/square_k_ro.gif'\" onMouseOut=\"nutrient_rollover.src='/common/images/interface/blank.gif'\"><img src=\"/common/images/interface/nutrients/square_k.gif\" width=\"22\" height=\"17\" border=\"0\"></a><img src=\"/common/images/interface/blank.gif\" width=\"20\" height=\"1\"></td></tr></table></td></tr>");
	}
}

function outputImage(flash, nonflash)
{
	if(hasRightVersion)
	{
		document.write('<img src="')
		document.write(flash);
		document.write('">');
	}
	else
	{
		document.write('<img src="')
		document.write(nonflash);
		document.write('">');
	}
}

detectFlash();
