/*****************************************************************************
 * 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 ImagePreview() {}

// Our object's functions.
ImagePreview.prototype.upload = function(id, result_id, submit_id) {

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

    $('#'+submit_id+'_loading').ajaxStart(function() {
            $(this).show();
    });
    $('#'+submit_id+'_loading').ajaxComplete(function() {
            $(this).hide();
    });
    $.ajaxFileUpload({
        url: '/cgi-bin/directory/imagepreview.cgi',
        secureuri: false,
        fileElementId: id,
        dataType: 'json',
        success: function (data, status) {
            if (data.status == 1) {
                // Show the preview image.
                $('#'+result_id).hide();
                $('#'+result_id).empty();
                $('#'+result_id).append('<img src="'+data.data.image_thumbnail+'" />');
                $('#'+result_id).slideDown("slow");

                // Set the 'real' image as our form input.
                $('#'+submit_id).val(data.data.image);
            }
            else {
                // Show the error message
                $('#'+result_id).hide();
                $('#'+result_id).empty();
                $('#'+result_id).append(data.message);
                $('#'+result_id).slideDown("slow");

                // Don't set the form input since there was an error.
                $('#'+submit_id).val();
            }
        },
        error: function (data, status, e) {
            alert(e);
        }
    });
    return false;
};

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