
var dialog_width = 980;
var dialog_height = 768;
var top_x = 0, top_y = 0, bottom_x = 0, bottom_y = 0;
var globalfield;

function rt_crop(src, width, height, field) {
    
    globalfield = field;
    var obj = $('img[src^="' + src + '"]');
    
    //init dialog
    if (!$('#dialog_' + field).length) {
        obj.append('<div id="dialog_' + field + '" title="{title_crop_dialog}"></div>');
    }
    
    var img_src = obj.attr('src');
    
    if (img_src == undefined || img_src == '') {
        return false;
    }
    
    var img_load = new Image();
    img_load.src = img_src;
    
    $('#dialog_' + field).html('<img src="' + img_src + '?' + new Date().getTime() + '" />').dialog({
        buttons: [
            {
              text: '{ln_crop}',
              click: function() {
                  
                  var self_dialog = $(this);
                  var scr_crop = img_src.split('?')[0];
                  //send post
                  $.post(window.location.pathname, {upload_action: 'crop', field: field, src: scr_crop, top_x: top_x, top_y: top_y, bottom_x: bottom_x, bottom_y: bottom_y, width: width, height:height}, function() {
                        
                        var hash = '?' + new Date().getTime();
                        obj.attr('src', img_src + hash);
                        if (obj.parent().attr('href') != undefined) {
                            obj.parent().attr('href', img_src + hash);
                        }
                        self_dialog.dialog('close');
                  });
              }
          },
          {
              text: '{btn_cancel}',
              click: function() {
                $(this).dialog('close');
              }
          },
          {
            text: '{ln_del_image}',
            click: function() {
                remove_image($('.delete-anchor[rel="' + field + '"]'), field);
                $(this).dialog('close');
              }
          }        
      ],
      
      width: (img_load.width == 0 || img_load.height == 0 || img_load.width > dialog_width || img_load.height > dialog_height) ? dialog_width : img_load.width + 50,
      height: (img_load.width == 0 || img_load.height == 0 ||img_load.width > dialog_width || img_load.height > dialog_height) ? dialog_height : img_load.height + 150
      
    });
    
    var remove_btn = ($('.crop[rel="' + field + '"]').attr('remove_btn') !== undefined) ? 1 : 0;
    if (remove_btn === 1) {
        $('.crop[rel="' + field + '"]').removeAttr('remove_btn');
        $('#dialog_' + field).parent().find('.ui-button:eq(2)').css('display','none');
        $('#dialog_' + field).parent().find('.ui-dialog-titlebar-close').css('display','none');
        
    } else {
        $('#dialog_' + field).parent().find('.ui-button:eq(3)').css('display','none');
        $('#dialog_' + field).parent().find('.ui-dialog-titlebar-close').css('display','');
    }
    
    $('#dialog_' + field + ' img').Jcrop({
        setSelect: [0, 0, width, height],
        onSelect: set_size,
        onChange: showCoords,
        bgOpacity: .4,
        aspectRatio: width/height
    });
    
    return true;
}

function set_size(el) {

    top_x = el.x;
    top_y = el.y;
    bottom_x = el.x2;
    bottom_y = el.y2;
}

function showCoords(el) {
    
    var cur = Math.floor(el.w) + ' x ' + Math.floor(el.h);
    var wrapper = $('#dialog_' + globalfield).parent();
    
    if (!wrapper.find('.ui-dialog-title span').length) {
        wrapper.find('.ui-dialog-title').html(wrapper.find('.ui-dialog-title').html() + '<span>' + cur + '</span>');
    } else {
        wrapper.find('.ui-dialog-title span').html(cur);
    }
    
}