var optional_ads={
  window_chrome_width: 24,  //the width of website chrome including window frame and scrollbars
  website_width: 970,       //the width of the website from shadow to shadow
  allow_right_column: false,
  allow_left_column: false,
  left_ad_column: null,
  right_ad_column: null,
  ad_column_height: 0,
  offset_website: false,
  ad_spacing: 8,
  init:
    function() {
      //We require 152 pixels for an additional ad column, and 304 pixels for
      //two ad columns. Where there is space for two, the site remains central
      //where there is space for one, it is offset left. If none, optional ads
      //will not be displayed.
      var screenwidth=$(window).width()-this.window_chrome_width;
      if (screenwidth - this.website_width >= 304) {
        this.allow_right_column = true;
        this.allow_left_column = true;
        this.offset_website = false;
      } else if (screenwidth - this.website_width >= 152) {
        this.allow_right_column = true;
        this.allow_left_column = false;
        this.offset_website = true;
        $('body').addClass("display-optional-ads");
      } else {
        this.allow_right_column = false;
        this.allow_left_column = false;
      }
      
      if (this.allow_right_column || this.allow_left_column) {
        $(window).bind('load', function(){
          optional_ads.init2();
        });
      }
    },
  init2:
    function() {
      //now we need to generate the containers for the adverts and populate them with adverts.
      if (this.allow_right_column) {
        this.right_ad_column = $('<div id="optional-ads-right"></div>');
        $('body').append(this.right_ad_column);
        this.right_ad_column.css({
          position: 'absolute',
          top: 136,
          height: (this.ad_column_height = $(document).height()-280),
          width: 160
        });
        this.reposition_boxes();
        this.load_ads();
        $(window).resize(this.reposition_boxes);
      }
    },
  reposition_boxes:
    function() {
      var hpl=Math.round($('#header-position').offset().left);
      optional_ads.right_ad_column.css({ left: hpl+966 });
    },
  load_ads:
    function() {
      var sidebarlist = null;
      
      //get a list of adverts from the server
      $.ajax({
          async: false,
          type: "POST",
          url: "/jqsidebarads.kmod",
          dataType: "xml",
          success: function(data, status) {
            sidebarlist=$(data);
          },
          error: function(request, status, error) {
          }
      });
      if (sidebarlist && sidebarlist.size() != 0) {
        var sidebars = $('sidebar', sidebarlist);
        var position = 0;
        for (var i = 0; i < sidebars.size(); i++)
        {
          var ad = $('<div class="optional-ad" />');
          this.right_ad_column.append(ad);
          ad.css({ position: "absolute", left: 0, top: position, margin: 0, padding: 0 });
          ad.hide();

          $.ajax({
              async: false,
              type: "GET",
              url: "/jqgetadvert.kmod?sidebarid=" + $("id", sidebars.eq(i)).text(),
              dataType: "html",
              success: function(data, status) {
                ad.html(data);
                ad.css({ position: "absolute", top: position });
                if (position+Number(ad.height()) > optional_ads.ad_column_height) {
                  position=-1;
                  return;
                }                
                position += Number(ad.height())+optional_ads.ad_spacing;
                ad.fadeIn(500);
              }
          });
          if (position == -1) {
            break;
          }
        }
      }
    }
};
