function escapeHTML (s) {
  return s.replace(/&/g, '&amp;').replace(/\"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

/* Some code simplified from MochiKit */
function E (tag, attrs/*, children */) {
  var elem = document.createElement(tag);
  if (attrs) {
    for (var k in attrs) {
      var v = attrs[k];
      elem.setAttribute(k, v);
    }
  }
  if (arguments.length > 2) {
    for (var i = 2; i < arguments.length; i++) {
      elem.appendChild(arguments[i]);
    }
  }
  return elem;
}

function T (text) {
  return document.createTextNode(text);
}

function pwTOC(itemclass, tocid) {
  var toc = document.getElementById(tocid);
  if (toc) {
    toc.appendChild(E('h2', null, T('Albums')));
    var toclist = toc.appendChild(E('ul', null));

    var headers = document.getElementsByTagName('h2');
    var last = headers.length;
    var pat = new RegExp('(^|\s)' + itemclass + '(\s|$)');

    for (var i = 0; i < last; i++) {
      if (pat.test(headers[i].className)) {
	var h = headers[i];
	var n = h.nextSibling;
	while (n.id == null) {
	  n = n.nextSibling;
	}

	toclist.appendChild(E('li', null,
			      E('a', { 'href':'#' + n.id,
				    'title': h.firstChild.nodeValue },
				T(h.firstChild.nodeValue)
				)
			      )
			    );
      }
    }
  }
}

/* Code adapted from http://phydeaux3.blogspot.com/2006/11/picasa-web-albums-with-json.html */
function _handler(root, section) {
  var feed = root.feed;
  var contents = [];

  var num;
  var i;
  if (feed.subtitle.$t) {
    var link;
    if (feed.link) {
      num = feed.link.length;
      for (i = 0; i < num; i++) {
	if (feed.link[i].rel == 'alternate') {
	  link = feed.link[i].href;
	}
      }
    }

    if (link) {
      section.insertBefore(E('h3', null,
			     E('a', { 'href': link },
			       T(feed.subtitle.$t)
			       )
			     ),
			   section.childNodes[0]
			   );
    } else {
      section.insertBefore(E('h3', null,
			     T(feed.subtitle.$t)
			     ),
			   section.childNodes[0]
			   );
    }
  }

  num = feed.entry.length;
  for (i = 0; i < num; i++) {
    var entry = feed.entry[i];
    var title = entry.title.$t;
    var summary = entry.summary.$t;
    var link = entry.link[0];

    for (var j = 1; j < entry.link.length; j++) {
      if (entry.link[j].rel == 'alternate')
	link = entry.link[j];
    }

    var thumb = entry.media$group.media$thumbnail[0];
    for (var j = 1; j < entry.media$group.media$thumbnail.length; j++) {
      var t = entry.media$group.media$thumbnail[j];
      if (t.width <= 160) {
	if (t.width > thumb.width) {
	  thumb = t;
	}
      }
    }

    section.appendChild(E('p', { 'class': 'icon' },
			  E('span', null,
			    E('a', { 'href': link.href, 'title': title },
			      E('img', { 'src': thumb.url, 'width': thumb.width, 'height': thumb.height, 'alt': title }))
			    )
			  )
			);
  }
}

var _global = this;
function _customHandler (name, section) {
  _global[name] = function (root) { _handler(root, section); };
}

function pwTarget (albumclass) {
  var all = document.getElementsByTagName('div');
  var last = all.length;
  var pat = new RegExp('(^|\s)' + albumclass + '(\s|$)');

  for (var i = 0; i < last; i++) {
    if (pat.test(all[i].className)) {
      _customHandler(all[i].id, all[i]);
    }
  }
}
