/*
 * FlashObject embed
 * http://blog.deconcept.com/2004/10/14/web-standards-compliant-javascript-flash-detect-and-embed/
 *
 * by Geoff Stearns (geoff@deconcept.com, http://www.deconcept.com/)
 *
 * v1.0.7 - 11-17-2004
 *
 * Create and write a flash movie to the page, includes detection
 *
 * Usage:
 *
 *	myFlash = new FlashObject("path/to/swf.swf", "swfid", "width", "height", flashversion, "backgroundcolor");
 *	myFlash.altTxt = "Upgrade your Flash Player!";                // optional
 *	myFlash.addParam("wmode", "transparent");                     // optional
 *	myFlash.addVariable("varname1", "varvalue");                  // optional
 *	myFlash.addVariable("varname2", getQueryParamValue("myvar")); // optional
 *	myFlash.write();
 *
 */

FlashObject = 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 = '<br><br>'
	
	  + '<table width="300" border="0" align="center" cellpadding="0" cellspacing="0">'
	  + '  <tr>'
	  + '    <td align="center" style="font-family:Arial, Verdana, Sans-serif; font-size:11px"><p><img src="images/logo.jpg" width="157" height="183"><br>'
	  + '        We\'ve worked very hard to provide you with a cool and interactive online'
	  + '        experience. But to view this site, you\'ll need the most recent version'
	  + '        of the Macromedia Flash Player.</p><p>'
	  + '        <a href="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&P5_Language=English" target="_blank">'
	  + '        Download Flash</a> and visit us again!</p>'
	  + '    </td>'
	  + '  </tr>'
	  + '</table>';
	  
	this.bypassTxt = ''
	
	this.params = new Object();
	this.variables = new Object();
	if (c) this.color = this.addParam('bgcolor', c);
	this.addParam('quality', 'high'); // default to high
	this.doDetect = getQueryParamValue('detectflash');
}

FlashObject.prototype.addParam = function(name, value) {
	this.params[name] = value;
}

FlashObject.prototype.getParams = function() {
    return this.params;
}

FlashObject.prototype.getParam = function(name) {
    return this.params[name];
}

FlashObject.prototype.addVariable = function(name, value) {
	this.variables[name] = value;
}

FlashObject.prototype.getVariable = function(name) {
    return this.variables[name];
}

FlashObject.prototype.getVariables = function() {
    return this.variables;
}

FlashObject.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;
}

FlashObject.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;	
}


FlashObject.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;
    }
}

FlashObject.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) {
		return true;
	} else {
		return false;
	}
}

// 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;
	}
}

function popup(win_url, name, w, h, win_params) {
	window.open(win_url, name,'width='+w+', height='+h+', '+win_params).focus();
}

function imagePopup(url, width, height)
{
	width = width == null ? 500 : width;
	height = height == null ? 375 : height;
	var win = window.open("", "", "width=" + width + ",height=" + (height + 24) + ",top=" + ((screen.height-height-24)/2) + ",left=" + ((screen.width-width)/2));
	if (win == null)
		return alert("It appears that popup-blocking software has prevented the image from being displayed.");
	win.document.open();
	win.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
						"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
						"\t<head>\n" +
						"\t\t<title>" + document.title + "</title>\n" +
						"\t\t<link href=\"" + appPath + "/mystyle.css\" rel=\"stylesheet\" type=\"text/css\" />" +
						"\t</head>\n" +
						"\t<body style=\"margin:0px;\">\n" +
						"\t\t<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n" +
						"\t\t\t<tr><td><img src=\"" + appPath + "/content/images/" + url + "\" alt=\"\"" + (width != null ? " width=\"" + width + "\"" : "") + (height != null ? " height=\"" + height + "\"" : "") + " /></td></tr>\n" +
						"\t\t\t<tr><td height=\"24\" bgcolor=\"#2D409C\"><div align=\"center\" class=\"footer\" style=\"padding:0px;\"><div align=\"center\"><a href=\"javascript:window.close();\" style=\"color:#FFFFFF;\">Close Window</a></div></div></td></tr>\n" +
						"\t\t</table>\n" +
						"\t</body>\n" +
						"</html>\n");
	win.document.close();
	return false;
}

function correctPNG() 
{
	for(var i = 0; i < document.images.length; i++)
	{
		var img = document.images[i];
		var imgName = img.src.toLowerCase();
		if (imgName.substring(imgName.length - 3, imgName.length) == "png")
		{
			var imgID = img.id ? "id=\"" + img.id + "\" " : "";
			var imgClass = img.className ? "class=\"" + img.className + "\" " : "";
			var imgTitle = img.title ? "title=\"" + img.title + "\" " : "title=\"" + img.alt + "\" ";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			if (img.align == "left")
				imgStyle = "float:left;" + imgStyle;
			if (img.align == "right")
				imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href)
				imgStyle = "cursor:hand;" + imgStyle;
			var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px;height:" + img.height + "px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');" + imgStyle + "\"></span>";
			img.outerHTML = strNewHTML;
			i--;
		}
	}
}
if (window.attachEvent)
	window.attachEvent("onload", correctPNG);
