/*
  -- AOL Standard Feed Subscribe Widget --
  Author: Miodrag Kekic (miodrag.kekic@corp.aol.com)

  Credits:
    Dom object is based on Yahoo UILib YAHOO.util.Dom version 0.11.1
    Copyright (c) 2006, Yahoo! Inc. All rights reserved.
    License: http://developer.yahoo.net/yui/license.txt
*/

new function() {  // closure start

var sfsw =  {

   version: "1.3",
   Locale: 'en_us',  // i18n locale
   CustomText: null,        // custom text - i18n etc.
   CustomMyAOLReader: null, // custom myAOL link
   CustomReaders: null,     // custom reader list
   CustomAlertLink: null,   // custom alert link

   UpdateFeeds: null,

   refEl: null,
   popup: null,
   anchor: null,
   popupFirstTime : false,

   // Template patterns
   atomFeed: null,
   rssFeed:  null,

   /*
     * Customize the widget text
   */
   setText: function( text ) {
      this.CustomText = text;
   },

   /*
    * Customize the widget readers
    */
   setReaders: function( readers ) {
      this.CustomReaders = readers;
      this.atomFeed = this.rssFeed  = null;
   },
   setMyAOLReader: function( myAOL ) {
      this.CustomMyAOLReader = myAOL;
      this.atomFeed = this.rssFeed  = null;
   },

   /*
    * Customize the alert link
    */
   setAlertLink: function( link ) {
      this.CustomAlertLink = link;
      this.atomFeed = this.rssFeed  = null;
   },

   show: function(refEl, atomFeed, rssFeed, anchor) {
      var atom = atomFeed || null;
      var rss  = rssFeed  || null;
      setFeedURLs( atom, rss );
      this.anchor = anchor || null;

      var popup = document.getElementById( POPUP_ID );

      if ( ! popup ) {
         popup = document.createElement('div');
         popup.id  = POPUP_ID;
         popup.className = getClass();
         popup.style.position = 'absolute';
         popup.style.left = '0px';
         popup.style.top  = '0px';
         if ( refEl ) {
             popup.style.display = 'none';
             Dom.setOpacity(popup, 0);
         }
         document.body.appendChild(popup);
         popup.innerHTML = getHtml();
         configFeeds(popup);
         if ( refEl ) {
             popup.style.display = 'block';
             setTimeout(function() { fadeInPopup(0); }, 100);
         }
         else {
             setTimeout(function() { refreshImages(); }, 500);
         }
      } 
      else if ( refEl && popup.style.display != 'none' &&
                refEl == this.refEl ){
         closeTwisty();
         popup.style.display = 'none';
         return popup;
      }
      else  {
         if ( this.UpdateFeeds) {
            popup.innerHTML = getHtml();
            configFeeds(popup);
         }
         closeTwisty();
         if ( refEl ) {
            Dom.setOpacity(popup, 0);
         }
         popup.style.display = 'block';
         if ( refEl ) {
            setTimeout(function() { fadeInPopup(0); }, 100);
         }
      }

      this.popup = popup;
      this.refEl = refEl;
      return popup;
   },
 
   // Aol Journals friendly way to show popup using blog
   showBlog: function(refEl, blogURL, anchor) {
      var feeds = blogToFeedURLs(blogURL);
      return this.show(refEl, feeds[0], feeds[1], anchor );
   },

   // Show/hide subscribe popup
   hide: function () {
      var popup = document.getElementById( POPUP_ID );
      if ( popup != null ) {
         closeTwisty();
         popup.style.display = 'none';
      }
      return false;
   },

   // Turn twisty on/off
   toggleTwisty: function() {
      var feedsNode = document.getElementById( URLS_ID );
      var twistyImg = document.getElementById( TWISTY_ID );

      if ( twistyImg == null || feedsNode == null ) { return false; } // error

      if ( feedsNode.style.display == 'none' ) {
         twistyImg.className='twisty_open';
         feedsNode.style.height = '0px';
         feedsNode.style.display = 'block';
         slideDiv(0, false);
      }
      else {
         twistyImg.className='twisty_closed';
         slideDiv(100, true);
      }
      return false;
   }
}; // end of sfsw

// Text for the widget
var LocaleVars = {
   Text: {
      close    : 'Close', 
      text_0   : 'Subscribe To Updates Using myAOL', 
      text_1   : 'Looking for another service?',
      text_2   : 'Get the feed manually',
      text_3   : 'Copy and paste the appropriate feed into your reader.',
      text_3a  : 'Copy and paste this URL feed into your feed reader.',
      text_4   : 'RSS',
      text_5   : 'or',
      text_6   : 'Atom',
      text_7   : 'Set up an alert for the feed'
   },

   // The main reader - myAOL
   myAOLReader: {
     url:  'http://feeds.my.aol.com/add.jsp?url=__FEED__',
     img:  'http://o.aolcdn.com/art/webwidgets/sfsw_v1_3/images/feed-icons/add_myaol.gif',
     text: 'Subscribe via MyAOL',
     whatis_url: 'http://my.aol.com/v.6/locale/en_US/aol/help/tours_start.htm',
     whatis_text: 'what is myAOL?'
   },

   // Feed Readers' definition
   Readers: {
   reader_1: {
     url:  'http://fusion.google.com/add?feedurl=__FEED__',
     img:  'http://o.aolcdn.com/art/webwidgets/sfsw_v1_3/images/feed-icons/googlereader.gif',
     text: 'Subscribe via Google Reader',
     id:   'google'
     },
   reader_2: {
     url:  'http://www.newsgator.com/ngs/subscriber/subext.aspx?url=__FEED__',
     img:  'http://o.aolcdn.com/art/webwidgets/sfsw_v1_3/images/feed-icons/ngsub1.gif',
     text: 'Subscribe via NewsGator',
     id:   'newsgator'
     },
   reader_3: {
     url:  'http://www.bloglines.com/sub/__FEED__',
     img:  'http://o.aolcdn.com/art/webwidgets/sfsw_v1_3/images/feed-icons/sub_modern5.gif',
     text: 'Subscribe via Bloglines',
     id:   'bloglines'
     },
   reader_4: {
     url:  'http://add.my.yahoo.com/content?url=__FEED__',
     img:  'http://o.aolcdn.com/art/webwidgets/sfsw_v1_3/images/feed-icons/addtomyyahoo4.gif',
     text: 'Subscribe via Yahoo Reader',
     id:   'yahoo'
     },
   reader_5: {
     url:  'http://www.netvibes.com/subscribe.php?url=__FEED__',
     img:  'http://o.aolcdn.com/art/webwidgets/sfsw_v1_3/images/feed-icons/add2netvibes.gif',
     text: 'Subscribe via Netvibes',
     id:   'netvibes'
     },
   reader_6: {
     url:  'http://www.pageflakes.com/subscribe.aspx?url=__FEED__',
     img:  'http://o.aolcdn.com/art/webwidgets/sfsw_v1_3/images/feed-icons/pageflakes_add.gif',
     text: 'Subscribe via Pageflakes',
     id:   'pageflakes'
     }
   },

   AlertLink: 'http://alerts.aol.com/ar/dlink/dlink.rr?dl_type=rss&amp;dl_data=__FEED__'

};

var POPUP_ID  = "aol_feeds_subscribe_popup";
var URLS_ID   = "aol_feeds_subscribe_urls";
var TWISTY_ID = "aol_feeds_subscribe_twisty_img";	

function closeTwisty() {
   var feedsNode = document.getElementById( URLS_ID );
   var twistyImg = document.getElementById( TWISTY_ID );
   if ( twistyImg == null || feedsNode == null ) { return false; } // error
   feedsNode.style.display = 'none';
   twistyImg.className='twisty_closed';
}

function blogToFeedURLs(blogURL) {
   var feeds = [];
   var slash = (blogURL.lastIndexOf('/') == (blogURL.length - 1)) ? '' : '/';
   feeds[0] = blogURL + slash + "atom.xml";
   feeds[1] = blogURL + slash + "rss.xml";
   return feeds;
}

function setFeedURLs(atomFeed, rssFeed) {
   var changed = false;
   if ( sfsw.atomFeed != atomFeed ) {
        sfsw.atomFeed = atomFeed;
        changed = true;
   }
   if ( sfsw.rssFeed != rssFeed ) {
        sfsw.rssFeed = rssFeed;
       changed = true;
   }
   if ( changed ) {
      sfsw.UpdateFeeds = {
           atomFeed: sfsw.atomFeed,
           rssFeed:  sfsw.rssFeed 
      };
   }
}

// Find the widget base
 function getBase() {
   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_en_us.js' ) { return ''; }
         if ( /\/feeds_subscribe_en_us.js$/.test(src) ) {
            return src.replace(/(^.*\/)feeds_subscribe_en_us.js/, "$1");
         }
      }
   }
   return null;
};

