﻿var agt=navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);
var iePos = appVer.indexOf('msie');
if (iePos !=-1) {
is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
is_major = parseInt(is_minor);
}
var is_getElementById = (document.getElementById) ? "true" : "false"; 
var is_getElementsByTagName = (document.getElementsByTagName) ? "true" : "false";
var is_documentElement = (document.documentElement) ? "true" : "false"; 
var is_ie = ((iePos!=-1));
var is_ie3 = (is_ie && (is_major < 4));
var is_ie4 = (is_ie && is_major == 4);
var is_ie4up = (is_ie && is_minor >= 4);
var is_ie5 = (is_ie && is_major == 5);
var is_ie5up = (is_ie && is_minor >= 5);
var is_ie5_5 = (is_ie && (agt.indexOf("msie 5.5") !=-1)); 
var is_ie5_5up =(is_ie && is_minor >= 5.5); 
var is_ie6 = (is_ie && is_major == 6);
var is_ie7 = (is_ie && is_minor > 6);

var isFF2 = false;
var isFF3 = true;
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))  //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
{
	var ffversion=new Number(RegExp.$1); // capture x.x portion and store as a number
	if (ffversion>=3)
		isFF3 = true;
	else if (ffversion>=2)
		isFF2 = true;
}
var isFF = isFF2 || isFF3;

var isSafari3 = false;
if (window.devicePixelRatio) isSafari3 = true;

/*
Original Box structure:

<div class="box">
	<div class="boxTopLeft"></div>
	<div class="boxTopRight"></div>
	<div class="boxBottomLeft"></div>
	<div class="boxBottomRight"></div>
	<div class="innerBox">
		<div class="innerBoxLeft"></div>
		<div class="innerBoxRight"></div>
		<div class="boxContent">
			<h3>Hello</h3>
			<p>This is the dummy container</p>
		</div> 
	</div>
</div>

*/

function findDivByClassname(parent, className) {

	var divs = parent.getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++) {
	    //alert(divs[i].className);
	    if (divs[i].className == className)
	        return divs[i];
	}
}

function fixRoundedHeaders()
{
	var headers = document.getElementsByTagName("h3");
	var span;
	for (i = 0; i < headers.length; i++)
	{
		span = document.createElement("span");
		span.className = "topLeftCorner";
		headers[i].appendChild(span);
		span = document.createElement("span");
		span.className = "topRightCorner";
		headers[i].appendChild(span);
	}
}

function fixBox(box)
{
	var boxContent;
	var div;
	var innerBox;
	var boxTopLeft, boxTopRight, boxBottomLeft, boxBottomRight;
	var innerBoxLeft, innerBoxRight, boxTop, boxBottom;
	var temp;

		if (is_ie6)
		{
			boxContent = findDivByClassname(box, "boxContent");
			innerBoxLeft = findDivByClassname(box, "innerBoxLeft");
			innerBoxLeft.style.height = boxContent.offsetHeight + "px";
			innerBoxRight = findDivByClassname(box, "innerBoxRight");
			innerBoxRight.style.height = boxContent.offsetHeight + "px";
			boxBottomLeft = findDivByClassname(box, "boxBottomLeft");
			boxBottomLeft.style.top = boxContent.offsetHeight + 6 + "px";
			boxBottomRight = findDivByClassname(box, "boxBottomRight");
			boxBottomRight.style.top = boxContent.offsetHeight + 6 + "px";
			boxTop = createDiv("boxTop");
			temp = box.offsetWidth - 13;
			if (temp < 0) 
				temp = 0;
			boxTop.style.width = temp;
			box.appendChild(boxTop);
			temp = box.offsetWidth - 12;
			if (temp < 0) 
				temp = 0;
			boxContent.style.width = temp;
			boxBottom = createDiv("boxBottom");
			boxBottom.style.width = boxTop.offsetWidth;
			boxBottom.style.top = boxBottomLeft.offsetTop - 1;
			box.appendChild(boxBottom);
			boxContent.style.height = boxBottom.offsetTop;
		}
}

function fixBoxes()
{
	var divs = document.getElementsByTagName("div");
	var boxes = [];

	// Find all divs with class="box"
	for (var i = 0; i < divs.length; i++)
	{
		if (/\bbox\b/.exec(divs[i].className) || /\bgreenMenuBox\b/.exec(divs[i].className) || /\borangeMenuBox\b/.exec(divs[i].className))
			boxes[boxes.length] = divs[i];
	}
	for (i = 0; i < boxes.length; i++)
	{
		fixBox(boxes[i]);
	}
}

function createDiv(className)
{
	var div = document.createElement("div");
	div.className = className;
	return div;
}

function fixLinkButtons()
{
	var links = document.getElementsByTagName("a");
    var outerSpan, innerSpan, ieImg;
	var temp;
	for (var i = 0; i < links.length; i++)
	{
		if (links[i].className == "orangeButton" || links[i].className == "blackButton" || links[i].className == "smallWhiteButton")
        {
	        outerSpan = document.createElement("span");
	        innerSpan = document.createElement("span");
	        outerSpan.appendChild(innerSpan);
		    while (links[i].childNodes.length > 0)
		    {
			    innerSpan.appendChild(links[i].firstChild);
		    }
		    links[i].appendChild(outerSpan);
		ieImg = document.createElement("img");
		ieImg.className = "ieButtonFix";
		if (links[i].className == "orangeButton")
			ieImg.src = "Img/orangeButtonRightBG.png";
		if (links[i].className == "blackButton")
			ieImg.src = "Img/blackButtonRightBG.png";
		if (links[i].className == "smallWhiteButton")
			ieImg.src = "Img/smallWhiteButtonRightBG.png";
		if (is_ie6)
		{
		    links[i].appendChild(ieImg);
			if (links[i].style.width != "")
			{
				if (links[i].offsetWidth >= 22)
					innerSpan.style.width = links[i].offsetWidth - 22 + "px";
				links[i].style.width = "auto";
			}
		}
		if (isFF3 && links[i].style.width != "")
			innerSpan.style.marginLeft = -2 + "px";
		if (is_ie6 && links[i].style.width != "")
			innerSpan.style.marginRight = 2 + "px";
		if (is_ie7 && links[i].style.width != "")
			innerSpan.style.marginLeft = 0 + "px";

		    links[i].style.padding = 0 + "px";
		    if (links[i].offsetWidth > 24)
		        innerSpan.style.width = links[i].offsetWidth - 24 + "px";
		if (isSafari3)
		{
			innerSpan.style.height = 26 + "px";
			innerSpan.style.lineHeight = 18 + "px";
		}
        }
	}
}

function initDesign()
{
    if (is_ie6)
	    fixBoxes();
	setTimeout("fixRoundedHeaders();", 0);
	fixLinkButtons();
	if (is_ie7)
	{
		addLoadEvent(quickFixForInvisibleH4);
	}
}

function quickFixForInvisibleH4()
{
    var h4s = document.getElementsByTagName("h4");
    for (var i = 0; i < h4s.length; i++)
        h4s[i].style.visibility = "visible";
}

if (!is_ie6 && window.HTMLAnchorElement != undefined && !HTMLAnchorElement.prototype.click)
{
    HTMLAnchorElement.prototype.click = function() {
            var ev = document.createEvent('MouseEvents');
            ev.initEvent('click',true,true);
            if (this.dispatchEvent(ev) !== false) 
            {
                //safari will have already done this, but I'm not sniffing safari
                //just in case they might in the future fix it; I figure it's better
                //to trigger the action twice than risk not triggering it at all
                document.location.href = this.href;
            }
    }
}