
/**
 * Usage
 * $('a.selected').ajaxlink({});
 *
 * @param {Object} options
 */

(function($){

    var opts;

//-----------------------------------------------
    $.fn.ajaxlink = function(options)
    {
// build main options
        opts = $.extend({}, $.fn.ajaxlink.defaults, options);

        return this.each(function()
        {
            var target=opts.target;
            var inserttype=opts.inserttype;
            var data=opts.data;
            var ajaxtype=opts.ajaxtype;
            var beforepost=opts.beforepost;
            var afterpost=opts.afterpost;
            var self=this;

            var href=$(this).attr('href');

            var data_target=$(this).data('target');

            if(data_target) target=data_target;

            if( $(this).data('not_ajax')==true ) return;


            //Bind------
            $(this).unbind('click.ajaxlink');

            $(this).bind('click.ajaxlink', function(event)
            {
                event.preventDefault();

                if(href=='#' || href=='') return;

                beforepost(this);

                $.ajax({
                    type: ajaxtype,
                    url: href,
                    data: data,
                    dataType: "html",
                    cache: false,
                    success: function(result)
                    {
                        switch(inserttype)
                        {
                            case 'innerhtml' : $(target).html(result); break;
                            case 'append' : $(target).append(result); break;
                            case 'replace' : $(target).replaceWith(result); break;
                            case 'after' : $(target).after(result); break;
                            case 'before' : $(target).before(result); break;
                        }

                        afterpost(result, self);
                    }
                });

            });

        });

    };
//-----------------------------------------------
    $.fn.ajaxlink.defaults =
        {
            target		: 'body',
            inserttype	: 'innerhtml', // [innerhtml, append, replace, after, before]
            ajaxtype	: 'POST', //[GET, POST]
            data		: {},
            beforepost		: function () {},
            afterpost		: function () {}
        };


})(jQuery);



