﻿function dispReadMore() {
	tempColl = document.getElementById('main').getElementsByTagName("DIV");

	for (i = 0; i < tempColl.length; i++) {
    if (tempColl[i].className == "post-body") {
			postId = tempColl[i].id.substring(5);
			tempSpans = tempColl[i].getElementsByTagName("DIV");
			foundPostFull = false;
			for (j = 0; j < tempSpans.length; j++) {
				if (tempSpans[j].className == "postfull") {
					foundPostFull = true;
					break;
				}
			}
			if (foundPostFull) {
				document.getElementById('rm_' + postId).style.display = "block";
			}
		}
	}
}

function outUlOpenTag() {
	document.write("<ul class=\"archive-list\">");
}

function outLiListLinks(linkArray) {
  for (var i = linkArray.length - 1; i >= 0; i--) 
  { 
    document.write('<li><a href=\"' + linkArray[i][0] + '\">' + linkArray[i][1] + '</a></li>'); 
  } 
}

function outUlCloseTag() {
	document.write("</ul>");
}

function outCommentOpenTag() {
	document.writeln("<dl id=\"comments-block\">");
}

function outCommentItems(commentArray) {
	commentArray.sort();
  for (var i = commentArray.length - 1; i >= 0; i--) {
    document.writeln("<dt class=\"comment-data\">");
    document.writeln("  <a href=\"" + commentArray[i][1] + "\" title=\"" + commentArray[i][2] + "\">" + commentArray[i][3] + "</a>,");
    document.writeln("  <span class=\"comment-poster\">" + commentArray[i][4] + "</span> said...");
    document.writeln("</dt>");
    document.writeln("<dd class=\"comment-body\"><p>" + commentArray[i][5] + "</p></dd>");
  }
}

function outCommentCloseTag() {
	document.writeln("</dl>");
}

function sortComments(comments, size) {
  var dts = comments.getElementsByTagName("dt");
  var dds = comments.getElementsByTagName("dd");
  var items = new Array();
  var orgSize = dts.length;

  for (var i = 0; i < orgSize; i++) {
    if (dts[i].id) {
      items[items.length] = new Array(Date.parse(dts[i].childNodes[0].innerHTML), dts[i], dds[i]);
    }
  }

  items.sort();

  for (var i = items.length - 1; i >= 0; i--) {
    comments.removeChild(items[i][1]);
    comments.removeChild(items[i][2]);

    if (items.length - i <= size) {
      comments.appendChild(items[i][1]);
      comments.appendChild(items[i][2]);
    }
  }
}

function transDate(dateStr) {
    dateStr = dateStr.substring(0, dateStr.indexOf("."));
    dateStr = dateStr.replace("T", " ");
    dateStr = dateStr.replace(/\-/g, "/");
    return dateStr;
}

