(function($) {
    /**
     * FrostWire Search Box Widget jQuery Plugin by Gubatron - Feb 2010
     *
     * Usage:  $('#someDivId').fwSearchBox([settings])
     *
     * $.fwSearchBox(settingsOptional)
     * Creates a Torrent Search Text Box that points to http://frostwire.com/search
     *
     * @param settingsOpt:map - Optional Settings. Keys are:
     * textBoxValue:string    The text to show before the user enters search keywords
     * textBoxDefaultColor:string    CSS Hex color of the textbox text before user enters data
     * textBoxBackgroundColor:string CSS Hex color of the textbox background
     * textBoxUserColor:string       CSS Hex color of the textbox text the user enters
     * textBoxHeight:string          CSS Height of the textbox
     * textBoxWidth:string           CSS Height of the textbox
     * textBoxFontSize:string        CSS Font Size of the textbox
     * hasButton:boolean             Makes a submission button optional if false
     * buttonBackgroundColor:string  CSS Hex color of the submission button background
     * buttonColor:string            CSS Hex color of the text on the button
     * buttonDefaultValue:string     Text on the submission button
     * buttonHeight:string           CSS Height of the submission button
     * buttonMarginLeft:string       CSS distance. Separation between the searchbox and the button
     * buttonFontSize:string         CSS Font Size of the submission button
     */
    $.fn.fwSearchBox = function (settingsOpt) {
	var settings = {
	    'textBoxValue':'Find Torrents',
	    'textBoxDefaultColor':'#0ad',
	    'textBoxBackgroundColor':'#fff',
	    'textBoxUserColor':'#000',
	    'textBoxHeight':'11px',
	    'textBoxWidth':$.browser.msie ? '140px':'110px',
	    'textBoxFontSize':'10px',
	    'hasButton':true,
	    'buttonBackgroundColor':'#6AB7E8',
	    'buttonColor':'#fff',
	    'buttonDefaultValue':'Torrent Search',
	    'buttonHeight':'19px',
	    'buttonMarginLeft':'3px',
	    'buttonFontSize':$.browser.msie ? '9px' : '10px',
	}

	if (settingsOpt != undefined) {
	    $.extend(settings,settingsOpt);
	    debug('user passed new settings')
	}

	//What happens to every jQuery match.
	this.each(function () {
		var text = $('<input type="text" value="'+settings.textBoxValue+'"/>');
		text.css({'color':settings.textBoxDefaultColor,
			    'width':settings.textBoxWidth,
			    'height':settings.textBoxHeight,
			    'font-size':settings.textBoxFontSize,
			    'background-color':settings.textBoxBackgroundColor,
			    'font-weight':'bolder',
			    'float':'left'});
		
		text.bind('focus',function() {
			if (text.attr('value') == settings.textBoxValue) {
			    text.attr('value','');
			    text.css({'color':settings.textBoxUserColor});
			}
		    });

		text.bind('blur',function() {
			if (text.attr('value') == '') {
			    text.attr('value',settings.textBoxValue);
			    text.css({'color':settings.textBoxDefaultColor});
			}
		    });

		var onSubmit = function(event) {
		    if (event.keyCode == '13') {
			//nothing happens if you haven't typed a valid query
			if (text.attr('value') == settings.textBoxValue
			    || text.attr('value') == '') {
			    text.focus();
			    text.attr('value') = '';
			    return;
			}
			
			var path='/search/'+escape(text.attr('value'));
			var domain=location.href.split("http://")[1].split("/")[0];
			location.replace('http://'+domain+path);
		    }
		};

		text.bind('keypress',onSubmit);

		$(this).append(text);

		if (settings.hasButton) {
		    var button = $('<input type="button" value="'+settings.buttonDefaultValue+'"/>');
		    button.css({'background-color':settings.buttonBackgroundColor,
				'color':settings.buttonColor,
				'height':settings.buttonHeight,
				'font-size':settings.buttonFontSize,
				'font-weight':'bolder',
				'float':'left',
				'margin-left':settings.buttonMarginLeft,
				'padding-bottom':'5px',
				'position':'relative',
				'top':'-1px'
				});
		    button.bind('click',function() { onSubmit({keyCode:'13'}) });

		    $(this).append(button);
		}
	});

	return this;
    }
}) (jQuery);

/**
 * Debug on the console if one is available.
 */
function debug(obj) {
    if (window.console != undefined)
	window.console.log('$.fwSearchBox: ');
        window.console.log(obj)
}
