/* Copyright (c) 2006-2007, Apple Inc. All rights reserved. */
function prepare(inAlwaysRun) {
	if (window.unitTestHandler && (!inAlwaysRun)) return true;
	drawTooltips();
	server();
	serverui();
	gPoofManager = new PoofManager();
	gNotifier = new Notifier();
	gPopupManager = new PopupManager();
	gLinkPreviewGenerator = new LinkPreviewGenerator();
	gSearchPopup = new SearchPopup();
	gSearchResultFilter = new SearchResultFilter();
	if ($('paginator_choose')) paginator();
	gSearchResultItems = $$('#entries_list div.entry').collect(function(div) {
		return new SearchResultItem(div);
	});
}

function documentShouldUnload() {
	// search always allows unload
}

var SearchResultItem = Class.create();
SearchResultItem.prototype = {
	initialize: function(inElement) {
		bindEventListeners(this, ['handleClick']);
		this.mElement = $(inElement);
		this.mElement.style.cursor = 'pointer';
		this.href = this.mElement.down('h2').down('a').href;
		observeEvents(this, this.mElement, {click:'handleClick'});
	},
	handleClick: function(inEvent) {
		if (Event.isLeftClick(inEvent) && !inEvent.metaKey && !inEvent.ctrlKey && !inEvent.shiftKey && !inEvent.altKey) window.location.href = this.href;
	}
}

var SearchResultFilter = Class.create();
SearchResultFilter.prototype = {
	initialize: function() {
		bindEventListeners(this, ['handlePopupChange', 'handleFormSubmit']);
		var searchField = $('inline_search_query');
		this.mInitialSearchValue = $F('inline_search_query');
		this.mElement = $('search_form');
		this.mHiddenSortDirection = $('sortDirection');
		// search hint
		if (!SafariFixes.isWebKit) this.mSearchHinter = new HintedTextField(searchField, $('search_field').title);
		// add enabled services
		if ($('linkBlo')) $('search_filter_show').appendChild(Builder.node('option', {value:'weblog'}, Loc.search_kind_weblog));
		if ($('linkCal')) $('search_filter_show').appendChild(Builder.node('option', {value:'calendar'}, Loc.search_kind_calendar));
		if ($('linkML')) $('search_filter_show').appendChild(Builder.node('option', {value:'mailinglist'}, Loc.search_kind_mailinglist));
		// populate popups from GET request
		kindMatch = window.location.search.match(/kind=(all|weblog|wiki|calendar|mailinglist)/);
		$('search_filter_show').value = kindMatch ? kindMatch[1] : 'all';
		sortMatch = window.location.search.match(/sort=(date|title|kind|lastModifiedAuthor)/);
		$('search_filter_sort_by').value = sortMatch ? sortMatch[1] : 'date';
		// submit form when popups are changed
		Event.observe('search_filter_show', 'change', this.handlePopupChange, false);
		Event.observe('search_filter_sort_by', 'change', this.handlePopupChange, false);
		Event.observe(this.mElement, 'submit', this.handleFormSubmit, false);
		// observe all of the checkboxes
		$A(this.mElement.getElementsByTagName('input')).each(function(cb) {
			if (cb.name == 'tag') Event.observe(cb, 'change', this.handlePopupChange, false);
		}.bind(this));
	},
	populateHiddenInputs: function() {
		if (this.mInitialSearchValue != $F('inline_search_query')) {
			$A(this.mElement.getElementsByTagName('input')).each(function(cb) {
				if (cb.name == 'tag') {
					Event.stopObserving(cb, 'change', this.handlePopupChange);
					cb.checked = false;
				}
			});
		}
		// should reverse-sort by date
		this.mHiddenSortDirection.value = ($F('search_filter_sort_by') == 'date') ? 'reverse' : 'forward';
		// hinter will add "search" to the query if we're not careful
		if (this.mSearchHinter && (this.mSearchHinter.getValue() == '')) $('inline_search_query').value = '';
		// clear any checkbox duplicates
		$A(this.mElement.getElementsByTagName('input')).each(function(cb) {
			if (cb.name == 'tag' && !cb.checked) $(cb).disabled = true;
		});
	},
	handlePopupChange: function(inEvent) {
		this.populateHiddenInputs();
		this.mElement.submit();
	},
	handleFormSubmit: function(inEvent) {
		this.populateHiddenInputs();
	}
}

if (window.loaded) loaded('search.js');