// Replace feed vars in the widget popup template
function replaceFeedVars(el, attr) {
   var atomFeed = sfsw.UpdateFeeds.atomFeed;
   var rssFeed  = sfsw.UpdateFeeds.rssFeed;
   var feed     = atomFeed || rssFeed;
   if ( el[attr] ) {
      replaceFeedVar( el, attr, '__FEED__', feed);
      replaceFeedVar( el, attr, '__ATOM_FEED__', atomFeed);
      replaceFeedVar( el, attr, '__RSS_FEED__', rssFeed);
      if ( el[attr].indexOf('http://some.domain.com/') == 0 ) {
          el[attr] = el[attr].replace('http://some.domain.com/', '');
      }
   }
}

function replaceFeedVar(el, attr, feedVar, feed) {
   if ( el[attr] ) {
      if ( el[attr].indexOf('/' + feedVar) >= 0 ) {
         el[attr] = el[attr].replace(feedVar, encodeURI(feed));
      }
      else if ( el[attr].indexOf('=' + feedVar) >= 0 ) {
         el[attr] = el[attr].replace(feedVar, encodeURIComponent(feed));
      }
      else if ( el[attr].indexOf(feedVar) >= 0 ) {
         el[attr] = el[attr].replace(feedVar, feed);
      }
   }
};

 function configFeeds(popup) {
   if ( sfsw.UpdateFeeds == null ) { return ; }
   var els = popup.getElementsByTagName('a');
   for ( var i=0; i < els.length; i++ ) {
      if ( els[i].href ) {
         replaceFeedVars( els[i], "href" );
      }
   } 
   els = popup.getElementsByTagName('input');
   for ( var i=0; i < els.length; i++ ) {
      if ( els[i].value ) {
         replaceFeedVars( els[i], "value" );
      }
   } 

   // Hide rss or atom feed if undefined
   var el = document.getElementById('aol_feeds_subscribe_rss_feed');
   if ( el ) {
      el.style.display = sfsw.rssFeed == null ? 'none' : '';
   }
   el = document.getElementById('aol_feeds_subscribe_atom_feed');
   if ( el ) {
      el.style.display = sfsw.atomFeed == null ? 'none' : '';
   }

   if ( sfsw.CustomReaders == null &&
      sfsw.CustomAlertLink == null ) {
      sfsw.UpdateFeeds = null;
    }
}

