/**
 * Animator
 */
var Animator = function (startValue, endValue, f, t, r) {
  this.step  = 5;              // The step by which timer counts from 0 to 100
  this.rate  = 5;              // The rate in ms at which steps are taken
  this.power = 2;              // The steepness of the animation curve
  this.type  = this.EASEINOUT; // The type of easing
  this.duration = 0;           // The total duration

  this.min = startValue;
  this.max = endValue;
  this.isReversing = false;
  this.animateFunction = f;
  this.terminateFunction = t;
  this.reversalFunction = r;

  this.precalc = new Array(100);
  for (var i=0; i <= 100; i++) {
    this.precalc[i] = this.calculate(i);
  }
}

  Animator.prototype = {
    NONE: 0,
    EASEIN: 1,
    EASEOUT: 2,
    EASEINOUT: 3,

    setDuration:function (duration) {
      this.duration = duration;
    },

    setStep:function (step) {
      this.step = step;
    },

    setRate:function (rate) {
      this.rate = rate;
    },

    setPower:function (power) {
      this.power = power;
      for (var i=0; i <= 100; i++) {
        this.precalc[i] = this.calculate(i);
      }
    },

    setType:function (type) {
      this.type = type;
      for (var i=0; i <= 100; i++) {
        this.precalc[i] = this.calculate(i);
      }
    },

    setAnimateFunction:function (f) {
      this.animateFunction = f;
    },

    start:function () {
      if (this.interval) {
        return;
      }

      this.time = 0;
      this.startTime = new Date();
      this.isReversing = false;
      this.interval = setInterval(this.method(this.animate), this.rate);
     },

    reverse:function () {
      this.isReversing = (this.isReversing) ? false : true;
    },

    stop:function () {
      if (this.interval) {
        clearInterval(this.interval);
      }

      this.timeLeft = 0;
      this.interval = null;
    },

    pause:function () {
      if (this.interval) {
        clearInterval(this.interval);
      }

      this.interval = null;
      if (this.duration) {
        var now = new Date() - this.startTime;
        this.timeElapsed = now;
      }
    },

    resume:function () {
      if (this.duration) {
        var now = new Date().getTime();
        this.startTime = new Date(now-this.timeElapsed);
      }

      this.interval = setInterval(this.method(this.animate), this.rate);
    },

    isAnimating:function () {
      return Boolean(this.interval);
    },

    animate:function () {
      var finished = false;
      if (this.duration) {
        var now = new Date() - this.startTime;
        if (now < this.duration) {
          this.animateFunction(this.calculate(now/this.duration));
        } else { finished = true; }
      } else {
        if (this.time >= 0 && this.time <= 100) {
          this.animateFunction(this.precalc[this.time]);
          this.time = (this.isReversing) ? this.time - this.step : this.time + this.step;
        } else { finished = true; }
      }

      if (finished) {
        clearInterval(this.interval);
        this.animateFunction(this.max);
        this.interval = null;

        if (!this.isReversing && this.terminateFunction) {
          this.terminateFunction();
        }

        if (this.isReversing && this.reversalFunction) {
          this.reversalFunction();
        }
      }
    },

    calculate:function (t) {
      var factor;

      if (!this.duration) {
        t = t/100;
      }

      switch (this.type) {
        case this.NONE      : factor = this.easeNone(t);break;
        case this.EASEIN    : factor = this.easeIn(t);break;
        case this.EASEOUT   : factor = this.easeOut(t);break;
        case this.EASEINOUT : factor = this.easeInOut(t);break;
      }

      return parseInt(this.min + factor*(this.max-this.min));
    },

    easeNone:function (t) {
      return t;
    },

    easeIn:function (t) {
      return Math.pow(t,this.power);
    },

    easeOut:function (t) {
      return 1-this.easeIn(1-t);
    },

    easeInOut:function (t) {
      if (t < 0.5) {
        return this.easeIn(t*2)/2;
      }

      return 0.5 + this.easeOut((t-0.5)*2)/2;
    },

    method:function (method) {
      var context = this;

      return function () {
        return method.apply(context, arguments);
      }
    }
  }
	
	$(document).ready(function() {
	  // navigation
		new Navigation(document.getElementById('navigation'));
		
		// actionbox
		$("div.actionbox").hover(function() {
		  $(this).addClass("roundedbox-dark1");
			$(this).css("cursor", "pointer");
			//window.status=$(this).find("p.navlink a").attr("href");
		}, function() {
		  $(this).removeClass("roundedbox-dark1");
			$(this).css("cursor", "default");
			//window.status="";
		});
		
		$("div.actionbox").click(function() {
			window.location=$(this).find("p.navlink a").attr("href"); return false;
		});
		
		// slides
    $("div.slides dl.slide dt").click(function () {
			var visible = !$(this).parent("dl").hasClass("active");			
		  $(this).parents("div.slides").find("dl").each(
				function() {
					if ($(this).hasClass("active")) {
						$(this).removeClass("active");
						$(this).find("dd").slideUp("slow");
					}
				}
			);
			
			if (visible) {
			  $(this).parent("dl").addClass("active");
				$(this).next("dd").slideDown("slow");
			}
    });
  });	