var currentmenu, currentartistmenu, currentArtist, currentThumb, jdebug = false, imageScroll;
var imgLeftStart, imgLeftEnd, menuTopStart, menuTopEnd, menuBkgTopStart, menuBkgTopEnd, animStep=0, animSteps=40;
menus = new Array();
menus['works'] = new Array('postwar', 'modern');
initFlag = false;

function menu(which) {
	if (currentmenu == 'home') {
		getRawObject('home-menu').innerHTML = '<a href="javascript:void(\'\');" onmouseup="javascript:menu(this);" id="home" class="unselected"><img src="images/logo.gif" alt="" width="98" height="39" /></a>';
	} else {
		if (getRawObject(currentmenu + '-menu') != null) {
		var obj = getRawObject(currentmenu + '-menu');
		obj.innerHTML = '<a href="javascript:void(\'\');" onmouseup="javascript:menu(this);" id="' + currentmenu + '" class="unselected">' + currentmenu + '</a>';
		}
	}
	currentmenu = which.id;
	var obj = getRawObject(currentmenu + '-menu');
	if (currentmenu == 'home') {
		obj.innerHTML = '<img src="images/logo.gif" alt="" width="98" height="39" />';
	} else {
		obj.innerHTML = currentmenu;
	}
	var obj = getRawObject('content');
//	obj.style.display = "block";
	obj.innerHTML = '<p>loading...</p>';
	theURL = 'pages/' + currentmenu + '.php';
	AJAXcallback = 'showContent';
	makeHttpRequest(theURL, AJAXcallback, false);
	return;
	if (!initFlag) {
		unsetDisplay("frontpagemenu_bkg");
		unsetDisplay("frontpageimage");
		getRawObject('menutopline').innerHTML = ' --------------------';
		initFlag = true;
	}
	if (!pageLoaded) { return; }
	if (which.id == null) {//this makes it so that it can be called by name as well as id
		id = which;
	} else {
		id = which.id;
	}
	//we're making this script do triple-duty for the main menu, the exhibitions menu, and the artist's menu
	artistmenuflag = false;
	//console.log(id);
	if (id.substr(0,10) == 'artistpage' || id.substr(0,14) == 'exhibitionpage') {
		artistmenuflag = true;
	}
	if (artistmenuflag) {
		theMenu = currentartistmenu;
		AJAXcallback = 'showArtistContent';
		var obj = getRawObject('col4');
		obj.style.display = "block";
		obj.innerHTML = '<p>loading...</p>';
	} else {
		theMenu = currentmenu;
//		console.log(id);
		AJAXcallback = 'showContent';
		if (id == 'news' || id == 'pubs' || id == 'artistindex' || id == 'exhibitions' || id == 'exhibitions_current') {
			var obj = getRawObject('col3');
			obj.style.display = "block";
			obj.innerHTML = '<p>loading...</p>';
		}
	}
//	console.log(id);
	arr = id.split('_');
	main = arr[0];
	sub = arr[1];
	subsub = arr[2];
	if (typeof(sub) === 'undefined') {
		sub = '';
	}
	if (typeof(subsub) === 'undefined') {
		subsub = '';
	}
	if (theMenu) {
		carr = theMenu.split('_');
		cmain = carr[0];
		csub = carr[1];
		csubsub = carr[2];
		if (csubsub && csubsub != subsub) {
			if (getRawObject(theMenu) != null) {
				changeClass(theMenu, 'unselected');
			}
		}
		if (csub && csub != sub) {
			changeClass(cmain + "_" + csub, 'unselected');
			if (csubsub) {
				unsetDisplay(cmain + "_" + csub + "_0");
			}
		}
		if (cmain != main) {
			changeClass(cmain, 'unselected');
			if (getRawObject(cmain + "_0") != null) {
				unsetDisplay(cmain + "_0");
			}
		}
	}
	changeClass(id, 'selected');
	if (getRawObject(id + "_0") != null) {
		setDisplay(id + "_0");
		if (artistmenuflag) {
			//in the unlikely event that an artist has a bio but not work, switch to bio page.
			if (getRawObject(id + "_work") != null) {
				newMenu = id + "_work";
			} else {
				newMenu = id + "_bio";
			}
		} else {
			newMenu = id + "_" + menus[id][0];
		}
		//alert(id);
		changeClass(newMenu, 'selected');
	} else {
		newMenu = id;
	}
	if (artistmenuflag) {
		currentartistmenu = newMenu;
	} else {
		currentartistmenu = '';//reset artists menu, since it's no longer valid
		currentmenu = newMenu;
	}
	if (getRawObject(newMenu + "_0") == null) {//no more submenus, so display content
		arr = newMenu.split('_');
		if (typeof(arr[1]) === 'undefined') {
			arr[1] = 0;
		}
		if (typeof(arr[2]) === 'undefined') {
			arr[2] = 0;
		}
		theURL = 'pages/' + arr[0] + '.php?s=' + arr[1] + '&ss=' + arr[2];
		makeHttpRequest(theURL, AJAXcallback, false);
	}
	return;
}

