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

ImagePreview.prototype.initDigits = function(inClass) {
  $('.'+inClass).each(ImagePreview.checkDigits);
  $('.'+inClass).keypress(ImagePreview.checkDigits);
  $('.'+inClass).keyup(ImagePreview.checkDigits);
  $('.'+inClass).change(ImagePreview.checkDigits);
};

ImagePreview.prototype.checkDigits = function() {
  var string = $(this).val();
  var match = string.match(/^(\d+(\.\d*)?)?$/);
  var labelId;
  if (!match) {
    $(this).addClass('input_error');
    labelId = '#'+$(this).attr('id')+'_label';
    var length = $(labelId).children().length;
    if (! length) {
        $(labelId).append('<div class="error_message">(Real Numbers Only)</div>');
    }
  }
  else {
    $(this).removeClass('input_error');
    labelId = '#'+$(this).attr('id')+'_label';
    $(labelId).children().remove();
  }
};

ImagePreview.prototype.removeImageUpload = function(name) {
  $('#' + name + '_result').empty();
  $('#' + name).val('');
  $('#' + name + '_description').val('');
  return false;
};

ImagePreview.prototype.showImageUpload = function(name) {
  $('#' + name + '_container').show('slow');
  return false;
};

// Our object's functions.
ImagePreview.prototype.upload = function(id, result_id, submit_id) {
    $('#'+submit_id+'_loading').ajaxStart(function() {
        $(this).show();
    });
    $('#'+submit_id+'_loading').ajaxComplete(function() {
        $(this).hide();
    })
    $('#'+submit_id+'_text').ajaxStart(function() {
        $(this).show();
    });
    $('#'+submit_id+'_text').ajaxComplete(function() {
        $(this).hide();
    })

    $.ajaxFileUpload({
        url: '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();
