/*
 * jQuery Safari search plug-in 1.0
 *
 * Based on Sven Fuchs prototype version
 * http://www.artweb-design.de/2007/4/16/safari-beautiful-search-input-tag-fixed
 * http://docs.jquery.com/Plugins/SafariSearch
 * 
 * $("input.search").safariSearch();
 *
 * Copyright (c) 2009 Matt Levy
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
 
(function($){
	$.fn.safariSearch = function(options) {
    	
		var defaults = {
			autosave: "agtsys.ru", // change this to your own domain name or whatever else 
      		results: 5,
      		label: ""
		};
		var options = $.extend(defaults, options);
		
		var isSafari = $.browser.safari;
		
		return $(this).each(function() {
			if (isSafari) {
				make_search_input(this);
			} else {
				fake_search_input(this);
			}
		});
		
		function make_search_input(element) {
			element.type = "search";
			$(element).attr("placeholder", defaults.label);
			$(element).attr("autosave", defaults.autosave); 
			$(element).attr("results", defaults.results);
			if ($(this).val() == defaults.label) $(this).val("");
		}
		
		function fake_search_input(element) {
			$(element).click(function() { 
				if ($(this).val() == defaults.label) $(this).val(""); 
			});
			
			$(element).focus(function() { 
				$(this).parent().attr("class", "search-container search-active"); 
			});
			
			$(element).blur(function() { 
				$(this).parent().attr("class", "search-container"); 
				if ($(this).val() == "") $(this).val(defaults.label);
			});
			
			var container = $("<div class=\"search-container\"></div>").css("width", element.clientWidth + "px");
			container.append("<div class=\"search-left\"></div>");
			container.append("<div class=\"search-right\"></div>");
			$(container).insertBefore(element);
			container.append(element);
		}
		
		beautify_search_inputs();
 	};
})(jQuery);