﻿ if(!window.PT){window.PT=new Object();window.PT["buildVersion"]='5667.145';window.PT["dbg"]='';window.PT["host"]='http://forums.abcnews.go.com';window.PT.IncludeScript=function(scriptUrl){var headElm=document.getElementsByTagName('head')[0];var scriptElm=document.createElement('script');scriptElm.setAttribute('type','text/javascript');scriptElm.setAttribute('src',scriptUrl);headElm.appendChild(scriptElm);}}


/**
    Prospero Active Widgets
    Copyright (c) 2007 Prospero Technologies LLC
    
    Prospero Active Widgets are dynamic elements of Prospero-served community or social media content embedded in
    web pages served by Prospero clients or their affilliates.
    Each Active Widget may use a token  named  "__WIDGET_ID__" to deal with namespacing
    The __WIDGET_ID__ is the unique key passed back to the server and is used as a key to reference the Widget in the registry
**/


function PT_WidgetMan()
{
  /**
  singleton if one already exists then use it
  **/
  if(window.PT_WIDGET_MAN)
  {
    /**
      If the WidgetMan object exists already then exit
    **/
    return;
  }
  window.PT_WIDGET_MAN = this;
  this.Widgets = new Object();
  this.Apps = new Object();
  /* TODO - load this up from infrastructure; perhaps move to PT? */
  this.Apps["forum"] = '/n/pfx/forum.aspx';
  this.element_count = 0;
  this.pollId = 0;  // polling ID
  this.domComplete = false;  // flag to test if the DOM is ready
  if(document.addEventListener && document.readyState) // Safari and Opera
  {
    this.startPoll();  // start Polling
  }
  this.initialize();  // add listeners to window.document or window
}


function PT_WidgetMan_StartPolling() 
{
  if(this.pollId) 
  {
    return;
  }
  var self = this;
  this.pollId = setInterval(function(){ self.loadWidgets(); }, 1000);
  //alert("Start Poll I="+this.pollId+" L="+this.domComplete);
}


function PT_WidgetMan_StopPolling() 
{
  if (!this.pollId) 
  {
    return;
  }
  //alert("Stop Poll I="+this.pollId+" L="+this.domComplete);
  clearInterval(this.pollId);
  this.pollId = null;
}


/**
 Add listeners to window.document or to the window itself.
 Once the DOM is ready, stop polling and run loadWidgets one last time.
**/
function PT_WidgetMan_Initialize()
{
  var self = this;
  var callBack = function(){
    if(document.readyState) // IE and Opera 9
    {
      if(document.readyState == 'complete')
      { 
        //alert("onReadyStateChange fired");
        self.domComplete = true; 
        self.loadWidgets();
      }
    }
    else // Firefox
    {
      //alert("DOMContentLoaded or onLoad fired"); 
      self.domComplete = true; 
      self.loadWidgets();
    }
  }
  
  if(document.addEventListener) // W3C Recommendation
  {
    if(document.readyState) // Opera and Safari
  {
    //alert("listen to onReadyStateChange via addEventListener");
    document.addEventListener("onreadystatechange", callBack, false);
  }
  else // Firefox
  {
    //alert("listen to DOMContentLoaded via addEventListener");
    document.addEventListener("DOMContentLoaded", callBack, false);
  }
  }
  else if(document.attachEvent) // IE
  {
    //alert("listen to onReadyStateChange via attachEvent");
  document.attachEvent("onreadystatechange", callBack);
  }
  else
  {
    //window.onload = callBack;
  }
}
  

function PT_WidgetMan_LoadWidgets()
{
  /* Scan for all widgets and kick off each */
  var elements = document.getElementsByTagName("paw:widget");
  var len = elements.length;
  
  /*
  if (len == 0)
  {
  alert("No widgets here!");
  }
  else
  {
  alert("Got " + len + " widgets.");
  }
  */
  
  for(var elementIndex = 0; elementIndex <len;elementIndex++)
  {
    var cModule = new PT_Widget(elements[elementIndex]);
    var cWidgetId = cModule.__WIDGET_ID__;
    //alert("Created " + elementIndex + " as widgetId=" + cWidgetId);
    //alert(this.Widgets);
    this.Widgets[cWidgetId] = cModule;
    //alert("Added widget " + cWidgetId + " to collection.");
    cModule.Load();
  }
  
  if(this.domComplete)
  {  
    //alert("Count="+ elements.length);
    this.stopPoll();
  }
}


function PT_WidgetMan_UpdateContent(moduleId, uri)
{
    if(this.Widgets[moduleId])
  {
        this.Widgets[moduleId].Update(PT["host"] + uri);
    }
}


function PT_WidgetMan_SetContent(moduleId,sContent)
{
    //alert("Setting content for widget " + moduleId);
    if(this.Widgets[moduleId])
    {
        this.Widgets[moduleId].SetContent(sContent);
    }
    /*
    else
    {
      alert("but couldn't find module " + moduleId);
    }
    */
}


/**
 Determine if request is coming from:
  0. External Domain
  1. Same Domain
  2. Same Host
**/
function PT_WidgetMan_GetEnvironment()
{
  // Test Same Host
  if(PT["host"] == window.location.host)
  {
    return 2;
  }
  // Test Same Domain
  else 
  { 
    // This assumes that ajaxHost is a fully qualified domain name.
    // To Do: Probably should use a regular expression to tighten this up
    var ajaxHost = PT["host"].split('//');
    var ajaxDomain = ajaxHost[1].split('\.');
    var xhrDomain = window.location.host.split('\.');
    for(i = 0; i < xhrDomain.length - 1; i++)
    {
      for(j = 1; j < ajaxDomain.length - 1; i++)
      {
        if(xhrDomain[i] == ajaxDomain[j]);
        {
          return 1;
        }
      }
    }
    // Must be an External Domain
    return 0;
  }
}