function showContent(theHTML) {
	var obj = getRawObject('content');
	obj.innerHTML = theHTML;
	return;
	if (currentmenu.substr(0,5) == 'works' || currentmenu == 'artists' || currentmenu == 'editions' || (currentmenu.substr(0,11) == 'exhibitions' && currentmenu != 'exhibitions_upcoming')) {
		getObject("col3").width= '140px';
	} else {
		getObject("col3").width= '715px';
	}
	var obj = getRawObject('col3');
	obj.style.display = "block";
	obj.innerHTML = theHTML;
	var obj = getRawObject('col4');
	obj.style.display = "none";
	if (currentmenu.substr(0,11) == 'exhibitions') {
		var exCount = getRawObject('exCount');
		if (exCount.innerHTML < 2) {
			var exKey = getRawObject('exKey');
			var obj = getRawObject('col3');
			obj.style.display = "none";
			theURL = 'pages/exhibitionpage.php?s=' + exKey.innerHTML;
			makeHttpRequest(theURL, 'showArtistContent', false);
		}
	}
	mudBlur();
}

function showIndexContent(theHTML) {
	showContent(theHTML);
	menu('artistpage_' + currentArtist);
}

function showImages(pagekey,imgkey,artistkey) {
	theURL = 'includes/img_overlay.php?pagekey=' + pagekey + '&imgkey=' + imgkey + '&artistkey=' + artistkey;
	makeHttpRequest(theURL, 'showOverlay', false);
}

function showOverlay(theHTML) {
	var obj = getRawObject('imgOverlay');
	obj.style.display = "block";
	obj.innerHTML = theHTML;
	getRawObject('veil').style.width = getBrowserSize()[0] + "px";
	getRawObject('veil').style.height = getBrowserSize()[1] + "px";
}

function hideOverlay() {
	var obj = getRawObject('imgOverlay');
	obj.style.display = "none";
	obj.innerHTML = '';
}

function showArtistContent(theHTML) {
	var obj = getRawObject('col4');
	obj.style.display = "block";
	obj.innerHTML = theHTML;
	var imgsToLoad = getRawObject('imgsToLoad');
	var largeImgsToLoad = getRawObject('largeImgsToLoad');
	if (imgsToLoad.innerHTML >= 0) {
		currentThumb = parseInt(imgsToLoad.innerHTML);
		mrg_imgs.seek(imgsToLoad.innerHTML);
		mrg_thumbs.seek(imgsToLoad.innerHTML);
		mrg_thumbs_ro.seek(imgsToLoad.innerHTML);
		if (getRawObject('scroll_holder') != null) {//this is for thumb scroll
			initScrollLayers();
		}
		if (getRawObject('imageSCContainer') != null) {//this is for img scroll
			imageScroll = new jb_scroller('image', 407, 305, 460, 0, 0); // id, width, height, speed, initLeft, initTop
			imageScroll.load();
		}
	}
	if (largeImgsToLoad.innerHTML >= 0) {
		mrg_lg_imgs.seek(largeImgsToLoad.innerHTML);
	}
	mudBlur();
}

function showArtist(key, cat) {//coming from artist index page
	currentArtist = key;
	switch (cat) {
		case 1:
			theURL = 'pages/works.php?ss=' + key;
			menu('works');
			menu('works_postwar');
		break;
		case 2:
			theURL = 'pages/works.php?s=modern&ss=' + key;
			menu('works');
			menu('works_modern');
		break;
		case 3:
			menu('artists');
			theURL = 'pages/artists.php?ss=' + key;
		break;
	}
	makeHttpRequest(theURL, 'showIndexContent', false);
}

function mrg_getPathForImgKey(which) {
//	alert(typeof(getRawObject('imgcaption_' + which)) + " " + which);
	var obj = getRawObject('imgcaption_' + which);
	if (obj==null) {
		return "inline_2lines";
	}
	h = obj.offsetHeight;
	if (h > 26) {
		return "inline_3lines";
	} else {
//		alert("2lines " + h);
		return "inline_2lines";
	}
}

function initScrollLayers() {
	// arguments: id of layer containing scrolling layers (clipped layer), id of layer to scroll, 
	// if horizontal scrolling, id of element containing scrolling content (table?)
	var wndo1 = new scroll_scrollObj('scroll_win', 'scroll_layer', "thumbscroll");
	
	// arguments: dragBar id, track id, axis ("v" or "h"), x offset, y offset
	// (x/y offsets of dragBar in track)
	wndo1.setUpScrollbar("dragBar1", "track1", "h", 0, 0);
	
	scroll_showLayers("scrollbar1");
}

function scroll_showLayers() {
	if ( document.getElementById ) {
		var lyr, i;
		for (i=0; arguments[i]; i++) {
			lyr = document.getElementById( arguments[i] );
			lyr.style.visibility = "visible";
		}
	}
}

