﻿$(document).ready(function() {
    $(".zoom-image").each(function() {

		//get the <dl> parent to find the sibling with 'big-image' class


    $(this).mouseover(function() {
    var parent = $(this).parent().parent("dl");
    var bigImg = parent.children(".big-image-container");
    var imageContainer = bigImg.find("img");
    var image = imageContainer.attr("alt");
    imageContainer.attr("src", image);

    //remove alt after load
    //imageContainer.attr("alt", "");

			//not in use?
// 			if($(this).imageUrl == null) {
// 				$(this).imageUrl = image;
// 			}
// 			else {
// 				image = $(this).imageUrl;
// 			}

			//set position of "popup"
			var topOffset = 0;
			var margin = 5;
			var marginBottom = 10;
			var headerOffset = 55;
			var top = Math.round(parent.position().top);

			//Check for clipping caused by window out bounds. 
			var scrolltop = window.pageYOffset;
			var bottom = $(window).height();
			var bottomOffset = scrolltop + bottom;
			var heightOffset = top + 250;

			if ((top + headerOffset) < scrolltop) {
				//top part of (or all of) popupwindow will be outside of window bounds, adjusting
				topOffset = scrolltop + margin;
			}
			else if (bottomOffset < heightOffset) {
				//bottom part of (or all of) popupwindow will be outside of window bounds, adjusting
				var delta = (heightOffset - bottomOffset) + marginBottom;
				topOffset = top - delta;
			}
			else { //not out of bounds, but adjust for heading
				topOffset = top + headerOffset;
			}

			//set top and left position for the big image block.
			bigImg.css({
				"top": topOffset + "px",
				"left": "320px",
				"display": "block"
			});
}).mouseout(function() {
var parent = $(this).parent().parent("dl");
var bigImg = parent.children(".big-image-container");
			bigImg.css("display", "none");
        });
    });
});
