// JavaScript Document

window.addEvent('domready', function() {
	//alert('domready');

	$$('.menu-item').addEvents({
		'mouseenter': function(e) {
			var target = $(e.target);
			//console.log(target);
			if(!target.hasClass("menu-item")) {
				target = target.getParent('.menu-item');
			}
			var menu = target.id;
			//console.log(menu);
	
			//alert(showMenu);
			var showMenu = document.getElementById(menu);
			var menuLeft = showMenu.offsetLeft;
			var menuTop = showMenu.offsetHeight + showMenu.offsetTop;
			target.getElements('.title').each(function(title){
				//console.log(title);
				title.style.color='#9a0000';
			});
//			showMenu.style.color="#9a0000";
//			showMenu.style.backgroundColor="#333333";
			showMenu.style.backgroundColor="#ffffff";

			//console.log(menuLeft);
			//console.log(menuTop);
			//get dd1
			target.getElements('.ddmenu').each(function(ddMenu){
				//console.log(ddMenu);
				//ddMenu.style.top= menuTop+'px';
				//ddMenu.style.left=menuLeft+'px';
				ddMenu.style.display='';
			});


//		menu.style.backgroundColor='#ffffff';
						
//			var ddMenu = document.getElementById('dd1');
			
//			ddMenu.style.top= menuTop+'px';
//			ddMenu.style.left=menuLeft+'px';
//			ddMenu.style.display='';

		},
		'mouseleave': function(e){
			var target = $(e.target);
			if(!target.hasClass("menu-item")) {
				target = target.getParent('.menu-item');
			}

			
			target.getElements('.ddmenu').each(function(ddMenu){
				ddMenu.style.display='none';
			});


			var menu = target.id;
			//console.log(menu);
	
			//alert(showMenu);
			var showMenu = document.getElementById(menu);

			target.getElements('.title').each(function(title){
				//console.log(title);
				title.style.color='#ffffff';
			});
//			showMenu.style.color="#9a0000";
			showMenu.style.backgroundColor="#9a0000";
			
			//console.log("exit!");
			//var ddMenu = $('dd1');
			//var fx = new Fx.Slide(ddMenu);
			//fx.hide();
			//ddMenu.style.display='none';
		}
	});
	$$('.ddmenu').each(function(ddmenu){
		//var fx = new Fx.Slide(ddmenu);
		//fx.hide();
		//ddmenu.style.display='none';
	});

	$$('.ddmenu-item').addEvents({
		'mouseenter': function(e) {

			var target = $(e.target);
			//console.log(target);
			if(!target.hasClass("ddmenu-item")) {
				target = target.getParent('.ddmenu-item');
			}
			var dditem = target.id;
			var menuitem = document.getElementById(dditem);
			menuitem.style.backgroundColor='#b7d8fb';
	},
		'mouseleave': function(e){
			var target = $(e.target);
			//console.log(target);
			if(!target.hasClass("ddmenu-item")) {
				target = target.getParent('.ddmenu-item');
			}
			var dditem = target.id;
			var menuitem = document.getElementById(dditem);
			menuitem.style.backgroundColor='#ffffff';

	}
	});
/*
	var imgList1=['url(ill1.jpg)','url(ill2.jpg)','url(ill3.jpg)'];
	var imgCount1 = 3;
	var imgList2=['url(amica1.jpg)','url(amica2.jpg)','url(amica3.jpg)','url(amica4.jpg)']
	var imgCount2 = 4;
	
	var i = 0;
	var j = 0;
	
	var slideShowFunc = function(){
		//alert(this.imgList[1]);
		//alert(this.imgCount);
		//alert(this.counter);
		//this.counter++;
		var effect = new Fx.Morph(this.element,{duration: 'long', transition: Fx.Transitions.Sine.easeOut});
		
		if (this.counter < (this.imgCount - 1)){
			effect.start({'opacity':[0,1],
						  'background-image':[this.imgList[this.counter+1]]
						 });
		}
		else{
			effect.start({'opacity':[0,1],
						  'background-image':[this.imgList[0]]
						 });
		}
		
		this.counter++;
		if (this.counter > (this.imgCount-1))this.counter=0;
		//alert('2'+this.counter);
	};
	
	var showSlideShow1 = { element: 'testImg',
						   imgList : imgList1,
						   imgCount : imgCount1,
						   counter: i};
	
	slideShowFunc.periodical(5000,showSlideShow1);
	
	var showSlideShow2= { element: 'testImg2',
						  imgList: imgList2,
						  imgCount: imgCount2,
						  counter: j};
	var	slideShowFunc2 = function(){
		slideShowFunc.periodical(5000,showSlideShow2);
	};
	
	slideShowFunc2.delay(2000);


	$$('.showPopupBlock').addEvent('click',function(e){
		e.stop();
		var x = document.body.clientWidth;
		var y = document.body.clientHeight;

		$('infoDisplay').style.width = x +'px';
		$('infoDisplay').style.height = y +'px';
		$('infoDisplay').style.display = '';
		
		var mid = x / 2 - 250;


		$('blueBox').style.top = '50px';
		$('blueBox').style.left = mid+'px';
		$('blueBox').style.display = '';
	});
	
	$$('closeInfo').addEvent('click',function(e){
		e.stop();
		$('blueBox').style.display='none';
		$('infoDisplay').style.display='none';
	});
*/
});


