﻿ (function($) {
     $.fn.mvc_load = function(url, data) {
         if (url == '')
             throw new Error('url cannot be empty.');
         $.mvc_loadUrl = url;
         this.load(url, data, function(data, status) {
             if (status == "error")
                 $.mvc_error();
             else
                 return data;
         });
     };

     $.fn.mvc_gallery = function(mode, callback) {
         $(this).click(function(e) {
             e.preventDefault();
             $('#image-gallery').mvc_load('/ImageGallery/Index/?mode=' + mode);
             $('#image-gallery').dialog('open');
             $('#image-gallery').each(function() {
                 this.callback = callback;
                 this.close = function() {
                     this.callback = null;
                     $(this).dialog('close');
                 };
             });
         });
     };

     $.fn.mvc_ajaxForm = function(options) {
         if (options == null)
             options = {};
         $.mvc_loadUrl = $(this).attr('action');
         options.error = function(request) {
             $.mvc_error();
         };
         this.ajaxForm(options);
     };
 })(jQuery);

 jQuery.extend({
     mvc_get: function(url, data, datatype, callback) {
         if (url == '')
             throw new Error('url cannot be empty.');
         jQuery.mvc_loadUrl = url;
         jQuery.ajax({ url: url, data: data, dataType: datatype, success: function(data) {
             callback(data);
         }, error: function(request, textStatus, errorThrown) {
             jQuery.mvc_error();
         }
         });
     },
     
     mvc_getJSON: function(url, data, callback) {
         jQuery.mvc_get(url, data, 'json', callback);
     },

     mvc_error: function() {
         window.open('/Error?url=' + jQuery.mvc_loadUrl);
     }
 });
