// DF1.1 :: domFunction 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
// GNU Lesser General Public License -- http://www.gnu.org/licenses/lgpl.html
//******************************************************

function domFunction(f, a)
{
	var n = 0;
	
	var t = setInterval(function()
	{
		var c = true;

		n++;
			if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null))
		{
			c = false;

			if(typeof a == 'object')
			{
				for(var i in a)
				{
					if
					(
						(a[i] == 'id' && document.getElementById(i) == null)
						||
						(a[i] == 'tag' && document.getElementsByTagName(i).length < 1)
					) 
					{ 
						c = true; 
						break; 
					}
				}
			}
			if(!c) { f(); clearInterval(t); }
		}
		
		if(n >= 60)
		{
			clearInterval(t);
		}
		
	}, 50);
};


//******************************************************

/* 
    ObjectSwap 
    (http://www.sitepoint.com/article/activex-activation-issue-ie)
    
    Bypasses the new ActiveX Activation requirement in Internet Explorer 
    by swapping existing ActiveX objects on the page with the same objects. 
    Can also be used for Flash version detection by adding the param:

    <param name="flashVersion" value="8" /> to the object tag.

    Author: Karina Steffens, www.neo-archaic.net
    Created: April 2006
    Changes and bug fixes: May 2006
    Bug fixes: June 2006
    Modified by Paul Willoughby to improve Flash major version detection Feb 2007, Nov 2008
*/



var ObjectSwap = {
    
    isIE: (null != window.ActiveXObject),
    
    doSwap: function(){
        if (!document.getElementsByTagName){
		    return;
	    }
        // An array of objects to replace if flash version not supported
        var replaceQueue = [];
        // Get a list of all OBJECT elements
        var obs = document.getElementsByTagName('object');
        for (var i=0,l=obs.length; i<l; i++){
            var hasFlash = true;
            var o = obs[i];
            var h = o.outerHTML;
            //The outer html omits the param tags, so we must retrieve and insert these separately
            var params = "";
            var paramTags = document.getElementsByTagName('param');
            var hasFlash = true;
            for (var j=0, m=paramTags.length; j<m; j++) {
                var p = paramTags[j];
                if (p.name == "flashVersion" && !this.gotFlash(parseInt(p.value))){
                    replaceQueue.push(o);
                    hasFlash = false;
                } 
                params += p.outerHTML;		       
            }
            
            // Only need to replace objects on IE browsers with Flash
            if (!hasFlash || !this.isIE){
                continue;
            }			
            
            // Get the tag and attributes part of the outer html of the object
            var openTag = h.split(">")[0] + ">";				
            
            // Add the attributes , params and embed back together and insert into page
            o.outerHTML = openTag + params + o.innerHTML + " </object>";
        }
        
        // Strip flash objects
        if (replaceQueue.length) {
            this.replaceFlash(replaceQueue)
        }
        // Make the objects visible again by disabling style element
        if (this.isIE){
            document.getElementById("hideObject").disabled = true;
        }
    },
    
    gotFlash: function(v){
        var str, iv, split_on;
        // ActiveX plugin (Internet Explorer)
        if (null != window.ActiveXObject) {
            try {
                ob = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.' + v);
                ob.AllowScriptAccess = 'always';
                str = ob.GetVariable("$version");
                split_on = ',';
                delete ob;
                    
            } catch (e) { return false; };        
        // Other plugin (Mozilla, Opera, WebKit)
        } else if (null != navigator.plugins && 
                    null != navigator.plugins['Shockwave Flash']) {
            str = navigator.plugins['Shockwave Flash'].description;
            split_on = '.';
        } else {
            return false;
        }
        // Extract version
        iv = parseInt(str.replace(/^[a-zA-Z ]+/g,'').split(split_on)[0]);
        return iv >= v;
    },
    
    replaceFlash: function(replaceQueue){
        if (!document.createElement){
            return;
        }
        for (var i=0, l=replaceQueue.length; i<l; i++){
            var o = replaceQueue[i];
            var str = o.innerHTML;
            
            // Find commented out alternate content
            var tmp = o.innerHTML.match(/<!--[^!]*-->/g);
            if(tmp[0]){
                // Strip comment tags from it
                newHTML = tmp[0].replace(/<!--\s/g, "").replace(/\s-->/g, "");
            } else {
                // If no alternate content provided, leave the Flash in there
                continue;
            }
            // Create a new div element with new HTML
            var d = document.createElement("div");
            d.innerHTML = newHTML;
            // ...and replace the object with it
            o.parentNode.replaceChild(d, o);
        }
    }
}

// Hide the object to prevent it from loading twice (bit of a hack)
if (ObjectSwap.isIE){
	document.write ("<style id='hideObject'> object{display:none;} </style>");
}

domFunction(function(){
                ObjectSwap.doSwap(); 
            },{'object':'tag'});




