/*

NewsAndSearch
SearchMenu
OptionsPanel

+ 2 functions: redirect & trace

*/

function NewsAndSearch(searchOpen,newsOpen) {
	this.searchOptionsDiv = document.getElementById("searchOptions");
	this.searchOptions1Div = document.getElementById("searchOptions1");
	this.optionGroupMenuDiv = document.getElementById("optionGroupMenu");
	this.newsDiv = document.getElementById("news");
	this.news1Div = document.getElementById("news1");
	this.newsListDiv = document.getElementById("newsList");
	this.searchOpen = searchOpen?searchOpen:false;
	this.newsOpen = newsOpen?newsOpen:false;
	this.click('search');
	//this.update();
}

NewsAndSearch.prototype.updateNews = function() {
	this.newsDiv.style.display = (this.newsOpen)?"block":"none";
	this.news1Div.style.display = (this.newsOpen)?"none":"block";
	this.newsListDiv.style.display = (this.newsOpen)?"none":"block";

	if(!this.newsOpen && this.searchOpen){
		this.updateSearch();
	}
	this.newsOpen = !this.newsOpen;
}

NewsAndSearch.prototype.updateSearch = function() {
	this.searchOptionsDiv.style.display = (this.searchOpen)?"block":"none";
	this.searchOptions1Div.style.display = (this.searchOpen)?"none":"block";
	this.optionGroupMenuDiv.style.display = (this.searchOpen)?"none":"block";

	if(!this.searchOpen && this.newsOpen){
		this.updateNews();
	}

	this.searchOpen = !this.searchOpen;
}
NewsAndSearch.prototype.click = function(obj) {
	if(obj=='search'){
		this.updateSearch();
	}
	else{
		this.updateNews();
	}
	var s = document.getElementById("subMenu");
	if (s!=null) {
		if (this.searchOpen||this.newsOpen) {
			s.style.visibility = "hidden";
		} else {
			s.style.visibility = "visible";
		}
	}
}


///////////////////////////////////////////


function SearchMenu() {
	this.options = new Array();
	this.selected = new Array("","","");
	this.subjectChoices = new Array("Figurative","Still Life","Landscape","Abstract","Other","Search All");
	this.mediumChoices = new Array("Painting","On Paper","Sculpture","Search All");
	this.timeChoices = new Array("1990-Current","1971-1989","1946-1970","1930-1945","1899-1929","Search All");

	this.outId = -1;
	this.lastHilight = null;
	this.chosen = null;
}
SearchMenu.prototype.init = function(){
	this.addOptions("byMovement");
	this.addOptions("byMedium");
	this.addOptions("byTime");
	this.addInput("byId");
	this.addInput("byArtist");
}
SearchMenu.prototype.addOptions = function(id){
	this.options[this.options.length] = new OptionsPanel(this,id);
}
SearchMenu.prototype.addInput = function(id){
	var o = new OptionsPanel(this,id);
	o.type = 1;
	this.options[this.options.length] = o;
}

SearchMenu.prototype.clearLastRollover = function(){
	if (this.outId!=-1) {
		clearTimeout(this.outId);
		this.outId = -1;
	}
	if (this.lastHilight != null) {
		this.lastHilight.light(false);
		this.lastHilight = null;
	}
}

SearchMenu.prototype.select = function(type,index){

	var menu=type.parentNode.parentNode.parentNode;
	var display = document.getElementById(menu.id + "Selected");
	if(menu.id=="byMovement"){
		display.firstChild.innerHTML=this.subjectChoices[index];
		search.selected[0] = this.subjectChoices[index];
	}
	else if(menu.id=="byMedium"){
		display.firstChild.innerHTML=this.mediumChoices[index];
		search.selected[1] = this.mediumChoices[index];
	}
	else if(menu.id=="byTime"){
		display.firstChild.innerHTML=this.timeChoices[index];
		search.selected[2] = this.timeChoices[index];

	}

	display.style.display="block";
	document.getElementById("searchID").style.display="block";
	document.getElementById("printAllID").style.display="none";
	this.choose(menu.firstChild.obj);
}

