// Fade in

function FadeIn(imageId) {
	this.imageId = imageId;
	this.imageDiv = null;
}

FadeIn.prototype.init = function()
{
	var _this = this;
	this.addLoadHandler(function(){_this.initImage();});
}

FadeIn.prototype.initImage = function()
{
	this.imageDiv = document.getElementById(this.imageId);	
	this.setOpacity(0);
	this.imageDiv.style.top = "0px";	
	this.imageDiv.style.display = "block";
	this.imageDiv.style.visibility = "visible";
	this.fadeIn(0);
	this.isSafari();
}

FadeIn.prototype.fadeIn = function(opacity) 
{
	if (document.getElementById) {
		if (opacity <= 100) {
			this.setOpacity(opacity);
			opacity += 7; // Snelheid
			var _this = this;
			window.setTimeout(function(){_this.fadeIn(opacity);}, 100);
		}
		else{
			this.setOpacity(100);
		}
	}
}

FadeIn.prototype.isSafari = function()
{
	var userAgentStr = navigator.userAgent;
	userAgentStr = userAgentStr.toLowerCase();
	var SafariPos = userAgentStr.indexOf("safari");
	if(SafariPos < 0) {
		return false;
	} else { 
		return true;
	}
}

FadeIn.prototype.setOpacity = function(opacity) 
{
	if(this.isSafari()) {
			opacity = (opacity == 100) ? 100 : opacity;
	} else {
			opacity = (opacity == 100) ? 99.999 : opacity;
	}
	// Safari<1.2, Konqueror
	this.imageDiv.style.KHTMLOpacity = opacity/100;
	// IE/Win
	this.imageDiv.style.filter = "alpha(opacity:"+opacity+")";
	// Older Mozilla and Firefox
	this.imageDiv.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	this.imageDiv.style.opacity = opacity/100;
}

FadeIn.prototype.addLoadHandler = function(handler)
{
	if(window.addEventListener)
	{
		window.addEventListener("load",handler,false);
	}
	else if(window.attachEvent)
	{
		window.attachEvent("onload",handler);
	}
	else if(window.onload)
	{
		var oldHandler = window.onload;
		window.onload = function piggyback()
		{
			oldHandler();
			handler();
		};
	}
	else
	{
		window.onload = handler;
	}
}

// Drop down box

function DropDownDivs(divid,dropDownSize,MSToDrop)
{
	this.divid = divid;
	this.div = null;
	this.MSToDrop = MSToDrop;
	
	this.startTime = 0;
	this.elapsedTime = 0;
	this.dropDownSize = dropDownSize;
	this.timer = null;
}

DropDownDivs.prototype.init = function()
{
	var _this = this;
	this.addLoadHandler(function(){_this.start();});
}

    DropDownDivs.prototype.startSlider = function() { this.startTime = (newDate()).getTime(); document.getElementById(this.divid).style.display = "inline";	

	
	var _this = this;
	var execfunction = function() {_this.DropDownTick(); };
	this.timer = new CTimer(execfunction,27);
	this.timer.init();
}

DropDownDivs.prototype.start = function()
{
	this.div = document.getElementById(this.divid);
	this.div.style.top = -this.dropDownSize + "px";
	
	var _this = this;
	window.setTimeout(function(){_this.startSlider();}, 2000)
}

DropDownDivs.prototype.DropDownTick = function()
{
	var ElapsedTime = (new Date()).getTime() - this.startTime; 
  if (ElapsedTime >= this.MSToDrop) {
    this.endSlide()
  } else {
    var NextStep = 0;
    var DistancePercent = 0;  
    DistancePercent = this.EaseOut(ElapsedTime/this.MSToDrop);
    NextStep = Math.round(DistancePercent * this.dropDownSize);
	  this.div.style.top = (-this.dropDownSize + NextStep) + "px";
	}
}

DropDownDivs.prototype.endSlide = function()
{
	this.timer.stop();
}

DropDownDivs.prototype.EaseOut = function(percent)
{
	percent = 100-(percent * 100);
	percent = (100-Math.pow(1.0465, percent))+1;
	if (percent < 1) { // remove flicker
		percent = 1;
	}
	return (percent/100); //return power curve based value from a percentage input
}

DropDownDivs.prototype.addLoadHandler = function(handler)
{
	if(window.addEventListener)
	{
		window.addEventListener("load",handler,false);
	}
	else if(window.attachEvent)
	{
		window.attachEvent("onload",handler);
	}
	else if(window.onload)
	{
		var oldHandler = window.onload;
		window.onload = function piggyback()
		{
			oldHandler();
			handler();
		};
	}
	else
	{
		window.onload = handler;
	}
}

function CTimer(evalfunction,intervalTime)
{
  this.evalfunction = evalfunction;
  this.counter = 0;
  this.stopped = true; 
  this.start = 0;
  this.intervalTime = intervalTime;
  this.interval1 = null;
  this.previousTime = 0;
}

CTimer.prototype.init = function()
{
  var d = new Date();
  this.start = d.getTime();
  var _this = this;
  this.interval1 = setInterval(function() {_this.intervaltick(); }, 15);
  this.stopped = false;
}

CTimer.prototype.intervaltick = function()
{
  if(!this.stopped) {
    var d = new Date();
    var current = d.getTime();
    var diff = this.previousTime - current;
    this.previousTime = current
    if (current < (this.start + (this.intervalTime*this.counter))) {
   } else {
      var executions = ((current - this.start) / this.intervalTime) - this.counter;
      for(var j = 0;j < executions;j++) {
        this.evalfunction();
        this.counter++;
      }
    }
  }
} 

CTimer.prototype.stop = function()
{
  this.stopped = true;
  clearInterval(this.interval1);
  return this.counter; 
}






