(function($) {
  $.mw = $.extend($.mw?$.mw:{}, {
    photo: {
      attachEditor: function($scope) {
        $(".mw_photo_delete", $scope).click(function(e) {
          if (!confirm("Do you really want to delete this photo?")) 
          {
            return e.preventDefault();
          }
        });
        $('.full_res').click(function(e) {
          window.open(this.href);
          return e.preventDefault();
        });
        $(".mw_photo_edit_action", $scope).click(function(e) {
          e.preventDefault();
          var action = this.href;
          $.getJSON(action, function(data) {
            $("IMG.mwphoto", $scope).each(function() {          
              this.src = this.src+'?'+Math.random();
              $(this).css({'width':'auto','height':'auto'});
            });
          });
        });
        
      }
    }});
  $(function() {
    if (!$.mw.photoSettings) {
      alert('The settings haven\'t been set');
    };
    $("#mw_photo_gallery_edit_form input.delete").click(function(e) {
      if (!confirm("Do you really want to delete this gallery?  This will delete all subgalleries, and may leave 'orphaned' photos."))
      {
        return e.preventDefault();
      }
    });
    $.mw.photo.attachEditor();
    
    // Gallery Ordering
    if ($.fn.sortable && $('.mw_photo_list').length && $('.mw_photo_list a.edit').length) {
      var $ajaxNotifier = $('Updating order for gallery');
      $("<div>To change the order of photos, click and drag.</div>").insertBefore($(".mw_photo_list"));
      $(".mw_photo_list").sortable({update: function() {
        var order = [];
        var galleryid = $('.mw_photo_list')[0].id.replace(/^mw_photo_gallery_/, '');
        $(".mw_photo_list .photo").each(function() {
          order.push(this.id.replace(/^mw_photo_/, ''));
        });
        $ajaxNotifier.insertBefore($('.mw_photo_list'));
        $.ajax({
          type: 'POST',
          url: $.mw.photoSettings.orderUrl.replace(/_id/, galleryid),
          dataType: 'json',
          data: {'order':order.join(':') },
          success: function(data) {
            $ajaxNotifier.remove();
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert('There was an error when trying to update order');
          }
        });
      }});
      
    }
    
    $('.mw_photo_list .photo .edit').click(function(e) {
      var $photo = $(this).parents('.photo');
      $.ajax({
        type:'GET', 
        url: this.href,
        success: function(data, textStatus) {
          $.prompt(data, {
            prefix: 'photo_edit',
            buttons: { 'Save': 'save', 'Delete Photo': 'delete'},
            submit: function(btn, text) {
              if (btn == 'delete') {
                var srcURL = $('.mw_photo_delete', text)[0].href;
                $.ajax({ type: 'GET', url: srcURL, success: function() {
                  $photo.remove();
                }});
              } else if (btn == 'save') {
                $('input[@name="longdesc"]', text).val(FCKeditorAPI.GetInstance('longdesc').GetHTML(true));
                $('form',text).ajaxSubmit({
                  dataType: 'json',
                  success: function(data) {
                    $('img', $photo).replaceWith(data.thumb);
                  },
                  error: function(data) {
                    $.prompt('There was an error saving that information');
                  }
                
                });
                
              }
              return true;
            }
          });
          $.mw.photo.attachEditor($('.photo_edit'));
          $('.photo_edit .richtext').rte('/mwscript/css/rte.css');
        },
        dataType: 'html'
      });
      return e.preventDefault();
    });
  });
})(jQuery);
