/*
 * form object events 
 * eform 1.03
 * krzysztof.martyniak@essyo.pl
 * 
 */

function eform( params ) {
    if(!params) params		= {};
    
    this._style_normal 		= params['normal'] ? params['normal'] : 'normal';
    this._style_focus 		= params['focus'] ? params['focus'] : 'focus';
    this._style_unfocused 	= params['unfocused'] ? params['unfocused'] : 'unfocused';
    this._style_required 	= params['required'] ? params['required'] : 'required';
    
    var self 				= this;

    // update
    this.update = function() {
        $('input, textarea').unbind('focus, blur');
        $('input, textarea').each(function(){
            $(this)
            .attr('tmp',$(this).attr('title'));
            if($(this).attr('value') == '' ) {
                $(this).val($(this).attr('title'));
                $(this).removeClass(self._style_unfocused); 
                $(this).addClass(self._style_normal);
                if($(this).is('.'+self._style_required))
                        $(this).removeClass(self._style_required);
            }
            else {
                if($(this).attr('value') != $(this).attr('title')) {
                    $(this).removeClass(self._style_normal);
                    $(this).addClass(self._style_unfocused);
                }
                else {
                    $(this).removeClass(self._style_unfocused);
                    $(this).removeClass(self._style_focus);
                    $(this).addClass(self._style_normal);
                }
            }
        });

        $('input, textarea').focus(function(){
            if($(this).is('.'+self._style_required))
                $(this).removeClass(self._style_required);
            
            $(this).removeClass(self._style_normal);
            $(this).removeClass(self._style_unfocused);
            $(this).addClass(self._style_focus);
            if($(this).val() == $(this).attr('tmp'))
                $(this).val('');
            $(this).addClass(self._style_focus);
        });
        $('input, textarea').blur(function(){
            if($(this).val() == '') {
                $(this).val($(this).attr('tmp'));
                if($(this).attr('test') == 'done') {
                    $(this).removeClass(self._style_focus);
                }
                else {
                    $(this).removeClass(self._style_focus);
                    $(this).addClass(self._style_normal);
                }
            }
            else{
                $(this).removeClass(self._style_focus);
                $(this).addClass(self._style_unfocused);
                if($(this).is('.'+self._style_required)) {
                    $(this).removeClass(self._style_required);
                    $(this).removeClass(self._style_focus);
                }
            }
        });
    };

    this.update();
    
}

