// Popups via class="popup"
// Source: http://adactio.com/atmedia2005/
function doPopups() {
  if (!document.getElementsByTagName)
    return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("tc_popup")) {
      links[i].onclick = function() {
        var win = window.open(this.href, "tc_popup", "width=350,height=452,scrollbars=no,resizable=no");
        if (window.focus)
          win.focus();
        return false;
      }
    }
    else if (links[i].className.match("popup")) {
      links[i].onclick = function() {
        var win = window.open(this.href, "popup", "width=600,height=485,scrollbars=yes,resizable=yes");
        if (window.focus)
          win.focus();
        return false;
      }
    }
  }
}

// Font size switching via N/N+/N++ widget
function fontsizeup() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'N' : 
      setActiveStyleSheet('N+');
      break;
    case 'N+' : 
      setActiveStyleSheet('N++');
      break;
    case 'N++' : 
      break;
    default :
      setActiveStyleSheet('N');
      break;
  }
}

function fontsizedown() {
  active = getActiveStyleSheet();
  switch (active) {

    case 'N+' : 
      setActiveStyleSheet('N');
      break;
    case 'N++' : 
      setActiveStyleSheet('N+');
      break;
    case 'N' : 
      break;
    default :
      setActiveStyleSheet('N');
      break;
  }
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(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").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");

  }
  return null;
}

function getPreferredStyleSheet() {
  return ('N');
}

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 cookie = readCookie("TimesFontSize");
var title = cookie ? cookie : getPreferredStyleSheet();
if (title == 'null') {
  title = getPreferredStyleSheet();
}

setActiveStyleSheet(title);


window.onload = function(e) {
  var cookie = readCookie("TimesFontSize");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  doPopups();
}

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

// go to URL 
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
