// 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);
};

//******************************************************
/*  
    Makes sure the content area is as tall as the left 
    hand navigation.
    
    document.minHeightProp is overridden in a conditional comment 
    in the HEAD for IE6 to use the height property as it doesn't 
    understand min-height.
    
    Could just use the height property for all browsers, except that's 
    causing weird results in IE7 with the content getting bunched on top
    of itself.
*/

document.minHeightProp = 'minHeight';

function setContentHeight(){

    if(!document.getElementById) return;
    var navLeft = document.getElementById('navLeft');
    var content = document.getElementById('content');    
    
    if(null == navLeft || null == content) return;
    var navHeight = navLeft.offsetHeight;
    var contentHeight = content.offsetHeight;
    if(navHeight > contentHeight){
        var result = content.style[document.minHeightProp] = (navHeight-36)+'px';
    }
    
    /* 
    If this is the browse by region page, add padding to the 
       second nested div to make sure it's blue background reaches 
       to the bottom of the page 
    */
    var ds = content.getElementsByTagName('DIV');
    if(null != ds[1]){
        if(ds[1].className.indexOf('resultsByMap') != -1){
            var diff = (navHeight-contentHeight) + 12;
            if (diff < 0) { diff = 0 ; }
            ds[1].style.paddingBottom = diff + 'px';
        }
    }
    
}

// Run when DOM is ready
domFunction(setContentHeight,{'terms':'id'});


//******************************************************
/*
    This function will add rollovers to image inputs. The function fires
    on window.load and rollovers will be added to all inputs with the type
    attribute "image". It requires addEvent.js (http://ejohn.org/projects/flexible-javascript-events/).
    
    The filename of the image for the over state should be the same
    as the one for the up state, but with 'On' appended.
    e.g. if the name of the up state image is 'btnSearch.gif' the over
    image must be called 'btnSearchOn.gif'
    
*/


function addSubmitRollovers(){
    
    var inps = document.body.getElementsByTagName('INPUT');
    
    if(null != inps){
        
        var onRollOver = function(){ 
                            if(this.hasFocus) return false; 
                            this.src = this.over_img.src;
                            this.hasFocus = true; 
                        };
        
        var onRollOut =  function(){ 
                            this.src = this.up_img.src; 
                            this.hasFocus = false; 
                        };
                        
        for(var i=0,l=inps.length;i<l;i++){         

            // Skip anything but image inputs
            var inp = inps[i];
            if(inp.type != 'image') continue;

            // Preload and assign images to input
            inp.up_img = new Image();
            inp.up_img.src = inp.src;
            inp.over_img = new Image();            
            // Work out over image based on naming convention 
            inp.over_img.src = inp.src.replace(/\.([a-z][a-z][a-z])$/,'On.$1');

            // Assign functions
            inp.onmouseover = inp.onfocus = onRollOver;
            inp.onmouseout = inp.onblur = onRollOut;
        }
    }
}
// Run when DOM is ready
domFunction(addSubmitRollovers,{'terms':'id'});


//******************************************************

/* 
    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 Feb 2007
*/



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
        if (this.isIE){
            document.getElementById("hideObject").disabled = true;
        }
    },
    
    gotFlash: function(v){
         // Windows check
        if (null != window.ActiveXObject) {
            try {
                var ob = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + v);
                delete ob;
            } catch (e) { 
                v = 0; 
            };        
        // Firefox/Opera check
        } else if (null != navigator.plugins && 
                    null != navigator.plugins['Shockwave Flash']) {
            var str = navigator.plugins['Shockwave Flash'].description;
            if(!(parseInt(str.charAt(str.indexOf(".") - 1)) >= v)){
                v = 0;
            }
        }
        return 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(); 
            },{'navTop':'id'});




