$(document).ready(function(){
	$("body").click(function() {
		$(".objThumb").remove();
	});

	$(".showEnlargeArea").click(function () { 
		var scrollTop = $(window).scrollTop();

		$("#activeBig").attr("src","/toah/images/hb/hb_"+$(this).attr("rel")+".jpg");
		$("#altViewParent").css({ "top":scrollTop+10, "left":"75px" });
		if (totalAV == 0) {
			$("#altViewThumbs").css({ "width":"10px","visibility":"hidden" });
			$("#altViewParentContent").css({ "width":"700px" });
		} else if (totalAV > 6) {
			$("#altViewThumbs").css({ "width":"105px" });
		}
		$("#altViewThumbs li img").css({ "border":"solid 1px #666666" });
		$("#altViewThumbs li img").animate({opacity: "0.6"}, 100)
		$(".view_mainView").animate({opacity: "1.0"}, 100)
		
		setAVText(this.src);

		$("#altViewThumbs li img").removeClass("activeImg");
		$(".view_mainView").addClass("activeImg");
		
		$('#altViewParent').fadeIn(100);		
		var divCloser = $('<div id="wholePageClose" class="wholePageClose">&nbsp;</div>');
		$('body').append(divCloser);
		document.getElementById("wholePageClose").onclick=new Function("hideEnlargement()");
    });
    
    $(".wholePageClose").click(function () { 
		$('#altViewParent').fadeOut(100);
		$("#activeBig").attr("src","/toah/images/spacer.gif");
		$("#wholePageClose").remove();
    });
    
    $(".closeEnlargeArea").click(function () { 
		$('#altViewParent').fadeOut(100);
		$("#activeBig").attr("src","/toah/images/spacer.gif");
		$("#wholePageClose").remove();
    });
    
    $(".printEnlargement").click(function () { 
		var orig_div_cont=document.getElementById("altViewEnlarged").innerHTML;
		var myWin=window.open("","myWin","menubar,scrollbars,left=30px,top=40px,height=400px,width=680px");
		myWin.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Heilbrunn Timeline of Art History | The Metropolitan Museum of Art</title><script type="text/javascript">function printFunction(){var t=setTimeout("window.print()",3000);}</script><style>body { background:none; margin:0px; padding:0px; font-family:Arial,sans-serif; font-size:14px; } #footer { font-size:11px; margin-top:3px; } img { margin-bottom:20px; display:block; }</style></head><body onload="printFunction();"><div id="dest_div"></div><div id="footer">Copyright &copy; 2000&ndash;2008 The Metropolitan Museum of Art. All rights reserved.</body></html>');
		myWin.document.close();
		myWin.document.getElementById("dest_div").innerHTML+=orig_div_cont;
    });

	// THIS IS INITIAL SETUP OF MAIN THUMBS
	$("#EnlargeHolder li img").each(
		function () { 
			var objAccNo;
			var objAVNum;
			var determinedLength;
			var re1 = new RegExp('.*ht_(.*)([_~])(.*)\.jpg','ig');
			var re2 = new RegExp('.*ht_(.*)\.jpg','ig');
			objAccNo = $(this).attr("src");
			objAccNo = objAccNo.replace(re1,"$1")
			objAVNum = $(this).attr("src");
			objAVNum = objAVNum.replace(re1,"$3")
			determinedLength = $(this).attr("src");
			determinedLength = determinedLength.replace(re1,"$2")
			//determinedLength = determinedLength.replace("~","_")
			//alert(determinedLength);
			if (objAccNo == $(this).attr("src")) {
				$(this).addClass("mainView");
				objAccNo = objAccNo.replace(re2,"$1")
			} else {
				var temp = "view"+determinedLength+objAVNum;
				temp = temp.replace("~av","_av1")
				$(this).addClass(temp);
			}
			$(this).attr("rel",objAccNo);
		}
	);

	// THIS IS WHEN THE MAIN PAGE THUMBNAILS ARE CLICKED
	$("#EnlargeHolder li img").click(function () { 
		var fullSize = this.src.replace(/\/ht\/ht_/, '/hb/hb_' );
		var scrollTop = $(window).scrollTop();
	
		$("#activeBig").attr("src",fullSize);
		$("#altViewParent").css({ "top":scrollTop+10, "left":"75px" });
		if (totalAV == 0) {
			$("#altViewThumbs").css({ "width":"10px" });
			$("#altViewParentContent").css({ "width":"700px" });
		} else if (totalAV > 6) {
			$("#altViewThumbs").css({ "width":"105px" });
		}
		
		$("#altViewThumbs li img").css({ "border":"solid 1px #666666" });
		$("#altViewThumbs li img").animate({opacity: "0.6"}, 100)
		$("."+$(this).attr('class')).animate({opacity: "1.0"}, 100)

		$("#altViewThumbs li img").removeClass("activeImg");
		$("."+$(this).attr('class')).addClass("activeImg");
		
		setAVText(this.src);
		
		$('#altViewParent').fadeIn(100);		
		var divCloser = $('<div id="wholePageClose" class="wholePageClose">&nbsp;</div>');
		$('body').append(divCloser);
		document.getElementById("wholePageClose").onclick=new Function("hideEnlargement()");

		$(".objThumb").remove();
	});

	$("#EnlargeHolder li img").hoverIntent(showPreviewer,hidePreviewer);
	function showPreviewer() {
			$(".objThumb").remove();
			var divID = $(this).attr("class");
			divID = divID.replace(/mainView/, "")
			divID = divID.replace(/ /, "")
			var accessNo = $(this).attr("rel");
			var whichAV = divID.replace("view","");

			if (whichAV == "mainView") { whichAV = ""; }
			//alert(whichAV);
			$(this).after("<div id=\""+divID+"Hover\" class=\"objThumb\"></div>");
			whichAV = whichAV.replace(/mainView/, "")
			whichAV = whichAV.replace(/activeImg/, "")
			whichAV = whichAV.replace(/ /, "")
			if (whichAV != "") {
				$("#"+divID+"Hover").load("/toah/content/ho/preview/ho_"+accessNo+whichAV+".htm");
			} else {
				$("#"+divID+"Hover").load("/toah/content/ho/preview/ho_"+accessNo+".htm");
			}
			//ajaxpage('/toah/content/ho/preview/ho_'+accessNo+'.htm', divID+'Hover');

			var offset = $(this).offset();
			var winHeight = jQuery(window).height();
			var scrollTop = $(window).scrollTop();
			var tipHeight = jQuery(".objThumb").height();

			$(this).parent().css('zIndex','3000');

			var totalDivHeight = tipHeight + 230;
			var orientDiv = offset.top-scrollTop;
			if (orientDiv > (winHeight/2)) {
				$(".objThumb").css('marginTop',"-100px");
			} else {
				//$(".objThumb").css('marginTop',offset.top+20);
			}
			


	}
	function hidePreviewer() {
			var divID = $(this).attr("class");
			divID = divID.replace(/mainView/, "")
			divID = divID.replace(/ /, "")
			$(this).parent().css('zIndex','2000');
			$("#"+divID+"Hover").fadeOut(100);
			$("#"+divID+"Hover").remove();
	}
	




	// THIS IS THE INITIAL SETUP FOR POPUP THUMBS
	$("#altViewThumbs li img").each(
		function() {
			var objAccNo;
			var objAVNum;
			var determinedLength;
			var re1 = new RegExp('.*ht_(.*)([_~])(.*)\.jpg','ig');
			objAccNo = $(this).attr("src");
			objAccNo = objAccNo.replace(re1,"$1")
			objAVNum = $(this).attr("src");
			objAVNum = objAVNum.replace(re1,"$3")
			determinedLength = $(this).attr("src");
			determinedLength = determinedLength.replace(re1,"$2")

			if (objAccNo == $(this).attr("src")) {
				objAVNum = "_mainView";
			} else {
				var temp = determinedLength + objAVNum;
				temp = temp.replace("~av","_av1")
				objAVNum = temp;
			}
			$(this).addClass("view"+objAVNum);
		}
	);

	// THIS IS WHEN THE POPUP THUMBNAILS ARE HOVERED
	$("#altViewThumbs li img").hover(
		//alert($(this).css({opacity}));
		function () { 
			$(this).animate({opacity: "1.0"}, 100)
		},
		function () { 
			if (!($(this).hasClass("activeImg"))) {
				$(this).animate({opacity: "0.6"}, 100);
			}
		}
	
	);


	// THIS IS WHEN THE POPUP THUMBNAILS ARE CLICKED
	$("#altViewThumbs li img").click(function () { 
		var fullSize = this.src.replace(/\/ht\/ht_/, '/hb/hb_' );
		var scrollTop = $(window).scrollTop();
		$("#activeBig").attr("src",fullSize);
		$("#altViewThumbs li img").css({ "border":"solid 1px #666666" });
		$("#altViewThumbs li img").animate({opacity: "0.6"}, 100)
		$(this).animate({opacity: "1.0"}, 1)
		
		setAVText(this.src);

		$("#altViewThumbs li img").removeClass("activeImg");
		$(this).addClass("activeImg");
	});
	

	var leftColHeight = $("#objImage").height();
	var rightColHeight = $("#objInformationVertical").height();
	if (rightColHeight == "null") {
		rightColHeight = $("#objInformationHorizontal").height();
		if (leftColHeight > rightColHeight) {
			$("#objInformationHorizontal").height(leftColHeight);
		}
	} else {
		if (leftColHeight > rightColHeight) {
			$("#objInformationVertical").height(leftColHeight);
		}
	}
    
    
    $(".showPDF").click(function(){
    	$(this).parent().children(".pdfList").toggle();
    })
    
});


