/*
Copyright (c) Copyright (c) 2007, Carl S. Yestrau All rights reserved.
Code licensed under the BSD License: http://www.featureblend.com/license.txt
Version: 1.0.4
*/
var FlashDetect = new function(){
    var self = this;
    self.installed = false;
    self.raw = "";
    self.major = -1;
    self.minor = -1;
    self.revision = -1;
    self.revisionStr = "";
    var activeXDetectRules = [
        {
            "name":"ShockwaveFlash.ShockwaveFlash.7",
            "version":function(obj){
                return getActiveXVersion(obj);
            }
        },
        {
            "name":"ShockwaveFlash.ShockwaveFlash.6",
            "version":function(obj){
                var version = "6,0,21";
                try{
                    obj.AllowScriptAccess = "always";
                    version = getActiveXVersion(obj);
                }catch(err){}
                return version;
            }
        },
        {
            "name":"ShockwaveFlash.ShockwaveFlash",
            "version":function(obj){
                return getActiveXVersion(obj);
            }
        }
    ];
    /**
     * Extract the ActiveX version of the plugin.
     * 
     * @param {Object} The flash ActiveX object.
     * @type String
     */
    var getActiveXVersion = function(activeXObj){
        var version = -1;
        try{
            version = activeXObj.GetVariable("$version");
        }catch(err){}
        return version;
    };
    /**
     * Try and retrieve an ActiveX object having a specified name.
     * 
     * @param {String} name The ActiveX object name lookup.
     * @return One of ActiveX object or a simple object having an attribute of activeXError with a value of true.
     * @type Object
     */
    var getActiveXObject = function(name){
        var obj = -1;
        try{
            obj = new ActiveXObject(name);
        }catch(err){
            obj = {activeXError:true};
        }
        return obj;
    };
    /**
     * Parse an ActiveX $version string into an object.
     * 
     * @param {String} str The ActiveX Object GetVariable($version) return value. 
     * @return An object having raw, major, minor, revision and revisionStr attributes.
     * @type Object
     */
    var parseActiveXVersion = function(str){
        var versionArray = str.split(",");//replace with regex
        return {
            "raw":str,
            "major":parseInt(versionArray[0].split(" ")[1], 10),
            "minor":parseInt(versionArray[1], 10),
            "revision":parseInt(versionArray[2], 10),
            "revisionStr":versionArray[2]
        };
    };
		
    /**
     * Parse a standard enabledPlugin.description into an object.
     * 
     * @param {String} str The enabledPlugin.description value.
     * @return An object having raw, major, minor, revision and revisionStr attributes.
     * @type Object
     */
    var parseStandardVersion = function(str){
        var descParts = str.split(/ +/);
        var majorMinor = descParts[2].split(/\./);
        var revisionStr = descParts[3];
        return {
            "raw":str,
            "major":parseInt(majorMinor[0], 10),
            "minor":parseInt(majorMinor[1], 10), 
            "revisionStr":revisionStr,
            "revision":parseRevisionStrToInt(revisionStr)
        };
    };
    /**
     * Parse the plugin revision string into an integer.
     * 
     * @param {String} The revision in string format.
     * @type Number
     */
    var parseRevisionStrToInt = function(str){
        return parseInt(str.replace(/[a-zA-Z]/g, ""), 10) || self.revision;
    };
    /**
     * Is the major version greater than or equal to a specified version.
     * 
     * @param {Number} version The minimum required major version.
     * @type Boolean
     */
    self.majorAtLeast = function(version){
        return self.major >= version;
    };
    /**
     * Is the minor version greater than or equal to a specified version.
     * 
     * @param {Number} version The minimum required minor version.
     * @type Boolean
     */
    self.minorAtLeast = function(version){
        return self.minor >= version;
    };
    /**
     * Is the revision version greater than or equal to a specified version.
     * 
     * @param {Number} version The minimum required revision version.
     * @type Boolean
     */
    self.revisionAtLeast = function(version){
        return self.revision >= version;
    };
    /**
     * Is the version greater than or equal to a specified major, minor and revision.
     * 
     * @param {Number} major The minimum required major version.
     * @param {Number} (Optional) minor The minimum required minor version.
     * @param {Number} (Optional) revision The minimum required revision version.
     * @type Boolean
     */
    self.versionAtLeast = function(major){
        var properties = [self.major, self.minor, self.revision];
        var len = Math.min(properties.length, arguments.length);
        for(i=0; i<len; i++){
            if(properties[i]>=arguments[i]){
                if(i+1<len && properties[i]==arguments[i]){
                    continue;
                }else{
                    return true;
                }
            }else{
                return false;
            }
        }
    };
    /**
     * Constructor, sets raw, major, minor, revisionStr, revision and installed public properties.
     */
    self.FlashDetect = function(){
        if(navigator.plugins && navigator.plugins.length>0){
            var type = 'application/x-shockwave-flash';
            var mimeTypes = navigator.mimeTypes;
            if(mimeTypes && mimeTypes[type] && mimeTypes[type].enabledPlugin && mimeTypes[type].enabledPlugin.description){
                var version = mimeTypes[type].enabledPlugin.description;
                var versionObj = parseStandardVersion(version);
                self.raw = versionObj.raw;
                self.major = versionObj.major;
                self.minor = versionObj.minor; 
                self.revisionStr = versionObj.revisionStr;
                self.revision = versionObj.revision;
                self.installed = true;
            }
        }else if(navigator.appVersion.indexOf("Mac")==-1 && window.execScript){
            var version = -1;
            for(var i=0; i<activeXDetectRules.length && version==-1; i++){
                var obj = getActiveXObject(activeXDetectRules[i].name);
                if(!obj.activeXError){
                    self.installed = true;
                    version = activeXDetectRules[i].version(obj);
                    if(version!=-1){
                        var versionObj = parseActiveXVersion(version);
                        self.raw = versionObj.raw;
                        self.major = versionObj.major;
                        self.minor = versionObj.minor; 
                        self.revision = versionObj.revision;
                        self.revisionStr = versionObj.revisionStr;
                    }
                }
            }
        }
    }();
};

