/*
 * @how_to_use
 * to use clear defaul text in input and textarea
 * set in element 'rel="clearVal"'
 *
 * @exemple
 * <input type="text" rel="clearVal" value="default_value" />
 *
 **/
jQuery(function($) {
    $("input[rel^='clearVal']").focus(function(){
        e = $(this);
        if(!e.attr("alt")){
            e.attr("alt", e.val());
        }
        txt = (e.attr("tagName").toLowerCase() == "input")?e.val():e.html();
        if(txt != e.attr("alt")){
            return false;
        }(e.attr("tagName").toLowerCase() == "input")?e.val(""):e.html("");
        e.blur(function(){
            if(e.attr("tagName").toLowerCase() == "input" && e.val() == ""){
                e.val(txt)
            }else if(e.html() == ""){
                e.html(txt);
            }
        });
    });
    $("textarea[rel^='clearVal']").focus(function(){
        e = $(this);
        if(!e.attr("alt")){
            e.attr("alt", e.val());
        }
        txt = (e.attr("tagName").toLowerCase() == "input")?e.val():e.html();
        if(txt != e.attr("alt")){
            return false;
        }(e.attr("tagName").toLowerCase() == "input")?e.val(""):e.html("");
        e.blur(function(){
            if(e.attr("tagName").toLowerCase() == "input" && e.val() == ""){
                e.val(txt)
            }else if(e.html() == ""){
                e.html(txt);
            }
        });
    });
});


(function($) {
    $.extend({
        submitForm : new function(){

            this.err = false;

            this.defaults = {
                loader: '/img/load.gif',
                tipclass: 'form_tooltip_show',
                usetipclass: true,
                container: '.container',
                tipL: 10,
                tipT: 20,
                css: {
                    'display': 'block',
                    'position': 'absolute',
                    'width': '250px',
                    'border': '1px solid #09547B',
                    'background-color': '#B6DBED',
                    'color': '#000',
                    'text-align': 'left',
                    'padding': '5px',
                    'font-weight': 'normal',
                    'z-index': '100'
                }
            };
            
            this.construct = function(options){
                // extending default settings
                defaults = $.extend($.submitForm.defaults, $.submitForm.defaults, options);
                _err = $.extend($.submitForm.err,$.submitForm.err);
                _this = $(this);
                _this.submit(function(){
                    var loader = $("<img />");
                    loader.attr("id", "form-loader");
                    loader.attr("src", $.submitForm.defaults.loader);
                    subBtn =_this.find("input[type=submit]");
                    subBtn.before(loader);
                    var _elements = this.elements;
                    var values = "";
                    $.each(_elements, function() {
                        setParam($(this));
                        values += this.name + "=" +$(this).val()+"&";

                    });
                    if(!$.submitForm._err){
                        subBtn.remove();
                        $.ajax({
                            url: _this.attr("action"),
                            processData: true,
                            data: values+"ajax=true",
                            type: "POST",
                            success: function(resp){
                                _this.before(resp).fadeIn(3000, function(){});
                                _this.fadeOut(300, function(){
                                    _this.remove();
                                })
                            }
                        });
                    }else{
                        $("#form-loader").remove();
                    }
                    return false;
                });
                
            //return this.each(function(e){
            //  $this = $(this);
            // creating span element
            //});
            };

            function setParam(e){

                if (e.hasClass("required") && (($(e).attr("alt") == null || $(e).attr("alt") == "") || $(e).attr("alt") == $(e).val())) {
                    if(!$(e).next("span").hasClass($.submitForm.defaults.tipclass)){
                        var span = $('<span />').attr('class',$.submitForm.defaults.tipclass);
                        if ($.submitForm.defaults.usetipclass==false){
                            span.css($.submitForm.defaults.css);
                        }
                        $(e).after(span.html($(e).attr('title')));
                    }else{
                        $(e).next("span").show();
                    }
                    $.submitForm._err = true;
                    $(e).change(function(){
                        if($(e).next("span").hasClass($.submitForm.defaults.tipclass)){
                            $.submitForm._err = false;
                            $(e).next("span."+$.submitForm.defaults.tipclass).hide();
                        }
                    });
                }
            }
        }
    });
    /**
         * Extending jQuery fn
         */
    $.fn.extend({
        submitForm: $.submitForm.construct
    });
})(jQuery);