function setAVText(whichImage) {
	var objAccNo;
	var objAVNum;
	var determinedLength;
	var re1 = new RegExp('.*ht_(.*)([_~])(.*)\.jpg','ig');
	objAccNo = whichImage;
	objAccNo = objAccNo.replace(re1,"$1")
	objAVNum = whichImage;
	objAVNum = objAVNum.replace(re1,"$3")
	determinedLength = whichImage;
	determinedLength = determinedLength.replace(re1,"$2")

	$("#altViewEnlarged li").hide();

	if (objAccNo == whichImage) {
		//objAVNum = "mainView";
	} else {
		var temp = determinedLength + objAVNum;
		temp = temp.replace("~av","_av1")
		$("#tombText"+temp).show();
	}

	$("#tombText_mainView").show();
	
}

function hideEnlargement() {
	$('#altViewParent').fadeOut(100);
	$("#activeBig").attr("src","/toah/images/spacer.gif");
	$("#wholePageClose").remove();
}





function previewObj(accessNo,whichID) {
	var temp = eval(whichID);
	if (temp < 10) {
		temp = "_av" + temp;
	}
	if (temp == 10) { temp = "~av0"; }
	if (temp == 11) { temp = "~av1"; }
	if (temp == 12) { temp = "~av2"; }
	if (temp == 13) { temp = "~av3"; }
	if (temp == 14) { temp = "~av4"; }
	if (temp == 15) { temp = "~av5"; }
	if (temp == 16) { temp = "~av6"; }
	if (temp == 17) { temp = "~av7"; }
	if (temp == 18) { temp = "~av8"; }
	if (temp == 19) { temp = "~av9"; }
	if (whichID == "0") {
		$("#objThumbEnlarge"+whichID+" img").attr('src','/toah/images/h3/h3_'+accessNo+'.jpg')
	} else {
		$("#objThumbEnlarge"+whichID+" img").attr('src','/toah/tmp/h3test/hb_'+accessNo+temp+'.jpg.jpg')
	}
	//document.getElementById('objThumbEnlarge'+whichID).style.display = "block";
	$('#objThumbEnlarge'+whichID).fadeIn(100);
}

