// callback function to get the currently selected search type
// to appropriately modify the auto-suggest script behaviour
function getSearchType(el,qString){
	if(document.getElementById('searchClientField').value){
		var searchType = document.getElementById('searchClientField').value;
		qString += '&searchType='+searchType;
		return qString;	
	}
}

// directs the browser to the appropriate page when user
// selects a result from the auto-complete suggestions
function searchGo(selItem){
	var loc = selItem.getElementsByTagName('span')[1].innerHTML;
	if(loc != ''){
		window.location = "/"+loc;
	}else{
		document.getElementById('s').submit();
	}
}

function cornellAudioLink(link){
	var flashvars = {duration: 33, file: link, autostart: true, backcolor: '000099', frontcolor: 'FFFFFF', lightcolor: 'EE8036', stretching: 'none'};
	var params = {wmode: 'transparent'};
	swfobject.embedSWF('/swf/audioplayer.swf', 'cornellAudioLink', 175, 17, '8.0.0', false, flashvars, params, params);
	$('cornellAudioLink').insert({after: '<a href="?displayMode=cornell-credits" title="Credit details for the Cornell Lab of Ornithology Macaulay Library" class="cornellCreditLink"><img src="/images/cornell_lab.gif" alt="Cornell Lab of Ornithology Macaulay Library logo" onmouseover="this.src=\'/images/cornell_lab_over.gif\'" onmouseout="this.src=\'/images/cornell_lab.gif\'" /></a>'});
	return false;	
}

//
// new sidebar item rotation code and functions
//
var rotator = new Object();
rotator.interval = 9000;
rotator.count = 1;

rotator.topfive = new Object();
rotator.topfive.currentItem = 1;
rotator.topfive.icons = new Array('featurePause.gif', 'featurePaused.gif');

rotator.whatsNew = new Object();
rotator.whatsNew.currentItem = 1;
rotator.whatsNew.icons = new Array('featurePause.gif', 'featurePaused.gif');

rotator.ark_main_image = new Object();
rotator.ark_main_image.currentItem = 1;
rotator.ark_main_image.icons = new Array('homeImagePause.gif', 'featurePause.gif');


function initRotate(){
	var isTopFive,isArkMain;
	if(isArkMain = $('ark_main')){
		rotator.ark_main_image.numItems = $$('.ark_main_image').length;
		$('ark_main').down('.multiFeatureControls').insert({top: '<img src="/images/icons/homeImageBack.gif" id="ark_main_image_back"/><img src="/images/icons/homeImagePause.gif" id="ark_main_image_pause"/><img src="/images/icons/homeImageForward.gif" id="ark_main_image_forward"/>'});
		Event.observe($('ark_main_image_back'),'click',function(){controlChange('back','ark_main_image');return false;});
		Event.observe($('ark_main_image_pause'),'click',function(){controlChange('pause','ark_main_image');return false;});
		Event.observe($('ark_main_image_forward'),'click',function(){controlChange('forward','ark_main_image');return false;});
	}
	if(isTopFive = $('topfive')){
		rotator.topfive.numItems = $$('.topfive').length;
		$('topfive').insert({top: '<span class="multiFeatureControls"><img src="/images/icons/featureBack.gif" id="topfive_back"/><img src="/images/icons/featurePause.gif" id="topfive_pause"/><img src="/images/icons/featureForward.gif" id="topfive_forward"/></span>'});
		Event.observe($('topfive_back'),'click',function(){controlChange('back','topfive');return false;});
		Event.observe($('topfive_pause'),'click',function(){controlChange('pause','topfive');return false;});
		Event.observe($('topfive_forward'),'click',function(){controlChange('forward','topfive');return false;});
	}
	if($('whatsNew')){
		rotator.whatsNew.numItems = $$('.whatsNew').length;
		$('whatsNew').insert({top: '<span class="multiFeatureControls"><img src="/images/icons/featureBack.gif" id="whatsNew_back"/><img src="/images/icons/featurePause.gif" id="whatsNew_pause"/><img src="/images/icons/featureForward.gif" id="whatsNew_forward"/></span>'});
		Event.observe($('whatsNew_back'),'click',function(){controlChange('back','whatsNew');return false;});
		Event.observe($('whatsNew_pause'),'click',function(){controlChange('pause','whatsNew');return false;});
		Event.observe($('whatsNew_forward'),'click',function(){controlChange('forward','whatsNew');return false;});
		rotator.whatsNew.timeout = window.setTimeout(rotator.whatsNew.autoFunc, rotator['interval']*0.33);
		rotator.whatsNew.state = 'go';
	}
	// DO NOT merge these lines into the conditional block above - creation of the two periodical executers must be as close together as possible
	if(isTopFive){
		rotator.topfive.timeout = window.setTimeout(rotator.topfive.autoFunc, rotator['interval']*0.66);
		rotator.topfive.state = 'go';
	}
	if(isArkMain){
		rotator.ark_main_image.timeout = window.setTimeout(rotator.ark_main_image.autoFunc, rotator['interval']);
		rotator.ark_main_image.state = 'go';
	}
}