//-----------------------------------------------
(function($){

    var opts;

    /**
     * ajaxdataform Jquery object
     * @param {Object} options
     *
     * json response
     * {
     * errors	: { 'email' : '', 'username' : '' },
     * data		: { 'data1' : 'xy' },
     * msg		: 'message text',
     * success	: 1
     * }
     *
     */
    $.fn.ajaxdataform = function(options)
    {
// build main options
        opts = $.extend({}, $.fn.ajaxdataform.defaults, options);

        return this.each(function()
        {
            var mainform=this;


            var target=opts.target;
            var inserttype=opts.inserttype;
            var data=opts.data;
            var ajaxtype=opts.ajaxtype;
            var datatype=opts.datatype;
            var buttonselector=opts.buttonselector;
            var buttoncontainer=opts.buttoncontainer;
            var updateinputselector=opts.updateinputselector;
            var formmsgselector=opts.formmsgselector;
            var beforepost=opts.beforepost;
            var afterpost=opts.afterpost;
            var scrolltoerror=opts.scrolltoerror;


            var button=$(this).find(buttonselector);
            /*var button=$(this).find(buttoncontainer+' '+buttonselector);
            var buttoncontainer=$(this).find(buttoncontainer);*/

            var action=$(mainform).attr('action');


            $(this).unbind('submit.ajaxdataform');
            $(this).bind('submit.ajaxdataform', function(event)
            {
                $(button).trigger('click');
                return false;
            });

            if( opts.submitonenter )
            {
                $(this).find('input[type=text],input[type=password]').unbind('keyup.ajaxdataform');
                $(this).find('input[type=text],input[type=password]').bind('keyup.ajaxdataform', function(event)
                {
                    if(event.keyCode==13) $(button).trigger('click');
                });
            }

            //Bind------
            $(button).unbind('click.ajaxdataform');

            $(button).bind('click.ajaxdataform', function(event)
            {
                event.preventDefault();

                var cnt=beforepost(mainform, this);

                if(cnt==false) return;

                //$(mainform).find('.rght').removeClass('error');
                $(mainform).find('.field').removeClass('error');


                $(mainform).addClass('ondatasend');
                //$(buttoncontainer).find(formmsgselector).html( '' );

                $(mainform).find(formmsgselector).removeClass('red');
                $(mainform).find(formmsgselector).html( '' );

                var adddata=data();

                var buttonrel=$(this).attr('rel');

                if(buttonrel)
                    adddata+='&option='+buttonrel;

                $.ajax({
                    type: ajaxtype,
                    url: action,
                    data: $(mainform).serialize()+adddata,
                    dataType: datatype,
                    success: function(result)
                    {
                        if( inserttype!='' && datatype=='html' )
                        {
                            switch(inserttype)
                            {
                                case 'innerhtml' : $(target).html(result); break;
                                case 'append' : $(target).append(result); break;
                                case 'replace' : $(target).replaceWith(result); break;
                                case 'after' : $(target).after(result); break;
                                case 'before' : $(target).before(result); break;
                            }
                        }

                        if(datatype=='json')
                        {
                            $(mainform).find('.field .status').html('<span class="ico ok">Ok</span>');

                            if( !result || typeof(result)!='object' )
                            {
                                alert('Server response error');
                            }
                            else
                            {
                                if(result.success==0)
                                {
                                    for (idx in result.errors)
                                    {
                                        var value=result.errors[idx];

                                        $(mainform).find('.field_'+idx+' .msg').html(value);
                                        $(mainform).find('.field_'+idx).addClass('error');

                                        $(mainform).find('.field_'+idx+' .status').html('<span class="ico error">Error</span>');
                                    }

                                    if( scrolltoerror && $(mainform).find('.field.error').length>0)
                                    {
                                        $('body,html').animate({scrollTop: $(mainform).find('.field.error:first').offset().top }, 500);
                                        //$(document).scrollTo( $(mainform).find('.field.error:first'), 500, {axis:'y'} );
                                    }

                                    $(mainform).find(formmsgselector).addClass('red');
                                }
                                else
                                {
                                    if( updateinputselector && result.data.update )
                                    {
                                        $(mainform).find(updateinputselector).val(result.data.update);
                                    }

                                    if( result.data.cre_nick )  $(mainform).find('.cre_nick').html(result.data.cre_nick);
                                    if( result.data.cre_datum )  $(mainform).find('.cre_datum').html(result.data.cre_datum);
                                    if( result.data.mod_nick )  $(mainform).find('.mod_nick').html(result.data.mod_nick);
                                    if( result.data.mod_datum )  $(mainform).find('.mod_datum').html(result.data.mod_datum);
                                }

                                //$(buttoncontainer).find(formmsgselector).html( result.msg );
                                $(mainform).find(formmsgselector).html( result.msg );
                            }
                        }

                        afterpost(result, mainform);

                        $(mainform).removeClass('ondatasend');
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); }
                });

            });

        });

    };
//-----------------------------------------------
    $.fn.ajaxdataform.defaults =
        {
            target			: '',
            inserttype		: '', // [innerhtml, append, replace, after, before]
            ajaxtype		: 'POST', //[GET, POST]
            datatype		: 'html', // [html, json]
            data			: function() { return ''; },
            buttonselector	: '.submit',
            buttoncontainer	: '.cnt_button',
            formmsgselector	: '.formmsg',
            submitonenter	: false,
            scrolltoerror	: true,
            updateinputselector : 'input[name=update]',
            beforepost		: function () {},
            afterpost		: function () {}
        };


})(jQuery);






//-----------------------------------------------
/** @projectDescription jQuery Serialize Anything - Serialize anything (and not just forms!)
 * @author Bramus! (Bram Van Damme)
 * @version 1.0
 * @website: http://www.bram.us/
 * @license : BSD
 */

(function($) {

    $.fn.serializeAnything = function(type) {

        var toReturn    = [];
        var els         = $(this).find(':input').get();

        if(type==null) type='array';

        $.each(els, function() {
            if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password/i.test(this.type)))
            {
                var val = $(this).val();

                if( type=='string' )
                    toReturn.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( val ) );

                else if( type=='array' )
                {
                    var iobj={ name : this.name, value :  val  }
                    toReturn.push( iobj );
                }
            }
        });

        if( type=='string' )
            return toReturn.join("&").replace(/%20/g, "+");
        if( type=='array' )
            return toReturn;


    }

})(jQuery);


