var blnToolbarSupported = false;
var blnStrictCompatibility = false;

//Check browser version for support
if (navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.appVersion.substring(0,1) > 3)
{
	blnToolbarSupported = true;

	//Check for strict compatibility
	if (document.compatMode == "CSS1Compat")
	{
		blnStrictCompatibility = true;
	}
}

//If toolbar is supported, start building HTML
if (blnToolbarSupported)
{
	var chrNewLine = String.fromCharCode(10);
	var chrQuote = String.fromCharCode(34);
	var strHtml;
	var blnFirstMenu = true;
	var intX;
	var intY;
	var intX2;
	var intY2;
	var intIntervalId = new Array(3);

	//Make all setting changes here
	var intHeight = 18;
	var intSubMenuTop = 99;
	var intSubMenuWidth = 160;
	var intSubMenuTopPadding = 8;
	var intSubMenuLeftPadding = 0;
	var intSubMenuBottomPadding = 10;
	var strBgColor = "#808080";
	var strFont = "bold x-small Verdana";
	var strFontColor = "#FFFFFF";
	var strFontMouseoverColor = "#0066CC";
	var intFadeInterval = 50;

	//Start toolbar HTML
	strHtml = "<DIV ID='divToolbar' STYLE='background-color:" + strBgColor + ";width:100%;'>";
	strHtml += "<DIV ID='divMenuPane'  STYLE='position:relative;top:-2px;left:0px;height:" + intHeight + "px;background-color:" + strBgColor + 
		";float:left;' NOWRAP>&nbsp;<!-- TOP MENU TAG -->";
	strHtml += "</DIV></DIV>";

	//Add menu items
	BuildMenu();
}

//Write HTML to page
function DrawToolbar()
{
	document.write(strHtml);
}

//Create menu
function BuildMenu()
{
	//Add menu items for Home link
	AddMenuItem("home", "Home", "default.htm", "");

	//Add menu items for Dresser link
	AddMenuItem("dresser", "Dresser", "dresser.htm", "");
	AddSubMenuItem("dresser", "Style 38", "38.htm", "");
	AddSubMenuItem("dresser", "Style 40", "40.htm", "");
	AddSubMenuItem("dresser", "Style 62", "62.htm", "");
	AddSubMenuItem("dresser", "Style 63", "63.htm", "");
	AddSubMenuItem("dresser", "Style 65", "65.htm", "");
	AddSubMenuItem("dresser", "Style 70", "70.htm", "");
	AddSubMenuItem("dresser", "Style 88", "88.htm", "");
	AddSubMenuItem("dresser", "Style 90", "90.htm", "");
	AddSubMenuItem("dresser", "Style 91", "91.htm", "");
	AddSubMenuItem("dresser", "Style 118", "118.htm", "");
	AddSubMenuItem("dresser", "Style 138", "138.htm", "");
	AddSubMenuItem("dresser", "Style 193", "193.htm", "");
	AddSubMenuItem("dresser", "Style 253", "253.htm", "");
	AddSubMenuItem("dresser", "Style 360", "360.htm", "");
	AddSubMenuItem("dresser", "Gaskets", "gaskets.htm", "");

	//Add menu items for Steel Forgings link
	AddMenuItem("steel_forgings", "Steel Forgings", "steel_forgings.htm", "");
	AddSubMenuItem("steel_forgings", "Reinforcing Saddles", "reinforcing_saddles.htm", "");
	AddSubMenuItem("steel_forgings", "Full Encirclement Saddles", "full_enc_saddles.htm", "");
	AddSubMenuItem("steel_forgings", "Hot Tap Tees", "hot_tap_tees.htm", "");

	//Add menu items for Fittings and Flanges link
	AddMenuItem("fittings_flanges", "Fittings and Flanges", "fittings_flanges.htm", "");
	AddSubMenuItem("fittings_flanges", "Fittings", "fittings.htm", "");
	AddSubMenuItem("fittings_flanges", "Flanges", "flanges.htm", "");

	//Add menu item for Other Products link
	AddMenuItem("other", "Other Products", "other.htm", "");

	//Add menu item for links
	AddMenuItem("links", "Links", "links.htm", "");

	//Add menu item for Contact Us link
	AddMenuItem("contact", "Contact Us", "contact.htm", "");

	//DebugHTML(strHtml);
}