PT_WidgetMan.prototype = new Object();
PT_WidgetMan.prototype.getEnvironment = PT_WidgetMan_GetEnvironment;
PT_WidgetMan.prototype.initialize= PT_WidgetMan_Initialize;
PT_WidgetMan.prototype.loadWidgets = PT_WidgetMan_LoadWidgets;
PT_WidgetMan.prototype.SetContent= PT_WidgetMan_SetContent;
PT_WidgetMan.prototype.startPoll = PT_WidgetMan_StartPolling;
PT_WidgetMan.prototype.stopPoll = PT_WidgetMan_StopPolling
PT_WidgetMan.prototype.UpdateContent = PT_WidgetMan_UpdateContent;



function PT_Widget(Element)
{
    /* Generate a unique ID for the widget */
    this.__WIDGET_ID__ = PT_Widget.Prefix + PT_WIDGET_MAN.element_count++;
    this._Element=Element;
    
    /* The canvas will be the dynamically created div in which the widget operates */
    this._Canvas = null;
        
    this._DebugCanvas = null;
    this._debug= false;

    this.app = this._Element.getAttribute("app");
    if (!this.app || this.app == "")
    {
      this.app = "forum"; /* default */
    }
    //alert(this.app);
    this.webtag= this._Element.getAttribute("webtag");
    this.type = this._Element.getAttribute("type");
    this.args = this._Element.getAttribute("args");
    this.config = this._Element.getAttribute("config");
          
    this._script= null;
    this._errorMessage= "";
}



function PT_Widget_SetError(msg){
    this._errorMessage= msg;
    alert(this._errorMessage);
}

function  PT_Widget_SetContent(sContent)
{
    // modification to handle scripts
  if(this._Element)
  {
    //alert("I'm tired.  I need to catch my breath!");
    <!-- Needed to correct IE race condition -->
    var pawParent;
    if(this._Element.parentNode)
    {
      pawParent = this._Element.parentNode;
      //alert("pawParent by parentNode");
    }
    /*
    else
    {
      pawParent = document.getElementById("ptcWidgetParent");
      //alert("pawParent by getElementById");
    }
    */
    if(pawParent)
    {
      //alert("has parentNode");
      if(this._Canvas == null)
      {
        this._LoadingMessage = document.getElementById("ptcloading");
        if(this._LoadingMessage)
        {
            this._LoadingMessage.style.display = "none";
        }
        this._Canvas = document.createElement("div");
        pawParent.replaceChild(this._Canvas,this._Element);            
        this._Element= this._Canvas;
        //alert("Element: " + this._Element + "\nCanvas: " + this._Canvas);
      }
      this._Canvas.innerHTML = sContent;
      if(this._debug)
      {
         this._DebugCanvas= document.createElement("textarea");
         this._DebugCanvas.setAttribute("cols","50");
         this._DebugCanvas.setAttribute("rows","10");
         this._Canvas.appendChild(this._DebugCanvas);
         this._DebugCanvas.value=sContent;
      }
    }
  }
}


function PT_Widget_Load()
{
   var src = PT["host"] + PT_WIDGET_MAN.Apps[this.app] + "?webtag=" + this.webtag + "&widgetId="+ this.__WIDGET_ID__ +"&nav=jsscontent&dst="+escape(new Date().toString()); 
   if (this.type && this.type != "")
   {
      src += "&type=" + this.type;
   }
   if (this.args && this.args != "")
   {
      /* 4/30/07: Fix semicolon in subject title encoding issue */
      this.args = this.args.replace(/\%3B/g,'%20');
      /* Remove any < or > */
      this.args = this.args.replace(/\%3[Cc]/g,'%20');
      this.args = this.args.replace(/\%3[Ee]/g,'%20');
      this.args = this.args.replace(/\</g,'%20');
      this.args = this.args.replace(/\>/g,'%20');
      src += "&args=" + this.args;
   }
   if (this.config && this.config != "")
   {
      src += "&config=" + this.config;
   }
   var dbg = PT["dbg"];
   if (dbg && dbg != "")
   {
      src += "&dbg=" + dbg;
   }
   this.Update(src);
}
function PT_Widget_Update(src)
{
   if(this._script)
   {
    /* remove previously used script.  Someday we might make a history of scripts */ 
    document.body.removeChild(this._script);
    this._script= null;
   }
   this._script = document.createElement("script");
   this._src= src;
   //debug
   //alert(this._src);
   //window.open(this._src);
   this._script.setAttribute("src", this._src);
   document.body.appendChild(this._script);   
} 


PT_Widget.prototype                  = new Object();
PT_Widget.prototype.Load             = PT_Widget_Load;
PT_Widget.prototype.SetError         = PT_Widget_SetError;
PT_Widget.prototype.SetContent = PT_Widget_SetContent;
PT_Widget.prototype.Update = PT_Widget_Update;

PT_Widget.Prefix="PTWidget";
PT_Widget.rev =0;

/** 
    Create the singleton global object window.PT_WIDGET_MAN which does all the work of module creation registration and execution
**/
 
new PT_WidgetMan();


 