// First popup display might need repositioning. Use fade-in to mask movement.  
function fadeInPopup(opacity) { 
   var  anchor = sfsw.anchor;
   if ( sfsw.popup != null && sfsw.refEl != null ) {
      setPosition(sfsw.refEl, sfsw.popup);
      Dom.setOpacity(sfsw.popup, opacity/100);
      if ( opacity < 100 ) {
         opacity += 10;
         sfsw.anchor = anchor;
         setTimeout(function(){fadeInPopup(opacity);}, 50);
      }
      else if ( ! sfsw.popupFirstTime ) {
         sfsw.popupFirstTime = true;
         setTimeout(function() {refreshImages();}, 200);
      }
   }
}

// Refresh img src and background images on first widget show (IE workaround)
function refreshImages() {
   if ( sfsw.popup ) {
       var images = sfsw.popup.getElementsByTagName('img');
       for ( var i=0; i < images.length; i++ ) {
           images[i].src = images[i].src;
           var className = images[i].className;
           if ( className ) {
               images[i].className = className;
           }
       }
   }
}

// Slide in/out visual effect
function  slideDiv(percent, slideIn) { 
   var feedsNode = document.getElementById( URLS_ID );
   if ( ! slideIn ) {
      percent += 10;
      if ( percent > 90 ) {
         feedsNode.style.height = 'auto';
         if ( sfsw.anchor ) { setPosition(); }
         return;
      }
   }
   else {
      percent -= 10;
      if ( percent < 10 ) {
         feedsNode.style.display = 'none';
         if ( sfsw.anchor ) { setPosition(); }
         return;
      }
   }
   var height = feedsNode.scrollHeight;
   height = Math.round(height*(percent/100));
   feedsNode.style.height = height + 'px';
   if ( sfsw.anchor ) { setPosition(); }
   setTimeout(function(){slideDiv(percent,slideIn);}, 50);
}

