/* loupe.js Version 2.2.0 alpha - 20070907. Copyright 2008, Hamilton Ford. Visit: http://www.hamiltonford.com Please do not use this script without permission. */ var transparent = new Image(10,10); transparent.src = "infiniteloupe/graphics/transparent.gif"; var photoCache = new Array(); var loupeXoffset = loupeWidth + loupeOffset + (2 * loupeBorder) - 1; /* +1 TO CORRECT OFFSET DUE TO CURSOR WIDTH (Mac) */ var loupeYoffset = loupeHeight + loupeOffset + (2 * loupeBorder) - 1; /* OFFSET SHOULD BE 2 ON PC */ var loupeVisible = false, fixTransparency = false; var loupeSlide = 0, slideRate = 15; var loupePositionX = loupePositionY = loupeTargetX = loupeTargetY = -loupeOffset; var magRatioW = magRatioH = lastPicLeft = lastPicTop = winWidth = winHeight = scrollBarThickness = scrollBarWidth = scrollBarHeight = 0; function initialize() { checkBrowser(); fixEdge(); loupeDiv = document.createElement("div"); loupeDiv.id = "loupe"; loupeDiv.style.width = loupeWidth + "px"; loupeDiv.style.height = loupeHeight + "px"; loupeDiv.style.position = "absolute"; loupeDiv.style.top = 0; loupeDiv.style.left = 0; loupeDiv.style.zIndex = 10; loupeDiv.style.display = "none"; loupeDiv.style.borderWidth = loupeBorder + "px"; loupeDiv.style.borderStyle = "solid"; loupeDiv.style.borderColor = loupeBorderColor; lensDiv = document.createElement("div"); lensDiv.id = "lens"; lensDiv.style.width = loupeWidth + "px"; lensDiv.style.height = loupeHeight + "px"; if (fixTransparency == true) lensDiv.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='infiniteloupe/graphics/loupe-lens-fresnel.png', sizingMethod='scale')"; else lensDiv.style.backgroundImage = "url('infiniteloupe/graphics/loupe-lens-fresnel.png')"; lensDiv.style.backgroundColor = "transparent"; lensDiv.style.backgroundRepeat = "no-repeat"; lensDiv.style.backgroundPosition = "center center"; lensDiv.style.overflow = "hidden"; lensImg = document.createElement("img"); lensImg.id = "zoomage"; lensImg.src = transparent.src; lensImg.alt = "loading image..."; lensImg.style.position = "relative"; lensImg.style.top = 0; lensImg.style.left = 0; loupeInfoBoxDiv = document.createElement("div"); loupeInfoBoxDiv.style.width = loupeWidth + "px"; loupeInfoBoxDiv.style.position = "absolute"; loupeInfoBoxDiv.style.bottom = 0; loupeInfoBoxDiv.style.left = 0; loupeInfoTextP = document.createElement("p"); loupeInfoTextP.id = "loupeInfoText"; loupeInfoTextP.style.position = "relative"; loupeInfoTextP.style.top = 0; loupeInfoTextP.style.left = 0; loupeInfoTextP.style.color = "#FFFFFF"; loupeInfoTextP.style.backgroundColor = "transparent"; loupeInfoTextP.style.fontSize = "12px"; loupeInfoTextP.style.fontWeight = "normal"; loupeInfoTextP.style.textAlign = "center"; loupeInfoTextP.style.margin = "1px 5px 1px 5px"; loupeInfoBoxDiv.appendChild(loupeInfoTextP); lensDiv.appendChild(lensImg); loupeDiv.appendChild(lensDiv); loupeDiv.appendChild(loupeInfoBoxDiv); pageBody = document.getElementsByTagName("body"); pageBody[0].appendChild(loupeDiv); galleryDiv = document.getElementById("gallery"); gallerySubDivs = galleryDiv.getElementsByTagName("div"); for (i = 0, imageCount = -1; i < gallerySubDivs.length; i++) { currentDiv = gallerySubDivs[i]; currentImg = currentDiv.getElementsByTagName("img"); if(currentImg[0]) { currentImg[0].style.position = "absolute"; currentImg[0].style.zIndex = 10; createMask(currentImg[0],++imageCount,currentDiv); } } preloadPhotos(); } function checkBrowser() { browser = navigator.appName; browserVersion = navigator.appVersion; isMac = browserVersion.indexOf("Macintosh"); if (browserVersion.indexOf("Macintosh") != -1) scrollBarThickness = 15; else scrollBarThickness = 19; /* 17 IN FIREFOX, 19 IN NETSCAPE */ browserVersion = parseFloat(browserVersion); if (browser == "Microsoft Internet Explorer" && browserVersion < 7 && !isMac) return fixTransparency = true; } function fixEdge() { var widthadjust = (document.widthmeasure.width - 121)%34; var heightadjust = (document.heightmeasure.height - 121)%34; var topRight = document.getElementById('topright').style; topRight.background = "transparent url('graphics/bg-topright.gif') no-repeat "+(-widthadjust)+"px 0px"; var rightEdge = document.getElementById('right').style; rightEdge.background = "transparent url('graphics/bg-right.gif') repeat-y "+(-widthadjust)+"px 0px"; var bottomLeft = document.getElementById('bottomleft').style; bottomLeft.background = "transparent url('graphics/bg-bottomleft.gif') no-repeat 0px "+(-heightadjust)+"px"; var bottomEdge = document.getElementById('bottom').style; bottomEdge.background = "transparent url('graphics/bg-bottom.gif') repeat-x 0px "+(-heightadjust)+"px"; var bottomRight = document.getElementById('bottomright').style; bottomRight.background = "transparent url('graphics/bg-bottomright.gif') no-repeat "+(-widthadjust)+"px "+(-heightadjust)+"px"; measureWindow(); } function measureWindow() { if (parseInt(navigator.appVersion) > 3) { if (navigator.appName=="Netscape") { winWidth = window.innerWidth; winHeight = window.innerHeight; } if (navigator.appName.indexOf("Microsoft") != -1) { winWidth = document.body.offsetWidth; winHeight = document.body.offsetHeight; } } if (winWidth >= document.body.scrollWidth) scrollBarHeight = 0; else scrollBarHeight = scrollBarThickness; if (winHeight >= document.body.scrollHeight) scrollBarWidth = 0; else scrollBarWidth = scrollBarThickness; } function createMask(imageToMask,imageNo,borderDiv) { borderDiv.style.backgroundImage = "url(" + imageToMask.src + ")"; borderDiv.style.backgroundRepeat = "no-repeat"; borderDiv.style.backgroundPosition = "center center"; borderDiv.style.backgroundColor = imageToMask.style.borderColor; borderDiv.style.width = imageToMask.width; borderDiv.style.height = imageToMask.height; borderDiv.style.margin = 0; borderDiv.style.padding = 0; /* ADD BORDER ATTRIBUTE CHECKING */ borderDiv.style.borderWidth = imageToMask.style.borderWidth; borderDiv.style.borderStyle = imageToMask.style.borderStyle; borderDiv.style.borderColor = imageToMask.style.borderColor; maskImg = imageToMask; maskImg.style.borderWidth = 0; maskImg.style.borderColor = "transparent"; maskImg.id = "photo" + imageNo; maskImg.src = transparent.src; maskImg.style.position = "relative"; maskImg.style.zIndex = 20; maskImg.onmouseover = showMagBox; maskImg.onmousemove = getCoords; maskImg.onmouseout = hideMagBox; /* maskImg.onclick = status; */ return maskImg; } function preloadPhotos() { for (pNum = 0, numPhotos = (photoList.length/4), listPos = 0; pNum < numPhotos; pNum++) { photoURL = galleryDir + "/" + photoList[listPos++]; photoWidth = photoList[listPos++]; photoHeight = photoList[listPos++]; photoCredit = photoList[listPos++]; photoCache[pNum] = new Image(photoWidth,photoHeight); photoCache[pNum].src = photoURL; photoCache[pNum].width = photoWidth; photoCache[pNum].height = photoHeight; photoCache[pNum].name = photoCredit; } } function status() { alert(shiftLeft + ", " + shiftDown + ", " + winWidth + ", " + winHeight + ", " + document.body.scrollWidth + ", " + document.body.scrollHeight); } function showMagBox(t) { if (!t) t = window.event; thumb = t.toElement || t.target; /* NETSCAPE USES t.target */ var thumbPos = findThumbPosition(thumb); thumbX = thumbPos[0]; thumbY = thumbPos[1]; /* if (thumb.offsetParent) { thumbX = thumb.offsetLeft; thumbY = thumb.offsetTop; } else if (thumb.x) { thumbX = thumb.x; thumbY = thumb.y; } else setTimeout("showMagBox(t)",100); alert("Inadequate JavaScript support in this browser I'm afraid."); - NEED TO ADD A DELAY TO THIS IN CASE THE GALLERY ISN'T READY YET */ thumbID = thumb.id; prepareMagnifier(); } function findThumbPosition(obj) { var curleft = curtop = 0; if (obj.offsetParent) { do { curleft += obj.offsetLeft; curtop += obj.offsetTop; } while (obj = obj.offsetParent); } return [curleft,curtop]; } function prepareMagnifier() { photo = photoCache[thumbID.slice(5)]; magPic = document.getElementById("zoomage"); magPic.src = photo.src; magPic.width = photo.width; magPic.height = photo.height; if (photo.name) photoInfo = photo.name; else photoInfo = "© Hamilton Ford"; document.getElementById("loupeInfoText").innerHTML = photoInfo; thumbImage = document.getElementById(thumbID); thumbWidth = thumbImage.width; thumbHeight = thumbImage.height; magRatioW = photo.width / thumbWidth; magRatioH = photo.height / thumbHeight; thumbImage.style.cursor = "crosshair"; loupeDiv = document.getElementById("loupe"); loupeDiv.style.display = "block"; loupeVisible = true; } function hideMagBox() { loupeDiv = document.getElementById("loupe"); loupeDiv.style.display = "none"; loupeVisible = false; magPic = document.getElementById("zoomage"); magPic.src = transparent.src; } function getCoords(event) { if (!event) event = window.event; shiftLeft = document.body.scrollLeft; shiftDown = document.body.scrollTop; /* alert("clientX = " + event.clientX + "\npageX = " + event.pageX + "\nshiftLeft = " + shiftLeft); */ if (shiftLeft == 0) { /* TEST FOR NO SCROLL FIRST - shiftLeft MOST CONSISTENTLY ACCURATE VARIABLE */ if (event.clientX == event.pageX) this.posX = event.clientX; /* IF THE SAME THEN USE ONE */ else { if (event.pageX) this.posX = event.pageX; /* IF DIFFERENT CHECK event.pageX IS NOT NULL */ else this.posX = event.clientX; /* USE event.clientX IF IT IS */ } } else { /* IF PAGE IS SCROLLED RIGHT... */ if (event.clientX == event.pageX) this.posX = event.clientX; /* CATCHES OMNIWEB - BOTH VALUES INCLUDE SCROLL AMOUNT */ else this.posX = (event.clientX + shiftLeft); /* shiftLeft ADDED TO SCREEN COORD FOR MOST BROWSERS */ } if (shiftDown == 0) { if (event.clientY == event.pageY) this.posY = event.clientY; else { if (event.pageY) this.posY = event.pageY; /* FOR OLD BROWSERS (iCab) */ else this.posY = event.clientY; /* FOR IE */ } } else { if (event.clientY == event.pageY) this.posY = event.clientY; else this.posY = (event.clientY + shiftDown); } if (!this.posX || !this.posY) { alert("Can't get the cursor position, sorry."); return; } if (loupeVisible) moveBox(this.posX,this.posY); } function moveBox(coordX,coordY) { loupeDiv = document.getElementById("loupe"); if (coordX >= (loupePositionX + shiftLeft) && coordX <= (winWidth + shiftLeft + loupePositionX - scrollBarWidth - (loupeWidth + (2 * loupeBorder)))) { /* IF LOUPE IS WITHIN VIEWABLE WIDTH */ if (loupePositionX != loupeTargetX) { clearTimeout(loupeSlideX); slideLoupeX(coordX,loupePositionX,loupeTargetX); } else loupeDiv.style.left = (coordX - loupePositionX) + "px"; } else { if (loupeTargetX == loupeXoffset) loupeTargetX = -loupeOffset; else loupeTargetX = loupeXoffset; if (loupePositionX != loupeTargetX) { slideLoupeX(coordX,loupePositionX,loupeTargetX); } else loupeDiv.style.left = (coordX - loupeTargetX) + "px"; } if (coordY >= (loupePositionY + shiftDown) && coordY <= (winHeight + shiftDown + loupePositionY - scrollBarHeight - (loupeHeight + (2 * loupeBorder)))) { /* IF LOUPE IS WITHIN VIEWABLE HEIGHT */ if (loupePositionY != loupeTargetY) { clearTimeout(loupeSlideY); slideLoupeY(coordY,loupePositionY,loupeTargetY); } else loupeDiv.style.top = (coordY - loupePositionY) + "px"; } else { if (loupeTargetY == loupeYoffset) loupeTargetY = -loupeOffset; else loupeTargetY = loupeYoffset; if (loupePositionY != loupeTargetY) { slideLoupeY(coordY,loupePositionY,loupeTargetY); } else loupeDiv.style.top = (coordY - loupeTargetY) + "px"; } moveZoomage(coordX,coordY); } function moveZoomage(coordX,coordY) { picLeft = -Math.round(((coordX - thumbX) * magRatioW) + (magRatioW / 2)) + (loupeWidth / 2); picTop = -Math.round(((coordY - thumbY) * magRatioH) + (magRatioH / 2)) + (loupeHeight / 2); if (picLeft != lastPicLeft || picTop != lastPicTop) { adjScaleX = (picLeft - lastPicLeft) / magRatioW; adjScaleY = (picTop - lastPicTop) / magRatioH; for(adj=0;adj<=magRatioW;adj++) { newLeft = Math.round(adjScaleX * adj) + lastPicLeft; newTop = Math.round(adjScaleY * adj) + lastPicTop; picBox = document.getElementById("zoomage"); picBox.style.left = newLeft + "px"; picBox.style.top = newTop + "px"; } } lastPicLeft = picLeft; lastPicTop = picTop; } function slideLoupeX(coordX,currPosX,newPosX) { originX = coordX; if (currPosX != newPosX) { if (currPosX < newPosX) loupePositionX = currPosX + Math.ceil((newPosX - currPosX) / slideRate); else loupePositionX = currPosX - Math.ceil(Math.abs(newPosX - currPosX) / slideRate); loupeDiv.style.left = (originX - loupePositionX) + "px"; loupeSlideX = setTimeout("slideLoupeX(originX,loupePositionX,loupeTargetX)",10); } else return; } function slideLoupeY(coordY,currPosY,newPosY) { originY = coordY; if (currPosY != newPosY) { if (currPosY < newPosY) loupePositionY = currPosY + Math.ceil((newPosY - currPosY) / slideRate); else loupePositionY = currPosY - Math.ceil(Math.abs(newPosY - currPosY) / slideRate); loupeDiv.style.top = (originY - loupePositionY) + "px"; loupeSlideY = setTimeout("slideLoupeY(originY,loupePositionY,loupeTargetY)",10); } else return; }