//<!--
function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function getScrollTop() {
  var result = 0;
  if (self.pageYOffset) // all except Explorer
  {
	  result = self.pageYOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
	  // Explorer 6 Strict
  {
	  result = document.documentElement.scrollTop;
  }
  else if (document.body) // all other Explorers
  {
	  result = document.body.scrollTop;
  }
  return result;
}

function getScrollHeight() {
  var result = 0;
  var test1 = document.body.scrollHeight;
  var test2 = document.body.offsetHeight;
  //alert(test1 + ", " + test2 )
  if (test1 < test2) // all but Explorer Mac
  {
	  result = document.body.scrollHeight;
  }
  else // Explorer Mac;
      //would also work in Explorer 6 Strict, Mozilla and Safari
  {
	  result = document.body.offsetHeight;
  }
  return result;
}

var headerTop = 0; 

var headerElement = null;

var headerWiggle;

var wigglers = new Array;

function WIGGLE(obj) {
  
  this.range = 10;
  this.id = wigglers.length;
  wigglers[wigglers.length] = this;
  
  this.element = obj;
  this.currentX = 0;
  this.currentY = 0;
  this.targetX = 0;
  this.targetY = 0;
  
  this.movementInfluence = 0.5;
  this.deltaInfluence = 0.1;   // speed
  this.movementSnapAt = 0.5;
  this.deltaSnapAt = 0.5;
  this.delta = 0;
  this.running = false;
  this.movement = 0;
  

  this.move = function() {
    this.delta = this.targetY - this.currentY;
    this.movement = this.movement * this.movementInfluence + this.delta * this.deltaInfluence;
    //alert(this.element.style.top + ": " + this.currentY + ", " + this.targetY + ", " + this.delta + ", " + this.movement);

    if (( Math.abs( this.delta ) > this.deltaSnapAt ) && ( Math.abs( this.movement ) > this.movementSnapAt ) ){
      this.currentY += this.movement;
      this.element.style.top = Math.round(this.currentY) + "px";
      window.setTimeout("wigglers[" + this.id + "].move()", 50); 
    }
    else {
      this.element.style.top = Math.round(this.targetY) + "px";
      this.running = false;
    } 
  }
  
  this.start = function() {
    if (this.running) return;
    this.running = true;
    this.move();
  }

  this.snap = function() {
    this.currentY = this.targetY;
    this.start();
  }

}


function getElements() {
	if (headerElement == null) {
	  headerElement = document.getElementById('header');
	  headerWiggle = new WIGGLE(headerElement);
	}  
}

function setFunkyContainer() {
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
      getElements();
      headerWiggle.targetY = headerTop;
      headerWiggle.start();
		}
	}
}

function setContainer() {
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		
		if (windowHeight > 0) {
			getElements();

      scrollHeight = getScrollHeight() - 12;
      scrollTop = getScrollTop();
      headerElement = document.getElementById('header');
      headerTop = Math.round(scrollTop);
      headerElement.style.top = headerTop + "px";
      //setFunkyContainer();
    }
  }
}


function initContainer() {
  setContainer();
  window.setTimeout("setContainer()", 200);
}

loadProcs[loadProcs.length] = "initContainer()";


if (window.addEventListener)  { // W3C DOM
  //alert("addEventListener");
  window.addEventListener("scroll", setContainer, false);
  window.addEventListener("resize", setContainer, false);
} 
else {
  window.onresize = function() {
	  setContainer();
  }

  window.onscroll = function() {
	  setContainer();
  }
}       
//-->

