// stolen wholesale from Zeldman, Willison, and others
//  with thanks

function addpLinks() {
  var paras = document.getElementsByTagName('p');
  for (var i = 0; i < paras.length; i++) {
    var current = paras[i];
    if (/^p-/.test(current.id)) {
      // It's a purple link paragraph
      var plink = document.createElement('a');
      plink.href = document.location.href.split('#')[0] + 
        '#' + current.id;
      plink.className = 'plink';
      plink.appendChild(document.createTextNode(' ¶'));
      current.appendChild(plink);
    }
  }
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel") &&
        a.getAttribute("rel").indexOf("style") != -1 &&
        a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel") &&
        a.getAttribute("rel").indexOf("style") != -1 &&
        a.getAttribute("title") &&
        !a.disabled
        ) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel") &&
        a.getAttribute("rel").indexOf("style") != -1 &&
        a.getAttribute("rel").indexOf("alt") == -1 &&
        a.getAttribute("title")
        ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

var commentsReq;

function getComments() {
  var url = document.URL + ".comments";
    if (window.XMLHttpRequest) 
        commentsReq = new XMLHttpRequest();
    else
        commentsReq = new ActiveXObject("Microsoft.XMLHTTP");
    if (commentsReq) {
      commentsReq.onreadystatechange = gotComments;
      commentsReq.open("GET", url, true);
      commentsReq.send(null);
    }
}
function gotComments() {
  if (commentsReq.readyState != 4 || commentsReq.status != 200) 
    return;
  if (commentsReq.responseText == '')
    return;
  var tailDiv = document.createElement('div');
  tailDiv.innerHTML = "<hr/>\n" +
    "<h2 id='comments'>Contributions</h2>\n" +
    commentsReq.responseText;
  // var xml = commentsReq.responseXML.documentElement;
  var center = document.getElementById("centercontent");
  center.appendChild(tailDiv);
}

var picInfoReq;

function doThumbnail() 
{
    var url = "/ongoing/picInfo.xml";

    if (window.XMLHttpRequest) 
        picInfoReq = new XMLHttpRequest();
    else
        picInfoReq = new ActiveXObject("Microsoft.XMLHTTP");

    if (picInfoReq) {
      picInfoReq.onreadystatechange = gotPicInfo;
      picInfoReq.open("GET", url + "?o=" + document.URL, true);
      picInfoReq.send(null);
    }
}

function gotPicInfo() 
{
  // only if picInfoReq shows "complete"
  if (picInfoReq.readyState != 4 || picInfoReq.status != 200) 
    return;

  var xml = picInfoReq.responseXML.documentElement;
  var fUri = xml.getElementsByTagName('fragment')[0].firstChild.data;
  var title = xml.getElementsByTagName('title')[0].firstChild.data;
  var tnA = document.getElementById("tnA");
  if (tnA)
    tnA.setAttribute("href", fUri);
  var tnI = document.getElementById("tnI");
  if (tnI)
    tnI.setAttribute("title", title);
}

var inFeedListReq;

function commentsOK()
{
  var url = "/ongoing/in-feed.xml";

  if (window.XMLHttpRequest) 
    inFeedListReq = new XMLHttpRequest();
  else
    inFeedListReq = new ActiveXObject("Microsoft.XMLHTTP");

  if (inFeedListReq) {
    inFeedListReq.onreadystatechange = gotInFeedList;
    inFeedListReq.open("GET", url, true);
    inFeedListReq.send(null);
  }
}

function gotInFeedList()
{
  // only if inFeedListReq shows "complete"
  if (inFeedListReq.readyState != 4 || inFeedListReq.status != 200) 
    return;

  var xml = inFeedListReq.responseXML.documentElement;
  var allInFeed = xml.getElementsByTagName('o');
  var canComment = false;
  for (var i = 0; i < allInFeed.length; i++) {
    if (allInFeed[i].firstChild.nodeValue == document.location.pathname) {
      // comments allowed
      canComment = true;
    }
  }

  var commentHere = document.getElementById("commentHere");
  if (commentHere) {
    if (canComment) {
      titleEl = document.getElementsByTagName('title');
      title = titleEl.item(0).innerHTML.substring(10);
      commentHere.innerHTML = "<p>Please feel free to " +
	"<a href='/atompub/comment?frag=" +
	document.location.pathname + "&amp;title=" +
	title +
	"'>contribute a comment</a> on this fragment.</p>";
    }
    else
      commentHere.innerHTML = "<p>Comments on this fragment are closed.</p>";
  }
}

window.onload = function(e) {

  // serif/sans
  var cookie = readCookie("style");
  var title;
  if (cookie == 'serif' || 
      cookie == 'sans') { title = cookie; }
  else                  { title = 'serif'; }
  setActiveStyleSheet(title);

  // which homepage
  home = 'ongoing';
  if (document.URL.indexOf("home=the-world") != -1 ||
      document.URL.indexOf("ongoing/the-world") != -1) {
    home = 'the world';    
  }
  cookie = readCookie("home");
  if (cookie == 'the world')
    home = 'the world';

  var toHome;
  toHome = document.getElementById("home");
  if (toHome != null) {
    toHome.innerHTML = home;
  }

  addpLinks();
  doThumbnail();
  commentsOK();
  // getComments();
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
  createCookie("home", home);
}

var cookie = readCookie("style");
var title;
if (cookie == 'serif' ||
    cookie == 'sans' ) { title = cookie; }
else                   { title = 'serif'; }
setActiveStyleSheet(title);

// which homepage
home = 'ongoing';
if (document.URL.indexOf("home=the-world") != -1 ||
    document.URL.indexOf("ongoing/the-world") != -1) {
  home = 'the world';    
}
cookie = readCookie("home");
if (cookie == 'the world')
  home = 'the world';

var toHome;
toHome = document.getElementById("home");
if (toHome != null) {
  toHome.innerHTML = home;
}


addpLinks();
var home;
