
/*
SWF PUTTER v1.6
Copyright (c) 2007-2008 Keith Petrino
Author: Keith Petrino
www.kepe.org
keith@kepe.org

This script is released under the MIT license:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

------------------------------------------------------------------------------
*/

var isIEWin = (typeof ActiveXObject!="undefined");
var playerV = getPlayerVersion();
var swfDivs = [];
var swfs = [];


var initSwf = function() {
	
	/* find swfs */
	var divs = document.getElementsByTagName("div");

	for(var i=0; i<divs.length; i++) if(divs[i].className=="swf") swfDivs.push(divs[i]);
	for(var s=0; s<swfDivs.length; s++) {
		if(swfDivs[s].className=="swf") swfs.push(swfDivs[s].innerHTML);
	}
	if(swfs.length) parseInfo();
}(); /* This function will call itself automatically; remove '()' to prevent this. */




function parseInfo() {
	
	var nameValPattern = /(\w+)[\s\t]*=[^\n\w\/.]*(.+[^\n\t\s])?/g;
	
	for(var i=0; i<swfs.length; i++) {
		/* make an array of name/value strings */
		var nameVals = swfs[i].match(nameValPattern);
		/* convert these name/value strings into properties of an object */
		swfs[i] = {};
		for(var k=0; k<nameVals.length; k++) {
			swfs[i][nameVals[k].replace(nameValPattern,"$1")] = nameVals[k].replace(nameValPattern,"$2");
		}
	}

	putSwf();
}




function putSwf() {

	for(var i=0; i<swfs.length; i++) {
		
		/* validate version */
		var reqV = swfs[i].reqVersion.match(/\d+/g);
		for(var v=0; v<reqV.length; v++) {
			playerV[v] = parseInt(playerV[v]);
			reqV[v] = parseInt(reqV[v]);
		}
		var vOK = function() {
			/* major version */
			if(playerV[0] > reqV[0]) return true;
			if(playerV[0] < reqV[0]) return false;
			/* minor version */
			if(playerV[1] > reqV[1]) return true;
			if(playerV[1] < reqV[1]) return false;
			/* revision */
			if(playerV[2] >= reqV[2]) return true;
			return false;
		}();
		/* if version does not check out */
		if(!(playerV && vOK)) {
			putAlt(i,swfs[i].reqVersion);
			return;
		}
		
		/* put swf */
		var swf = '';
		if(isIEWin) {
			swf += '<object';
			swf += ' classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"';
			swf += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + reqV.toString() + ',0"';
			swf += ' id="' + swfs[i].id + '"';
			swf += ' width="' + swfs[i].width + '"';
			swf += ' height="' + swfs[i].height + '"';
			swf += ' align="' + (swfs[i].align?swfs[i].align:'center') + '"';
			swf += '>\n';
			swf += '<param name="movie" value="' + swfs[i].src + '" />\n';
		}
		else {
			swf += '<object';
			swf += ' type="application/x-shockwave-flash"';
			swf += ' data="' + swfs[i].src + '"';
			swf += ' id="' + swfs[i].id + '"';
			swf += ' width="' + swfs[i].width + '"';
			swf += ' height="' + swfs[i].height + '"';
			swf += ' align="' + (swfs[i].align?swfs[i].align:'center') + '"';
			swf += '>\n';
		}
		
		delete swfs[i].reqVersion;
		delete swfs[i].id;
		delete swfs[i].src;
		delete swfs[i].width;
		delete swfs[i].height;
		delete swfs[i].align;
		
		for(var p in swfs[i]) {
			if(swfs[i][p]) { /* make sure value is given */
				swf += '<param name="' + p + '" value="' + swfs[i][p] + '" />\n';
			}
		}

		swf += '</object>';

		swfDivs[i].innerHTML = swf;
	}
}




function putAlt(swfIdx,reqV) {
	
	var t = '';
	t += 'This content requires version ';
	t += reqV;
	t += ' of the Flash player. ';
	t += '<a href="http://www.adobe.com/go/getflashplayer">Click here to download it.</a>';
	swfDivs[swfIdx].innerHTML = t;
	swfDivs[swfIdx].className = "noswf";
}




