var host = window.location.hostname.replace(/.*?\./,"");
var url2 = document.location.href.match(/\/\/([^\.]*)\.([^\/]*)/);

function navigate(Event) {
  if(Event==null) Event=event;
  if(Event.type!='keyup') return;
  switch (Event.keyCode) {
    case 27:
      var url = ReadCookie("escape");
      if (url.length>0) { 
        document.location.href=url;
      } else {
        document.location.href="http://"+url2[1]+"." + url2[2] + "/myinbox.mws";
      }
      default: return;
  }
}

function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function getCalendarDate(now) {
  var months = new Array(13);
  months[0] = "January";
  months[1] = "February";
  months[2] = "March";
  months[3] = "April";
  months[4] = "May";
  months[5] = "June";
  months[6] = "July";
  months[7] = "August";
  months[8] = "September";
  months[9] = "October";
  months[10] = "November";
  months[11] = "December";
  var monthnumber = now.getMonth();
  var monthname = months[monthnumber];
  var monthday = now.getDate();
  var year = now.getYear();
  if(year < 2000) { year = year + 1900; }
  var dateString = monthname + ' ' + monthday + ', ' + year;
  return dateString;
} 

function getClockTime(now) {
  var hour = now.getHours();
  var minute = now.getMinutes();
  var second = now.getSeconds();
  var ap = "AM";
  if (hour > 11) { ap = "PM"; }
  if (hour > 12) { hour = hour - 12; }
  if (hour == 0) { hour = 12; }
  if (minute < 10) { minute = "0" + minute; }
  var timeString = hour +  ':' +  minute +  " " + ap;
  return timeString;
} 

function goDate(t) {
  d = new Date(t*1000);
  document.write(getCalendarDate(d)+" "+getClockTime(d));
}

document.onkeyup=navigate;

function why(friend) {
  var entry = parent.document.getElementById("whyinsert");
  entry.innerHTML = '<div id="insert" class="insertout" style="left:100px"><div class="insert">'+

  '<a style="cursor:pointer; float:right; font-weight:bold; border:1px solid" onclick="var insert = parent.document.getElementById(\'insert\');if (insert)  insert.parentNode.removeChild(insert);">X</a>'  +
    "<div class=\"blog\" id=\"replyreply$replyid\">\n"+
     "  <div class=\"subject\">This blog is only accessible to registered members.  If this is your blog, you can make your blog available to anonymous visitors by logging into your account and going to the Customize Blog page.<br><br>Why might you consider making your blog members-only?</div>\n"+
     "  <div class=\"text\"><ul><li><b>More privacy</b>: This site lets you organize your readers into groups such as classmates, family, co-workers, and online contacts.  Members-only ensures you make your blog entries available to only the appropriate groups.</li><li><b>Meet more people</b>:  With a members-only blog, your Recent Visitors list will include everyone that visits your blog.  This will let you check out all the blogs and profiles of your visitors.</li><li><b>No anonymous comments</b>: Members-only means less chance of harassment and abuse while also making it easier to communicate with all your readers.</li></ul><a href=\"javascript:signup("+friend+")\">Registering for an account</a> takes less than two minutes and makes your blogging experience infinitely better. And, it's FREE!</div>\n"+
     "  <div class=\"spacer\">&nbsp;</div>\n"+
     "</div>\n"+
  '</div></div>';
}

function signup(friend) {
  document.location.href="http://www."+host+"/create.mws?friend="+friend;
}

doit = function() {
  bb = document.getElementById('blogbar');
  if (document.all && bb && document.body && document.body.clientWidth) {
    var theWidth=0;
      theWidth = document.body.clientWidth
      bb.style.width=theWidth+'px';
  }
}

window.onload=doit;
window.onresize=doit;


function hidebar(domain) {
  if (confirm("You are about to hide the blogbar.  You can always re-enable it on your account page.")) {
    var ExpireDate = new Date ();
    ExpireDate.setTime(ExpireDate.getTime() + (10 * 365 * 24 * 3600 * 1000));
    setCookie("blogbar","hide",ExpireDate,false,domain);
    document.getElementById('blogbar').style.display="none";
    document.getElementById('blogbarpadding').style.display="none";
  }
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
