// Do not copy and paste the script to your document.
// It needs to be included as an external script!
// Please include this script in the <head> section of your (X)HTML document only!
//
// Version: 1.6.7

// --- USAGE ---
// writeFlashCode(flash_movie, fallback_image, flash_parameters);
// flash_movie      - URL to Flash Movie
// fallback_image   - URL to Fallback Image
// flash_parameters - Flash Parameters, e.g. width=775;height=600;version=6,0,65,0

// --- CUSTOMIZABLE PARAMETERS ---
var fsiPresetFlashVersion='6,0,65,0';   // Default value for required Flash version
var fsiImageFallbackLinked=false;        // Add a link to Flash Player download location to the fallback image
var fsiTextFallbackActive=true;         // Activate text fallback?
var fsiTextFallbackText='Flash Player Required.\nWould you like to download the latest version of Flash Player now?';
var flashFallbackImage='';

//
// EDITING THE LINES BELOW IS NOT RECOMMENDED
//

function flashDetector(presetFlashVersion, imageFallbackLinked, textFallbackActive, textFallbackText){

	this.flashVersion = 0;
	var flashDownload;
	var flashDownloadCab;

	this.init = function(){

		var scheme;
		if (window.location && window.location.protocol.indexOf('https') > -1) scheme='https://';
		else scheme='http://';
		flashDownload = scheme+'www.adobe.com/go/getflashplayer';
		flashDownloadCab = scheme+'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab';
		var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
		var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
		var isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1) ? true : false;

		if (isIE && isWin && !isOpera) {
			try {
				var axTester = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				var axVersionFull = axTester.GetVariable("$version");
				if (axVersionFull){
					var regVersion = axVersionFull.match(/(\d+),\d+,\d+,\d+/);
					if (regVersion && regVersion[1]){
						this.flashVersion = parseInt(regVersion[1]);
					}
				}
			}
			catch (e){}
		}
		else {
			var napl;
			if (navigator.plugins) napl = navigator.plugins;
			else napl = false;
			if (napl && napl.length > 0 && (napl["Shockwave Flash 2.0"] || napl["Shockwave Flash"])) {
				var flashDesc = napl["Shockwave Flash" + (napl["Shockwave Flash 2.0"] ? " 2.0" : "")].description;
				this.flashVersion = parseInt(flashDesc.split(" ")[2].split(".")[0]);
			}
		}
		// Scan Win/IE Old
		if (this.flashVersion == 0 && isIE && isWin && !isOpera) {
			pushVBDetection();
		}
	}

	this.writeFlashCode = function (fsiurl, fallbackimg, parameters, parent, altTag){

		var objcode='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" style="display:block" codebase="'+flashDownloadCab;
		var emdcode='<embed type="application/x-shockwave-flash" pluginspage="'+flashDownload+'"';

		var params = new Object();
		if (parameters){
			var parameters = parameters.split(";");
			for (var c=0; c < parameters.length; c++){
				var parameter=parameters[c].split("=");
				if (parameter[1]) params[parameter[0].toLowerCase()]=parameter[1];
				else if (parameter!="") alert("Wrong parameter: " + parameter);
			}
		}

		var useVersion;
		if (params["version"]) {
			useVersion=decodeURI(params["version"]);
			params["version"]='';
		}
		else useVersion=presetFlashVersion;
		objcode+='#version='+useVersion+'"';

		if (!params["quality"]) {
			params["quality"]='High';
		}

		if (!params["allowscriptaccess"]) {
			params["allowscriptaccess"]='always';
		}
		if (!params["allowfullscreen"]) {
			params["allowfullscreen"]='true';
		}

		var majorversion = useVersion.split(",")[0];

		if (this.flashVersion >= majorversion){
			var code;
			if (params["width"]) {
				code=' width="'+params["width"]+'"';
				objcode+=code;
				emdcode+=code;
				params["width"]='';
			}
			if (params["height"]) {
				code=' height="'+params["height"]+'"';
				objcode+=code;
				emdcode+=code;
				params["height"]='';
			}
			if (params["id"] || params["name"]) {
				objcode+=' id="'+params["id"]+'"';
				if (params["name"]) emdcode+=' name="'+params["name"]+'"';
				else emdcode+=' name="'+params["id"]+'"';
				params["id"]='';
				params["name"]='';
			}
			objcode+='>\n';
			params["src"]=encodeURI(fsiurl);
			params["movie"]=encodeURI(fsiurl);

			for (var pkey in params){
				if (params[pkey]){
					if (pkey != 'src') objcode+='<param name="'+pkey+'" value="'+decodeURI(params[pkey])+'"\/>\n';
					if (pkey != 'movie') emdcode+=' '+pkey+'="'+decodeURI(params[pkey])+'"';
				}
			}
			emdcode+='\/>';

			objcode+=emdcode+'\n';
			objcode+='<\/object>';
		}
		else if (fallbackimg){
			objcode='<img width="100%" src="'+fallbackimg+'" border=0 alt="' + altTag + '"/>';
			if (imageFallbackLinked) objcode='<a href="'+flashDownload+'" target="_blank">'+objcode+'</a>';
			objcode='<p align="center">'+objcode+'</p>';
		}
		if (!objcode){
			if (textFallbackActive && confirm(textFallbackText)) top.location=flashDownload;
		}
		else {
			//document.write(objcode);
			jQuery(parent).prepend(objcode);
		}
	}

	// Run VBScript detection part in old IE
	pushVBDetection = function(){
		var doc = '<scr' + 'ipt type="text/vbscript"\>\n';
		doc += 'On Error Resume Next\n';
		doc += 'Dim i\n';
		doc += 'For i = 30 to 6 Step -1\n';
		doc += 'If Not(IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & i))) Then\n';
		doc += 'Else\n';
		doc += 'fsiFD.flashVersion = i\n';
		doc += 'Exit For\n';
		doc += 'End If\n';
		doc += 'Next\n';
		doc += '<\/scr' + 'ipt>\n';
		document.write (doc);
	}

}

