// Set these to correct width/height available for image
var categoryPictureListImgWidth = 158;
var categoryPictureListImgHeight = 158;

function categoryPictureList() {
	
	var imgs = {}; // Map img tag from url to img object
	
	// Find all images in menu, swap their a hrefs title into the place of the image
	var productMenu = document.getElementById("ProductMenu_Table");
	
	var img = productMenu.getElementsByTagName("IMG"); // Images embedded in menu

	var img2 = []; // Copy to normal array (to be referenced from adjustfnc
	for (var i=0; i<img.length; i++) {
		img2[i] = img[i];
	}
	
	var imgTester = []; // Array for image objects to be tested for width/height
	
	var adjustfnc = function(imi, imWidth, imHeight) { // Function to adjust image
		
		var im = img2[imi]; 
		
		if (imWidth > categoryPictureListImgWidth) { // Adjust by width
			imHeight = im.height = Math.floor(imHeight * (categoryPictureListImgWidth / imWidth));
			imWidth = im.width = categoryPictureListImgWidth;
		}
		
		if (imHeight > categoryPictureListImgHeight) { // Adjust by height
			imWidth = im.width = Math.floor(imWidth * (categoryPictureListImgHeight / imHeight));
			imHeight = im.height = categoryPictureListImgHeight;
		}
		
		if (imHeight < categoryPictureListImgHeight) { // Vertical margin
			var margin = Math.floor((categoryPictureListImgHeight - imHeight) / 2);
			im.style.marginTop = margin + "px";
		}
		
		im.className = "show"; // Turn on (after load/scale)
		
	}
	
	for (var i=0; i<img.length; i++) {
		var parent = img[i].parentNode; // The a tag
		var title = (parent.tagName == "B" ? parent.parentNode.title : parent.title);  // title of a tag, which becomes link text
		var href = (parent.tagName == "B" ? parent.parentNode.href : parent.href)
		
		if (title) {

			if (categoryPictureListImgWidth && categoryPictureListImgHeight) {
			
				imgTester[i] = new Image();
				
				img2[i].className = "hide"; // Turn off until loaded/scaled
									
				eval('imgTester[' + i + '].onload = function() {\n' +
				'	adjustfnc(' + i + ', imgTester[' + i + '].width, imgTester[' + i + '].height);\n' +
				'};\n');

				imgTester[i].src = img[i].src;
				
			}
			
			imgs[href] = img[i];
			parent.innerHTML += title;
		}
		
	}
	
	// Find product list and move the image into that position
	var a = document.getElementsByTagName("A");
	
	for (var i=0; i<a.length; i++) {
		
		if ((a[i].className == "SubCats_Prodlink")) {
			
			var imgg = imgs[a[i].href];
			
			if (imgg) {
				var innerHTML = a[i].innerHTML;
				a[i].innerHTML = "";
				
				var span1 = document.createElement("SPAN");
				span1.className = "imgbox";
				span1.appendChild(imgg);
				
				a[i].appendChild(span1);
				
				var span2 = document.createElement("SPAN");
				span2.className = "txtbox";
				span2.innerHTML = innerHTML;
				
				a[i].appendChild(span2);
				
				a[i].onclick = function() {document.location = this.href; return false;} // Onclick due to IE bug on block A
			}
			
		}
	}
	
}

function fixPrMenuSpanTagMenu(){
    var tempSpan = document.getElementById('ProductMenu_Table').getElementsByTagName('SPAN');
    for (var i = 0; i < tempSpan.length; i++) {
        if (tempSpan[i].className == "ProductMenu_MenuItemBold") {
            tempSpan[i].parentNode.innerHTML += tempSpan[i].innerHTML;
            tempSpan[i].style.display = "none";
            tempSpan[i].innerHTML = "";
        }
    };
 }
