/*
#
# jquery.safarisearch.js
#
# by Ludwig Pettersson
# <http://luddep.se>
#
 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
*/
(function() {

	var default_settings = { 
		placeholder: "Search..."
    };
    
    
    function create_clearbutton() {
        var clearbutton = $("<span></span>").addClass("ss_clear");
                    
        clearbutton.mousedown(function() {
            $(this).css("background-position", "0 -68px");
        });
                    
        clearbutton.mouseup(function() {
            var btn = $(this);
            var input = btn.next();
            input.val('');
            input.focus();
            btn.hide();
            btn.css("background-position", "0 -57px");
        });
        
        return clearbutton;
    }
    
    function attach_events(input, settings) {
        input.focus(function() {
            if(input.val() == settings.placeholder) {
                input.val('');
                input.removeClass('ss_empty');
            }
        });
        input.blur(function() {
            if($(this).val().length == 0) {
                $(this).val(settings.placeholder)
                $(this).addClass('ss_empty');
                if($(this).prev().attr('class') == 'ss_clear')
                    $(this).prev().hide();
            }
        });
        input.keyup(function() {
            if($(this).prev().attr('class') != 'ss_clear') {
                var clearbutton = create_clearbutton();
                $('span.safarisearch input').before(clearbutton);
            } else {
                if($(this).val() != '')
                    $(this).prev().show();
                else
                    $(this).prev().hide();
            }
        });
    }
    
	jQuery.fn.safarisearch = function(settings) {
		var settings = jQuery.extend(default_settings, settings);
		this.each(function(i, input){
            try {
                $('input[type=search]').attr('type', 'text');
                var input = $(input);
                input.addClass('ss_empty');
            
                var wrap = $("<span></span>").addClass("safarisearch");
                var left = $("<span></span>").addClass("ss_left");
                var right =  $("<span></span>").addClass("ss_right");
                input.wrap(wrap).wrap(right).wrap(left);
            
                attach_events(input, settings);
                input.val(settings.placeholder);
            }catch(ex){}
		});
	};
	
})();