function topicSearch(url, list_maker) {
	if (url.length) {
		// If Listmaker, do an ajax load
		if (list_maker) {		
		topicList.entries.load(url + '&json=true');
		} else {
			// If they items, let's alert them
			var $mylist = $('#mylist');

			if ($mylist) {
				if (!$mylist.length || !$mylist.data('twitterIds').length || ($mylist.data('twitterIds').length && confirm('Are you sure you want to start your search over?  Your custom list will be reset?'))) {
					// if standard mode, change location
					document.location.href = url;
				}
			}
		}
	}
}
function isEditMode() {
	return ((typeof(topicList)==='undefined') ? false : (topicList.entries.mode === 'edit'));
}
function setSearchBoxListener(turn_on) {
	if (turn_on)
	{
	   $('.searchInput input').autocomplete('/header.topic.php',
			{
			minChars: 2, 
			selectFirst: true,
			width: 652,
			mustMatch: true
			}
		);
	}
	else
	{
		$('.searchInput input').unbind(".autocomplete");
	}
}
var SearchBox = {
	// Removes all pointer indicators
	noneActive: function() {
		$('.arrowsText .text a.active').removeClass('active');
	},
	
	// Activate
	activate: function(id) {
		this.noneActive();
		$('.arrowsText .' + id + '.text a').addClass('active');
	}
}

$(document).ready(function(e){ 
		$('#frmSearchTopic').submit(function(e) {
			e.preventDefault();
			var submit_url;
			var list_maker = isEditMode();

			switch ($('#txtType').val()) {
				case "username":
					username = $('.searchBox').val().replace(/^\s+|\s+$/g,"");
					username = escape(username.replace("@",""));
					submit_url = "/profile/summary/" + username;
					list_maker = false;

					break;

				case "topic":
					topic = escape($('.searchBox').val().replace(/^\s+|\s+$/g,""));
					submit_url = (list_maker) ? "/listmaker.search.php?type=topic&topic=" + topic : "/topic/lists/" + topic;

					break;

				case "lists":
					var path_parts = $('.searchBox').val().split( '/' );

					if (path_parts.length == 2) {
						var u = escape(path_parts[0]);
						var ln = escape(path_parts[1]);

					} else {
						u = 'Twitter';
						ln = 'team';
					}

					submit_url = (list_maker) ? "/listmaker.search.php?name=" + u  + "&type=topic&topic=" + ln + "&slug=" + ln : "/profile/lists/"+u+"/topic/"+ln+"/"+ln; 

					break;

				case "compare":
					$('.compareUser').each(function(e, f){
						f.value = f.value.replace(/^\s+|\s+$/g,"");
						f.value = f.value.replace("@","");
					});
					
					if ($('#u1').val().length > 0)
						submit_url = "/compare.php?" + $(this).serialize();

					break;
			}

			topicSearch(submit_url, list_maker);
		});

		//Search bar at top
		$('.searchTypeIndicator .text').click(function(e){
			e.preventDefault();
			var $this = $(this);
			var $searchBox = $('.searchBox');
			var $txtType = $('#txtType');
			var $a = $this.find('a');

			setSearchBoxListener(false); // setup autocomplete

			// If compare as clicked, we'll process that change
			if ($this.hasClass('compare') ){
				$this.css('display','none');
				$this.siblings('.compare').css('display','block');
				$searchBox.css('display','none');

				if ($this.hasClass('clicked')) {
					$('#txtSearch').css('display','block');

				} else {
					$('#compareUser').css('display','block');
				}

				// Hide the pointer arrow
				SearchBox.noneActive();

				$txtType.val('compare');
			}
			else 
			{
				var $searchTypeIndicator = $('.searchTypeIndicator');

				var is_users = $(this).hasClass('username');
				var is_topic = $(this).hasClass('topic');
				var is_lists = $(this).hasClass('lists');

				var defaults = {
					topic: 'Enter a topic here',
					username: 'Enter a Twitter username here',
					lists: 'username/listname (Twitter/team)'
				}
				
				if (is_topic) {
					$txtType.val('topic');
				} else if (is_lists) {
					$txtType.val('lists');
				} else {
					$txtType.val('username');
				}

				$searchBox.attr('value',defaults[$txtType.val()]).attr('defaultValue', defaults[$txtType.val()]);

					// Show the right arrow
				SearchBox.activate($txtType.val());

				$('.arrowsText .compare').css('visibility',((is_topic || is_lists)? 'hidden' : 'visible'));

				if (!is_topic && !is_lists)
				{
					//Make sure the compare users text is displayed, not the single user text
					$(this).siblings('.compare').css('display','block');
					$(this).siblings('.compare.clicked').css('display','none');
				}
			}
		});
	
		// Configure text fields
		$('.searchInput input')
			.focus(function() {
				setSearchBoxListener(false);
				if (this.value == this.defaultValue) this.value = '';
				if ($('#txtType').val() == 'topic')
					setSearchBoxListener(true); // setup autocomplete
			})
			.blur( function() {
				setSearchBoxListener(false); // setup autocomplete
				if (this.value == '') {
					  this.value = this.defaultValue;
					  $(this).removeClass('changed');
				}
			})
			.change( function() {
			  $(this).addClass('changed')
				})
		
	   $('.entry .topics a').live('click', function(e){
			e.preventDefault();
			var topic = escape($(this).html());
			var url = "/listmaker.search.php?type=topic&topic=" + topic;
			
			topicSearch(url, true);
	   });
});