/*****************************************************************************
 * Gossamer Links
 *
 *   Website  : http://gossamer-threads.com/
 *   Support  : http://gossamer-threads.com/scripts/support/
 *   Revision : $Id: imagepreview.js,v 1.6 2008/11/03 21:05:23 aaron Exp $
 *
 * Copyright (c) 2008 Gossamer Threads Inc.  All Rights Reserved.
 * Redistribution in part or in whole strictly prohibited. Please
 * see LICENSE file for full details.
 *****************************************************************************/

// The object wrapper function.
function FileUpload() {}

// Our object's functions.
FileUpload.prototype.upload = function(id) {
    var loading_id = id + '_loading';
    var result_id  = id + '_result';
    var upload_id  = id + '_upload';
    var name_id    = id + '_name';

// initializing the value to fakeinput if existed
    var fakeinput = $('#' + upload_id).siblings('.textinput');
    if (fakeinput.length == 1) {
        var path = $('#' + upload_id).val().replace(/c:\fakepath/i, '');
        fakeinput.attr({ readonly: true }).val(path);
    }

    var $loading =  $('#' + upload_id).siblings('.upload-loading');
    $loading.show();

    $.ajaxFileUpload({
        url: '/cgi-bin/directory/fileupload.cgi',
        secureuri: false,
        fileElementId: upload_id,
        dataType: 'json',
        success: function (data, status) {
            // Show the upload result text.
            $('#'+result_id).hide();
            $('#'+result_id).empty();
            $('#'+result_id).append(data.message);
            $('#'+result_id).slideDown("slow");
            $('#'+id).val(data.data.file_path);
            $('#'+name_id).val(data.data.file_name);
            $loading.hide();
        },
        error: function (data, status, e) {
            $loading.hide();
            alert(e);
        }
    });
    return false;
};

// Create the instance of our object.
var FileUpload = new FileUpload();
