
/*
AddEvent(window, "load", function() {
    for (var i = 0; i < document.images.length; i++) {
        if (!IsImageOk(document.images[i])) {
            document.images[i].style.visibility = "hidden";
        }
    }
});
*/
    
    
function IsImageOk(img) {
	if (img == null) return false;
    if (BrowserDetect.browser=="Safari") return true;
    
    // During the onload event, IE correctly identifies any images that
    // weren't downloaded as not complete. Others should too. Gecko-based
    // browsers act like NS4 in that they report this incorrectly.
    if (!img.complete) return false;

    // However, they do have two very useful properties: naturalWidth and
    // naturalHeight. These give the true size of the image. If it failed
    // to load, either of these should be zero.
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) return false;

    // No other way of checking: assume it's ok.
    return true;
}


function jsMagicImages(){
	for (var i = 0; i < document.images.length; i++) {
		if (!IsImageOk(document.images[i])) {
			var strSource = document.images[i].src;
			if (strSource.indexOf("/images/library/category") > 0) {
				if (!IsImageOk(document.getElementById(document.images[i].id + "_hid"))) {
					document.images[i].src = "/images/library/category/originals/missing picture.jpg";
				} else {
					document.images[i].style.display="none";
					document.getElementById(document.images[i].id + "_hid").style.display="block";
				}
			} else if (strSource.indexOf("/images/library/product") > 0) {
				document.images[i].src = strSource.substr(0,strSource.lastIndexOf("/") - 2) + "00/000000000.jpg";
			} else if (strSource.indexOf("/images/library/ranges") > 0) {
				document.images[i].src = strSource.substr(0,strSource.lastIndexOf("/") + 1) + "0000.jpg";
			} else if (strSource.indexOf("/images/library/logo/") > 0) {
			 	document.images[i].src = "/images/design/empty1px.png";
			}
		}
	}
}

function jsMagicImages2(){
	for (var i = 0; i < document.images.length; i++) {
		if (!IsImageOk(document.images[i])) {
			var strSource = document.images[i].src;
			
			if (strSource.indexOf("/images/library/category") > 0) {
				var sHiddenSource = document.getElementById(document.images[i].id + "_hid").src;
				if (!IsImageOk(document.getElementById(document.images[i].id + "_hid"))) {
					document.getElementById(document.images[i].id + "_hid").src = document.getElementById(document.images[i].id + "_hid").src.replace("/01/","/"+sHiddenSource.substr(sHiddenSource.lastIndexOf("/")+1, 2)+"/");
					//alert("Was " + sHiddenSource + "Now its /"+sHiddenSource.substr(sHiddenSource.lastIndexOf("/")+1, 2)+"/")
					//document.getElementById(document.images[i].id + "_hid").src = document.getElementById(document.images[i].id + "_hid").src.replace("/01/","/07/");
					if (IsImageOk(document.getElementById(document.images[i].id + "_hid"))) {
						document.getElementById(document.images[i].id + "_hid").style.display="block";
						document.images[i].style.display="none";
					}
					else
					{
						//document.images[i].style.display="none";
						document.images[i].src = "/images/library/category/originals/missing picture.jpg";
					}
				} else {
					//alert(document.images[i].id);
					document.images[i].style.display="none";
					document.getElementById(document.images[i].id + "_hid").style.display="block";
				}
			} else if (strSource.indexOf("/images/library/product") > 0) {
				//document.images[i].src = strSource.replace("/01/","/07/");
				//document.images[i].src = strSource.substr(0,strSource.lastIndexOf("/") - 2) + "00/000000000.jpg";
			} else if (strSource.indexOf("/images/library/ranges") > 0) {
				document.images[i].src = strSource.substr(0,strSource.lastIndexOf("/") + 1) + "0000.jpg";
			} else if (strSource.indexOf("/images/library/logo/") > 0) {
			 	document.images[i].src = "/images/design/empty1px.png";
			}
		}
	}
}

