$(document).ready(function() {
    $(".mg_zoom").click(function() {
    	var figNum = $(this).children(".dFig").text();
    	$('#pop_dFig').text(figNum);
    	var bregmaVal = $(this).children(".dBregma").text();
    	if(bregmaVal != "") {
    	    $('#pop_dBregma').text("Bregma " +bregmaVal+" mm");
    	    $('#pop_dInter').text("Interaural " + $(this).children(".dInter").text()+" mm");
    	}
    	else {
    	    $('#pop_dBregma').text("");    	
    		$('#pop_dInter').text("Lateral " + $(this).children(".dLateral").text()+" mm");
    	}
    	
        $('#shdw1').hide();
        $('#shdw2').hide();
        $('#shdw3').hide();
        $('#shdw4').hide();
        $('#shdw5').hide();
        $('#browse_popup_outer').hide();
        $('#popup_image_wrap').css('visibility','hidden');
            	
		var linkURL = $('#link_slice_'+figNum).attr('href');
		
		var imgURL = $('#image_slice_'+figNum).attr('src');
		$('#popup_image_img').attr('src',imgURL+'&size=med');
		
		var highlight= $(this).children(".dHighlight").text();
			$('#popup_image_img_highlight').attr('src',imgURL+'&size=med&highlight='+highlight);

		$('#popup_detailed_link').attr('href',linkURL);
		        		
		var popElem = document.getElementById("browse_popup_outer");
		var offset = -365; // half the width of the popup
		
		centerElement(popElem,offset++);
		// Make sure we didn't end up off the page
		var top = parseInt(popElem.style.top);
		var left = parseInt(popElem.style.left);
		var topOffset = 0, leftOffset = 0;
		if(top < 0) {
			topOffset = -1 * top;
			popElem.style.top = "0px";
		} else if(left < 0) {
			leftOffset = -1 * left;
			popElem.style.left = "0px";
		}
		
		var shdwElem = document.getElementById("shdw1");
		centerElement(shdwElem,offset++);
		shdwElem.style.top = parseInt(shdwElem.style.top) + topOffset + "px";
		shdwElem.style.left = parseInt(shdwElem.style.left) + leftOffset + "px";
		shdwElem = document.getElementById("shdw2");
		centerElement(shdwElem,offset++);
		shdwElem.style.top = parseInt(shdwElem.style.top) + topOffset + "px";
		shdwElem.style.left = parseInt(shdwElem.style.left) + leftOffset + "px";
		shdwElem = document.getElementById("shdw3");
		centerElement(shdwElem,offset++);
		shdwElem.style.top = parseInt(shdwElem.style.top) + topOffset + "px";
		shdwElem.style.left = parseInt(shdwElem.style.left) + leftOffset + "px";
		shdwElem = document.getElementById("shdw4");
		centerElement(shdwElem,offset++);
		shdwElem.style.top = parseInt(shdwElem.style.top) + topOffset + "px";
		shdwElem.style.left = parseInt(shdwElem.style.left) + leftOffset + "px";
		shdwElem = document.getElementById("shdw5");
		centerElement(shdwElem,offset++);
		shdwElem.style.top = parseInt(shdwElem.style.top) + topOffset + "px";
		shdwElem.style.left = parseInt(shdwElem.style.left) + leftOffset + "px";

        $('#browse_popup_outer').show();  
        $('#shdw1').show();
        $('#shdw2').show();
        $('#shdw3').show();
        $('#shdw4').show();
        $('#shdw5').show();
        			
        setTimeout("showPopupImg()",300);
        return false;
    })
    
    $("#bwPopupClose").click(function() {
        $('#shdw1').hide();
        $('#shdw2').hide();
        $('#shdw3').hide();
        $('#shdw4').hide();
        $('#shdw5').hide();
        $('#browse_popup_outer').hide();
        return false;
    })
    
    function getViewportDimensions() {
		var intH = 0, intW = 0;
        if(self.innerHeight) {
			intH = window.innerHeight;
			intW = window.innerWidth;
		} 
		else {
			if(document.documentElement && document.documentElement.clientHeight) {
				intH = document.documentElement.clientHeight;
				intW = document.documentElement.clientWidth;
			}
			else {
				if(document.body) {
					intH = document.body.clientHeight;
					intW = document.body.clientWidth;
				}
			}
		}

		return {
 			height: parseInt(intH, 10),
			width: parseInt(intW, 10)
		}
	}
    
    function getScrollXY() {
		var scrOfX = 0, scrOfY = 0;
		if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		return [ scrOfX, scrOfY ];
	}
    
    function centerElement(elem,add) {
    	if(elem == null) { return; }
		var viewport = getViewportDimensions();
		var left = (viewport.width == 0) ? 50 : parseInt((viewport.width - elem.offsetWidth) / 2, 10);
		var top = (viewport.height == 0) ? 50 : parseInt((viewport.height - elem.offsetHeight) / 2, 10);
		var scroll = getScrollXY();
		elem.style.left = (left + add) + 'px';
        elem.style.top = (top + add + scroll[1]) + 'px';
    }
});

function showPopupImg() {
  $('#popup_image_wrap').css('visibility','visible');
}