//set up the functions that will be called at intervals
rotator.whatsNew.autoFunc = function(){
	rotator['whatsNew']['currentItem'] = changeItem('whatsNew', 'forward');
	rotator.whatsNew.timeout = window.setTimeout(rotator.whatsNew.autoFunc, rotator['interval']);
}

rotator.topfive.autoFunc = function(){
	rotator['topfive']['currentItem'] = changeItem('topfive', 'forward');
	rotator.topfive.timeout = window.setTimeout(rotator.topfive.autoFunc, rotator['interval']);
}

rotator.ark_main_image.autoFunc = function(){
	rotator['ark_main_image']['currentItem'] = changeItem('ark_main_image', 'forward');
	rotator.ark_main_image.timeout = window.setTimeout(rotator.ark_main_image.autoFunc, rotator['interval']);
}

// function to handle click of the item next/prev/pause buttons
function controlChange(command, itemType){
	if(command == 'forward'){
		window.clearTimeout(rotator[itemType]['timeout']);
		rotator[itemType].autoFunc.call();
		rotator[itemType]['state'] = 'go';
		$(itemType+'_pause').writeAttribute('src', value='/images/icons/'+rotator[itemType].icons[0]);
	}		
	else if(command == 'back'){
		window.clearTimeout(rotator[itemType]['timeout']);
		rotator[itemType]['state'] = 'stop';
		rotator[itemType]['currentItem'] = changeItem(itemType, 'back');
		$(itemType+'_pause').writeAttribute('src', value='/images/icons/'+rotator[itemType].icons[1]);
	}		
	else if(command == 'pause'){
		if(rotator[itemType]['state'] == 'go'){
			window.clearTimeout(rotator[itemType]['timeout']);
			rotator[itemType]['state'] = 'stop';
			$(itemType+'_pause').writeAttribute('src', value='/images/icons/'+rotator[itemType].icons[1]);
		}
		else if(rotator[itemType]['state'] == 'stop'){
			rotator[itemType].autoFunc.call();
			rotator[itemType]['state'] = 'go';
			$(itemType+'_pause').writeAttribute('src', value='/images/icons/'+rotator[itemType].icons[0]);
		}
		else alert('There is a problem...');
	}				
}

// function to change from one item to the next/previous, in a looping fashion	
function changeItem(itemType, direction){
	var currentEl = itemType+rotator[itemType]['currentItem'];
	if(direction == 'forward')
		var Next = rotator[itemType]['currentItem']>=rotator[itemType]['numItems'] ? 1 : rotator[itemType]['currentItem']+1;
	else if(direction == 'back')
		var Next = rotator[itemType]['currentItem']==1 ? rotator[itemType]['numItems'] : rotator[itemType]['currentItem']-1;
	else return rotator[itemType]['currentItem'];
	var nextEl = itemType+Next;
	if(itemType=='ark_main_image'){
		new Effect.Fade(currentEl, {duration: 2});			
		new Effect.Appear(nextEl, {duration: 2});
	}else{
		new Effect.Fade(currentEl, {duration: 0.5, queue: {position: 'end', scope: itemType}});
		new Effect.Appear(nextEl, {duration: 0.5, queue: {position: 'end', scope: itemType}});
	}
	rotator.count++;
	return Next;
}