window.onload = function () {
	CreateRoundedCorners();
	FixIE7ZoomBug();
	ProductsSubmenu.Init();
}

function CreateRoundedCorners() {
	var cornerBL, // Bottom left corner
	    cornerBR, // Bottom right corner
	    cornerTL, // Top left corner
	    cornerTR, // Top right corner
	    secondaryContent = document.getElementById('secondaryContent'),
	    secondaryContentDivs, // Array of all div elements within secondaryContent
	    i;

	if (secondaryContent) {
		secondaryContentDivs = secondaryContent.getElementsByTagName('div');
		for (i = 0; i < secondaryContentDivs.length; i++) {
			if (secondaryContentDivs[i].className.indexOf('secondaryContentProduct') != -1) {
			
				// Create corner elements
				cornerBL = document.createElement('div');
				cornerBR = document.createElement('div');
				cornerTL = document.createElement('div');
				cornerTR = document.createElement('div');
				
				// Assign appropriate class names
				cornerBL.className = 'corner cBL';
				cornerBR.className = 'corner cBR';
				cornerTL.className = 'corner cTL';
				cornerTR.className = 'corner cTR';
				
				// Place elements within document
				secondaryContentDivs[i].appendChild(cornerBL);
				secondaryContentDivs[i].appendChild(cornerBR);
				secondaryContentDivs[i].appendChild(cornerTL);
				secondaryContentDivs[i].appendChild(cornerTR);
			}
		}
	}
}

ProductsSubmenu = function () {
	var button,        // 'Products' button
	    submenu,       // Console navigation to appear on 'Products' button rollover
	    timer,         // Stores timeout function. Allows for easy clearing
	    timeout = 500; // Nunber of milliseconds before submenu disappears

	return {
		// Grab elements from the document
		Init : function () {
			button  = document.getElementById('navProducts');
			submenu = document.getElementById('consoleNav');
			if (!button || !submenu) {
				return;
			}
			ProductsSubmenu.AddEvents();
		},
		// Add events to trigger showing and hiding of submenu on mouseover/out
		AddEvents : function () {
			button.onmouseover = function () {
				ProductsSubmenu.Show();
			};
			submenu.onmouseover = function () {
				ProductsSubmenu.Show();
			};
			button.onmouseout = function () {
				timer = setTimeout(ProductsSubmenu.Hide, timeout);
			};
			submenu.onmouseout = function () {
				timer = setTimeout(ProductsSubmenu.Hide, timeout);
			};
		},
		// Show submenu
		Show : function () {
			submenu.style.display = 'block';
			clearTimeout(timer);
		},
		// Hide submenu
		Hide : function () {
			submenu.style.display = 'none';
		}
	};
}();

/*  IE7 has an annoying bug which causes the rendering engine to fail to zoom background images applied to
    the body. Applying an image to the body is the most sensible way to achieve the styling we require, and
    the only workaround is to	create a div element to surround everything and apply a background to this.
    This non-semantic div compromises the integrity of the markup and thus, I have opted to create the
    surrounding div dynamically with JavaScript and apply it only to users browsing in IE7.	*/
		
function FixIE7ZoomBug() {
	var isIE7 = ((document.all) && (navigator.appVersion.indexOf('MSIE 7.')!=-1));
	if (isIE7) {
		var body = document.getElementsByTagName('body')[0],
			bodyInnerHTML = body.innerHTML,
			surroundingDiv = document.createElement('div');
		
		// Remove background image and inner HTML from body
		body.style.background = 'none';
		body.innerHTML = '';
		
		// Assign ID to viewport div and append to the DOM
		surroundingDiv.setAttribute('id','viewport');
		body.appendChild(surroundingDiv);
		
		// Grab viewport within document, style background image and apply HTML
		docSurroundingDiv = document.getElementById('viewport');
		docSurroundingDiv.style.background = '#FFF url(../images/bg-r.gif) repeat-x 0 0';
		docSurroundingDiv.innerHTML = bodyInnerHTML;
	}
}

// A cross-browser function to add an event when the document has loaded
function addLoadEvent(fn) {
	var oldOnload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = fn;
	}
	else {
		window.onload = function () {
			oldOnload();
			fn();
		}
	}
}