(function () {
  "use strict";
    
  function receiveMessage(event) {
    if (event.data.url) {
      window.location.href = event.data.url;
    };
    return false;
  }

  window.addEventListener("message", receiveMessage, false);
  
  var PaymentForm = function (options) {
    if (!options.url) {
      return false;
    };
    
    this.url = options.url;
    this.styles = options.style;
    this.type = options.type;
    this.anchor = options.id;
    this.iframeDivId = 'paymentForm';

    this.size = { height: 270, width: 430 };
  };

  PaymentForm.prototype.isMobile = function () {
    if ( navigator.userAgent.match(/Android/i) ||
      navigator.userAgent.match(/iPhone/i) ||
      navigator.userAgent.match(/Windows Phone/i) ) {
      return true;
    } else {
      return false;
    }
  }

  PaymentForm.prototype.prepareIframe = function (url) {
    var iframeDiv = document.createElement("div");
    iframeDiv.setAttribute("id", this.iframeDivId);
    iframeDiv.style.display = "none";

    var iframe = document.createElement("IFRAME");
    iframe.setAttribute("src", url);
    iframe.setAttribute("frameborder", "0");
    iframe.style.width = this.size.width + "px";
    iframe.style.height = this.size.height + "px";

    iframeDiv.appendChild(iframe);

    if (this.anchor && document.getElementById(this.anchor)) {
      document.getElementById(this.anchor).appendChild(iframeDiv);
    } else {
      document.body.appendChild(iframeDiv);
    };

    var styles = this.styles;

    iframe.onload = function() {
      iframe.contentWindow.postMessage({ css: styles }, "*");
    };
    return iframe;
  }

  PaymentForm.prototype.buildOverlayForm = function (url) {
    var linkId = '#' + this.anchor;
    var size = this.size;
    var that = this;
    var dialogId = "dialogWindow";

    var emptyDiv = document.createElement('div');
    emptyDiv.setAttribute("id", dialogId);
    document.body.appendChild(emptyDiv);
    
    var reg = new RegExp('([http|https].+\/)checkout')
    var path =  reg.exec(this.url)[1];
    var cssPath = path + 'stylesheets/jquery-ui.min.css'
    $('body').append('<link rel="stylesheet" href="' + cssPath + '">');

    $(linkId).click(function(event) {
      event.preventDefault();

      if (that.isMobile()) {
        window.open(that.url);
        return false;
      }

      $('#' + dialogId).dialog({
        modal: true,
        width: size.width - 5,
        height: size.height + 60,
        show: {
          effect: "fadeIn"
        },
        hide: {
          effect: "fadeOut"
        },
        open: function () {
          if ($('#' + dialogId).children('iframe').size() == 0) {
            document.getElementById(dialogId).appendChild(that.prepareIframe(url + "&iframe=true"));
          };
        }
      });
      
    });
  };

  PaymentForm.prototype.buildInlineForm = function(url) {
    if (!this.isMobile()) {
      url = url + "&iframe=true";
    };

    this.prepareIframe(url);
    $('#' + this.iframeDivId).show();
  }

  PaymentForm.prototype.buildForm = function () {
    if (options.style) {
      this.styles = options.style;
    };

    if (this.type == 'overlay') {
      this.buildOverlayForm(this.url);
      return false;
    };

    if (this.type == 'inline') {
      this.buildInlineForm(this.url);
      return false;
    };
  };

  window.PaymentForm = PaymentForm;
}());

@@ -486,16 +486,87 @@
 Form';%0A%0A
+    if (options.size) %7B%0A      this.size = options.size;%0A    %7D else %7B%0A  
     this
@@ -587,10 +587,10 @@
 ht: 
+3
 2
-7
 0, w
@@ -599,11 +599,20 @@
 th: 
-430
+350 %7D;  %0A   
  %7D;%0A
@@ -1207,24 +1207,118 @@
 der%22, %220%22);%0A
+    iframe.style.display = %22block%22;%0A    iframe.style.margin = %22auto%22;%0A%0A    if (this.size) %7B%0A  
     iframe.s
@@ -1354,16 +1354,18 @@
 + %22px%22;%0A
+  
     ifra
@@ -1406,16 +1406,23 @@
  + %22px%22;
+%0A    %7D;
 %0A%0A    if
@@ -1768,24 +1768,122 @@
 es %7D, %22*%22);%0A
+      $('a#closeWindow').click(function() %7B%0A        $('#dialogWindow').dialog('close');%0A      %7D);%0A
     %7D;%0A    r
@@ -2687,11 +2687,12 @@
 dth 
-- 5
++ 10
 ,%0A  
@@ -2723,9 +2723,9 @@
 t + 
-6
+3
 0,%0A 
@@ -3027,16 +3027,158 @@
 rue%22));%0A
+            if ($('#closeWindow').size() == 0) %7B%0A              $('.ui-dialog').prepend(%22%3Ca href='#' id='closeWindow'%3E%3C/div%3E%22);%0A            %7D;%0A
         
@@ -3192,16 +3192,16 @@
       %7D%0A
+
       %7D)
@@ -3206,15 +3206,8 @@
 %7D);%0A
-      %0A
     

Update paymentr.js with new functionality: - new close button - block and auto margin for iframe - allow to pass size params