/*
	Init Class for all manditory elements of the CLF 2.0 Template
*/
var Tltps = {
    "tooltip": null,
    "parent": null,
    "timer": null,
    "delay": null,
    execute: function(){
        if (typeof document.getElementsByTagName != "undefined") {
            Tltps.tags = document.getElementsByTagName("*");
            Tltps.tagsLen = Tltps.tags.length;
            for (var i = 0; i < Tltps.tagsLen; i++) {
                if (Tltps.tags[i].title) {
                    Tltps.tags[i].onfocus = Tltps.focusTimer;
                    Tltps.tags[i].onblur = Tltps.blurTip;
                    Tltps.tags[i].onmouseover = Tltps.blurTip;
                }
            }
        }
    },
    focusTip: function(obj){
        Tltps.blurTip();
        Tltps.delay = setInterval("Tltps.blurTip()", 4000);
        if (Tltps.tooltip == null) {
            if (typeof window.innerWidth != "undefined") {
                Tltps.window = {
                    x: window.innerWidth,
                    y: window.innerHeight
                };
            }
            else {
                if (typeof document.documentElement.offsetWidth != "undefined") {
                    Tltps.window = {
                        x: document.documentElement.offsetWidth,
                        y: document.documentElement.offsetHeight
                    };
                }
                else {
                    Tltps.window = {
                        x: document.body.offsetWidth,
                        y: document.body.offsetHeight
                    };
                }
            }
            Tltps.tooltip = (typeof document.createElementNS != "undefined") ? document.createElementNS("http://www.w3.org/1999/xhtml", "div") : document.createElement("div");
            Tltps.tooltip.setAttribute("class", "");
            Tltps.tooltip.className = (navigator.userAgent.indexOf("AppleWebKit/") > -1) ? "safetooltip" : "tooltip";
            if (Tltps.parent == null) {
                Tltps.parent = {
                    x: Tltps.getRealPosition(obj, "x") - 3,
                    y: Tltps.getRealPosition(obj, "y") + 2
                };
            }
            Tltps.parent.y += obj.offsetHeight;
            Tltps.tooltip.style.left = Tltps.parent.x + "px";
            Tltps.tooltip.style.top = Tltps.parent.y + "px";
            Tltps.tooltip.appendChild(document.createTextNode(obj.title));
            document.getElementsByTagName("body")[0].appendChild(Tltps.tooltip);
            if (Tltps.tooltip.offsetWidth > 300) {
                Tltps.tooltip.style.width = "300px";
            }
            Tltps.extent = {
                x: Tltps.tooltip.offsetWidth,
                y: Tltps.tooltip.offsetHeight
            };
            if ((Tltps.parent.x + Tltps.extent.x) >= Tltps.window.x) {
                Tltps.parent.x -= Tltps.extent.x;
                Tltps.tooltip.style.left = Tltps.parent.x + "px";
            }
            if (typeof window.pageYOffset != "undefined") {
                Tltps.scroll = window.pageYOffset;
            }
            else {
                if (typeof document.documentElement.scrollTop != "undefined") {
                    Tltps.scroll = document.documentElement.scrollTop;
                }
                else {
                    Tltps.scroll = document.body.scrollTop;
                }
            }
            if ((Tltps.parent.y + Tltps.extent.y) >= (Tltps.window.y + Tltps.scroll)) {
                Tltps.parent.y -= (Tltps.extent.y + obj.offsetHeight + 4);
                Tltps.tooltip.style.top = Tltps.parent.y + "px";
            }
        }
    },
    getRealPosition: function(ele, dir){
        Tltps.pos = (dir == "x") ? ele.offsetLeft : ele.offsetTop;
        Tltps.tmp = ele.offsetParent;
        while (Tltps.tmp != null) {
            Tltps.pos += (dir == "x") ? Tltps.tmp.offsetLeft : Tltps.tmp.offsetTop;
            Tltps.tmp = Tltps.tmp.offsetParent;
        }
        return Tltps.pos;
    },
    blessLink: function(_8c){
        _8c.onfocus = Tltps.focusTimer;
        _8c.onblur = Tltps.blurTip;
        _8c.onmouseover = Tltps.blurTip;
    },
    focusTimer: function(e){
        if (Tltps.timer != null) {
            clearInterval(Tltps.timer);
            Tltps.timer = null;
            Tltps.focusTip(e);
        }
        else {
            Tltps.tmp = (e) ? e.target : event.srcElement;
            Tltps.timer = setInterval("Tltps.focusTimer(Tltps.tmp)", 400);
        }
    },
    blurTip: function(){
        if (Tltps.tooltip != null) {
            document.getElementsByTagName("body")[0].removeChild(Tltps.tooltip);
            Tltps.tooltip = null;
            Tltps.parent = null;
        }
        clearInterval(Tltps.timer);
        clearInterval(Tltps.delay);
        Tltps.timer = Tltps.delay = null;
    }
};

