/**
*
* @author Sven Kummer
* @company rapidsoft GmbH
*
*/

(function (jQuery){

    this.version = '(1.0)';
    this.scripts = {};
    this.wlId = 0;

    this.load = function(boxId, boxPosition, userId, boxPositionSwitch) {

        var box = $('#profileBox_' + boxPositionSwitch + '_' + boxId);
            box.html('<img src="/wl/' + this.wlId + '/images/stuff/loader.gif" alt="Loading..." border="0" />');

        $.get(this.scripts.loader, {box_id: boxId, box_position: boxPosition, user_id: userId, switch_position: boxPositionSwitch}, function(data) {
            box.html($('data', data).text());
        });

    },

    this.saveOrder = function(userId, position, order) {

        $.get(this.scripts.saveorder + '?box_id[]=' + $.makeArray(order).join('&box_id[]='), {user_id: userId, position: position}, function(data) {
        });

    },

    this.remove = function(boxId) {

        $.get(this.scripts.remove, {box_id: boxId}, function(data) {

            if ($('#profileBox_left_' + boxId).length > 0) {
                $('#profileBox_left_' + boxId).remove();
            }

            if ($('#profileBox_right_' + boxId).length > 0) {
                $('#profileBox_right_' + boxId).remove();
            }

        });

    },

    this.create = function(type) {

        $this = this;

        $.get(this.scripts.creator + '?type=' + type, {}, function(data) {

            $('#profile_box_new').hide();

            var boxId = $('id', data).text();
            var boxPosition = $('position', data).text();

            if ($('message', data).text() == 'OK') {

                var box = $('<div></div>');
                    box.attr('id', 'profileBox_' + boxPosition + '_' + boxId);
                    box.addClass('profileBox');
                    box.addClass('profileBox' +  upperFirst(boxPosition));

                $('#container-' + boxPosition).append(box);

                $.base.callMethod('refresh');

                $this.load(boxId, boxPosition, user_id, boxPosition);

            } else {
                $.base.setSystemMessage($.base.phraseGet('profile_box_new_type_already'));
            }

        });

    }



    jQuery.profileBox = this;

    return jQuery;

})(jQuery);