function hideObj(accessNo,whichID) {
	//document.getElementById('objThumbEnlarge'+whichID).style.display = "none";
	$('#objThumbEnlarge'+whichID).fadeOut(100);
}

function makeObjectsActive(whichItem) {
	if (totalAV > 0) {
		// First make all invisible //
		document.getElementById('av0').style.border = "solid 1px #666";
		document.getElementById('av0').style.marginLeft = "0px";
		document.getElementById('av0').style.marginTop = "0px";
		for (i=1;i<(totalAV+1);i++) 
		{
			document.getElementById('tombTextAlt'+i).style.display = "none";
			document.getElementById('av'+i).style.border = "solid 1px #666";
			document.getElementById('av'+i).style.marginLeft = "0px";
			document.getElementById('av'+i).style.marginTop = "0px";
		}	
			// Now make the appropriate text space visible if it's not null //
		document.getElementById('tombTextAlt0').style.display = "block";
		document.getElementById('tombTextAlt'+whichItem).style.display = "block";
		document.getElementById('av'+whichItem).style.border = "solid 2px #444";
		document.getElementById('av'+whichItem).style.marginLeft = "-1px";
		document.getElementById('av'+whichItem).style.marginTop = "-1px";
		if (whichItem != 0) {
			//document.getElementById('av'+whichItem).style.marginTop = "0px";
		}
		for (x in replaceArray)
		{
			if (replaceArray[x] == whichItem) {
				document.getElementById('tombTextAlt0').style.display = "none";
			}
		}				
	}
}
	
	function toggleEnlarge(xxx,yyy,whichItem) {
		var el = document.getElementById(xxx,whichItem);
			document.getElementById('activeBig').src='/toah/images/hb/hb_'+yyy+'.jpg';
			document.getElementById("altViewParent").style.top = "15px";
			document.getElementById("altViewParent").style.left = "75px";
			if (totalAV == 0) {
				document.getElementById("altViewThumbs").style.width = "10px";
				document.getElementById("altViewParentContent").style.width = "700px";
			} else if (totalAV > 6) {
				document.getElementById("altViewThumbs").style.width = "105px";
			}
			el.style.display = 'block';
		makeObjectsActive(whichItem);
		var newdiv = document.createElement('div');
		newdiv.id = "wholePageClose";
		newdiv.style.width = "100%";
		newdiv.style.height = "100%";
		newdiv.style.position = "absolute";
		newdiv.style.left = 0;
		newdiv.style.top = 0;
		
		newdiv.style.zIndex = 1;  
		document.body.appendChild(newdiv);
		document.getElementById("wholePageClose").onclick=new Function("hideEnlargement()");
	}

	function toggleEnlargeThematic(xxx,yyy,whichItem) {
		var el = document.getElementById(xxx);
			document.getElementById('activeBig').src='/toah/images/hb/hb_'+yyy+'.jpg';
			document.getElementById("altViewParent").style.top = "15px";
			document.getElementById("altViewParent").style.left = "75px";
			el.style.display = 'block';
	
		makeObjectsActive(whichItem);
		var newdiv = document.createElement('div');
		newdiv.id = "wholePageClose";
		newdiv.style.width = "100%";
		newdiv.style.height = "100%";
		newdiv.style.position = "absolute";
		newdiv.style.left = 0;
		newdiv.style.top = 0;
		
		newdiv.style.zIndex = 1;  
		document.body.appendChild(newdiv);
		document.getElementById("wholePageClose").onclick=new Function("hideEnlargement()");
	}

	function toggleEnlargedView(accessNo,whichItem) {
		var el = document.getElementById('activeBig');
			el.src='/toah/images/hb/hb_'+accessNo+'.jpg';
	
		makeObjectsActive(whichItem);
	}

	function toggleEnlargedViewThematic(accessNo,whichItem) {
		var el = document.getElementById('activeBig');
			el.src='/toah/images/hb/hb_'+accessNo+'.jpg';
	
		makeObjectsActive(whichItem);
	}

	