SearchMenu.prototype.choose = function(choice) {
	var oldchoice = this.chosen;
	this.chosen = choice;
	//alert(choice.showing);

	var display = document.getElementById(choice.div.id + "Selected");

	//there are some old choice, hide it first.
	if (oldchoice != null) {
		oldchoice.showOptions(false);
		oldchoice.light(false);
		//if old choice is the same as current choice, then set it to null
		if (oldchoice == choice) {
			this.chosen = null;
			return;
		}
	}

		this.chosen.light(true);
		this.chosen.showOptions(true);
}
SearchMenu.prototype.out = function(which) {
	if (this.outId==-1) {
		window.SearchMenuTimeout = this;
		this.outId = setTimeout("eval('window.SearchMenuTimeout.out1()');", 200);
	}
}
SearchMenu.prototype.out1 = function() {
	window.SearchMenuTimeout = null;
	this.lastHilight.light(false);
	this.lastHilight = null;
	this.outId = -1;
	if (this.chosen!=null) {
		this.chosen.light(true);
	}
}
function OptionsPanel(parent,id) {
	this.type = 0;
	this.parent = parent;
	this.div = document.getElementById(id);
	if (this.div==null) { return; }

	this.menu = this.div.childNodes[0];
	this.menu.obj = this;

	var tmp = this.div.childNodes[1].childNodes[1];

	if(tmp!=null && tmp.tagName=="DIV"){
		this.panel = tmp;
	}
	else{
		this.panel = this.div.childNodes[1];
	}

	this.panel.obj = this;

	var menuState = this.menu.getElementsByTagName("DIV");
	this.menuOption = menuState[0];
	this.menuOptionHilight = menuState[1];

}
OptionsPanel.prototype.choose = function() {
	this.parent.choose(this);
}
OptionsPanel.prototype.out = function() {
	this.parent.out(this);
}
OptionsPanel.prototype.over = function() {
	this.parent.clearLastRollover();
	this.parent.lastHilight = this;
	this.light(true);
}
OptionsPanel.prototype.light = function(onOff) {
	if (this.showing) return;

	if (onOff) {
		this.menuOption.style.display = "none";
		this.menuOptionHilight.style.display = "block";
	} else {
		this.menuOption.style.display = "block";
		this.menuOptionHilight.style.display = "none";
	}
}

OptionsPanel.prototype.getIndex = function(){
	var id = this.div.id;

	for(var i=0;i<this.parent.selected.length;i++){
		if(this.parent.selected[i]!=""){
			if(id=="byMedium")
				return 1;
			else if(id=="byMovement")
				return 0;
			else if(id=="byTime")
				return 2;
		}
	}
	return 0;
}

OptionsPanel.prototype.showOptions = function(isItShown) {
	this.showing = isItShown;
	var display = document.getElementById(this.div.id + "Selected");
	if (isItShown) {
		this.panel.style.display = "block";

		if (this.type==1) {
			this.menuOption.style.display = "none";
			this.menuOptionHilight.style.display = "none";

			//alert(this.menuOption.className);
			//alert(this.menuOptionHilight.className);

			//alert(this.panel.className);
			//for(var i=0;i<this.panel.childNodes.length;i++){
			//	alert(this.panel.childNodes[i].name);
			//}

			this.panel.childNodes[0].txtInput.focus();
			this.panel.childNodes[0].txtInput.select();
			//this.panel.childNodes[0].value = "";
			//this.panel.childNodes[0].focus();

		}
		else{

			display.style.display="none";
		}
	} else {
		this.panel.style.display = "none";
		if (this.type==1) {
			this.light(false);
		}
		else{

			if(this.parent.selected[this.getIndex()]!=""){
				display.style.display="block";
			}
		}
	}
}

///
function redirect(location) {
	document.location.href = location;
}
function trace(s) {
	document.getElementById("debug").innerHTML += s+"<br/>";
}


