/**
 * Developed for PowerFuse
 * By Brookside Studios
 *
 * @author Matthew Dunham <mdunham@geekrescue.com>
 */

if (typeof(jQuery) === 'undefined') {
	// Shoot, no jQuery, lets load it.
	document.write('<script src="http://code.jquery.com/jquery-1.3.2.min.js">\x3C/script>');	
	// Reload this script
	document.write('<script src="/js/fuse.quicksearch.js">\x3C/script>');
} else {
	jQuery(function($){
		if (typeof($.widget) == 'undefined') {
			
			$.ajax({
				url: '/js/css/smoothness/jquery-ui-1.8.11.custom.css',
				dataType: 'css',
				success: function() {
					$('<link rel="stylesheet" type="text/css" href="/js/css/smoothness/jquery-ui-1.8.11.custom.css" />').appendTo("head");
				}
			});

			$.getScript("/js/jquery-ui-1.8.11.custom.min.js", function(){
				initFuseQuickSearch();
			});
			
		} else {
			initFuseQuickSearch();
		}
	});
	
	function initFuseQuickSearch() {
		// This piece is commented out because efen fuses only wants the search on the view inventory page.
		//$('<div id="quickSearch"><input class="init" name="quicksearch" value="Quick Search" type="text" /></div>').insertBefore($('.header ul'));
		if ($('#quickSearch').length) {
			
			setTimeout("$('#quickSearch').show();", 500);
			
			$('#quickSearch input').focus(function(){ 
				if($(this).val() == $(this).attr('defaultValue')) {
					$(this).val('').removeClass('init');
				}
			});
			
			$('#quickSearch input').blur(function(){
				if($(this).val() == '') {
					$(this).val($(this).attr('defaultValue')).addClass('init');
				} 
			});
			
			$('#quickSearch input').autocomplete({
				source: "/js/quicksearch.php",
				minLength: 2,
				select: function( event, ui ) {
					$('#quickSearch input').val(ui.item.value);
					submitSearch();
				}
			});
			
			$('#quickSearch input').keydown(function(e){
				if (e.keyCode == 13) {
					//submitSearch();
				}
			});
		}
	}
	
	function submitSearch() {
		var fuse = $('#quickSearch input').val();
		window.location.href = '/quote_form.php' + fuse;
	}
}