//#### Handle the shadows ####//
var gradientshadow={}
gradientshadow.depth=6 //Depth of shadow in pixels
gradientshadow.containers=[]

gradientshadow.create=function(){
var a = document.all ? document.all : document.getElementsByTagName('*')
for (var i = 0;i < a.length;i++) {
	if (a[i].className == "shadow") {
		for (var x=0; x<gradientshadow.depth; x++){
			var newSd = document.createElement("DIV")
			newSd.className = "shadow_inner"
			newSd.id="shadow"+gradientshadow.containers.length+"_"+x //Each shadow DIV has an id of "shadowL_X" (L=index of target element, X=index of shadow (depth) 
			if (a[i].getAttribute("rel"))
				newSd.style.background = a[i].getAttribute("rel")
			else
				newSd.style.background = "black" //default shadow color if none specified
			document.body.appendChild(newSd)
		}
	gradientshadow.containers[gradientshadow.containers.length]=a[i]
	}
}
gradientshadow.position()
window.onresize=function(){
	gradientshadow.position()
}
}

gradientshadow.position=function(){
if (gradientshadow.containers.length>0){
	for (var i=0; i<gradientshadow.containers.length; i++){
		for (var x=0; x<gradientshadow.depth; x++){
  		var shadowdiv=document.getElementById("shadow"+i+"_"+x)
			shadowdiv.style.width = gradientshadow.containers[i].offsetWidth + "px"
			shadowdiv.style.height = gradientshadow.containers[i].offsetHeight + "px"
			shadowdiv.style.left = gradientshadow.containers[i].offsetLeft + x + "px"
			shadowdiv.style.top = gradientshadow.containers[i].offsetTop + x + "px"
		}
	}
}
}

if (window.addEventListener)
window.addEventListener("load", gradientshadow.create, false)
else if (window.attachEvent)
window.attachEvent("onload", gradientshadow.create)
else if (document.getElementById)
window.onload=gradientshadow.create


/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 * Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
	minY = 0;
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

