var zoom;
var magInterval;

function LoadProductImage(handle,showloadingimage){
	
	var mainImageID = "myimage";
	var instructionsID = "ProductImageInstructions";
	var loadingImageSrc = "/_resources/karechiefs.com/images/ajax-loader.gif";
	var thumbWidth = 50;
	var thumbHeight = 50;
	var mainImageWidth = 250;
	var mainImageHeight = 250;
	var zoomImageWidth = 400;
	var zoomImageHeight = 400;	
	
	var imgSrc;
	var magnifySrc;	
	imgSrc = handle.src;
	imgSrc = imgSrc.replace("width="+thumbWidth,"width="+mainImageWidth);
	imgSrc = imgSrc.replace("height="+thumbHeight,"height="+mainImageHeight);
	magnifySrc = imgSrc;
	magnifySrc = magnifySrc.replace("width="+mainImageWidth,"width="+zoomImageWidth);
	magnifySrc = magnifySrc.replace("height="+mainImageHeight,"height="+zoomImageHeight);
	
	if(zoom){zoom.parentNode.removeChild(zoom);}
	
	if(showloadingimage){
		// -- if used in IE before main image is done loading, we get the '1 item loading' bug --- //
		var parentHandle = getElement(mainImageID).parentNode;
		parentHandle.removeChild(getElement(mainImageID));
	
		var imageLoading = new Image();
		imageLoading.onload = function() {
			imageLoading.onload = null;
		}
		imageLoading.src = loadingImageSrc;
		imageLoading.id = mainImageID;
	
		parentHandle.appendChild(imageLoading);
		// --- end IE bug --- //
	}
	
	var zoomImage = new Image();
	zoomImage.onload = function() {
		zoomImage.onload = null;
		var image = new Image();
		
		image.onload = function() {
			
			image.onload = null;
			getElement(mainImageID).src = imgSrc;
			
			var imageWidth = getElement(mainImageID).offsetWidth;			
			if(imageWidth == 16){
				magInterval = setInterval("TryToMagnifyIt('" + mainImageID + "','" + instructionsID + "','" + magnifySrc + "')", 50);
			}
			else{
				MagnifyIt(mainImageID,instructionsID,magnifySrc);
			}
			
		};
		image.src = imgSrc;
		
	};
	zoomImage.src = magnifySrc;		
}

function TryToMagnifyIt(mainImageID,instructionsID,magnifySrc){
	var imageWidth = getElement(mainImageID).offsetWidth;
	if(imageWidth > 16){
		clearInterval(magInterval);
		MagnifyIt(mainImageID,instructionsID,magnifySrc);
	}
}

function MagnifyIt(mainImageID,instructionsID,magnifySrc){
	//alert("MagnifyIt");
	// move stuff out of the way...
	var imageWidth;
	var imageHeight;
	var instructionsHeight;
	
	var applyFirefoxFix = false;

	imageWidth = getElement(mainImageID).offsetWidth;
	imageHeight = getElement(mainImageID).offsetHeight;
	instructionsHeight = getElement(instructionsID).offsetHeight;
	
	if(imageWidth > 0){				
		getElement("ProductDetailLeft").style.width = imageWidth + "px";
	}
	if(imageHeight > 0){	
		getElement("ProductMultipleImages").style.top = (imageHeight + instructionsHeight) + "px";
		getElement("ProductMultipleImages").style.display = "block";
	}	
	zoom = MojoMagnify.makeMagnifiable(getElement(mainImageID),magnifySrc);
}

function getElement(name) {
	var o1 = null;
	if(document.all) {
		o1 = document.all(name);
	}
	else if(document.getElementById) {
		o1 = document.getElementById(name);
	}
	return o1;
}

function getInternetExplorerVersion() {

    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer') {

        var ua = navigator.userAgent;

        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");

        if (re.exec(ua) != null)

            rv = parseFloat(RegExp.$1);

    }

    return rv;

}


//Buy Online Scripts
function center(element) {    
    var o = null;

    if (document.all) { o = document.all(element); }
    else if (document.getElementById) { o = document.getElementById(element); }


    var my_width = 0;
    var my_height = 0;

    if (typeof (window.innerWidth) == 'number') {
        my_width = window.innerWidth;
        my_height = window.innerHeight;
    } else if (document.documentElement &&
             (document.documentElement.clientWidth ||
               document.documentElement.clientHeight)) {
        my_width = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if (document.body &&
            (document.body.clientWidth || document.body.clientHeight)) {
        my_width = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }
    
    o.style.position = 'absolute';    
    o.style.zIndex = 99;
    

    var scrollY = 0;

    if (document.documentElement && document.documentElement.scrollTop) {
        scrollY = document.documentElement.scrollTop;
    } else if (document.body && document.body.scrollTop) {
        scrollY = document.body.scrollTop;
    } else if (window.pageYOffset) {
        scrollY = window.pageYOffset;
    } else if (window.scrollY) {
        scrollY = window.scrollY;
    }

    var elementDimensions = getDimensions(element);

    var setX = (my_width - elementDimensions[0]) / 2;
    var setY = (my_height - elementDimensions[1]) / 2 + scrollY;

    setX = (setX < 0) ? 0 : setX;
    setY = (setY < 0) ? 0 : setY;

    o.style.left = setX + "px";
    o.style.top = setY + "px";

    o.style.display = 'block';
}


function getDimensions(element) {

    var dimensionArr = new Array;

    var o = null;

    if (document.all) { o = document.all(element); }
    else if (document.getElementById) { o = document.getElementById(element); }


    if (o.style.display != 'none') {
        dimensionArr[0] = o.offsetWidth;
        dimensionArr[1] = o.offsetHeight;
        return dimensionArr;
    }

    // All *Width and *Height properties give 0 on elements with display none,
    // so enable the element temporarily
    var els = o.style;
    var originalVisibility = els.visibility;
    var originalPosition = els.position;
    els.visibility = 'hidden';
    els.position = 'absolute';
    els.display = '';
    var originalWidth = o.clientWidth;
    var originalHeight = o.clientHeight;
    els.display = 'none';
    els.position = originalPosition;
    els.visibility = originalVisibility;

    dimensionArr[0] = originalWidth;
    dimensionArr[1] = originalHeight;
    return dimensionArr;
}

function hideStuff(element) {

    var o = null;
    if (document.all) {
        o = document.all(element);
    }
    else if (document.getElementById) {
        o = document.getElementById(element);
    }

    if (o != null) {
        o.style.display = "none";
    }

    return false;
}

function ChangeImage(sSelect, sImage, sEnlarge) {

    var oSel = null;
    var oImg = null;
    var oEnl = null;
    if (document.all) {
        oSel = document.all(sSelect);
        oImg = document.all(sImage);
        oEnl = document.all(sEnlarge);
    }
    else if (document.getElementById) {
        oSel = document.getElementById(sSelect);
        oImg = document.getElementById(sImage);
        oEnl = document.getElementById(sEnlarge);
    }


    if (oSel != null && oImg != null && oEnl != null) {
        

        var splEnlarge = oSel.value.split("---", 2)[1];
        var sImgSrc = oSel.value.split("---", 1);
        oEnl.href = splEnlarge;
        //alert(splEnlarge);
        //oEnl.onclick = splEnlarge;
        oImg.src = sImgSrc;

    }

}


function zoomCustom(img) {
    imgwin = window.open("/Products/Custom-Design/zoomcustom.asp?img=" + [img] + "", "poproomimage", "width=200,height=200,toolbar=no,menubar=no,scrollbars=no,resizable=no");
    imgwin.focus();
}