function setSearchType(type){
	var x = document.getElementById("searchtype");
	switch(type){
		case 0:
			x.value="X";
			break;
		case 1:
			x.value="t";
			break;
		case 2:
			x.value="a";
			break;
		case 3:
			x.value="d";
			break;

	}
}
/*
function breadcrumbs(){
	var loc = window.location.toString()
	var start = loc.search(/newlib.academyart.edu/);
	var path=loc.substr(start+22);
	var breadcrumbs = "";
	//alert(path);
	
	document.write("<ul>");
	while(path.search(/\//) >= 0){
		slashLoc = path.search(/\//);
		path1=path.substring(0,slashLoc);
		var showLocation=showLoc(path1);
		document.write("<li>"+showLocation+"</li>");
		breadcrumbs=breadcrumbs+' '+path1;
		//alert(breadcrumbs);
		path=path.substr(slashLoc+1);
		//alert(path);
		//breadcrumbs=breadcrumbs
	}
	if (path.search(/index.html/) < 0){
		alert('no index');
	}
	else{
		alert('index');
	}
	//alert(breadcrumbs+' '+document.title);
	document.write("<li>"+document.title+"</li>");
	document.write("</ul>");
	//alert(window.location.toString());
}

function showLoc(loc){
	var newloc = loc.replace(/-/, " ");
	var newloc = newloc.replace(/_/, " ");
	var returnloc = '';
	returnloc=newloc;
	returnloc = returnloc.substr(0, 1).toUpperCase() + returnloc.substr(1);

	while(newloc.search(/ /) >= 0){
		spaceIndex = newloc.search(/ /);
		returnloc = returnloc.substr(spaceIndex+1, 1).toUpperCase() + returnloc.substr(soaceIndex+2);
		newloc = newloc.substr(spaceIndex);
	}

	return returnloc;
}
*/
//NEED TO COMMENT HEAVILY!!!!!!!
function breadcrumbs(){
	var url;
	var crumbArray = new Array();
	var urlArray = new Array();
	url = window.location.toString();
	var start = url.search(/elmo.academyart.edu/);
	var url=url.substr(start+20);
	var urlLen = url.length;
	
	//alert(url);
	//alert(urlLen-1);
/*
	var lastSlash = url.lastIndexOf("/");
	alert(lastSlash);
	if (lastSlash == urlLen-1){
		alert('no file name');
	}
	else if (url.search(/index.html/) >= 0){
		alert('index.html');
	}
	else{
		alert('other .html or .php');
	}
*/
	urlArray=url.split("/");
	var hrefValue = 'http://elmo.academyart.edu/';
	//document.write(urlArray+"<br />");
	document.write('<li style="display:inline"><a href="http://library.academyart.edu">Home</a></li>');
	for(var i= 0; i < urlArray.length; i++){
		if(urlArray[i] != ''){
		if ((urlArray[i].search(/.htm/) < 0) && (urlArray[i].search(/.php/) < 0)){
			hrefValue = hrefValue+urlArray[i]+'/';
		}
		else{
			hrefValue = hrefValue+urlArray[i];
			if (urlArray[i].search(/index.html/) >= 0){
				//alert('break');
				break;
			}
	
		}
		if((urlArray[i].search(/.html/) >= 0) || (urlArray[i].search(/.php/) >= 0)){ 
			crumbValue=document.title;
		}
		else{
			if((urlArray[i+1].search(/index.html/) >= 0) || (urlArray[i+1]==''))
				crumbValue=document.title;
			else
				crumbValue = displayCrumb(urlArray[i]);
		}
		document.write(' &gt; <li style="display:inline"><a href="'+hrefValue+'">'+crumbValue+'</a></li>');
	}
	//var test = makeUpperLower(urlArray[0]);
	//document.write(test);
	//document.write(urlArray[0]);
	}
}

function displayCrumb(str){
//	alert(str)
	str= str.replace(/-/g, " ");
	str = str.replace(/_/g, " ");
//	alert(str);
	var crumbs = new Array();
	var returnStr = '';
	crumbs=str.split(" ");
//	alert('crumbs='+crumbs);
	for (var i=0; i < crumbs.length; i++){
		returnStr = returnStr +' '+ makeUpperLower(crumbs[i]);
//		alert('return '+returnStr);
	}
//	if ((returnStr.search(/.htm/) >= 0) || (returnStr.search(/.php/) >= 0)){
//		returnStr = document.title;
//	}
	return returnStr;
}

function makeUpperLower(str){
	str=str.toLowerCase();
	str = str.substr(0, 1).toUpperCase() + str.substr(1);
	return str;
}

function emailAddr(username,fullname,domain){
	email=username+'@'+domain;
	if (fullname=="showAddress")
		fullname = email;
	document.write('<a href="mailto:'+email+'">' + fullname + '</a>');
	
}