function hookEvent(el, eventName, callback)
{
  if(typeof(el) == "string")
    element = document.getElementById(el);
  if(element == null)
    return;
  if(element.addEventListener)
  {
    if(eventName == 'mousewheel')
      element.addEventListener('DOMMouseScroll', callback, false);  
    element.addEventListener(eventName, callback, false);
  }
  else if(element.attachEvent)
    element.attachEvent("on" + eventName, callback);
}

function unhookEvent(element, eventName, callback)
{
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
    return;
  if(element.removeEventListener)
  {
    if(eventName == 'mousewheel')
      element.removeEventListener('DOMMouseScroll', callback, false);  
    element.removeEventListener(eventName, callback, false);
  }
  else if(element.detachEvent)
    element.detachEvent("on" + eventName, callback);
}

function cancelEvent(e)
{
  e = e ? e : window.event;
  if(e.stopPropagation)
    e.stopPropagation();
  if(e.preventDefault)
    e.preventDefault();
  e.cancelBubble = true;
  e.cancel = true;
  e.returnValue = false;
  return false;
}

var lock = 0;
var currentb = 0;
var longtext = false;

function eobject() {
	this.detail = 0;
}

$(window).keydown(function(event){
  var e = new eobject();
  switch (event.keyCode) {
	case 38:
		e.detail = -3;
		break;
	case 40:
		e.detail = 3;
		break;
  }
  printInfo(e);
});


function printInfo(e)
{
 if (lock != 0) return;
  lock = 1;
  e = e ? e : window.event;
  var raw = e.detail ? e.detail : e.wheelDelta;
  var normal = e.detail ? e.detail * -1 : e.wheelDelta / 40;
  normal = normal / 3;
  
   if (longtext) {
	var top = $("#content").css("top");
    top = parseInt(top);
	var height = parseInt($("#contenttextlong"+(currentb)).css("height"));
	
    if (top < (-currentb*196-height-normal*25) || top > (-currentb*196-normal*25)) {
		lock = 0;
		return;
	}
	$("#content").animate({top: "+="+normal*25+"px"}, 0);
  } else {
  
  if ((currentb - normal) < 0 ||(currentb-normal) >= numofboxes) {
	lock = 0;
	return;
  }

  $("#box"+(currentb)).animate({opacity: ".60"}, 1000, "easeInOutQuint");
  currentb -= normal;
  $("#box"+(currentb)).animate({opacity: "1"}, 1000, "easeInOutQuint");
  $("#content").animate({top: "+="+(normal*196)+"px"}, 1000, "easeInOutQuint");
  }
  cancelEvent(e);
  lock = 0;
  
}

function onBoxMouseIn(id) {
	if (id == currentb) return;
	//$("#box"+id).animate({opacity: "1"}, 1000, "easeInOutQuint");
	//alert("HERE");
}

function onBoxMouseOut(id) {
	if (id == currentb) return;
	//$("#box"+id).animate({opacity: ".60"}, 1000, "easeInOutQuint");
	//alert("OUT");
}

var e1 = false;
function onBoxReadMore(id) {
	if (e1) return;
	e1 = true;
	if (longtext) {
		if (id == currentb) {

		$("#contenttextlong"+id).animate({height: "0"}, 2000, "easeInOutQuint");
		$("#content").animate({top: (-currentb*196-3)+"px"}, 2000, "easeInOutQuint");
		$("#contentreadmore"+id).html("read more");
		setTimeout('$("#contenttextlong'+id+'").css("visibility", "hidden");', 2000);
		longtext = false;
		} else {
		$("#contenttextlong"+currentb).animate({height: "0"}, 2000, "easeInOutQuint");
		$("#content").animate({top: (-id*196-3)+"px"}, 2000, "easeInOutQuint");
		$("#contentreadmore"+currentb).html("read more");
		setTimeout('$("#contenttextlong'+currentb+'").css("visibility", "hidden");', 2000);
		currentb = id;
		longtext = false;
		setTimeout('onBoxReadMore('+id+');', 1000);
		}
	} else {
		if (id != currentb) {
			$("#box"+(currentb)).animate({opacity: ".60"}, 1000, "easeInOutQuint");
			currentb = id;
			$("#box"+(currentb)).animate({opacity: "1"}, 1000, "easeInOutQuint");
			$("#content").animate({top: (-currentb*196-3)+"px"}, 2000, "easeInOutQuint");
		}
		var sheight = $("#contenttextlong"+id)[0];
		sheight = sheight.scrollHeight;
		$("#contenttextlong"+id).animate({height: (sheight+20)+"px"}, 2000, "easeInOutQuint");
		$("#contenttextlong"+id).css("visibility", "visible");
		$("#contentreadmore"+id).html("close");
		longtext = true;
	}
	e1 = false;
}

function onLoaded() {
	hookEvent("body1", 'mousewheel', printInfo);
}