function setPosition(refEl, popup) { 

   refEl = refEl || sfsw.refEl;
   if ( refEl == null || refEl == document.body ) { return; }
   popup = popup || sfsw.popup;

   var anchor   = sfsw.anchor;    
   popup.style.position = 'absolute';

   // Ref el geometry
   var elXY = Dom.getXY(refEl);
   if ( ! elXY ) { return; }
   var elLeft = elXY[0];
   var elTop  = elXY[1];
   var elWidth  = refEl.offsetWidth;
   var elHeight = refEl.offsetHeight;

    // Viewport geometry 
   var vpLeft   = document.documentElement.scrollLeft ||
                  document.body.scrollLeft;
   var vpTop    = document.documentElement.scrollTop  ||
                  document.body.scrollTop ;
   var vpWidth  = Dom.getViewportWidth();
   var vpHeight = Dom.getViewportHeight();

   if ( elLeft < vpLeft) { vpWidth += vpLeft - elLeft; vpLeft = elLeft; }
   if ( vpWidth < (elLeft + elWidth) ) { vpWidth = elLeft + elWidth; }

   var vpLeftMax = vpLeft + vpWidth;
   var vpTopMax  = vpTop  + vpHeight;

    // Popup geometry
   var popupLeft = 0;
   var popupTop  = 0;
   var popupLeftMax = 0;
   var popupTopMax = 0;
   var popupWidth = popup.offsetWidth;
   var popupHeight = popup.offsetHeight;

    // Position 1: bottom left [SW]
   popupLeft = elLeft;
   popupTop  = elTop + elHeight;
   if ( 'SW' == anchor) {
      Dom.setXY(popup, [ popupLeft, popupTop ]);
      return;        
   }
   else if ( ! anchor ) {
      popupLeftMax = popupLeft + popupWidth;
      popupTopMax  = popupTop  + popupHeight;
      if (  popupTop  >= vpTop  && popupTopMax  <= vpTopMax   && 
            popupLeft >= vpLeft && popupLeftMax <= vpLeftMax  ) {
         Dom.setXY(popup, [ popupLeft, popupTop ]);
         sfsw.anchor = 'SW';
         return;
      }
   }

   // Position 2: bottom right [SE]
   popupLeft = elLeft + elWidth - popupWidth;
   popupTop  = elTop + elHeight;
   if ( 'SE' == anchor) {
      Dom.setXY(popup, [ popupLeft, popupTop ]);
      return;        
   }
   else if ( ! anchor ) {
      popupLeftMax = popupLeft + popupWidth;
      popupTopMax  = popupTop  + popupHeight;
      if (  popupTop  >= vpTop  && popupTopMax  <= vpTopMax   && 
            popupLeft >= vpLeft && popupLeftMax <= vpLeftMax  ) {
         Dom.setXY(popup, [ popupLeft, popupTop ]);
         sfsw.anchor = 'SE';
         return;
      }
   }

   // Position 3: top left [NW]
   popupLeft = elLeft;
   popupTop  = elTop - popupHeight;
   if ( 'NW' == anchor) {
      Dom.setXY(popup, [ popupLeft, popupTop ]);
      return;        
   }    
   else if ( ! anchor ) {
      popupLeftMax = popupLeft + popupWidth;
      popupTopMax  = popupTop  + popupHeight;
      if (  popupTop  >= vpTop  && popupTopMax  <= vpTopMax   && 
            popupLeft >= vpLeft && popupLeftMax <= vpLeftMax  ) {
         Dom.setXY(popup, [ popupLeft, popupTop ]);
         sfsw.anchor = 'NW';
         return;
      }
   }

   // Position 4: top right [NE]
   popupLeft = elLeft + elWidth - popupWidth;
   popupTop  = elTop - popupHeight;
   if ( 'NE' == anchor) {
      Dom.setXY(popup, [ popupLeft, popupTop ]);
      return;        
   }
   else if ( ! anchor ) {
      popupLeftMax = popupLeft + popupWidth;
      popupTopMax  = popupTop  + popupHeight;
      if (  popupTop  >= vpTop  && popupTopMax  <= vpTopMax   && 
            popupLeft >= vpLeft && popupLeftMax <= vpLeftMax  ) {
         Dom.setXY(popup, [ popupLeft, popupTop ]);
         sfsw.anchor = 'NE';
         return;
      }
   }

   // Can not fit in viewport - apply position 1 or 2
   // Position 1: bottom left /SW
   popupLeft = elLeft;
   popupTop  = elTop + elHeight;
   popupLeftMax = popupLeft + popupWidth;
   if ( popupLeft >= vpLeft && popupLeftMax <= vpLeftMax  ) {
      Dom.setXY(popup, [ popupLeft, popupTop ]);
      sfsw.anchor = 'SW';
      return;
   }

   // Position 2: bottom right /SE
   popupLeft = elLeft + elWidth - popupWidth;
   popupTop  = elTop + elHeight;
   popupLeftMax = popupLeft + popupWidth;
   if ( popupLeft >= vpLeft && popupLeftMax <= vpLeftMax  ) {
      Dom.setXY(popup, [ popupLeft, popupTop ]);
      sfsw.anchor = 'SE';
      return;
   }

   Dom.setXY(popup, [elLeft, elTop + elHeight]);
}
   
