﻿var baseurl = "http://www.bibsonomy.org/";
// adapt to "ht09 here"
var content = "tag/ht09"
var tagsurl = baseurl + content;
var jsonurl = baseurl + "json/relatedtags/" + content + "?callback=buildTagCloud";


// ids of the elements we work on
var tagcloudId = "tagcloud";
var infoId     = "tagcloud_info";
// will hold the elements later
var tagcloud;
var info;

function insertTagCloud() {
  // append the script with the JSON from BibSonomy
  // the JSON then calls buildTagCloud()
  appendScript(jsonurl);
}

// font size computation of tags
function getFontSize(count) {
  return 100 + Math.pow(count, 2) + "%";
}

// does the main work
function buildTagCloud(result) {

    // get the main elements we work on
    info = document.getElementById(infoId);
    tagcloud = document.getElementById(tagcloudId)

    var ul = document.createElement("ul");

    var json = eval(result);
    var tags = json.items;

    quick_sort(tags);

    for (i = 0; i < tags.length; i++) {
      var tag = tags[i]
      if (tag.type = "Tag") {
        /* 
         * build one list element
         */
        var li = document.createElement("li");
        var a  = document.createElement("a");
        a.href = tagsurl + "+" + tag.label;
        a.target = "_blank";
        /*
         * set style for Hypertext 2009
         */
        //a.style.color = "#2a5a8a";
        //a.style.textDecoration = "underline";

        // set font size
        li.style.fontSize = getFontSize(tag.count);
        // set title
        if (tag.count < 2) {
          li.title = tag.count + " post";
        } else {
          li.title = tag.count + " posts";
        }

        var text = document.createTextNode(tag.label);

        // connect the nodes
        a.appendChild(text);
        li.appendChild(a);
        li.appendChild(document.createTextNode(" "));
        ul.appendChild(li);
      }

    }


//    tagcloud.appendChild(document.createTextNode(jsonurl));
//    tagcloud.appendChild(ul);
    tagcloud.insertBefore(ul, info);
    appendHover(tagcloud);
}

function appendHover(elem) {
  elem.onmouseover = function (event) {
    info.style.display = "block";
  };
  elem.onmouseout = function (event) {
    info.style.display = "none";
  }
}

// inserts a new 'script' element with the given URL in the header of the document
function appendScript(url) {
    var headID = document.getElementsByTagName("head")[0];
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = url;
    headID.appendChild(newScript);
}


/*
 * Quicksort starts here
 */ 

// change sort criterion here
function compare(a, b) {
  return (a.label.toLowerCase() <= b.label.toLowerCase());
}

function partition(array, begin, end, pivot)
{
	var piv=array[pivot];
	array.swap(pivot, end-1);
	var store=begin;
	var ix;
	for(ix=begin; ix<end-1; ++ix) {
                // change sort criterion here
		//if(String.toLowerCase(array[ix].label) <= String.toLowerCase(piv.label)) {
                if(compare(array[ix], piv)) {
			array.swap(store, ix);
			++store;
		}
	}
	array.swap(end-1, store);

	return store;
}
Array.prototype.swap=function(a, b)
{
	var tmp=this[a];
	this[a]=this[b];
	this[b]=tmp;
}
function qsort(array, begin, end)
{
	if(end-1>begin) {
		var pivot=begin+Math.floor(Math.random()*(end-begin));

		pivot=partition(array, begin, end, pivot);

		qsort(array, begin, pivot);
		qsort(array, pivot+1, end);
	}
}
function quick_sort(array)
{
	qsort(array, 0, array.length);
}