function getPlayerVersion() {
	
	var playerVersion;
		
	var IDplayer = function() {

		if(!isIEWin) {
			var plugin = navigator.plugins;
			plugin = plugin["Shockwave Flash"] ? plugin["Shockwave Flash"] : plugin["Shockwave Flash 2.0"];
			if(plugin) playerVersion = plugin.description;
			return;
		}
		
		try {
			/* set for versions 7.x and higher */
			var axoSwfPlayer = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			playerVersion = axoSwfPlayer.GetVariable("$version"); /* $version works with 4.0r11 and later */
			return;
		} catch(err) {}
		try {	
			/* set for 6.x players */
			var axoSwfPlayer = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			try {
				axoSwfPlayer.AllowScriptAccess = "always"; /* AllowScriptAccess introduced in 6.0.r47 */
				playerVersion = axoSwfPlayer.GetVariable("$version"); /* crashes on 6.0.22 through 6.0.29 */
			} catch(err) {
				/* defaults to this version if AllowScriptAccess throws an error */
				playerVersion = "WIN 6,0,21,0";
			}
			return;
		} catch(err) {}
		try {
			var axoSwfPlayer = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			try {
				/* set for 4.x and 5.x players */
				playerVersion = axoSwfPlayer.GetVariable("$version"); /* works on versions 3,0,18,0 and higher */
			} catch(err) {
				/* set for 3.x players */
				playerVersion = "WIN 3,0,18,0";
			}
			return;
		} catch(err) {}
		try {
			/* set for 2.x players */
			var axoSwfPlayer = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			playerVersion = "WIN 2,0,0,11";
			return;
		} catch(err) {}
	}();

	if(!playerVersion) return;
	return (playerVersion=playerVersion.match(/\d+/g)).length>3 ? playerVersion.splice(0,3) : playerVersion;
}






/*

INSTRUCTIONS:

-----------------------------------------------------
I. Embedding Flash content on your site
-----------------------------------------------------

The following is a typical example of how to use this script to embed Flash 
content on your page:

<div class="swf"><!--
   reqVersion = 9.0.0
   id = theswf
   src = ./theswf.swf
   width = 300
   height = 300
   wmode = window
   salign = 
--></div>

As you can see, all it really is, is an HTML comment wrapped in a div 
container. The comment contains a series of name/value pairs with all the 
required information needed to embed the Flash content. Everything in the 
above example is required with the exception of the final two name/value 
pairs; Flash has default values for 'wmode' and 'salign'. You may include any 
additional parameters, as well. The example omits the 'salign' parameter's 
value to illustrate the script's lenience. You may include as many embed 
blocks as possible on your page--the script will sniff them all out 
automatically.

All that's left to do is to include the following script tag at the bottom 
of your page, below any content, but above the closing 'body' tag:
 
<script type="text/javascript" src="./putSwf.js"></script>


-----------------------------------------------------
II. HTML publishing template for added convenience
-----------------------------------------------------

If you wish to automate creation of the flash-embedding comments, use the 
following publishing template to create a new HTML file; name it whatever you 
like. Put this file in the following locations:


a) If the Flash application has been run before:

Mac/CS3: 
	"~/Library/Application Support/Adobe/Flash CS3/en/Configuration/HTML/" 
* "~" is the user's home directory.

Win/CS3:
	?


b) If the Flash application has not yet been launched for the first time:

Mac/CS3: 
	"/Applications/Adobe Flash CS3/First Run/HTML/"

Win/CS3:
	"C:\Documents and Settings\[user]\Local Settings\Application Data\Adobe\Flash CS3\[language]\Configuration\HTML\"
	
Note: Directories with [] are different per user's computer. Also, 
"Application Data" is a hidden folder, so you have to show hidden 
files/folders.

You may have noticed under the HTML tab in your publish settings, a checkbox 
saying 'Detect Flash Version', which also allows you to enter a specific 
major version, minor version, and or revision of the Flash player to require. 
To be able to use that feature, a duplicate of this template file must be 
included in the following locations:

Mac/CS3:
	"~/Library/Application Support/Adobe/Flash CS3/en/Configuration/Detection/"
	
Win/CS3:
	?

Note: The second line of the template ($DXSwf_Putter_Detection.html) 
specifies the name of the detection version of the template file; name it 
whatever you like. Please remember that this line should be removed in the 
detection version.

Once the file(s) are in place, go to Flash's publish settings. Under the HTML 
tab, select the name of the new template, namely "Swf Putter", adjust your 
settings, and publish!

When publishing, you will have to manually place this script file in the same 
directory as the HTML page with the swf content, or manually change the src 
attribute of the script tag to point to this file.

....................................................................................

$TTSwf Putter
$DXSwf_Putter_Detection.html
$DS
Copyright (c) 2007 Keith Petrino
Created by Keith Petrino
www.kepe.org
keith@kepe.org
$DF
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>$TI</title>
<style type="text/css">
	body {
		margin:0px;
		padding:0px;
		background-color:$BG;
	}
	.noswf {
		padding:5px;
		background-color:#000000;
		color:#FFFFFF;
	}
</style>
</head>
<body>
<!--url's used in the movie-->
$MU
<!--text used in the movie-->
$MT
<!-- saved from url=(0013)about:internet -->
<div class="swf"><!--
	reqVersion = $FV.$JR.$NR 
	id = $TI 
	src = $TI.swf 
	width = $WI 
	height = $HE 
	quality = $QU 
	align = $HA 
	play = $PL 
	loop = $LO 
	scale = $SC 
	wmode = $WM 
	devicefont = $DE 
	bgcolor = $BG 
	menu = $ME 
	allowFullScreen = false 
	allowScriptAccess = sameDomain 
	salign = $SA 
--></div>

<script type="text/javascript" src="./putSwf.js"></script>
</body>
</html>

....................................................................................


*/