// Classes and code for dynamic ads reloading (without reloading the whole page)

var GoogleAdIframe = new Class({
  getOptions: function() {
    return {
      className: '', // A CSS class name, to identify this iframe.
      frameBorder: '0', // [0=on, 1=off] Specifies whether or not to display a border around an iframe
      scrolling: 'no', // [no, yes, auto] Specifies whether or not to display scrollbars in an iframe
      google_ad_height: '600', // Specifies the height of the iframe. The unit is pixel (but this number is only literal)
      google_ad_width: '160', // Specifies the width of the iframe. The unit is pixel (but this number is only literal)
      google_ad_format: '160x600_as' // To see all available formats: http://www.adsense-generator.com/
    };
  },
  initialize: function(src, container, options) {
    this.setOptions(this.getOptions(), options);
    this.container = container;
    this.src = src;
    this.insertIframeIntoContainer();
  },
  createIframe: function(){
    var iframeElement = new Element('iframe');
    iframeElement.setProperties({
      src: this.src,
      frameBorder: this.options.frameBorder,
      scrolling: this.options.scrolling,
      height: this.options.google_ad_height,
      width: this.options.google_ad_width
    })
    iframeElement.addClass(this.options.className);
    return iframeElement;
  },
  insertIframeIntoContainer: function(){
    if ($(this.container)) {
      var iframeElement = this.createIframe();
      $(this.container).adopt(iframeElement);
    }
  }
});

GoogleAdIframe.implement(new Events);
GoogleAdIframe.implement(new Options);

var RechargeableGoogleAdIframe = GoogleAdIframe.extend({
  insertIframeIntoContainer: function(){
    if ($(this.container)) {
      var iframeElement = this.createIframe();
      $(this.container).innerHTML = "";
      $(this.container).adopt(iframeElement);
    }
  },
  reload: function(){
    this.insertIframeIntoContainer();
  }
});

RechargeableGoogleAdIframe.implement(new Events);
RechargeableGoogleAdIframe.implement(new Options);

var googleAdIframes = [];

function createAdIframes() {
  if (navigator.appName == 'Microsoft Internet Explorer') {
    /*
     * If the browser is IE, we must replace the googleAdContainer with only one iFrame. We need to do this
     * because IE doesn't allow to modify the source url of Google ads generated iframes for security reasons.
     * Other browsers allow this.
     * Tested with IE 7 and 8.
     * Additionally we couldn't use this trick for all the browsers because FF versions 4 to 8 (and beyond?) 
     * display the following bug when reloading ads:
     * https://bugzilla.mozilla.org/show_bug.cgi?id=635548
     */
    // SideBar Ad
    googleAdIframes[0] = new RechargeableGoogleAdIframe('ads/sideBody.inc', 'sideBarContainer', {
      className: 'googleAdIframe'
    });
    
    // BottomBar Ad
    googleAdIframes[1] = new RechargeableGoogleAdIframe('ads/bottomBody.inc', 'bottomBarContainer', {
      className: 'googleAdIframe',
      google_ad_height: '90',
      google_ad_width: '728',
      google_ad_format: '728x90_as'
    });
  }
}

function reloadAdsOnSubmitForm(){
  $$('#btn_submit', '#btn_submit_bottom').each(function(button){
    $(button).addEvent('click',  refreshAds);
  })
}

function refreshAds() {
  if (navigator.appName == 'Microsoft Internet Explorer') {
    /*
     * If the browser is IE, then we have an own iframe (one iframe per ad). 
     * And we must reload that ad deleting this iframe and creating a new one.
     */
    var googleAds = $$('.googleAdContainer iframe.googleAdIframe'); // e.g.: ad = ".googleAdIframe" (my own iframe)
    googleAds.each(function(ad){
      for ( var i = 0; i < googleAdIframes.length; i++) {
        googleAdIframes[i].reload()
      }
    });
    
  } else {
    /*
     * If the browser is NOT IE, then we have the Google ad iFrame inside our div.googleAdContainer.
     * That ad (e.g.: #aswift_0) contains another iFrame (e.g.: #google_ads_frame1). 
     * So we can reload that ad, adding a rsnd field (with the new Time as value), to the final iFrame.
     */
    var timeField = "&rsnd=";
    
    var googleAds = $$('.googleAdContainer iframe'); // e.g.: ad = "#aswift_0"
    googleAds.each(function(ad){
      var documentElm = ad.contentWindow.document;
      var htmlElm = documentElm.childNodes[documentElm.childNodes.length - 1];
      var iframeElm = $E('iframe', htmlElm);
      if (iframeElm) {
        var posTimeField = iframeElm.src.indexOf(timeField);
        var newTime = timeField + new Date().getTime();
        if (posTimeField == -1) {
          var src = iframeElm.src + newTime;
          iframeElm.src = src;
        } else {
          var src = iframeElm.src.substr(0, posTimeField) + newTime;
          iframeElm.src = src;
        }
      }
    });
    
  }
}

window.addEvent('load', function() {
  createAdIframes();

  reloadAdsOnSubmitForm();
});

