
		




$(document).ready(function(){
	
//begin autocomplete
	(function($) {
		$.widget("ui.combobox", {
			
			_create: function() {
				
				var self = this;
				var select = this.element.hide();
				var input = $("<input>")
					.insertAfter(select)
					.autocomplete({
						
					
						
						source: function(request, response) {
							var matcher = new RegExp(request.term, "i");
							response(select.children("li").map(function() {
								var text = $(this).text();
								if (this.value && (!request.term || matcher.test(text)))
									return {
										id: this.value,
										label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
										value: text
									};
							}));
						},
						delay: 0,
						change: function(event, ui) {
							if (!ui.item) {
								// remove invalid value, as it didn't match anything
								$(this).val("");
								return false;
							}
							select.val(ui.item.id);
							self._trigger("selected", event, {
								item: select.find("[value='" + ui.item.id + "']")
							});
			
							
						},
						
						select: function(event,ui){
							var li = select.children("#"+ui.item.id);
							var link = $(li).children("a")[0].getAttribute("href");
	
							
							window.location = link;
						},
						
						minLength: 0
					})
					.addClass("ui-widget ui-widget-content ui-corner-left searchLabel");
				$("<button>&nbsp;</button>")
				.attr("tabIndex", -1)
				.attr("title", "Show All Items")
				.insertAfter(input)
				.button({
					icons: {
						primary: "ui-icon-triangle-1-s"
					},
					text: false
				}).removeClass("ui-corner-all")
				.addClass("ui-corner-right ui-button-icon")
				.click(function() {
					// close if already visible
					if (input.autocomplete("widget").is(":visible")) {
						input.autocomplete("close");
						return;
					}
					// pass empty string as value to search for, displaying all results
					input.autocomplete("search", "");
					input.focus();
				});
				
				var self = this;
				input[0].value= this.options.searchLabel;
				input[0].onfocus = function(){
					if(this.value == self.options.searchLabel ){
						this.value = ""; 
						input.removeClass("searchLabel");
					}
				};
				input[0].onblur = function(){
					if(this.value == "" ){
						this.value = self.options.searchLabel;
						input.addClass("searchLabel");
					}
				};
				
				
			}
		});

	})(jQuery);
	
// end autocomplete
	
	
	
 $("ul.subnav").parent().append('<button></button>'); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav

 $("ul.topnav").click(function() { //When trigger is clicked...

 //Following events are applied to the subnav itself (moving subnav up and down)
 $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

 $(this).parent().hover(function(){}, 
		 function(){
 			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
 });

 //Following events are applied to the trigger (Hover events for the trigger)
 }).hover(function() {
 $(this).addClass("subhover"); //On hover over, add class "subhover"
 }, function(){ //On Hover Out
 $(this).removeClass("subhover"); //On hover out, remove class "subhover"
 });
 
 $(function() {
	 	var authorSearch = $("#author_combobox").combobox({
	 		searchLabel : " Search author.."
	 	
	 	});
		
	 
	 	$("#pub_combobox").combobox({
	 		searchLabel : " Search by title.."
	 	});
		/*$("#toggle").click(function() {
			$("#pub_combobox").toggle();
		});*/
		
	});
});


