/*
Dynamic functions for homepage www.kindermonument.nl
Copyright (c) 2002 Ylab, Utrecht, NL
Author: Yohan Creemers
version 1.1.1
1.1: Added parameters to startPulley()
     SwitchLight/Made SwitchLight document independent and fail safe
     SwitchLight/Recalculate curtain height after scrolling
1.1.1: Typo in clipNmove
- Pulley
- SwitchLight
- clipNmove
*/

var pulleyObject, displacement;
var movement = new Object()
var angles = [70,75,80,85];
var lightIsOn = true;
var clipIsMoving = false;
var docHeight = 0;

/*Pulley 
  Sample use:
  startPulley("agenda", 8, 40);
  objectId: id of the object
  interval: seconds between two swingPulleys [optional, defaults to 7 seconds]
  amplitude: maximum amplitude [optional, defaults to 40 pixels]
*/
function startPulley(objectId, interval, amplitude){
  pulleyObject = document.getElementById(objectId);
  pulleyObject.style.position="relative";
  
  //set parameters
  movement.interval = (!interval ? 7 : interval);
  movement.amplitude = (!amplitude ? 40 : amplitude);
  movement.initpos = getLayerTop(pulleyObject);
  resetPulley();
}

function resetPulley(){
  //reset parameters, schedule next pulley movement
  movement.finalpos = (movement.finalpos != 100 ? 100 : movement.initpos);
  movement.oscillations = 0;
  setTimeout("swingPulley();", movement.interval*1000);
}
function swingPulley(){
  //up (-1) or movePulley (+1)
  var direction = Math.pow(-1, movement.oscillations);
  //calculate deflection, simulate an oscillation
  var deflection = direction * Math.cos(angles[movement.oscillations] * Math.PI / 180) * movement.amplitude;
  //calculate new position
  var pos = movement.finalpos + Math.floor(deflection);
  //raise counter
  movement.oscillations++;
  //reached endposition or need another go?
  if (movement.oscillations <= angles.length) {movePulley(pos,direction);}
  else {resetPulley();}
}
/*actual movement
  the movement is split up in small parts to see the element 
  moving over the screen, with a decreasing speed*/
function movePulley(targetPosition, direction){
  currentPosition=getLayerTop(pulleyObject);
  //calculate movement in px
  displacement = Math.round((targetPosition - currentPosition)/8)
  //set minimum displacement to 1px
  if (displacement == 0) {displacement = direction*1;}

  //set new position
  currentPosition += displacement;
  setLayerTop(pulleyObject,currentPosition);
  
  //define if targetPosition is reached
  if (((direction==1)&&(currentPosition < targetPosition))||((direction==-1)&&(currentPosition > targetPosition))){
    var code = "movePulley(" + targetPosition + "," + direction + ")";
    setTimeout(code,1);
  }
  else{
    swingPulley();
  }
}

//----------------------------------------------------------------------

/*Simulate a light switch
  Sample use:
  <a href="javascript: switchLight(); void(0);">
  <img onclick="switchLight();">
*/
function switchLight(){
  var img = document.getElementById("button-agenda");
  var curtain = document.getElementById("curtain");
  //create semi-transparent curtain if neccessary
  if (!curtain){
    curtain = document.createElement("div");
    curtain.id="curtain";
    curtain.style.display="none";
    curtain.style.position = "absolute";
    curtain.style.left = "0px";
    curtain.style.top = "0px";
    curtain.style.width = "100%";
    curtain.style.filter = "alpha(opacity=50)";
    curtain.style.backgroundColor = "rgb(0,0,0)";
    curtain.style.zIndex = "2";
    document.body.appendChild(curtain);
    docHeight = (navigator.appName == "Netscape") ? document.body.height : document.body.clientHeight + document.body.scrollTop;
  }
	if (lightIsOn){
	  setStyle("header", "backgroundImage", "none")
	  setStyle("header", "puree", "none")
	  setStyle("project", "backgroundColor", "rgb(70%, 80%, 100%)")
	  setStyle("project", "color", "rgb(16%, 20%, 50%)")
	  setStyle("news", "backgroundImage", "none")
    if (img){
      img.src="artikelen/images/button-agenda-home-pushed-image.gif";
      img.initialized = false;
      imgHover(img,'pushed-image=pushed-text','area-agenda');
    }
    //display curtain, only if the filter property is supported by the browser
    if (isIE){
      curtain.style.height = docHeight;
      curtain.style.display="block";
    }
  }
  else{
	  setStyle("header", "backgroundImage", "url('general/header/images/verloop-top.gif')")
	  setStyle("project", "backgroundColor", "rgb(100%, 100%, 100%)")
	  setStyle("project", "color", "rgb(0%, 0%, 0%)")
	  setStyle("news", "backgroundImage", "url('general/submain/images/verloop-bottom.gif')")
    if (img){
      img.src="artikelen/images/button-agenda-home-normal-image.gif";
      img.initialized = false;
      imgHover(img,'normal-image=normal-text','area-agenda');
    }
	  setStyle("curtain", "display", "none")
  }
  lightIsOn = !lightIsOn;
}

addScrollFunction("if (!lightIsOn){fitToPageHeight('curtain')};");

//----------------------------------------------------------------------

/*Prepare image for clipping effect
  Sample use:
  <img onload="clipNmove('myId','DadsId')">
  imgId: id of the image
  parentId: id of the parent object (container)
*/
function clipNmove(imgId, parentId){
  var img = document.getElementById(imgId);
  var par = document.getElementById(parentId);

  if (isNav){return;}
  //set parameters
  img.leftPosition = 0;
  img.topPosition = 0;
  img.horizontalDirection = -1;
  img.verticalDirection = -1;
  img.canvasWidth = img.width - parseInt(par.style.width);
  img.canvasHeight = img.height - parseInt(par.style.height);
  img.style.position = "absolute"; 
  //Schedule transition
  setInterval("clipIsMoving = !clipIsMoving;", 5000);
  var code = "clipNmoveSlide('" + imgId  + "')";
  var interval = (isNav)?80:20;
  setInterval(code, interval);
}

function clipNmoveSlide(imgId){
  if (!clipIsMoving){return;}
  img = document.getElementById(imgId);
  //verify extreme positions
  if ((img.horizontalDirection == -1) && (-img.leftPosition > img.canvasWidth)){img.horizontalDirection = 1;}
  else{
    if ((img.horizontalDirection == 1) && (img.leftPosition > 0)){img.horizontalDirection = -1;}
  }
  if ((img.verticalDirection == -1) && (-img.topPosition > img.canvasHeight)){img.verticalDirection = 1;}  else{
    if ((img.verticalDirection == 1) && (img.topPosition > 0)){img.verticalDirection = -1;}
  }
  //calculate en store new position and move image
  img.leftPosition += (img.horizontalDirection * 4);
  img.style.left = img.leftPosition + "px";
  img.topPosition += (img.verticalDirection * 2);
  img.style.top = img.topPosition + "px";
}

  