/*
  Dom object is created from the Yahoo UILib YAHOO.util.Dom version 0.11.1
*/
var Dom = {
   isOpera: (navigator.userAgent.toLowerCase().indexOf('opera') > -1),
   isSafari: (navigator.userAgent.toLowerCase().indexOf('safari') > -1),
   isFirefox: (navigator.userAgent.toLowerCase().indexOf('firefox') > -1),
   isIE: !!window.ActiveXObject,

   toCamel: function(property) {
      var convert = function(prop) {
         var test = /(-[a-z])/i.exec(prop);
         return prop.replace(RegExp.$1, RegExp.$1.substr(1).toUpperCase());
      };
      while(property.indexOf('-') > -1) {
         property = convert(property);
      }
      return property;
   },
   
   toHyphen: function(property) {
      if (property.indexOf('-') > -1) { // assume hyphen
         return property;
      }
      var converted = '';
      for (var i = 0, len = property.length;i < len; ++i) {
         if (property.charAt(i) == property.charAt(i).toUpperCase()) {
            converted = converted + '-' + property.charAt(i).toLowerCase();
         } else {
            converted = converted + property.charAt(i);
         }
      }
      return converted;
   },

   getStyle: function(el, property) {
      var Dom = this;
      var value = null;
      var dv = document.defaultView;

      var camel =  Dom.toCamel(property);
      var hyphen = Dom.toHyphen(property);

      if (el.style[camel]) { // camelCase for valid styles
         value = el.style[camel];
      }
      else if (Dom.isIE && el.currentStyle && el.currentStyle[camel]) {
         // camelCase for currentStyle;
         // isIE to workaround broken Opera 9 currentStyle
         value = el.currentStyle[camel];
      }
      else if ( dv && dv.getComputedStyle ) {
         // hyphen-case for computedStyle
         var computed = dv.getComputedStyle(el, '');
         if (computed && computed.getPropertyValue(hyphen)) {
            value = computed.getPropertyValue(hyphen);
         }
      }
      return value;
   },

   setOpacity: function(el, val) {
      var Dom = this;
      if (Dom.isIE && typeof el.style.filter == 'string') {
         el.style.filter = 'alpha(opacity=' + val * 100 + ')';
      }
      else {
        el.style.opacity = val;
        el.style['-moz-opacity'] = val;
        el.style['-khtml-opacity'] = val;
      }
   },

   getXY: function(el) {
      var Dom = this;
      
      // has to be part of document to have pageXY
      if (el.offsetParent === null || Dom.getStyle(el, 'display') == 'none') {
         return false;
      }
         
      var parentNode = null;
      var pos = [];
      var box;
         
      if (el.getBoundingClientRect) { // IE
         box = el.getBoundingClientRect();
         var doc = document;
         if ( !Dom.inDocument(el) && parent.document != document) {
            // might be in a frame, need to get its scroll
            doc = parent.document;
   
            if ( !Dom.isAncestor(doc.documentElement, el) ) {
               return false;                 
            }
         }

         var scrollTop = Math.max(doc.documentElement.scrollTop,
                                  doc.body.scrollTop);
         var scrollLeft = Math.max(doc.documentElement.scrollLeft,
                                  doc.body.scrollLeft);
         return [box.left + scrollLeft, box.top + scrollTop];
      }
      else { // safari, opera, & gecko
         pos = [el.offsetLeft, el.offsetTop];
         parentNode = el.offsetParent;
         if (parentNode != el) {
            while (parentNode) {
               pos[0] += parentNode.offsetLeft;
               pos[1] += parentNode.offsetTop;
               parentNode = parentNode.offsetParent;
            }
         }
         if (Dom.isSafari && Dom.getStyle(el, 'position') == 'absolute' ) {
            // safari doubles in some cases
            pos[0] -= document.body.offsetLeft;
            pos[1] -= document.body.offsetTop;
         } 
      }

      if (el.parentNode) { parentNode = el.parentNode; }
      else { parentNode = null; }

      while (parentNode && parentNode.tagName.toUpperCase() != 'BODY' &&
             parentNode.tagName.toUpperCase() != 'HTML') 
      { // account for any scrolled ancestors
         pos[0] -= parentNode.scrollLeft;
         pos[1] -= parentNode.scrollTop;

         if (parentNode.parentNode) { parentNode = parentNode.parentNode; } 
         else { parentNode = null; }
      }
      return pos;
   },

   setXY: function(el, pos, noRetry) {
      var Dom = this;
      var style_pos = Dom.getStyle(el, 'position');
      if (style_pos == 'static') { // default to relative
         el.style.position = 'relative';
         style_pos = 'relative';
      }

      var pageXY = Dom.getXY(el);
      if (pageXY === false) {return false;} //has to be part of doc to have pageXY

      var delta = [ // assuming pixels; if not we will have to retry
         parseInt( Dom.getStyle(el, 'left'), 10 ),
         parseInt( Dom.getStyle(el, 'top'), 10 )
      ];

      if ( isNaN(delta[0]) ) {// in case of 'auto'
         delta[0] = (style_pos == 'relative') ? 0 : el.offsetLeft;
      }
      if ( isNaN(delta[1]) ) { // in case of 'auto'
         delta[1] = (style_pos == 'relative') ? 0 : el.offsetTop;
      }

      if (pos[0] !== null) { el.style.left = pos[0] - pageXY[0] + delta[0] + 'px'; }
      if (pos[1] !== null) { el.style.top = pos[1] - pageXY[1] + delta[1] + 'px'; }

      var newXY = Dom.getXY(el);

      // if retry is true, try one more time if we miss 
      if (!noRetry && (newXY[0] != pos[0] || newXY[1] != pos[1]) ) {
         Dom.setXY(el, pos, true);
      }
   },

    isAncestor: function(haystack, needle) {
      if (!haystack || !needle) { return false; }
      var Dom = this;

      if (haystack.contains && !Dom.isSafari) {
         // safari "contains" is broken
         return haystack.contains(needle);
      }
      else if ( haystack.compareDocumentPosition ) {
         return (haystack.compareDocumentPosition(needle) & 16) != 0;
      }
      else { // loop up and test each parent
         var parent = needle.parentNode;
      
         while (parent) {
            if (parent == haystack) {
               return true;
            }
            else if (parent.tagName.toUpperCase() == 'HTML') {
               return false;
            }
            parent = parent.parentNode;
         }
         return false;
      }
    },

    inDocument: function(el) {
      var Dom = this;
      return Dom.isAncestor(document.documentElement, el);
   },

   getViewportHeight: function() {
      var Dom = this;
      var height = -1;
      var mode = document.compatMode;

      if ( (mode || Dom.isIE) && !Dom.isOpera ) {
         switch (mode) { // (IE, Gecko)
            case 'CSS1Compat': // Standards mode
               height = document.documentElement.clientHeight;
               break;

            default: // Quirks
               height = document.body.clientHeight;
         }
      } else { // Safari, Opera
         height = self.innerHeight;
      }
      return height;
   },

    getViewportWidth: function() {
      var Dom = this;
      var width = -1;
      var mode = document.compatMode;
         
      if (mode || Dom.isIE) { // (IE, Gecko, Opera)
         switch (mode) {
         case 'CSS1Compat': // Standards mode 
            width = document.documentElement.clientWidth;
            break;
         
         default: // Quirks
            width = document.body.clientWidth;
         }
      } else { // Safari
         width = self.innerWidth;
      }
      return width;
   }

};  // end of Dom object

