/*
 * Embed flash with flash version detect
 *
 * Kwok Lee (kwok.lee@nationalarchives.gov.uk)
 *
 * Date: 25th Jan  2005
 * browser IE 5+, NS6+ (but no NS4, Opera) gives flash movie
 * but only if flash 6 plugin or higher already installed.
 * added to give static jpg for safari users
 * 
 * Date: 8th Mar 2005
 * added to give static jpg for all linux users
 */

flashdetect = function(swf, id, w, h, ver, c) {
	this.swf = swf;
	this.id = id;
	this.width = w;
	this.height = h;
	this.version = ver || 6; // default to 6
	this.align = "middle"; // default to middle
	this.redirect = "";
	this.sq = document.location.search.split("?")[1] || "";
	this.altTxt = "";
	this.bypassTxt = "";
	this.params = new Object();
	this.variables = new Object();
	if (c) this.color = this.addParam('bgcolor', c);
	this.addParam('wmode', 'transparent');
	this.addParam('quality', 'true');
	this.addParam('menu', 'false');
	this.doDetect = getQueryParamValue('detectflash');
}

flashdetect.prototype.addParam = function(name, value) {
	this.params[name] = value;
}

flashdetect.prototype.getParams = function() {
    return this.params;
}

flashdetect.prototype.getParam = function(name) {
    return this.params[name];
}

flashdetect.prototype.addVariable = function(name, value) {
	this.variables[name] = value;
}

flashdetect.prototype.getVariable = function(name) {
    return this.variables[name];
}

flashdetect.prototype.getVariables = function() {
    return this.variables;
}

flashdetect.prototype.getParamTags = function() {
    var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
    }
    if (paramTags == "") {
        paramTags = null;
    }
    return paramTags;
}

flashdetect.prototype.getHTML = function() {
    var flashHTML = "";
    if (window.ActiveXObject && navigator.userAgent.indexOf('Mac') == -1) { // PC IE
        flashHTML += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '" align="' + this.align + '">';
        flashHTML += '<param name="movie" value="' + this.swf + '" />';
        if (this.getParamTags() != null) {
            flashHTML += this.getParamTags();
        }
        if (this.getVariablePairs() != null) {
            flashHTML += '<param name="flashVars" value="' + this.getVariablePairs() + '" />';
        }
        flashHTML += '</object>';
    }
    else { // Everyone else
        flashHTML += '<embed type="application/x-shockwave-flash" src="' + this.swf + '" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '" align="' + this.align + '"';
        for (var param in this.getParams()) {
            flashHTML += ' ' + param + '="' + this.getParam(param) + '"';
        }
        if (this.getVariablePairs() != null) {
            flashHTML += ' flashVars="' + this.getVariablePairs() + '"';
        }
        flashHTML += '></embed>';
    }
    return flashHTML;	
}

flashdetect.prototype.getVariablePairs = function() {
    var variablePairs = new Array();
    for (var name in this.getVariables()) {
        variablePairs.push(name + "=" + escape(this.getVariable(name)));
    }
    if (variablePairs.length > 0) {
        return variablePairs.join("&");
    }
    else {
        return null;
    }
}

flashdetect.prototype.write = function(elementId) {
	if(detectFlash(this.version) || this.doDetect=='false') {
		if (elementId) {
			document.getElementById(elementId).innerHTML = this.getHTML();
		} else {
			document.write(this.getHTML());
		}
	} else {
		if (this.redirect != "") {
			document.location.replace(this.redirect);
		} else {
			if (elementId) {
				document.getElementById(elementId).innerHTML = this.altTxt +""+ this.bypassTxt;
			} else {
				document.write(this.altTxt +""+ this.bypassTxt);
			}
		}
	}		
}

function getFlashVersion() {
	var flashversion = 0;
	if (navigator.plugins && navigator.plugins.length) {
		var x = navigator.plugins["Shockwave Flash"];
		if(x){
			if (x.description) {
				var y = x.description;
	   			flashversion = y.charAt(y.indexOf('.')-1);
			}
		}
	} else {
		result = false;
	    for(var i = 15; i >= 3 && result != true; i--){
   			execScript('on error resume next: result = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
   			flashversion = i;
   		}
	}
	return flashversion;
}

function detectFlash(ver) {	
	if (
((getFlashVersion() >= ver) && !document.all && document.getElementById && (navigator.userAgent.indexOf('Safari') == -1))
	|| ((getFlashVersion() >= ver) && document.all && document.getElementById) 
	|| ((getFlashVersion() >= ver) && (navigator.userAgent.indexOf('Linux') == -1))
){

		return true;
	} else {
var 
insertimage = '<a href="http://collections.europarchive.org/tna/20060829101539/http://www.nationalarchives.gov.uk/imagelibrary/people/03.htm"><img src="http://collections.europarchive.org/tna/20060829101539/http://www.nationalarchives.gov.uk/images/homepage/topleft/happyman.jpg"  width="316" height="196" alt="Richard Leaky, labourer, 1950. Cat. ref: WORK 25/202" border="0"></a>';
    document.write(insertimage); 
	}
}

// get value of querystring param
function getQueryParamValue(param) {
	var q = document.location.search;
	var detectIndex = q.indexOf(param);
	var endIndex = (q.indexOf("&", detectIndex) != -1) ? q.indexOf("&", detectIndex) : q.length;
	if(q.length > 1 && detectIndex != -1) {
		return q.substring(q.indexOf("=", detectIndex)+1, endIndex);
	} else {
		return "";
	}
}

/* add Array.push if needed */
if(Array.prototype.push == null){
	Array.prototype.push = function(item){
		this[this.length] = item;
		return this.length;
	}
}

