var clickedcolor='red';
var normalcolor='#000033'
var mousecolor='brown';

var clicked;

function init() {
  clicked=document.getElementById('tagcloudall');
  clicked.onmouseover=allmousein;
  clicked.onmouseout=mouseout(clicked);
  clicked.onmouseup=allmouseup;
  clicked.style.backgroundColor=clickedcolor;

  if (f_clientHeight() < 400) {
    var cloud=document.getElementById('tag-cloud');
    cloud.style.position='absolute';
  }

  var arr = document.getElementsByTagName('div');
  for (var i=0; i<arr.length; i++) {
    if (arr[i].id=='tagcloudpiece') {
      arr[i].style.backgroundColor=normalcolor;
      arr[i].onmouseover=mousein(arr[i]);
      arr[i].onmouseout=mouseout(arr[i]);
      arr[i].onmouseup=mousedown(arr[i]);
    }
  }
}

function allmouseup() {
  updateclicked(document.getElementById('tagcloudall'));
  foreachmatching('', function(matched,x) { showdiv(x); });
}

function allmousein() {
  var all=document.getElementById('tagcloudall');
  if (all != clicked)
    all.style.backgroundColor=mousecolor;
  foreachmatching('',function(matched,x) { x.style.backgroundColor='tan'; });
}

function mousedown(x) {
  return function() {
    updateclicked(x)
    foreachmatching(x.getAttribute('word'),
      function(matched,x) {
		      if (matched)
			showdiv(x);
		      else
			hidediv(x);
		    })
  };
}

function hidediv(ele) {
  ele.style.display = 'none';
}

function showdiv(ele) {
  ele.style.display='block';
}

function updateclicked(x) {
    if (clicked)
      clicked.style.backgroundColor=normalcolor;
    clicked=x;
    x.style.backgroundColor=clickedcolor;
}

function mousein(x) {
  return function () {
    if (x != clicked)
      x.style.backgroundColor=mousecolor;
    foreachmatching(x.getAttribute('word'),
      function(matched,x) {
		      if (matched)
			x.style.backgroundColor='tan';
		      else
			x.style.backgroundColor='';
		    })
  };
}
function mouseout(x) {
  return function () {
    if (x != clicked)
      x.style.backgroundColor=normalcolor;
    foreachmatching(x.getAttribute('word'),
      function(matched,x) { x.style.backgroundColor=''; })
  };
}

function foreachmatching(s,f) {
    var arr = document.getElementsByTagName('div');
    for (var i=0; i<arr.length; i++) {
      if (arr[i].id=='pub') {
	var strings = arr[i].getAttribute('words').split(':');
	var matched = false;
	for (var j=0; j<strings.length; j++) {
	  if (strings[j]==s)
	    matched=true;
	}
	f(matched,arr[i]);
      }
    }
}

function f_clientWidth() {
        return f_filterResults (
                window.innerWidth ? window.innerWidth : 0,
                document.documentElement ? document.documentElement.clientWidth : 0,
                document.body ? document.body.clientWidth : 0
        );
}
function f_clientHeight() {
        return f_filterResults (
                window.innerHeight ? window.innerHeight : 0,
                document.documentElement ? document.documentElement.clientHeight : 0,
                document.body ? document.body.clientHeight : 0
        );
}
function f_scrollLeft() {
        return f_filterResults (
                window.pageXOffset ? window.pageXOffset : 0,
                document.documentElement ? document.documentElement.scrollLeft : 0,
                document.body ? document.body.scrollLeft : 0
        );
}
function f_scrollTop() {
        return f_filterResults (
                window.pageYOffset ? window.pageYOffset : 0,
                document.documentElement ? document.documentElement.scrollTop : 0,
                document.body ? document.body.scrollTop : 0
        );
}
function f_filterResults(n_win, n_docel, n_body) {
        var n_result = n_win ? n_win : 0;
        if (n_docel && (!n_result || (n_result > n_docel)))
                n_result = n_docel;
        return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

window.onload = init;