function getClass() {
   var classSet = '';
   var ua = navigator.userAgent.toLowerCase();
   
   if (ua.indexOf('opera') > -1) {
      classSet = 'opera';
   }
   else if (ua.indexOf('safari') > -1) { 
      classSet = 'safari';
   }
   else if (ua.indexOf('firefox') > -1) { 
      classSet = 'firefox';
   }
   else if ( window.ActiveXObject ) {
      classSet = 'ie';
   } 
   if ( classSet && document.compatMode &&
        document.compatMode.toLowerCase() == 'backcompat' ) {
      classSet += ' quirks_mode';
   }
   return classSet;
}


function getReaderHtml (base) {
   var readers = sfsw.CustomReaders || LocaleVars.Readers;
   var html = '';
   var ids = [];

   for ( var i in readers ) { ids.push(i); }
   ids.sort( function(x,y) {
             return Number(x.replace(/[^0-9]/g, '')) -
                    Number(y.replace(/[^0-9]/g, ''));
   });
    
   for (var i=0; i < ids.length; i++) {
      var reader = readers[ids[i]];
      var img = reader.img.indexOf('http')==0 ? reader.img : base+reader.img;
      html += 
      "<div class='reader_cell' id='aol_feeds_subscribe_" + reader.id + "'>" +
      "<a target='_blank' href='" + reader.url + "' " +
      "onclick='aol_feeds_subscribe.hide();' title='" +reader.text+ "'>" + 
      "<img class='feedIcon' src='" +img+ "'" + 
      " alt='" + reader.text + "'/></a>" +
      "</div>";
   }

   return html;
};


 function getMyAOLHtml(base) {
   var myaol = sfsw.CustomMyAOLReader || LocaleVars.myAOLReader;
   var html = '';

   var img = myaol.img.indexOf('http')==0 ? myaol.img : base+myaol.img;
   html += 
   "<div id='aol_feeds_subscribe_myaol'>" +
   "<a target='_blank' href='" + myaol.url + "' " +
   "onclick='aol_feeds_subscribe.hide();' title='" +myaol.text+ "'>" + 
   "<img src='" +img+ "'" + " alt='" + myaol.text + "'/></a>";

   if ( myaol.whatis_url ) {
      html += 
      "<div><a target='_blank' href='" + myaol.whatis_url + "'>" +
       myaol.whatis_text + "</a></div>";
   }

   html +=
   "</div>";

   return html;
}