//Add top level menu
function AddMenuItem(strId, strText, strUrl, strHelpText)
{
	var strSubLookUpTag = "<!-- TOP MENU TAG -->"
	var strTemp = chrNewLine;

	//If menu is not first, add separator
	if (blnFirstMenu == false)
	{
		strTemp += "<SPAN STYLE='font:" + strFont + ";color:" + strFontColor + ";'>|&nbsp;</SPAN>";
	}

	//Start anchor tag	
	strTemp += "<A ";

	//Add URL if there, if not kill click event
	if (strUrl != "")
	{
		strTemp += "HREF='" + strUrl + "' ";
	}
	else
	{
		strTemp += "HREF='' ONCLICK='window.event.returnValue=false;' ";
	}

	//Finish anchor tag
	strTemp += "ID='" + strId + "' TITLE='" + strHelpText + "' STYLE='text-decoration:none;cursor:hand;font:" + 
		strFont + ";color:" + strFontColor + "; background-color:" + strBgColor + ";' ONMOUSEOUT=" + chrQuote + 
		"MouseOverColor('out'); HideMenu('sub_" + strId + "');" + chrQuote + " ONMOUSEOVER=" + chrQuote + "MouseOverColor('over'); ShowSubMenu('sub_" + 
		strId + "');" + chrQuote + ">" + "&nbsp;" + strText + "&nbsp;</A>";

	strTemp += strSubLookUpTag;
	strHtml = strHtml.replace(strSubLookUpTag, strTemp);
	blnFirstMenu = false;
}

//Add item to submenu
function AddSubMenuItem(strId, strSubMenuText, strSubMenuUrl, strSubMenuHelpText)
{
	var strSubLookUpTag = "<!-- SUB_" + strId + "_TAG -->";
	var intTagPosition = strHtml.indexOf(strSubLookUpTag);
	var strTemp;

	//If new menu column, start new span and div
	if (intTagPosition <= 0)
	{
		strHtml += chrNewLine + chrNewLine + "<SPAN ID='sub_" + strId + "' STYLE='display:none;position:absolute;width:" + intSubMenuWidth + 
			"px;background-color:" + strBgColor + ";padding-top:" + intSubMenuTopPadding + "px;padding-left:" + intSubMenuLeftPadding + 
			"px;padding-bottom:" + intSubMenuBottomPadding  + "px;z-index:9px;' ONMOUSEOUT=" + chrQuote + "HideMenu('sub_" + strId + "');" + chrQuote + ">";

		strHtml += "<DIV STYLE='position:relative;left:0px;top:0px;'>";
	}

	//Add body for all submenu items
	strTemp = chrNewLine + "<A HREF='" + strSubMenuUrl + "' ID='sub_" + strId + "' STYLE='text-decoration:none;cursor:hand;font:" + strFont + 
		";color:" + strFontColor + "' ONMOUSEOUT="  + chrQuote + "MouseOverColor('out');" + chrQuote + " ONMOUSEOVER=" + chrQuote + 
		"MouseOverColor('over');" + chrQuote + ">&nbsp;" + strSubMenuText + "</A><BR>" + strSubLookUpTag;

	//Close or replace with new lookup tag
	if (intTagPosition <= 0)
	{
		strHtml += strTemp + "</DIV></SPAN>";
	}
	else
	{
		strHtml = strHtml.replace(strSubLookUpTag, strTemp);
	}
}

//Toggle color of link on mouseover
function MouseOverColor(strDirection)
{
	if (strDirection == "over")
	{
		window.event.srcElement.style.color = strFontMouseoverColor;
	}
	else
	{
		window.event.srcElement.style.color = strFontColor;
	}
}

