/**
 * Each picture with "avatar" class is replaced by default avatar in case of error
 */ 
function avatarReplace()
{
    $('.avatar').error(function() {
        $(this).attr('src', '/resources/images/avatars/default.png'); 
    });
}
$(avatarReplace);


/**
 *
 */ 
function ajaxUploadInit()
{
    if ($('#sendPicture').length) {
        new AjaxUpload('#sendPicture', {
            // Location of the server-side upload script
            // NOTE: You are not allowed to upload files to another domain
            action: '/user/profile/upload-ajax',
            // File upload name
            name: 'userfile',
            // Additional data to send
            data: {},
            // Submit file after selection
            autoSubmit: true,
            // The type of data that you're expecting back from the server.
            // HTML (text) and XML are detected automatically.
            // Useful when you are using JSON data as a response, set to "json" in that case.
            // Also set server response type to text/html, otherwise it will not work in IE6
            responseType: false,
            // Fired after the file is selected
            // Useful when autoSubmit is disabled
            // You can return false to cancel upload
            // @param file basename of uploaded file
            // @param extension of that file
            onChange: function(file, extension){},
            // Fired before the file is uploaded
            // You can return false to cancel upload
            // @param file basename of uploaded file
            // @param extension of that file
            onSubmit: function(file, extension) {
                $('#picture').attr('src', '/resources/images/ajax-loader.gif');
            },
            // Fired when file upload is completed
            // WARNING! DO NOT USE "FALSE" STRING AS A RESPONSE!
            // @param file basename of uploaded file
            // @param response server response
            onComplete: function(file, response) {
                $('#picture').attr('src', '/resources/images/avatars/' + response + '.png?'+(new Date()).getTime());
            }
        });
    }
}
$(ajaxUploadInit);


/**
 *
 */
function confirmLinksInit()
{
    $('a.confirm').click(function() {
        return window.confirm('Êtes-vous certain de vouloir faire cette action ?');
    });
}
$(confirmLinksInit);

/**
 *
 */
function homeColumns()
{
    if ( $('#home-lastperf .perf-comments-content').length
      && $('#home-lastcontre .perf-comments-content').length
    ) {
        var ph = $($('#home-lastperf .perf-comments-content')[0]).height();
        var ch = $($('#home-lastcontre .perf-comments-content')[0]).height();
        if (ph > ch) {
            $($('#home-lastcontre .perf-comments-content')[0]).height(ph);
        } else {
            $($('#home-lastperf .perf-comments-content')[0]).height(ch);
        }
    }
}
$(homeColumns);


/**
 *
 */ 
function popupLinks()
{
    $('a.popup').click(function() {
        window.open(this.href);
        return false;
    });
}
$(popupLinks);