/*
 * Copyright (C) 2008-2010 APN, LLC. All rights reserved
 * References to external sources embedded in the code.
 * 
 * Author: Christine Hodges
 * Description: Toolbar Content Prefetching mechanism.
 */
var ATB_Prefetch=function(){this.PREFETCH_PREFIX="asktb-prefetch-";this.init()};ATB_Prefetch.prototype={init:function(){ATB.Logger.info("Start *Prefetch* Init...");if(ATB.Core.isMasterToolbar()){this.prefetchToolbarContent()}else{ATB.Logger.info("Prefetch init: this is not the master TB. Skipping: prefetch init.")}ATB.Logger.info("End *Prefetch* Init...")},prefetchToolbarContent:function(){var g=ATB.Core.getButtonNodesWithAttr(ATB.Constants.BTN_TYPE_PREFETCH,true);var h=ATB.Core.getPrefetchDelay();var k=ATB.Prefs.getToolbarLocale();ATB.Logger.debug("Prefetch init: FYI delay between prefetches is set to (in milliseconds): ",h);for(var d=0,l=0;d<g.length;d++){var j=g[d];if(!ATB.Locale.isElementSupported(j,k)){ATB.Logger.debug("Prefetch: skipping unsupported button: ",j.id);continue}var e=ATB.Core.getCompleteUrl(j.getAttribute(ATB.Constants.BTN_TYPE_WIDGET));var f=ATB.Core.getCompleteUrl(j.getAttribute(ATB.Constants.BTN_TYPE_FEED));ATB.Logger.debug("Prefetch: Found item to prefetch : ",j.id," [widget: ",e,", feed: ",f,"]");if(e){var c=ATB.Core.getInnerUrl(e,true);window.setTimeout(this.prefetchWrapper(c,j.id),(h*l++))}}if(g.length>0){var a=parseInt(ATB.Core.getConfigValue(ATB.Constants.CONFIG_PREFETCH_TEARDOWN));if(isNaN(a)){a=ATB.Constants.DEFAULT_PREFETCH_TEARDOWN}ATB.Logger.debug("Prefetch init: time until teardown prefetching (in minutes) : ",a);a=a*60*1000;window.setTimeout(this.prefetchTeardown,a)}},prefetchWrapper:function(a,b){return function(){ATB.Prefetch.prefetchUrl(a,b)}},prefetchUrl:function(b,e){var a=getTopWin().document.documentElement;var c=ATB.Prefetch.PREFETCH_PREFIX+e+new Date().getTime();ATB.Logger.debug("Prefetch: Kick off loading: ",c,", URL: ",b);var d=document.createElement("iframe");d.setAttribute("id",c);d.setAttribute("hidden",true);d.setAttribute("collapsed",true);d.setAttribute("type","content");d.setAttribute("src",b);a.appendChild(d)},prefetchTeardown:function(){try{ATB.Logger.info("Prefetch teardown: removing prefetchers from chrome.");var a=getTopWin().document.documentElement;var g=a.getElementsByTagName("iframe");var d=[];for(var c=0;c<g.length;c++){var e=g.item(c);var h=(e.hasAttribute("id"))?e.getAttribute("id"):"";ATB.Logger.debug("Prefetch teardown: got frame id: ",h);if(h.slice(0,ATB.Prefetch.PREFETCH_PREFIX.length)==ATB.Prefetch.PREFETCH_PREFIX){d.push(e)}}while(d.length>0){a.removeChild(d.pop())}}catch(b){ATB.Logger.warn("Prefetch teardown: ERROR: ",b)}}};