function detectFlash(){
	if(!FlashDetect.installed){
		// NO FLASH
		showFlashUpdate();
	}else{
		if(FlashDetect.major < 9 ){
			// LOWER VERSION
			showFlashUpdate();	
		} else {
			// ROCK ON
		}	
	}	
}

function showFlashUpdate() {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById('flashUpdater').style.visibility = 'visible';
		document.getElementById('showMenu').style.visibility = 'hidden';
	} else {
		if (document.layers) { // Netscape 4
			document.flashUpdater.visibility = 'visible';
			document.showMenu.visibility = 'hidden';
		} else { // IE 4
			document.all.flashUpdater.style.visibility = 'visible';
			document.all.showMenu.style.visibility = 'hidden';
		}
	}
} 

var jActivating =
{

	/**
	 * Declare constants
	 * IS_MSIE	checks for Internet Explorer 6 and later versions
	 * TAGS		array with affected HTML tags
	 */
	IS_MSIE : (document.removeNode && document.createAttribute) ? true : false,
	TAGS : ['object', 'embed', 'applet'],
	
	
	/**
	 * Activates embedded content reinserting it.
	 * @return void
	 */
	activateContent : function()
	{
		var i = 0;
		for(var _tagName; _tagName = jActivating.TAGS[i]; i++)
		{
			var j = 0;
			for(var _node; _node = document.getElementsByTagName(_tagName)[j]; j++)
			{
				if(jActivating.IS_MSIE)
				{
					jActivating.reinsertHtml(_node);
				}
				else
				{
					jActivating.reinsertNode(_node);
				}
			}
			
		}
		jActivating = null;
	},
	
	
	/**
	 * Sets outerHTML property.
	 * @param object	- The node
	 * @return void
	 */
	reinsertHtml : function(_node)
	{
		var _htmlCode = jActivating.getHtmlCode(_node);
		if(_htmlCode)
		{
			_node.outerHTML = _htmlCode;
		}
	},
	
	
	/**
	 * Replaces the node for a clone.
	 * @param object - The original node
	 * @return void
	 */
	reinsertNode : function(_node)
	{
		var _clone = _node.cloneNode(true);
		var _parent = _node.parentNode;
		if(_clone && _parent)
		{
			_parent.replaceChild(_clone, _node);
		}
	},
	
	
	/**
	 * Retrieves the object and its content in HTML.
	 * @param object - The analyzed node
	 * @return string - The HTML received, NULL for malformed syntax (end tag)
	 */
	getHtmlCode : function(_node)
	{
		var _htmlCode = _node.outerHTML;
		var _name = _node.nodeName.toLowerCase();
		if(_name == 'embed')
		{
			return _htmlCode;
		}
		if(_name == 'object' || _name == 'applet')
		{
			var _startTag = _htmlCode.substr(0, _htmlCode.indexOf('>') + 1);
			var _endTag = _htmlCode.substr(_htmlCode.length - 9).toLowerCase();
			// Filters malformed syntax for avoid unexpected results
			if(_endTag != '</object>' && _endTag != '</applet>')
			{
				return null;
			}
			var _innerHtml = jActivating.getInnerHtml(_node);
			_htmlCode = _startTag + _innerHtml + _endTag;
			return _htmlCode;
		}
	},
	
	
	/**
	 * Retrieves the HTML between start and end tags of the object.
	 * @param object - The analyzed node
	 * @return string - The HTML received
	 *
	 * NOTE: the innerHTML property isn't used because Internet Explorer
	 * sometimes returns a empty result.
	 */
	getInnerHtml : function(_node)
	{
		var _innerHtml = '';
		var i = 0;
		for(var _childNode; _childNode = _node.childNodes[i]; i++)
		{
			_innerHtml += _childNode.outerHTML;
		}
		return _innerHtml;
	}
}

// Execute script only for Internet Explorer (6+) and Opera (9+)
if(jActivating.IS_MSIE)
{
	jActivating.activateContent();
}
else if(window.opera)
{
	document.addEventListener('DOMContentLoaded', jActivating.activateContent, false);
}

FlashDetect.JS_RELEASE = "1.0.4";