The Plugin stops its work and logs an error, if a required option is missing
[openx] / jquery.openx.js
index 5825593..f156ba5 100644 (file)
@@ -25,7 +25,7 @@
 
   var
 
-  domain, id, node,
+  settings, _zones, _options, domain, id, node,
 
   count = 0,
   slots = {},
   output = [];
 
 
-  $.openx = function( server, zones, options ) {
+  $.openx = function( zones, options ) {
+
+    var name, src, errors = [], i;
 
     if (domain) {
-      if (console.error)
+      if (console.error) {
         console.error('jQuery.openx was already initialized!');
+        console.log('Configured zones: ', _zones);
+        console.log('Configured options: ', _options);
+      }
+      return;
+    }
+
+    _zones = zones;
+    _options = options;
+
+    if (!options.server)
+      errors.push('Required option "server" is missing!');
+    if (errors.length > 0) {
+      if (console.error) {
+        for (i=0; i<errors.length; i++)
+          console.error('Required option "server" is missing!');
+        console.log('options: ', options);
+      }
       return;
     }
 
-    domain = document.location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server;
+    settings = $.extend(
+      {
+        'protocol': document.location.protocol
+      },
+      options
+      );
 
-    var
-    name,
-    src = domain;
+    domain = settings.protocol + '//';
+    domain += settings.server;
+    if (settings.protocol === 'http:' && settings.http_port)
+      domain += ':' + settings.http_port;
+    if (settings.protocol === 'https:' && settings.https_port)
+      domain += ':' + settings.https_port;
 
     /**
      * Without this option, jQuery appends an timestamp to every URL, that
@@ -56,7 +83,7 @@
      */
     $.ajaxSetup({ cache: true });
 
-    src += "/www/delivery/spc.php?zones=";
+    src = domain + '/www/delivery/spc.php?zones=';
 
     /** Only fetch banners, that are really included in this page */
     for(name in zones) {