/* Copyright (c) 2006-2009, Apple Inc. All rights reserved. */

var Toolbar = Toolbar || {};
Toolbar.autocreate = false;

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

var SearchResultPage = Class.createWithSharedInstance('searchResultPage', true);
SearchResultPage.prototype = {
	initialize: function()
	{
		if (!Element.hasClassName(d.body, 'search')) return invalidate;
		
		bindEventListeners(this, [
			'onSearchAllClick'
		]);
		
		this.mSearchResultItems = $$('#entries_list div.entry').collect(function(div) {
			return new SearchResultItem(div);
		});
		
		var section = $('search_filter');
		if (section)
		{
			this.typeButtons = section.down('ul').select('.button').collect(function(element) {
				return new FilterByTypeButton(element);
			});
		}
		var section = $('search_tags');
		if (section)
		{
			this.tagsCollapsibleList = new CollapsibleList(section, 20);
			this.tagButtons = section.down('ul').select('.button').collect(function(element) {
				return new FilterByTagButton(element);
			});
		}
		
		var search_all = $('search_all_wikis');
		if (search_all) search_all.observe('click', this.onSearchAllClick);
	},
	
	onSearchAllClick: function(e)
	{
		e.stop();
		var hash = $H(window.location.search.toQueryParams());
		// clear out the pagination params... before redirecting to cross-group search page
		hash.unset('startIndex');
		hash.unset('howMany');
		window.location = '/search/?' + hash.toQueryString();
	}
};

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 FilterByTypeButton = Class.create(Button, {
	type:      'toggle',
	options:   [null, 'selected'],
	onExecute: function(btn)
	{
		// get the array of all the currently selected types (minus 'all' since it's handled below).
		var types = btn.element.up('.module').select('.button.selected').invoke('getAttribute', 'name').without('all');
		// 'all' can't be combined with other types and deselecting all types implies that you want to see everything
		if (types.length == 0) {
			types = ['all'];
		}
		// set the types, convert back into a URL, and redirect.
		var hash = $H(window.location.search.toQueryParams());
		var q = hash.get('q');
		if (q) {
			hash.set('q', q.replace(/\+/g, ' ')); // #7369021
		}
		hash.set('kind', types);
		hash.unset('startIndex'); // #6687242
		window.location = [window.location.pathname, hash.toQueryString()].join('?');
	}
});

// a tag in the search results sidebar
var FilterByTagButton = Class.create(Button, {
	type:      'toggle',
	options:   [null, 'selected'],
	onExecute: function(btn)
	{
		// get the array of all the tags in the querystring...
		var hash = $H(window.location.search.toQueryParams());
		var tags = hash.get('tag') || [];
		if (!Object.isArray(tags)) {
			tags = [tags.toString()];
		}
		var q = hash.get('q');
		if (q) {
			hash.set('q', q.replace(/\+/g, ' ')); // #7369021
		}
		tags = tags.collect(function(t) { return t.replace(/\+/g, ' '); }); // #6798636
		// add or remove the button's tag based on the button state...
		var tagname = btn.element.getAttribute('title');
		var tags = (btn.getValue() == 'selected') ? tags.concat(tagname) : tags.without(tagname);
		// set the tags, convert back into a URL, and redirect.
		hash.set('tag', tags);
		hash.unset('startIndex'); // #6687242
		window.location = [window.location.pathname, hash.toQueryString().replace(/\%20/g, '+')].join('?');
	}
});

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