$(document).ready(function() {		
			$.ajax({
				url: "/lookup/named.search_autocomp.bam?active_sw='Y'&sport_code='mlb'&all_star_sw='N'&team_all.col_in=name_display_full,file_code",
				dataType: "xml",
				success: function(xmlResponse) {
					var autocomplete_xml_response = $("row", xmlResponse).map(function() {

            		var row = $(this),
               		fileCode = row.attr("file_code"),
                	n, p, t, img, additional_data;

					if (typeof fileCode === 'undefined') {
						n = row.attr("n");
						p = row.attr("p");
						t = row.attr("t");
						img = "<img src='/mlb/images/fantasy/wsfb/24x29/"+p+".jpg' />";
              			additional_data = (t === "") ? "<br />Free Agent" : "<br />"+t;

						return {
							label: "<div class='auto_result auto_player'>"+img+"<div class='auto_result_text'>"+n+additional_data+"</div></div>",
							value: n,
							id :   p,
							alt:   t,
							player : true
						};
							
					} else {
						n = row.attr("name_display_full");
						img = "<img src='/mlb/images/team_logos/29x20/"+fileCode+".png' />";
						return {
							label:  "<div class='auto_result auto_team'>"+img+"<div class='auto_result_text'>"+n+"</div></div>",
							value:  n,
							id:     fileCode,
							player: false
						};
					}

				}).get();

				$("#query_text1")
						.one("focus", function() { $(this).val(""); })
						.autocomplete({
							source: function( request, response ) {
        				// escape regex characters
        				var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
        				var primaryResults = [];
        				var secondaryResults = [];
						var tertiaryResults = [];

                $.each( autocomplete_xml_response, function(n, value) {
                  	if (matcher.test(value.value) && (value.player == true)) {
                    	primaryResults.push(value);
                  	} else if (value.alt && matcher.test(value.alt) && (value.player == true)) {
                   	 	secondaryResults.push(value);
                  	} else if ((value.player == false) && matcher.test(value.value)) {
						tertiaryResults.push(value);
					}
        		});
				var first_merge = $.merge(primaryResults, secondaryResults);
				var second_merge = $.merge(first_merge,tertiaryResults);
        		response(second_merge);
        	},
			minLength: 3,
			select: function(event, ui) { 
				bam.tracking.track({
              		async:{
                    	isDynamic    : false,
                        compName     : "Site Search",
                       	compActivity : "Site Search: Suggestion Click",
                        actionGen    : false,
						queryText    : ui.item.value
               		}
				});

				if (ui.item.label.indexOf('auto_player') != -1) {
					window.location="http://mlb.mlb.com/team/player.jsp?player_id="+ui.item.id;
				} else {
					window.location="http://mlb.mlb.com/index.jsp?c_id="+ui.item.id;
				}
			},
			open: function() {
				$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
			},
			close: function() {
				$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
			}
		});
	}
});				
		
		$("dl.top_search a").live("click",function() {
			bam.tracking.track({
            	async:{
                	isDynamic    : false,
                    compName     : "Site Search",
                    compActivity : "Site Search: Top Searches Click",
        			actionGen    : false
               	}
			});
		});
		
		$("#hdr_search").submit(function() {
			bam.tracking.track({
            	async:{
                	isDynamic    : false,
                    compName     : "Site Search",
                    compActivity : "Site Search: Go Click",
        			actionGen    : false
               	}
			});
			var _searchBox = $("#query_text1").val();
			$("input[name='query']").val(_searchBox);
			return true;
		});
});
