(function($) {
  $.fn.mmdialog = function(options) {
    var options = $.extend({}, $.fn.mmdialog.defaults, options);

    return this.each(function() {
      var opts = options;
      var $this = $(this);
      var label = opts['sTitle'];

      if (opts['type'] == "productopties") {
        $this.click(function() {
          $("body").append('<div id="overlay"></div>');
          $("#overlay").css("background", "#000");
          createDialog();
          var productID = $this.attr('id');
          getForm(productID);
        });
      }

      function getForm(productID) {
        $.ajax({
          url: '/ajax/productoptions.php',
          type: "POST",
          data: "productID="+productID,
          dataType: "json",
          success: function(msg) {
            if (msg['error'] === undefined) {
              $(".content", $("#dialogcontainer")).html(msg['sHtml']);
              $("select", $("#dialogcontainer")).change(function() {
                var value = $(this).val();
                $(this).siblings("img").hide();
                $(this).siblings("img[alt='"+value+"']").show();
              });
              $("input[name='Toevoeging']").change(function() {
                $(this).siblings("table").hide();
                switch($(this).val()) {
                  case "Borduren":
                  $("#bordurensub").show();
                  break;
                  case "Drukken":
                  $("#drukkensub").show();
                  break;
                  case "Graveren":
                  $("#graverensub").show();
                  break;
                }
              });

              resizeDialog();
            } else {
              destroyDialog();
              return;
            }
          }
        });
      }

      function createDialog() {
        var html = '<div id="dialogcontainer">'
        + '<div class="window dialog">'
        + '<div class="title"></div>'
        + '<div class="content"></div>'
        + '<div style="padding:5px">'
        + '<input type="button" value="Toevoegen aan offerte" class="button toevoegen buttonnext"/>'
    + '<input type="button" value="Venster sluiten" class="button annuleren buttonannuleren"/>'
        + '</div>'
        + '</div>';
        var dialogwindow= $(html);
        $("body").append(dialogwindow);
        $("#dialogcontainer .title").html("Product toevoegen aan offerte");
        resizeDialog();
        $(window).resize(function() {
          resizeDialog();
        });

        $("input.toevoegen", $("#dialogcontainer")).click(function() {
          $("#offerteForm").submit();
          //destroyDialog();
        });

        $("input.annuleren", $("#dialogcontainer")).click(function() {
          destroyDialog();
        });
      }

      function destroyDialog() {
        $("#dialogcontainer").remove();
        $("#overlay").remove();
        $(window).unbind('resize');
      }

      function resizeDialog() {
        var dialogwindow= $("#dialogcontainer");
        var winH = $(window).height();
        var winW = $(window).width();
        dialogwindow.css('top', winH/2-dialogwindow.height()/2);
        dialogwindow.css('left', winW/2-dialogwindow.width()/2);
      }
    });
  }
  $.fn.mmdialog.defaults = {
    type: "productopties",
    sTitle: "Kies opties"
  }
})(jQuery);
