// JavaScript Document
Event.observe(window, "load", function() {
	//Element.toggle( $('classicLitForm'))
	$$('div#classicLitForm').invoke('hide');
	
	$$('a#productDrop, a#publicationDrop').invoke('observe','click', stopme);
	
	$$('div#productDropDown a', 'div#publicationDropDown a').invoke('observe','click', selectDropDownOption); //add listener to each drop down
	$$('div#literatureSelect input.submitButton').invoke('observe','click', doLiteratureSearch); //add listener to each drop down
	$$('div#litSearchForm').invoke('show'); //show JS form

	var currentURL = window.location.toString();	
	if ((currentURL.indexOf('/institutional') > 0) || (currentURL.indexOf('/international') > 0)){
	//	$('productDrop').hide();
	}	
	
	if ($('litSearchForm')) { $('litSearchForm').style.display = 'inline'; } //http://www.dougboude.com/blog/1/2008/12/Elementshowhide-anomoly-in-Prototype.cfm
	$$('div#productDrop').invoke('show');
	$$('div#publicationDrop').invoke('show');
	
});

function stopme(e){
	Event.stop(e);
}

function doDrop(dropdown){

	$(dropdown+'DropDown').toggle(); //show hide drop downs
	
	//if required close other drop down 
	if (dropdown == "product" && $('publicationDropDown').visible()){
		$('publicationDropDown').hide();	
	} else if (dropdown == "publication" && $('productDropDown').visible()){ //don't think users can get to this menu when the other is open but _feels_ cleaner
		$('productDropDown').hide();	
	}
}

function selectDropDownOption(e){
	var dropdown = Event.findElement(e, 'div').id; //get the id of the drop down element
	var startPoint = dropdown.length - 8; 
	var whichDropDown = dropdown.substr(0, startPoint); 
	$(whichDropDown+'Drop').innerHTML = this.innerHTML+'<span></span>'; //update selected item
	doDrop(whichDropDown); //close the drop down
	
	Event.stop(e);
}

function doLiteratureSearch(){
	
	
	//tidy up input
	var pubData = $('publicationDrop').innerHTML;
	pubData = pubData.replace('<span></span>', '');
	pubData = pubData.replace('<SPAN></SPAN>', '');
	// no product data selector in institutional or investor
	
	var prodData = $('productDrop').innerHTML;
	prodData = prodData.replace('<span></span>', '');
	prodData = prodData.replace('<SPAN></SPAN>', '');
	
	//check to see if we have a valid input - if not send all
	if(pubData == 'Select publication'){
		pubData = 'all';
	}
	
	if(prodData == 'Select product'){
		prodData = 'all';
	}
	
	//send post data to form location
	var formLocation = $('literatureForm').action;
	formLocation = formLocation + "?product=" + prodData + "&publication=" + pubData;
	window.location = formLocation;
	//alert(formLocation);
	//alert(prodData+' '+pubData);	
}
