/*
  Lazy load of Feeds Widget javascript/css
 */
aol_feeds_subscribe = { version: "1.2" };

/* wrapper call */
aol_feeds_subscribe.setText = function(text) {
   aol_feeds_subscribe_lazyload.pendingCalls.push( {
      method: 'setText', text: text});
}

/* wrapper call */
aol_feeds_subscribe.setReaders = function(readers) {
   aol_feeds_subscribe_lazyload.pendingCalls.push( {
      method: 'setReaders', readers: readers});
}

/* wrapper call */
aol_feeds_subscribe.setAlertLink = function(link) {
   aol_feeds_subscribe_lazyload.pendingCalls.push( {
      method: 'setAlertLink', link: link});
}

/* wrapper call */
aol_feeds_subscribe.show = function(refEl, atomFeed, rssFeed, anchor) {
   aol_feeds_subscribe_lazyload.pendingCalls.push({
      method: 'show', refEl: refEl,
      atomFeed: atomFeed, rssFeed: rssFeed, anchor: anchor });
   aol_feeds_subscribe_lazyload.load();
   return null; // no popup element
}
 
/* wrapper call */
aol_feeds_subscribe.showBlog = function(refEl, blogURL, anchor) {
   aol_feeds_subscribe_lazyload.pendingCalls.push({
       method: 'showBlog', refEl: refEl, blogURL: blogURL, anchor: anchor});
   aol_feeds_subscribe_lazyload.load();
   return null; // no popup element
}

aol_feeds_subscribe_lazyload = {};

/* load feeds_subscribe_fr_fr.js with notification and timeout */
aol_feeds_subscribe_lazyload.load = function (callback, timeout) {
   if ( ! aol_feeds_subscribe_lazyload.loading ) {

      if ( typeof callback == 'function' && typeof timeout == 'number' ) {
         aol_feeds_subscribe_lazyload.loadCallback = callback;
         setTimeout("aol_feeds_subscribe_lazyload.loadTimeout()", timeout); 
         aol_feeds_subscribe_lazyload.loadCallback = callback;
      }

      aol_feeds_subscribe_lazyload.loading = true;
      var base = aol_feeds_subscribe_lazyload.getBase();

      // css
      if ( ! aol_feeds_subscribe_lazyload.cssLoaded() ) {
         var link = document.createElement('link');
         link.setAttribute('type', 'text/css');
         link.setAttribute('rel', 'stylesheet');
         if ( ! window.ActiveXObject ) { // not IE
            link.setAttribute('href', base + 'feeds_subscribe_fr_fr.css');
         }
         else {
            //Set href with a delay - workaround for IE6 (AimPages env)
            link.setAttribute('id', 'aol_feeds_subscribe_css');
            setTimeout('aol_feeds_subscribe_lazyload.setLinkHref()', 10);
         }
         document.getElementsByTagName('head')[0].appendChild(link);
      }

      // js 
      var script = document.createElement('script');
      script.setAttribute('type', 'text/javascript');
      if ( ! window.ActiveXObject ) { // not IE
         script.setAttribute('src', base + 'feeds_subscribe_fr_fr.js');
      }
      else {
         //Setting src with a delay - workaround for IE6 (AimPages env)
         script.setAttribute('id', 'aol_feeds_subscribe_js');
         setTimeout('aol_feeds_subscribe_lazyload.setScriptSrc()', 20);
      }
      document.getElementsByTagName('head')[0].appendChild(script);

   }
}

aol_feeds_subscribe_lazyload.setLinkHref = function () {
   var link = document.getElementById('aol_feeds_subscribe_css');
   if ( link ) {
      var base = aol_feeds_subscribe_lazyload.getBase();
      link.setAttribute('href', base + 'feeds_subscribe_fr_fr.css');
   }
}

aol_feeds_subscribe_lazyload.setScriptSrc = function () {
   var script = document.getElementById('aol_feeds_subscribe_js');
   if ( script ) {
      var base = aol_feeds_subscribe_lazyload.getBase();
      script.setAttribute('src', base + 'feeds_subscribe_fr_fr.js');
   }
}

aol_feeds_subscribe_lazyload.loading = false;
aol_feeds_subscribe_lazyload.timeoutId;
aol_feeds_subscribe_lazyload.loadCallback;
aol_feeds_subscribe_lazyload.pendingCalls = [];

aol_feeds_subscribe_lazyload.isLoaded = function() {
   return ( typeof aol_feeds_subscribe.getHtml != 'undefined' );
}

aol_feeds_subscribe_lazyload.loadTimeout = function() {
   if ( aol_feeds_subscribe_lazyload.loadCallback ) {
      var callback = aol_feeds_subscribe_lazyload.loadCallback;
      aol_feeds_subscribe_lazyload.loadCallback = null;
      aol_feeds_subscribe_lazyload.timeoutId = null;
      callback(false);
   }
}

// Find the widget base
aol_feeds_subscribe_lazyload.getBase = function() {
   var scripts = document.getElementsByTagName('script');
   for ( var i=0; i < scripts.length; i++) {
      var src = scripts[i].src;
      if ( src && src.length ) {
         if ( src == 'feeds_subscribe_lazyload_fr_fr.js' ) {
            return "";
         }
         if ( /\/feeds_subscribe_lazyload_fr_fr.js$/.test(src) ) {
            return src.replace(/(^.*\/)feeds_subscribe_lazyload_fr_fr.js/, "$1");
         }
      }
   }
   return null;
};

aol_feeds_subscribe_lazyload.cssLoaded = function() {
   var links = document.getElementsByTagName('link');
   for ( var i=0; i < links.length; i++) {
      var href = links[i].href;
      if ( href && href.length ) {
         if ( href.indexOf('feeds_subscribe_fr_fr.css') >= 0 ) {
            return true;
         }
      }
   }
   return false;
};