var SkipNavFix = {
	 execute: function(){
        if (!jQuery.support.opacity) {
            var aTags = $("a");
            for (var i = 1; aTags[i].parentNode.className == "navaid"; i++) {
                SkipNavFix._patchNavLink(aTags[i]);
            }
        }
    },
    _patchNavLink: function(obj, ref){
        obj.style.zoom = 1;
        obj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
        obj.onmouseover = function(){
            SkipNavFix.changeIEOpacity(this, 100);
        };
        obj.onmouseout = function(){
            SkipNavFix.changeIEOpacity(this, 0);
        };
        obj.onfocus = function(){
            SkipNavFix.changeIEOpacity(this, 100);
        };
        obj.onblur = function(){
            SkipNavFix.changeIEOpacity(this, 0);
        };
    },
    changeIEOpacity: function(obj, opacity){
        obj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(Opacity=" + opacity + ")";
    }
	
};

/*
    PNGFix - To allow for IE 5.5 + to show proper transparency for png's
    Note :: Increased the fix to encompass all page png's
*/
var PngFix = {
	
	execute : function() {
			if ((PE.parameters.pngfix ) && (typeof PE.parameters.pngfix != "undefined") ){
		 	// Replace gif with png
	   		jQuery(this).find("img[class$=pngfix]").each(function() {
				jQuery(this).attr('src',jQuery(this).attr('src').substring(0,jQuery(this).attr('src').lastIndexOf('.')) + '.png');
			});
			// Now search and apply patch to png
			var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	   		if (badBrowser) {
	     		// get all pngs on page
	     		$('img.pngfix').each(function() {
	       		if (!this.complete) {
	        		 this.onload = function() { PngFix.fixPng(this) };
	       		} else {
	        	 PngFix.fixPng(this);
	       		}
	    	 })
		}
	  }
	},
	fixPng : function(png) {
   		// get src
   		var src = png.src;
   		// replace by blank image
   		png.onload = function() { };
   		png.src = PE.parameters.pngfix;
   		// set filter (display original image)
   		png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
 	}

}
var ieOverflow = {
	execute: function() { 
	if ( /MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32" ) {
		$("div.center").find(".ieoverflow").each(function() {
	 		var hght = Math.round((jQuery(this).height()/16)*Math.pow(10,1))/Math.pow(10,1);
			var cssH = hght + "em";
			var stylet = 'height:'+cssH+';width:98.5%';
			jQuery(this).wrap($("<div></div>").css("height",cssH).css("width","98.5%"));
			jQuery(this).css('position','absolute');
			jQuery(this).parent().after('<br /><br /><br /><br /><br />');

			}
		)}
	}
}
/** Execute Inits on Page Read **/

$.readyOrDone( Tltps.execute );
$.readyOrDone( SkipNavFix.execute );
$.readyOrDone( PngFix.execute );
$.readyOrDone( ieOverflow.execute );