function getAlertLinkHtml(t) {
   var a = sfsw.CustomAlertLink;
   if (a == null) { a = LocaleVars.AlertLink };
   var html = '';
   if ( a ) { 
    html +=
   "<div id='aol_feeds_subscribe_alert'>" +
   "<a target='_blank' href='" +a+ "' onclick='aol_feeds_subscribe.hide();'>" + 
   "<span id='aol_feeds_subscribe_text_7'>" + t.text_7 + "</span></a>" +
   "</div>";
   }
   return html;
}

function getManualFeedHtml(t) {
   var singleFeed = sfsw.rssFeed == null || sfsw.atomFeed == null;
   var html = '';

   if ( singleFeed ) {
     html += 
    "<div class='feed_cell single_feed'>" +
    "<input type='text' readonly='readonly' value='__FEED__' " +
    "onclick='this.focus(); this.select();'/>" +
    "</div>" +
 
     "<div id='aol_feeds_subscribe_text_3' class='info_text'>"+
       t.text_3a+
     "</div>";
     return html;
   }

   html += 
   "<div id='aol_feeds_subscribe_rss_feed' class='feed_cell'>" +
   "<a target='_blank' href='http://some.domain.com/__RSS_FEED__' " +
   "onclick='aol_feeds_subscribe.hide();'>" + 
   "<span id='aol_feeds_subscribe_text_4'>" + t.text_4 + "</span></a>" +
   "<input type='text' readonly='readonly' value='__RSS_FEED__' " +
   "onclick='this.focus(); this.select();'/>" +
   "</div>" +
 
   "<div id='aol_feeds_subscribe_text_5' class='info_text'>"+t.text_5+"</div>" +

   "<div id='aol_feeds_subscribe_atom_feed' class='feed_cell'>" +
   "<a target='_blank' href='http://some.domain.com/__ATOM_FEED__' " + 
   "onclick='aol_feeds_subscribe.hide();'>" +
   "<span id='aol_feeds_subscribe_text_6'>" + t.text_6 + "</span></a>" +
   "<input type='text' readonly='readonly' value='__ATOM_FEED__' " + 
   "onclick='this.focus(); this.select();'/>" +
   "</div>" +
 
   "<div id='aol_feeds_subscribe_text_3' class='info_text'>"+t.text_3+"</div>";

   return html;
}

