function showSearch(which){
	document.getElementById('sKeyword').style.display = 'none';
	document.getElementById('sTitle').style.display = 'none';
	document.getElementById('sAuthor').style.display = 'none';
	document.getElementById('sSubject').style.display = 'none';
	document.getElementById('sCallNumber').style.display = 'none';
	document.getElementById('sISBNISSN').style.display = 'none';
	document.getElementById(which).style.display = 'block';
}

var mtl = mtl || {};

mtl.validate = function(form) {
	var formName = form.title;
	var forms = {
		classic: {
			defaultText: 'Search catalog',
			searchField: 'searcharg',
			url: 'http://sflib1.sfpl.org'
		},
		encore: {
			defaultText: 'Explore Catalog',
			searchField: 'target',
			url: 'http://encore.sfpl.org/iii/encore/'
		},
		photos: {
			defaultText: 'Search photos',
			searchField: 'searcharg',
			url: 'http://sflib1.sfpl.org:82'
		}
	};
	
	if (typeof forms[formName] !== 'undefined') {
		if (form[forms[formName].searchField].value === forms[formName].defaultText || form[forms[formName].searchField].value === '') {
			window.location = forms[formName].url;
			return false ;
		}
		else {
			return true;
		}
	}
};



var t;
var features;
var featureCount = 0;
var fHREF = new Array();
var fIMG = new Array();
var fTITLE = new Array();
var fTEXT = new Array();

function rotateFeatures(which,count){
	if (which < featureCount || featureCount == 0){
		next = which + 1;
	} else {
		next = 1;
  }
  updateFeature(which);
	t=setTimeout("rotateFeatures("+next+","+featureCount+");",8000);
}

function updateFeature(which){
	clearTimeout(t);
	el = document.getElementById('feature');
	el.innerHTML = '';
	fadeBalloon('0');
	if(featureCount == 0) {
		ajaxFeature('Featured/featuredata.xml',which);
	} else {
	  updateFeatureText(which);
	}
}

function ajaxFeature(file,which){
  var xmlObj = null;
  if(window.XMLHttpRequest){
      xmlObj = new XMLHttpRequest();
  } else if(window.ActiveXObject){
      xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
      return;
  }

  xmlObj.open('GET', file, true);
  xmlObj.onreadystatechange = function(){
    if(xmlObj.readyState == 4 && xmlObj.status == 200){
	    parseFeatures(xmlObj.responseXML);
	    updateFeatureText(which);
	  }
  }
  xmlObj.send('');
}

function parseFeatures(featureXML) {
	var features = featureXML.getElementsByTagName("feature");
	featureCount = features.length;
	for(i=0;i<featureCount;i++) {
		fHREF[i]  = features[i].getElementsByTagName("href")[0].firstChild.data;
		fIMG[i]   = features[i].getElementsByTagName("img")[0].firstChild.data;
		fTITLE[i] = features[i].getElementsByTagName("title")[0].firstChild.data;
		fTEXT[i]  = features[i].getElementsByTagName("text")[0].firstChild.data;
	}
}

function updateFeatureText(which) {
	var i = which - 1;

	var html = '<a href="' + fHREF[i] + '"><img src="' + fIMG[i] + '" width="250" height="225" alt="' + fTITLE[i] + '" /></a>';
	html += '<h2>Library Highlights</h2>';
	html += '<div id="theFeature">';	
	html += '<div class="title"><a href="' + fHREF[i] + '">' + fTITLE[i] + '</a></div>';
	html += '<div id="featureText">' + fTEXT[i] + '</div>';
	html += '</div>';
	html += '<a href="' + fHREF[i] + '">More...</a>';
	html += '<div id="featureLinks">';
	for(j=1;j<featureCount+1;j++) {
		if(j==which) {
			html += '<a href="#" class="selected">' + j + '</a>';
		} else {
			html += '<a href="#" onclick="updateFeature('+j+');" title="' + fTITLE[j-1] + '">' + j + '</a>';
		}
	}
	html += '</div>';

	document.getElementById('feature').innerHTML = html;
	launchBalloon('25');
}

function removeDefault (which, theDefault) {
	if (which.value == theDefault){
		which.value = '';
		which.style.color = '#000000';	
		which.style.fontStyle = 'normal';	
	}
}

function addDefault (which, theDefault) {
	if (which.value == ''){
		which.value = theDefault;
		which.style.color = '#999999';	
		which.style.fontStyle = 'italic';	
	}
}


 function fadeBalloon(theAlpha){
	el = document.getElementById('feature');
	el.className = "alpha_"+theAlpha;
	
	
	if (theAlpha == '75'){
		var t=setTimeout("fadeBalloon('50');",30);
	} else {
		if (theAlpha == '50'){
			var t=setTimeout("fadeBalloon('0');",30);
		} else {
			if (theAlpha == '25'){
				var t=setTimeout("fadeBalloon('0');",30);
			} else {
			//	var t=setTimeout("fadeBalloon('0');",10);
			}
		}
	}
 }
 
 function launchBalloon(theAlpha){
	el = document.getElementById('feature');
	el.className = "alpha_"+theAlpha;
	
	
	if (theAlpha == '75'){
		var t=setTimeout("launchBalloon('100');",25);
	} else {
		if (theAlpha == '50'){
			var t=setTimeout("launchBalloon('75');",25);
		} else {
			if (theAlpha == '25'){
				var t=setTimeout("launchBalloon('50');",25);
			} else {
			//	var t=setTimeout("fadeBalloon('0');",10);
			}
		}
	}
 }
 
 
// for new materials

function toggle(tblID) {
  el = document.getElementById(tblID);
	if (el.className=='open') {
	  el.className = 'closed';
	}  else {
	  el.className = 'open';
	}
}

