// maximum 100 links brought in from del.icio.us, 

var firefox = (navigator.product) && (navigator.product.match(/gecko/i));
var debug = 1;

// transform tag lists to hash, rank links

var tagTable = new Array;
var rankTable = new Array;

function buildTables() 
{

  for (var i=0, post; post = Delicious.posts[i]; i++) 
  {
    var rank = 0; // all posts must have a rating! init to 0*
//if (debug) { alert( post.t.splice(0,post.t.length-1).join(',')); }
    for (var lx=0, tag; tag = post.t[lx]; lx++) 
    {
      var r = new Array; // check for rank first
      r = tag.match(/^([0-9]+)\*$/);
      if (r) { rank = Number(r[1]); }
      if (firefox) { tag += '"' ; }
      if (undefined === tagTable[tag]) { tagTable[tag] = new Array; }
      tagTable[tag].push(i);
    }
    rankTable[i] = rank;
  }
}

// function: insert a link to Delicious.posts array

function insertLink(tag,tagtext, linkicon, linkpri, linkurl) 
{
  var post = new Object();
  post.t = new Array();

  post.n = -1; // mark as inserted link
  post.t.push(tag);
  post.t.push(linkpri);
  post.t.push(linkicon);
  post.d = tagtext;
  post.u = linkurl;

  Delicious.posts.push(post);
}

// function: timeout on img load so delete it
function clearImg(obj, img) 
{ 
  if (!img.complete) { obj.removeChild(img); } 
} 

// function: onLoad function to display image
function showImage(img)
{ 
  return (function(){ img.style.display='inline' }) ;
}


// sort tags numerically, by rank, if present, highest to lowest
// return random result for ==
function tagorder(a,b) 
{
  var na = rankTable[a];
  var nb = rankTable[b];
//  if (debug) { alert('na=' + na + " nb=" + nb)};

  if (na == nb) { return(Math.random()-0.5); } // 50% chance a prints first
  if (na > nb) { return -1; }
  return (1);

}

var divID = "linksContainer"; // default
function setDivId (tag) { divID = tag; }

// **********************************************************************
// showList of links with "tag", use tagtext as heading, and max ct links
// ct == 0? then print all links

function showList(tag, tagtext, ct, flags, tHt, tWd) 
{
  var all = ct == 0;
  var thumbs = 0
  if (flags) thumbs = flags.match(/t/);

  // firefox breaks if tag=="watch" -- so make all tags >"tag< in ff
  if (firefox) { tag += '"' ; }

  if (0 == rankTable.length) { buildTables(); }

  // insert heading if there is one
  if (tagtext) 
  {
    var h = document.createElement('p');
    h.appendChild(document.createTextNode(tagtext));
// .SideBarTitle{font-weight:600;letter-spacing: 1px;padding:2em 1em 0.25em 1em;}
    h.style.fontWeight='600';
    h.style.letterSpacing='1px';
    h.style.padding='2em 1em 0.25em 1em';
    document.getElementById(divID).appendChild(h);
  }

  var links = tagTable[tag];
  if (links) 
  { 
    links.sort(tagorder);

    // insert sorted links

    for (var i=0, post; post = Delicious.posts[links[i]]; i++) 
    {
      // display ct links, but print all inserted links
      if (!all && ct <= 0 && post.n != -1) { continue; }

      var p = document.createElement('p')
      var img = document.createElement('img')
      var a = document.createElement('a')

      if (thumbs)
      {
	var br = document.createElement('br')
	// http://www.flickr.com/photos/96635529@N00/190533440/
	var nW = new Array;
	nW = post.n.split(' ');
	var uID = post.u.split('/')[4];
	var nStyle= nW[0];
	var nThumb= nW[1]; 

	p.style.marginTop = '16px';
	p.style.marginBottom = '16px';

	img.style.width = tWd + 'px';
	img.style.height = tHt + 'px';
	img.style.display = 'none';
	img.src = nThumb;
	img.onload = showImage(img);

	/* build gallery URL */

//	var gall = 'http://splashr.com/show/' + nStyle + '/' + uID + '/' + post.t.splice(0,post.t.length-1).join(',') + '/25';
	var gall = 'http://splashr.com/show/' + nStyle + '/' + uID + '/' + post.t[0] + '/25';

	a.setAttribute('href', gall)
	a.setAttribute('target', '_blank')

	a.appendChild(img)
	a.appendChild(br)
	a.appendChild(document.createTextNode(post.d))

	p.appendChild(a)
      } 
      else 
      {
	// default behaviour... 
	// blog roll random order within rank

	p.style.lineHeight = '20px';
	a.style.marginLeft = '20px'
	img.style.position = 'absolute'
	img.style.height = img.style.width = '16px';
	img.style.display = 'none'
	if (post.n == -1) 
	{ // hack to insert links
	  img.src = post.t[2]
	}
	else 
	{
	  img.src = post.u.split('/').splice(0,3).join('/')+'/favicon.ico'
	}
	img.onload = showImage(img);

	a.setAttribute('href', post.u)

	var targ = '_earhead';
	if (post.u.match(/^java/)) targ = '_self';
	if (tag.match(/archivelist/)) targ = '_self';

	a.setAttribute('target', targ)
	// text for ancher, remove everything from " - "
	a.appendChild(document.createTextNode(post.d.replace(/ - .*/,'')))
	p.appendChild(img)
	p.appendChild(a)

      }

      if (firefox) { window.setTimeout(clearImg, 2000, a, img); }
      document.getElementById(divID).appendChild(p)
      ct--;
    }
  }
}