/* popup_html extracted from _feeds_subscribe_.html template */
function getHtml() {
   var t = sfsw.CustomText || LocaleVars.Text;
   var b = getBase() || '';

   var html =
   "<a id='aol_feeds_subscribe_close' href='#'" +
   " onclick='return aol_feeds_subscribe.hide();' title='"  + t.close + "'>" +
   "<img class='close_btn' src='" + b + "http://o.aolcdn.com/art/webwidgets/sfsw_v1_3/images/clear_pix.gif' alt='" + t.close + "'/></a>" +

   "<div id='aol_feeds_subscribe_head'>" + t.text_0 + "</div>" +

   "<div id='aol_feeds_subscribe_body'>" +

    getMyAOLHtml(b) +

   "<div id='aol_feeds_subscribe_readers'>" +
    "<p id='aol_feeds_subscribe_text_1'>"  + t.text_1 + "</p>" +
    getReaderHtml(b) +
   "</div>" +
 
   getAlertLinkHtml(t) +

   "<div id='aol_feeds_subscribe_get_feeds'>" +
   "<a href='#' onclick='return aol_feeds_subscribe.toggleTwisty();'>" +
   "<img id='aol_feeds_subscribe_twisty_img' class='twisty_closed'" + 
   " src='" + b + "http://o.aolcdn.com/art/webwidgets/sfsw_v1_3/images/clear_pix.gif' alt=''/></a>" +
   "<a href='#' onclick='return aol_feeds_subscribe.toggleTwisty();'>" +
   "<span id='aol_feeds_subscribe_text_2' class='twisty_text'>" + 
   t.text_2 + "</span></a>" +
   "</div>" +
 
   "<div id='aol_feeds_subscribe_urls' style='display: none;'>" +
     getManualFeedHtml(t) + 
   "</div>" + 

   "</div> <!-- aol_feeds_subscribe_body -->";

   return html;
};

/*
   Lazy loading with feeds_subscribe_lazyload.js 
   Send load notification or complete panding calls
*/
if ( typeof aol_feeds_subscribe_lazyload != 'undefined' ) {

   if ( aol_feeds_subscribe_lazyload.loadCallback ) {
      if ( aol_feeds_subscribe_lazyload.timeoutId ) {
         clearTimeout(aol_feeds_subscribe_lazyload.timeoutId);
      }
      aol_feeds_subscribe_lazyload.timeoutId = null;

      var callback = aol_feeds_subscribe_lazyload.loadCallback;
      aol_feeds_subscribe_lazyload.loadCallback = null;
      if ( callback ) {
         callback(true); // Send loaded notification
      }
   }

   // Exec pending calls
   for ( var i=0; i < aol_feeds_subscribe_lazyload.pendingCalls.length; i++ ){
      var call = aol_feeds_subscribe_lazyload.pendingCalls[i];
      if ( call.method == 'setText' ) {
         sfsw.setText(call.text);
      }
      if ( call.method == 'setReaders' ) {
         sfsw.setReaders(call.readers);
      }
      if ( call.method == 'setMyAOLReader' ) {
         sfsw.setMyAOLReader(call.myAOL);
      }
      if ( call.method == 'setAlertLink' ) {
         sfsw.setAlertLink(call.link);
      }
      if ( call.method == 'showBlog' ) {
         sfsw.showBlog(call.refEl, call.blogURL, call.anchor);
      }
      if ( call.method == 'show' ) {
         sfsw.show(call.refEl, call.atomFeed, call.rssFeed, call.anchor);
      }
   }
}

// Create the global widget object
window.aol_feeds_subscribe = sfsw;
	
}();   // closure end


