/**
 * Replace link targets to ajax request
 *
 * @category  Public
 * @package   Javascript
 * @package   Cms
 * @author    Jochen Werner <jochen.werner@netresearch.de>
 * @version   $Id$
 */
var pattern    = 'a.autoreplace.*, form.autoreplace.*';
var imgLoading = 'loadingAnimation';
(function(jQuery) {
    jQuery.fn.autoreplace = function(o) {
        return this.each(function() {
            new jQuery.autoreplace(this, o);
        });
    };
    /**
     * Start replaceing
     */
    jQuery.autoreplace = function (e, o) {
        var object = jQuery(e);
        if (!object.attr('action')) {
            jQuery.link(object);
        } else {
            jQuery.form(object);
        }
    };

    /**
     * Replace link
     */
    jQuery.link = function (link) {
        /*
         * Add click event on matched ajax link
         */
        link.click(function() {
            /*
             * Set target url to request
             */
            try {
                var url = link.attr('href').replace(/^#/, '');
            } catch(e) {
                return;
            }
            /*
             * Get target html container from class name splitting
             * e.g. "ajax content" means node with id "content"
             */
            var classNames = link.attr('className').split(' ');
            if (classNames[2] == 'small') {
                imgLoading = 'loadingSmall';
            }
            if (classNames[2] == 'small-dark') {
                imgLoading = 'loadingSmall-dark';
            }
            var target     = jQuery('#' + classNames[1]);

            /*
             * Add loading image while request ist running
             */
            jQuery.loading(target);
            /*
             * Remove the href attribute, otherwise the browser reload the page
             * while timeout
             */
            link.removeAttr('href');
            /*
             * Send GET request to target url
             */
            jQuery.get(url, null, function(data) {
                /*
                 * Write response data to target node
                 */
                target.html(data);
                /*
                 * Add the removed href attribute for next operations
                 */
                link.attr('href', url);
                /*
                 * Initalize link replaceing again
                 */
                jQuery(document).ready(function() {
                    jQuery(pattern).autoreplace();
                });
            });
        });
    };
    /**
     * Replace form
     */
    jQuery.form = function (form) {
        try {
            var classNames = form.attr('className').split(' ');
            if (classNames[2] == 'small') {
               imgLoading = 'loadingSmall'
            }
        } catch(e) {}
        /*
         * Set target url to request
         */
        form.submit(function() {
            var a = form.attr('action');
            if (-1 == form.attr('action').search(/javascript+/)) {
            var action = "javascript:jQuery.postForm('";
                action+= form.attr('id') + "', '";
                action+= form.attr('action').replace(/^#/, '')+"');";

                form.attr('action', action);
            }
        });
    }

    /**
     * Add loading image while request ist running
     */
    jQuery.postForm = function (id, url) {

        var targetUrl = url;
        var form      = jQuery('#'+id);
        /*
         * Add the removed href attribute for next operations
         */
      //  form.attr('action', url);

        /*
         * Build data object
         */
        data = new Object();
        $(':input').each(function() {
            if (this.type == 'checkbox') {
                if (this.checked == true) {
                    data[this.name] = this.value;
                }
            } else {
                data[this.name] = this.value;
            }
        });
        data.ajax = 'true';

        var classNames = form.attr('className').split(' ');
        var target     = jQuery('#' + classNames[1]);
        target.attr('display', 'block');
        /*
         * Add loading image while request ist running
         */
        jQuery.loading(target);
        /*
         * Send POST request to target url
         */
        jQuery.post(targetUrl, data, function(response) {
            /*
             * Write response data to target node
             */
            target.html(response);
            /*
             * Initalize link replaceing again
             */
            jQuery(document).ready(function() {
                jQuery(pattern).autoreplace();
            });
        });
    };

    /**
     * Add loading image while request ist running
     */
    jQuery.loading = function (target) {

        var container = jQuery('<div />').attr('id',  'loadingAnimationContainer');
        container.html(jQuery('<img />').
            attr('id',  'loadingAnimation').
            attr('src', '/images/'+imgLoading+'.gif').
            attr('alt', '')
        );
        target.html(container);
    };

})(jQuery);
/*
 * Initalize link replaceing
 */
jQuery(document).ready(function() {
    jQuery(pattern).autoreplace();
});