/*
 * jQuery inlineEdit
 *
 * Copyright (c) 2009 Ca-Phun Ung <caphun at yelotofu dot com>
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://yelotofu.com/labs/jquery/snippets/inlineEdit/
 *
 * Inline (in-place) editing.
 *
 * @version 0.2.1
 */

(function($) {

    $.fn.inlineEdit = function(options) {

        options = $.extend({
            hover: 'hover',
            value: '',
            save: '',
            buttonText: 'Save',
            placeholder: '#removed#'
        }, options);

        return $.each(this, function() {
            $.inlineEdit(this, options);
        });
    }

    $.inlineEdit = function(obj, options) {
        var self = $(obj),
            placeholderHtml = '<span class="inlineEdit-placeholder">'+ options.placeholder +'</span>';

        self.value = function(newValue) {
            if (arguments.length) {
                self.data('value', $(newValue).hasClass('inlineEdit-placeholder') ? '' : newValue);
            }
            return self.data('value');
        }

        self.value($.trim(self.text()) || options.value);

        self.bind('dblclick', function(event) {
            var $this = $(event.target);


            if ($this.is(self[0].tagName) || $this.hasClass('inlineEdit-placeholder')) {
                self.html('<input id="inlineedit_' + self.attr('id') + '" class="inputfields editableinput" type="text" value="' +self.text() + '"> <input id="inlinebutton_' + self.attr('id') + '" type="button" class="buttons editablesubmit" value="'+ options.buttonText +'" />');
        	$('#inlinebutton_' + self.attr('id')).bind('click', function(event) {
                	var hash = {
                    		value: $input = $('#inlineedit_' + self.attr('id')).val(),
                    		refname: self.attr('data') ? self.attr('data') : self.attr('id')
                	};
			$('#inlineedit_' + self.attr('id')).attr('value', '');
	                if (($.isFunction(options.save) && options.save.call(self, event, hash)) !== false || !options.save) {
	                    self.value(hash.value);
        	        }
		});
                $('#inlineedit_' + self.attr('id')).bind('blur', function() {
                            if (self.timer) {
                                window.clearTimeout(self.timer);
                            }
                            self.timer = window.setTimeout(function() {
                                self.html(self.value() || placeholderHtml);
                                self.removeClass(options.hover);
                            }, 200);
                        });
                $('#inlineedit_' + self.attr('id')).focus();
            }
        })
        .hover(
            function(){
                $(this).addClass(options.hover);
            },
            function(){
                $(this).removeClass(options.hover);
            }
        );

        if (!self.value()) {
            self.html($(placeholderHtml));
        } else if (options.value) {
            self.html(options.value);
        }
    }

})(jQuery);
