// ==UserScript==
// @name         devSearchAll
// @namespace    http://www.five-by-five.se/v2/code/greasemonkey/
// @description  Makes all the new v5 searches available from all pages.
// @include      http://*.deviantart.com/*
// @include      http://deviantart.com/*
// ==/UserScript==

/*
	devSearchAll
	Copyright (c) 2006 Kalle Räisänen, <http://xork.deviantart.com/>.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* 0.1.3 - 2006-09-08. */

(function()
{
	var topmiddle = document.getElementById('top-middle');
	if(!topmiddle)
		return;
	var sform     = topmiddle.getElementsByTagName('form')[0];
	var inputs    = sform.getElementsByTagName('input');
	var isect, itext, ibutt;
	var currsect  = 0;
	var searches  = ['browse', 'shop', 'chat', 'news', 'today', 'forum'];
	var titles    = ['Art', 'Shop', 'Chat', 'News', 'Today', 'Forum'];
	var visible   = !Boolean(GM_getValue('devsearchall_visible', true));

	var cont = document.createElement('form');

	for(var i = 0; i < inputs.length; i++) {
		if(inputs[i].name == 'q')
			itext = inputs[i];
		else if(inputs[i].name == 'section')
			isect = inputs[i];
		else if(inputs[i].type == 'submit')
			ibutt = inputs[i];
	}

	sform.style.width = '40%';
	ibutt.style.position = 'absolute';
	ibutt.style.right  = '2px';
	itext.style.width = '70%';

	topmiddle.appendChild(cont);

	makemenu();
	togglebuttons();

	function setsect()
	{
		currsect = Number(String(this.id).replace(/[^\d]/g, ''));

		isect.value = searches[currsect];
		sform.submit();
	}


	function togglebuttons()
	{
		visible = !visible;
		for(var i = 0; i < searches.length; i++) {
			var btn = document.getElementById('devsearchall-btn' + i);
			if(btn)
				btn.style.display = (visible ? 'inline' : 'none');
		}
		var btn = document.getElementById('devsearchall-toggle');
		btn.value = visible ? '<<' : '>>'; //visible ? '\u00AB' : '\u00BB';
		btn.style.marginLeft = visible ? '4px' : '0';
		GM_setValue('devsearchall_visible', visible);
	}

	function makemenu()
	{
		cont.innerHTML = '';
		for(var i = 0; i < searches.length; i++) {
			if(searches[i] == isect.value) {
				currsect = i;
				continue;
			}
			var btn = makebutton('devsearchall-btn' + i, titles[i]);

			btn.addEventListener('click', setsect, false);
			cont.appendChild(document.createTextNode(' '));
			cont.appendChild(btn);
		}
		var btn = makebutton('devsearchall-toggle', '');
		btn.style.letterSpacing = '-.4em';
		btn.style.paddingRight  = '.4em';
		btn.style.fontWeight    = 'normal';
		btn.addEventListener('click', togglebuttons, false);
		cont.appendChild(btn);
	}

	function makebutton(id, value)
	{
		var btn = document.createElement('input');
		btn.type = 'button';
		btn.id = id;
		btn.value = value;
		btn.setAttribute(
			'style',
			'-moz-border-radius: 4px; '
			+ 'background-color: #374341; '
			+ 'border: 2px solid #899B91; '
			+ 'color: #899B91; '
			+ 'font: bold 12px Verdana, Arial, Helvetica, sans-serif; '
			+ 'height: 23px; '
			+ 'margin: 4px -5px 0 0; '
			+ 'padding: 0; '
		);
		return btn;
	}
})();