// Fetch the recent comments from Blogger Feed with JSON code and call back func is "dispComments"
function getComments(blogId, counts) {
   
  // Temporarily hide it
  var dl = document.getElementById("comments-block");
  dl.style.display = "none";

  // Retrieve the JSON feed.
  var script = document.createElement('script');
  script.setAttribute('src', 'http://www2.blogger.com/feeds/' + blogId +
                      '/comments/default?alt=json-in-script&callback=dispComments&start-index=1&max-results=' + counts);
//  script.setAttribute('src', 'jscripts/comment-json.js');
  script.setAttribute('id', 'jsonScript-recent-comments');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

function dispComments(json) {
  var dl = document.getElementById("comments-block");

  // get entry template
  var dt = dl.getElementsByTagName("dt")[0];
  var dd = dl.getElementsByTagName("dd")[0];

  dl.removeChild(dt);
  dl.removeChild(dd);

  for (var i = 0; i < json.feed.entry.length; i++) {
    var entry = json.feed.entry[i];
    var curDt = dt.cloneNode(true);
    var curDd = dd.cloneNode(true);

    // Time
    curDt.childNodes[0].href = entry.link[0].href;
    curDt.childNodes[0].appendChild(document.createTextNode(transDate(entry.published.$t)));

    // Author
    curDt.childNodes[2].childNodes[0].appendChild(document.createTextNode(entry.author[0].name.$t));
    if (entry.author[0].uri) {
      curDt.childNodes[2].childNodes[0].href = entry.author[0].uri.$t;
    }
    else {
      curDt.childNodes[2].childNodes[0].removeAttribute("href");
    }
    dl.appendChild(curDt);
    dl.appendChild(curDd);

    //Content
    curDd.childNodes[0].appendChild(document.createTextNode(entry.title.$t));
  }

  dl.style.display = "block";
  var commentLoading = document.getElementById("comment-loading");
  commentLoading.style.display = "none";
}

function backlinkIconToggle(obj) {
  var dt = obj.parentNode;
  var dd = dt.nextSibling;
  while (dd.tagName != "DD") {
    dd = dd.nextSibling; 
  }

  if (obj.className == 'comment-toggle') {
    obj.className = 'comment-toggle-exp';
    dd.style.display = "block";
  }
  else {
    obj.className = 'comment-toggle';
    dd.style.display = "none";
  }
}

function setTags() {
  var tagsList = document.getElementById('tagsbar').getElementsByTagName('DIV');
  //alert(location.href);

  var currentTagName = "all";

  for (var i = 1; i < tagsList.length; i += 2) {
    var tag = tagsList[i];
    var tagName = tag.id.substring(4);
    var image = new Image();
    var tagLink = tag.childNodes[0].childNodes[0];

    if (tagName == currentTagName) {
      image.src = "./imgs/notebook/tag_" + tagName + "_en.gif";
      if (i != 1) {
        tagsList[0].className = "tag-start-blue";
        tagsList[2].className = "tag-blue-blue";
      }
      tagsList[i - 1].className = (i == 1 ? "tag-start-enable" : "tag-blue-enable");
      tagsList[i + 1].className = (i == tagsList.length - 2 ? "tag-enable-end" : "tag-enable-blue");
    }
    else {
      image.src = "./imgs/notebook/tag_" + tagName + ".gif";
    }

    tag.style.background = "url('" + image.src + "') no-repeat left top";
    tagLink.innerHTML = "&nbsp;";
    tagLink.style.display = "block";
    tag.style.height = image.height;
    tag.childNodes[0].style.width = image.height;
    tagLink.style.width = image.height;

  }

}

var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function isLabelPage() {
  return location.href.indexOf("/labels/") > 0;
}

function getPageLabelAsKey() {
  return location.href.substring(location.href.indexOf("labels/") + 7, location.href.lastIndexOf('.')).replace('%20', '-').toLowerCase();
}

function isUtfText(input) {
  var i = 0;

  while (i < input.length) {
    if (input.charCodeAt(i) > 127) {
      return true;
    }
    i++;
  }

  return false;
}

function getEncodedTagName(input) {

  return isUtfText(input) ? Base64.encode(input) : input;
}

function getPageLabelAsName() {
  var labelText = unescape(location.href.substring(location.href.indexOf("labels/") + 7, location.href.lastIndexOf('.')));

  var isUtfLabel = (labelText.charAt(0) == '5' || labelText.charAt(0) == '6');

  return isUtfLabel ? Base64.decode(labelText) : labelText;
}

var tagImages = new Array();

// preload tag images to ensure their height could be fetch correctly.
function preloadTagImages() {
  var currentTagName = tagNames[0];

  // get current tag name by url
  if (isLabelPage()) {
    currentTagName = getPageLabelAsName();
  }

  for (var i = 0; i < tagNames.length; i++) {
    var image = new Image();
    if (currentTagName == tagNames[i]) {
      image.src = "./imgs/notebook/tag_" + getEncodedTagName(tagNames[i]) + "_en.gif";
    }
    else {
      image.src = "./imgs/notebook/tag_" + getEncodedTagName(tagNames[i]) + ".gif";
    }
    tagImages[i] = image;
  }
}

preloadTagImages();

function dispTags() {
  var tagDiv = document.getElementById("tagsbar");
  var sampleTags = tagDiv.getElementsByTagName("DIV");
  var spacorSample = sampleTags[0];
  var bodySample = sampleTags[1];
  bodySample.childNodes[0].childNodes[0].innerHTML = "&nbsp;";
  bodySample.childNodes[0].childNodes[0].style.display = "block";

  // remove the samples
  tagDiv.removeChild(sampleTags[1]);
  tagDiv.removeChild(sampleTags[0]);

  var currentTagName = tagNames[0];

  // get current tag name by url
  if (isLabelPage()) {
    currentTagName = getPageLabelAsName();
  }

  var image;

  for (var i = 0; i < tagNames.length; i++) {
    //leading spacor;
    var spacor = spacorSample.cloneNode(true);
    var body = bodySample.cloneNode(true);

    if (currentTagName == tagNames[i]) {
      spacor.className = (i == 0 ? "tag-start-enable" : "tag-blue-enable");
      //image.src = "./imgs/notebook/tag_" + getEncodedTagName(tagNames[i]) + "_en.gif";
    }
    else {
      if (i == 0) {
        spacor.className = "tag-start-blue";
      }
      else {
        spacor.className = (currentTagName == tagNames[i - 1] ? "tag-enable-blue" : "tag-blue-blue");
      }
      //image.src = "./imgs/notebook/tag_" + getEncodedTagName(tagNames[i]) + ".gif";
    }

    image = tagImages[i];

    body.style.background = "url('" + image.src + "') no-repeat left top";
    body.style.height = image.height + 'px';

    body.childNodes[0].style.height = image.height + 'px';

    var tagLink = body.childNodes[0].childNodes[0];

    tagLink.style.height = image.height + 'px';

    // update the link url
    if (i != 0) {
      var tagHref = tagLink.href;
      tagLink.removeAttribute("href");
      tagLink.setAttribute("href", tagHref + "labels/" + escape(getEncodedTagName(tagNames[i])) + ".html");
      tagLink.setAttribute("title", "Label: " + tagNames[i]);
    }

    tagDiv.appendChild(spacor);
    tagDiv.appendChild(body);
  }

  // add the ending tag spacor
  var ending = spacorSample.cloneNode(true);
  ending.className = (currentTagName == tagNames[tagNames.length - 1] ? "tag-enable-end" : "tag-blue-end");
  tagDiv.appendChild(ending);
}

function dispLabelFeedLink() {
  if (isLabelPage()) {
    var label = getPageLabelAsName();
    var labelFeed = document.getElementById('label-feed');
    var labelFeedLink = labelFeed.getElementsByTagName("a")[0];

    var temp = labelFeedLink.getAttribute('href');
    labelFeedLink.removeAttribute('href');
    labelFeedLink.setAttribute('href', temp + escape(Base64._utf8_encode(label)));
    
    temp = labelFeedLink.getAttribute('title');
    labelFeedLink.removeAttribute('title');
    labelFeedLink.setAttribute('title', temp + "'" + label + "'");

    labelFeedLink.innerHTML = "Subscribe Label '" + label + "' (Atom 1.0)";

    labelFeed.style.display = "block";
  }

  removeTextAboutMe();

}

function removeTextAboutMe() {

	// remove the text 'About Me'
	var sidebarTitles = document.getElementById('profile-container').getElementsByTagName('H2');
	sidebarTitles[0].innerHTML = "";

}