//Show submenu pane
function ShowSubMenu(strMenuId)
{
	//Set display properties of all dropdowns to false - need better way of doing this
	document.getElementById('sub_dresser').style.display = "none";
	document.getElementById('sub_steel_forgings').style.display = "none";
	document.getElementById('sub_fittings_flanges').style.display = "none";

	//See if there is a sub menu for this menu item
	if (document.getElementById(strMenuId) == null)
	{
		//If not, kill event
		window.event.cancelBubble = true;
		return false;
	}
	else
	{
		//If so, set properties of div
		intX = window.event.srcElement.offsetLeft + window.event.srcElement.offsetParent.offsetLeft;
		intY = intSubMenuTop;

		document.getElementById(strMenuId).style.left = intX;
		document.getElementById(strMenuId).style.top = intY;
		document.getElementById(strMenuId).style.clip = "rect(0 0 0 0)";
		document.getElementById(strMenuId).style.display = "block";
		document.getElementById(strMenuId).style.zIndex = 102;
		document.getElementById(strMenuId).style.filter = "alpha(opacity = '0')";

		//Get right and bottom edges
		intX2 = intX + document.getElementById(strMenuId).offsetWidth;
		intY2 = intY + document.getElementById(strMenuId).offsetHeight;
	}

	//Delay 2 millsecond to allow the value of ToolbarMenu.offsetHeight be set
	window.setTimeout("DisplaySubMenu('" + strMenuId + "')", 2);
}

//Fade in sub menu
function FadeInSubMenu(strMenuId)
{
	if (document.getElementById(strMenuId).filters.alpha.opacity < 100)
	{
		document.getElementById(strMenuId).filters.alpha.opacity += 10;
	}
	else
	{
		if (strMenuId == "sub_dresser")
		{
			clearInterval(intIntervalId[0]);
		}
		else if (strMenuId == "sub_steel_forgings")
		{
			clearInterval(intIntervalId[1]);
		}
		else if (strMenuId == "sub_fittings_flanges")
		{
			clearInterval(intIntervalId[2]);
		}
	}
}

//Display submenu
function DisplaySubMenu(strMenuId)
{
	document.getElementById(strMenuId).style.clip = "rect(auto auto auto auto)";
	document.getElementById(strMenuId).filters.alpha.opacity = 10;

	if (strMenuId == "sub_dresser")
	{
		clearInterval(intIntervalId[0]);
		intIntervalId[0] = setInterval("FadeInSubMenu('" + strMenuId + "')", intFadeInterval);
	}
	else if (strMenuId == "sub_steel_forgings")
	{
		clearInterval(intIntervalId[1]);
		intIntervalId[1] = setInterval("FadeInSubMenu('" + strMenuId + "')", intFadeInterval);
	}
	else if (strMenuId == "sub_fittings_flanges")
	{
		clearInterval(intIntervalId[2]);
		intIntervalId[2] = setInterval("FadeInSubMenu('" + strMenuId + "')", intFadeInterval);
	}
}

function HideMenu(strMenuId)
{
	//Get position of cursor when event occurred
	var intCurX = event.clientX;
	var intCurY = event.clientY;

	//Check to see if it is a dropdown
	if (document.getElementById(strMenuId) == null)
	{
		//If not, kill event
		window.event.cancelBubble = true;
		return; 
	}
	else
	{
		//If so, check mouse off coordinates to see if the mouse is still on the submenu div
		if ((intCurX >= (intX + 5) && intCurX <= intX2) && ((intCurY > (intY - 10) && intCurY <= intY2)))
		{
			//If so, kill event
			window.event.cancelBubble = true;
			return;
		}			
	}
	
	//If mouse is not on submenu, hide submenu
	document.getElementById(strMenuId).style.display = "none";

	//Clear interval for menu
	if (strMenuId == "sub_dresser")
	{
		clearInterval(intIntervalId[0]);
	}
	else if (strMenuId == "sub_steel_forgings")
	{
		clearInterval(intIntervalId[1]);
	}
	else if (strMenuId == "sub_fittings_flanges")
	{
		clearInterval(intIntervalId[2]);
	}

	window.event.cancelBubble = true;
}

//Code for image swap
var theImages = new Array();
//var j = 0

theImages[0] = "images/pipes_fade_396x200.jpg";
theImages[1] = "images/derrick_fade_396x200.jpg";
theImages[2] = "images/pipes_close_fade_396x200.jpg";

var p = theImages.length;
var preBuffer = new Array();

for (i = 0; i < p; i++)
{
   preBuffer[i] = new Image();
   preBuffer[i].src = theImages[i];
}

//Choose random image
var whichImage = Math.round(Math.random() * (p - 1));


function ShowImage()
{
	document.write("<img src='" + theImages[whichImage] + "' height='200' width='396' alt='A master distributor of domestic pipe fittings, flanges, and Dresser couplings since the 1920's'>");
}