function scrollMedTo(x) {
	imageScroll.smoothScrollTo(x,0,.4);
}

function showBig() {
	thumbon = getRawObject("thumbimg_" + currentThumb);
	big_src = thumbon.src.replace(/thumb_rollover\/.*/, "expanded/" + mrg_imgs.getImgnameForIndex(currentThumb));
	obj = getRawObject('expanded_image_bkg');
	obj.style.visibility = "visible";
	obj = getRawObject('expanded_image');
	obj.style.visibility = "visible";
	obj.innerHTML = '<img src="' + big_src + '" id="expandedimage" /><br /><a href="javascript:hideBig()" class="enlarge">-- MINIMIZE</a>';
}

function hideBig() {
	obj = getRawObject('expanded_image_bkg');
	obj.style.visibility = "hidden";
	obj = getRawObject('expanded_image');
	obj.style.visibility = "hidden";
}

function switchThumb(which) {
	if (currentThumb == which) { return; }
	thumbon = getRawObject("thumbimg_" + currentThumb);
	thumboff = getRawObject("thumbimg_" + which);
	src_off = thumbon.src.replace(/thumb_rollover\/.*/, "thumb/" + mrg_imgs.getImgnameForIndex(currentThumb));
	src_on = thumboff.src.replace(/thumb\/.*/, "thumb_rollover/" + mrg_imgs.getImgnameForIndex(which));
	thumboff.src = src_on;
	thumbon.src = src_off;
	currentThumb = which;
	mrg_imgs.seek(which);
	mrg_thumbs.seek(which);
	mrg_thumbs_ro.seek(which);
}

function mrg_imageLoaded(which) {//loading graphic is centered, loaded image shouldn't be
	getObject(which).marginLeft = "0px";
	getObject(which).marginTop = "0px";
}

function introAnimInit() {
	setDisplay("frontpagemenu");
	imgLeftStart = 0 - getRawObject('frontpageimage').offsetWidth;
	imgLeftEnd = getRawObject('col2').offsetLeft + 5;
	menuBkgHeight = getRawObject('col2').offsetHeight - 8;
	menuTopStart = -7 - menuBkgHeight;
	menuBkgTopStart = menuTopStart - 5;
	menuTopEnd = 5;
	menuBkgTopEnd = 14;
	getRawObject('frontpageimage').style.left = imgLeftStart + "px";
	getRawObject('frontpagemenu_bkg').style.top = menuBkgTopStart + "px";
	getRawObject('frontpagemenu_bkg').style.left = imgLeftEnd + "px";
	getRawObject('frontpagemenu_bkg').style.width = getRawObject('col2').offsetWidth + "px";
	getRawObject('frontpagemenu_bkg').style.height = menuBkgHeight + "px";
	getRawObject('frontpagemenu').style.left = imgLeftEnd + "px";
	getRawObject('frontpagemenu').style.top = menuTopStart + "px";
	introAnim();
}

function introAnim() {
	animStep++;
	increment = animStep/animSteps;
	getRawObject('frontpageimage').style.left = imgLeftStart + (imgLeftEnd - imgLeftStart) * increment + "px";
	getRawObject('frontpagemenu_bkg').style.top = menuBkgTopStart + (menuBkgTopEnd - menuBkgTopStart) * increment + "px";
	getRawObject('frontpagemenu').style.top = menuTopStart + (menuTopEnd - menuTopStart) * increment + "px";
	if (animStep < animSteps) {
		setTimeout('introAnim()',40);
	} else {
		showLayer("col2");
		unsetDisplay("frontpagemenu");
	}
}

function letsDoThatAgainPlease() {
	if (typeof(currentmenu) === 'undefined') {
		return;
	} else {
		carr = currentmenu.split('_');
		cmain = carr[0];
		csub = carr[1];
		changeClass(cmain, 'unselected');
		if (csub) {
			changeClass(cmain + "_" + csub, 'unselected');
		}
		if (getRawObject(cmain + "_0") != null) {
			unsetDisplay(cmain + "_0");
		}
	}
	if (currentartistmenu != '') {
		changeClass(currentartistmenu, 'unselected');
	}
	hideLayer("col2");
	getRawObject('col3').innerHTML = '';
	getRawObject('col4').innerHTML = '';
	getRawObject('frontpageimage').style.left = "-871px";
	animStep=0;
	currentmenu = '';
	currentartistmenu = '';
	initFlag = false;//true after any menu's clicked: use to hide first big image & menu bkg.
	setDisplay("frontpagemenu_bkg");
	setDisplay("frontpageimage");
	introAnimInit();
}

function addToMailingList() {
	email = document.mailing.mailinglist.value;
	if (!email || !emailCheck(email)) {
		return false;
	}
	makeHttpRequest('includes/mailingListAdd.php?email=' + email, 'mailingListAdd', false);
}

function mailingListAdd() {
	document.mailing.mailinglist.value = 'THANK YOU!';
}