/**
 * xase5ePlaceholder - Xtensible And Simple Evented html5 Enhancement
 * ************************************************************************************************************** 
 * Adds placeholder attribute-functionality for browser with no native support
 * 
 * @author      Noel Bossart
 * @copyright   2010 by Noël Bossart
 * @namespace   $.fn
 * @class       xase5ePlaceholder
 * @extends     $.fn 
 * @version     0.1.0 Alpha
 */

     $.fn.xase5ePlaceholder = function(userOptions) {
        var $t = $(this);

        if ($t.length < 1) {
            return;
        }

        var userOptions = userOptions || {};
        // setup holds the custom events and will be applayed to "this"
        var setup = customEvents;

        if (typeof(userOptions.extend) === 'function') {
            // create copy of customEvents
            var userCustomEvents = $.extend(true, {},
            customEvents);
            // extend base options
            setup = userOptions.extend(userCustomEvents) || setup;
        }

        // caching the inputs...
        var $inputs = $t.find('input[placeholder]');
        if ($inputs.length < 1) {
            return;
        }
        $t.data('$xase5ePlaceholder', $inputs);

        var options = $.extend({},
        $.fn.xase5ePlaceholder.defaults, userOptions);
        // iterates over all custzom event arrays and alls each function inside. This functions bind
        // the custom events on the Element and pass the arguments and options provided to the plugin
        // to all those custom events
        for (customEvent in setup) {
            var $self = $t;
            if (setup[customEvent] instanceof Array) {
                for (var i = 0; i < setup[customEvent].length; i++) {
                    setup[customEvent][i].call($self, options);
                }
            } else {
                setup[customEvent].call($self, options);
            }
        }

        // Browsercheck:
        for (var browser in options.compatible) {
            if ($.browser[browser] && $.browser.version >= options.compatible[browser]) {
                return;
            }
        }

        $t.trigger('initialize.' + options.namespace);
        return this;
    }


    /**
	 * @name xase5ePlaceholder.defaults
	 * @description these are the plugin defaults with a public interface.
	 */
    $.fn.xase5ePlaceholder.defaults = {
        namespace: 'xase5ePlaceholder',
        // insert compatible browsers:
        compatible: {
            safari: 4.0,
            chrome: 1.0
        },
        className: 'placeholder'
    };


    /**
	 * @name nbSlideshow.base           
	 * @extends $.fn
	 * @description Contains our default set of events that can be triggered
	 */

    var customEvents = {
        initialize: [function(options) {
            this.bind("initialize." + options.namespace,
            function(e) {
                if (e.isDefaultPrevented()) {
                    return;
                }
                var $t = $(this);
                $t.data('$xase5ePlaceholder').trigger('blur.' + options.namespace).addClass('searchtext');
            });
        }],
        focus: [function(options) {
            $(this).data('$xase5ePlaceholder').bind("focus." + options.namespace,
            function(e) {
                if (e.isDefaultPrevented()) {
                    return;
                }
                var $t = $(this);
                if ($t.val() == $t.attr('placeholder')) {
                    $t.val('').removeClass('searchtext');
                }
            });
        }],
        submit: [function(options) {
            $(this).bind("submit." + options.namespace,
            function(e) {
                if (e.isDefaultPrevented()) {
                    return;
                }              
                $(this).data('$xase5ePlaceholder').each(function() {
                    var $t = $(this);
                    if ($t.val() == $t.attr('placeholder')) {
                        $t.val('').removeClass('searchtext');
                    }
                });
            });
        }],
        blur: [function(options) {
            $(this).data('$xase5ePlaceholder').bind("blur." + options.namespace,
            function(e) {
                if (e.isDefaultPrevented()) {
                    return;
                }
                var $t = $(this);
                if ($t.val() == '') {
                    $t.val($t.attr('placeholder')).addClass('searchtext');
                }
            });
        }]
    };

    $(document).ready(function() {
        $('form').xase5ePlaceholder();
    });


