// Load this script when page loads
$(document).ready(function(){
	$("#search").focus(function() {
		$(this).val("");
	});

	$('#search').keypress(function(e) {
	     return e.keyCode != 13;	
         });

	// Stadium/Team Search
	$( "#search" ).autocomplete({
		minLength: 1,
		position: {
     			my: "right top",
        		at: "right bottom"
    		},
		source: function(request, response) {
			$.ajax({
				url: baseUrl + "search/name",
				data: {"s": request.term},
				dataType: "json",
				method: "GET",
				success: function(data) {
					if(data && data.searchResults.length > 0) {
						response(data.searchResults);
					} else {
						data = ["No results found"];
						response(data);
					}
				}	
			});
		},
		focus: function( event, ui ) {
			if(ui.item.teamName && ui.item.stadiumName) {
				$( "#search" ).val( ui.item.teamName + " - " + ui.item.stadiumName);
			}
			return false;
		},
		select: function( event, ui ) {
			if(ui.item.teamName && ui.item.stadiumName) {
				var itemValue = ui.item.teamName + " - " + ui.item.stadiumName;
				$( "#search" ).val(itemValue);
				recordEventWithLabel('navigation', 'searchBox', itemValue);
				var targetUrl = ui.item.url;
				if(targetUrl && targetUrl.indexOf("/") == 0) {
					targetUrl = targetUrl.substring(1, targetUrl.length);
				}
				location.href= baseUrl + targetUrl;
			} else {
				$("#search").val(ui.item);
			}
			return false;
		}
	})
	.data( "autocomplete" )._renderItem = function( ul, item ) {
		var value = "<a>No results found</a>";
		if(item.teamName && item.stadiumName) {
			value = "<a>" + item.teamName + " - " + item.stadiumName + "</a>";
		}

		return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append(value)
			.appendTo( ul );
	};

	// Login Panel --------------------------------------------------------------

	// Expand Panel
	$("a#open").click(function(){
		$("div#login-panel").slideDown("slow");
	});	

	// Collapse Panel
	$("a#close").click(function(){
		$("div#login-panel").slideUp("slow");	
	});		

	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#toggle a").click(function () {
		$("#toggle a").toggle();
	});

	$("a#ranking_intro").click(function () {
		$("div#login-panel").slideDown("slow");
    		$("#toggle a").toggle();

	});

	// Sport/League Menu -----------------------------------------------------------
	
	$(".nav .menu").click(function(event) {
		event.preventDefault();
		var menuId = $(this).attr("id");
		
		$(".nav .menu").removeClass("selected");
		$(".nav-drop").each(function(index, el) {
			if($(this).hasClass(menuId + "-dropped")) {
				$(this).slideToggle("slow");
			} else {
				$(this).hide();
			}
			
			if($(this).is(":visible")) {
				$("#" + menuId).addClass("selected");
			}
		});
	});

	// Set up a listener so that when anything with a class of 'tab' 
	// is clicked, this function is run.
	$('.more-food').click(function () {

		// Remove the 'active' class from the active tab.
	  	$('.content-box > .content-tabs > li.active')
		.removeClass('active');
        
	  	// Remove the 'tab_contents_active' class from the visible tab contents.
	  	$('.reviews > .tab_contents_active')
		.removeClass('tab_contents_active');
        
	  	// Add the 'tab_contents_active' class to the associated tab contents.
	  	$(this.rel).addClass('tab_contents_active');

	  	// Add the 'active' class to the clicked tab.
	  	$('.content-box > .content-tabs > li.food')
		.addClass('active');
                
		return false;		
	});
	
	$('.content-tab').click(function () {

		// Remove the 'active' class from the active tab.
	  	$('.content-box > .content-tabs > li.active')
		.removeClass('active');
        
	  	// Add the 'active' class to the clicked tab.
	  	$(this).parent().addClass('active');
        
	  	// Remove the 'tab_contents_active' class from the visible tab contents.
	  	$('.reviews > .tab_contents_active')
		.removeClass('tab_contents_active');
        
	  	// Add the 'tab_contents_active' class to the associated tab contents.
	  	$(this.rel).addClass('tab_contents_active');
        
		return false;
	});

	$('.info-tab').click(function () {

		// Remove the 'active' class from the active tab.
	  	$('#info-box > .info-tabs > li.active')
		.removeClass('active');
        
	  	// Add the 'active' class to the clicked tab.
	  	$(this).parent().addClass('active');
        
	  	// Remove the 'tab_info_active' class from the visible tab info.
	  	$('#info-box > div.tab_info_active')
		.removeClass('tab_info_active');
        
	  	// Add the 'tab_info_active' class to the associated tab info.
	  	$(this.rel).addClass('tab_info_active');
        
		return false;
	});
	
});

