var glob_from = 0;
var fadeInDelay = 0;
var belofteTimeout = 0;

function $(id) {
  return document.getElementById(id);
}
function setOpacity(id, opacity) {
  var obj = (String(typeof(id)).toLowerCase() == "object") ? id : $(id);
  if(obj != undefined) {
    obj.style.opacity = obj.style.MozOpacity = obj.style.KhtmlOpacity = (opacity / 100);
    obj.style.filter = "alpha(opacity=" + opacity + ")";
  }
}
function removeObjectById(id) {
  var obj = $(id);
  if(obj != undefined) {
    obj.parentNode.removeChild(obj);
  }
}
function move(who, what, where, step, interval, suffix, isLooping, func) {
  if(isLooping === undefined) { isLooping = false; }
	if(suffix === undefined) { suffix = ""; }
  if(func === undefined) { func = ""; }
	if(!isLooping && isIE) { interval = interval/2; }
  var obj = (String(typeof(who)).toLowerCase() == "object") ? who : $(who);
	var curr = eval("obj.style." + what);
	curr = curr.replace("pt","");
	curr = curr.replace("px","");
	curr = curr.replace(" ","");
	if(curr > Number(where)) {
		if(Number(curr)-Number(step) > where) {
		  obj.style[what] = Number(curr)-Number(step) + suffix;
			setTimeout("move('" + who + "', '" + what + "', " + where + ", " + step + ", " + interval + ", '" + suffix + "', true, '"  + func + "')", interval);
		} else {
      obj.style[what] = where + suffix;
      if(func != undefined && func !== null && func != "") { eval(func); }
		}
	} else if(curr < Number(where)) {
		if(Number(curr)+Number(step) < where) {
		  obj.style[what] = Number(curr)+Number(step) + suffix;	
			setTimeout("move('" + who + "', '" + what + "', " + where + ", " + step + ", " + interval + ", '" + suffix + "', true, '"  + func + "')", interval);
		} else {
		  obj.style[what] = where + suffix;
		  if(func != undefined && func !== null && func != "") { eval(func); }
		}
	} else {
	  obj.style[what] = where + suffix;
		if(func != undefined && func !== null && func != "") { eval(func); }
	}
}
function fader(id, start, end, div, first, func) {
  if(first === undefined) { first = false; }
  if(func === undefined) { func = ""; }
  var obj = (String(typeof(id)).toLowerCase() == "object") ? id : $(id);
  if(obj != undefined) {
    if(obj.style.opacity == (end / 100) || obj.style.MozOpacity == (end / 100) || obj.style.KhtmlOpacity == (end / 100) || obj.style.filter == "alpha(opacity=" + end + ")") {
      if(func != undefined && func !== null && func != "") { eval(func); }
    } else {
      var step = Math.ceil(((end - start) / div) / 5) * 5;
      var opacity = start + step;
      var next = false;
      if(first) {
        opacity = start;
        next = true;
      } else if((step > 0 && opacity <= end) || (step < 0 && opacity >= end)) {
        next = true;
      } else {
        opacity = end;
      }
      obj.style.opacity = obj.style.MozOpacity = obj.style.KhtmlOpacity = (opacity / 100);
      obj.style.filter = "alpha(opacity=" + opacity + ")";
      if(next) {
        setTimeout(function() {
          fader(obj, opacity, end, div, false, func); }, 5);
      } else {
        if(func != undefined && func !== null && func != "") { eval(func); }
      }
    }
  }
}
function inArray(v, a) {
  for(var i in a) {
    if(a[i] == v) { return true; }
  }
  return false;
}
function createXmlHttpRequestObject() {
  var xmlHttp;
  if(window.ActiveXObject) {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      xmlHttp = false;
    }
  } else {
    try {
      xmlHttp = new XMLHttpRequest();
    } catch(e2) {
      xmlHttp = false;
    }
  }
  if(!xmlHttp) {
    alert("Error creating the XMLHttpRequest object.");
  } else {
    return xmlHttp;
  }
}
var xmlHttp = createXmlHttpRequestObject();

function getNewContent(nr) {
  if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    xmlHttp.open("GET", "includes/getcontent.php?from=" + glob_from + "&nr=" + nr, true);
    xmlHttp.onreadystatechange = handleServerResponseNewContent;
    xmlHttp.send(null);
  } else {
    setTimeout("getNewContent(" + nr + ")", 100);
  }
}
function handleServerResponseNewContent() {
  if(xmlHttp.readyState == 4) {
    if(xmlHttp.status == 200) {
      var bits = xmlHttp.responseText.split("]||[");
      $('container').innerHTML += bits[1];
      Cufon.replace(".tweetcolumn");
      var newId = "belofte" + bits[0];
      $(newId).style.visibility = "visible";
    } else {
      alert("There was a problem accessing the server: "+xmlHttp.statusText);
    }
  }
}
function refreshBeloftes() {
  if(glob_from == 0) {
    belofteTimeout = setTimeout("refreshBeloftes()", 30000);
  } else {
    clearTimeout(belofteTimeout);
  }
  for(var i = 0; i < 6; i++) {
    if(fadeInDelay != 0) setTimeout('removeObjectById("belofte' + i + '")', 1250 - i * 250);
    setTimeout('getNewContent(' + i + ')', fadeInDelay + i * 500);
  }
  fadeInDelay = 1500;
}
function nextBeloftes() {
  if((glob_from + 6) <= numB) {
    glob_from += 6;
    refreshBeloftes();
  }
}
function prevBeloftes() {
  if(glob_from != 0) {
    glob_from -= 6;
    if(glob_from < 0) glob_from = 0;
    refreshBeloftes();
  }
}
function gotoBeloftes(from) {
  if(glob_from != from) {
    glob_from = from;
    refreshBeloftes();
  }
}
function castVote(tweetid, vote) {
  if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    xmlHttp.open("GET", "includes/getcontent.php?tweetid=" + tweetid + "&vote=" + vote, true);
    xmlHttp.onreadystatechange = handleServerResponseVote;
    xmlHttp.send(null);
  } else {
    setTimeout("castVote(" + tweetid + ", '" + vote + "')", 100);
  }
}
function handleServerResponseVote() {
  if(xmlHttp.readyState == 4) {
    if(xmlHttp.status == 200) {
      var bits = xmlHttp.responseText.split("]||[");
      $(bits[0] + bits[1]).innerHTML = bits[2];
      Cufon.replace(".tweetcolumn");
    } else {
      alert("There was a problem accessing the server: "+xmlHttp.statusText);
    }
  }
}