function writeFlashCode(fsiurl, fallbackimg, parameters, parent, altTag){
	fsiFD.writeFlashCode(fsiurl, fallbackimg, parameters, parent, altTag);
}

fsiFD = new flashDetector(fsiPresetFlashVersion, fsiImageFallbackLinked, fsiTextFallbackActive, fsiTextFallbackText);
fsiFD.init();

// Copyright 2008 NeptuneLabs GmbH (http://www.neptunelabs.com)

//pfister integration methods

/**
 * Function called from a xhtml code. It will find and prepare the setup parameters and call the setupFSIViewerInternal function.
 
 * @param imageContainerId the id of the container where the FSI viewer or the image will be prepended.
 * @param setupAnchor anchor object which contains the parameters needed for FSI setup in its 'rel' attribute. This parameter can be omitted.
 */
function setupFSIViewer(imageContainerId, setupAnchor) {
	
	var imageContainer = jQuery("#" + imageContainerId);
	if (setupAnchor) {
		var imageSetup = jQuery(setupAnchor);
	} else {
		var imageSetup = jQuery(imageContainer).find('#fsi-initial-setup');
	}	
	setupFSIViewerInternal(imageContainer, imageSetup);
}

/**
 * Function that will mount the FSI viewer if the product image exists in the FSI server, otherwise it will mount the fallback image.
 * 
 * @param imageContainer the container object on which the FSI viewer or the image will be prepended.
 * @param imageSetup parameters needed for the FSI setup
 */
function setupFSIViewerInternal(imageContainer, imageSetup) {
	if (imageContainer.length > 0 && imageSetup.length > 0) {
		imageContainer.empty();
		var optionsLine = imageSetup.attr("rel");
		var options = parseOptions(optionsLine);
		if (options.exists == "true" && options.mobile == "false") {
			writeFlashCode(options.fsiurl, options.fallbackimg, options.parameters, imageContainer, options.altTag);
		} else {
			mountFallbackImage(imageContainer, options.fallbackimg, options.altTag);
		}
	}
}

/**
 * Mounts an image to the specified container element.
 * 
 * @param imageContainer container object (div,...)
 * @param fallbackImage the image to be mounted
 */
function mountFallbackImage(imageContainer, fallbackImage, altTag) {
	jQuery(imageContainer).prepend('<p align="center"><img width="100%" src="' + fallbackImage + '" alt="' + altTag + '"/></p>')
}

/**
 * Parses the specified query. 
 * @param query the query that contains key/value pairs that are to be parsed.
 * @return a map.
 */
function parseOptions(query) {
	var params = {};
	var pairs = query.split(/[|]/);
	for (var i = 0; i < pairs.length; i++ ) {
		var keyVal = pairs[i].split(':=');
		// skip invalid properties
		if (!keyVal || keyVal.length != 2 ) {
			continue;
		}
		var key = unescape(keyVal[0]);
		var val = unescape(keyVal[1]);
		params[key] = val;
	}
	return params;
}

