/** Intermate Uber-Nifty Layout Script door Tommy van der Vorst, 2007

Alle functies worden in het Intermate-object gestopt, zodat de namen nooit clashen met whatever
script van Drupal. Je weet maar nooit... en wel zo overzichtelijk. 

Blokken verbergen/laten zien: werkt via toggleBlock functie. In _blockVisibility zit een array met true/false per blok
of het visible is of niet. Iedere keer als een blok veranderd wordt, komt daar een waarde bij. saveBlockvisibility slaat dat 
vervolgens netjes op. In block.tpl.php wordt het cookie ook uitgelezen en worden de blokken die collapsed zijn vast ingeklapt.
In normale browsers (die opacity of -moz-opacity snappen, dus FF2, FF3, Safari 3, Opera) wordt opacity gebruikt om te faden. In IE6, IE7
wordt een filter gebruikt om uit te faden. In alle gevallen wordt er niet gefade als het niet ondersteund wordt.

En ook dit script is een resultaat van SOG!
**/

var Intermate = new Object();
Intermate._blockVisibility = new Array();
Intermate.TimerInterval = 83; // 1/(Aantal FPS) voor de bewegende bubbeltjesdinges
Intermate.ParticlesHeight = 200;
Intermate.ParticlesWidth = 800;

/** Cookie functies **/
Intermate.createCookie = function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

Intermate.readCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

Intermate.eraseCookie = function(name) {
	Intermate.createCookie(name,"",-1);
}

Intermate.particlesTick = function() {
  for(var a=0;a<Intermate._particles.length;a++) {
    var p = Intermate._particles[a];

    var x = parseFloat(p.style.left);
    var y = parseFloat(p.style.top);

    var dir = p.getAttribute('dir');
    var speed = p.getAttribute('speed');
    var nx = x+(speed*Math.cos(dir));
    var ny  = y+(speed*Math.sin(dir));
    //var ny = y;

    if(nx>810) nx = 0;
    else if(ny>200) ny = 0;
    else if(nx<0) nx = Intermate.ParticlesWidth+10;
    else if(ny<0) ny = Intermate.ParticlesHeight;

    p.style.left = nx+"px";
    p.style.top = ny+"px";
  }
  setTimeout("Intermate.particlesTick()", Intermate.TimerInterval);
}

Intermate.getAnimationsEnabled = function() {
  var enable = Intermate.readCookie("intermate-animations");
  var checkbox = document.getElementById('animations');
  if(enable=="no" ) {
    checkbox.checked = false;
    return false;
  }
  checkbox.checked = true;
  return true;
}

Intermate.setAnimationsEnabled = function(b) {
  var checkbox = document.getElementById('animations');
  checkbox.checked = b;
  Intermate.createCookie("intermate-animations", b?"yes":"no", 365);
}

Intermate.toggleAnimations = function() {
  Intermate.setAnimationsEnabled(!Intermate.getAnimationsEnabled());
  document.location.reload();
}

Intermate.initializeParticles = function(path) {
  if(Intermate.getAnimationsEnabled()) {
    var particles = document.getElementById('particles');

    Intermate._particles = new Array();
    for(var a=0;a<10;a++) {
      var p = document.createElement("img");
      p.src = path;
      p.style.left = (Math.random()*Intermate.ParticlesWidth)+"px";
      p.style.top = (Math.random()*Intermate.ParticlesHeight)+"px";
      var size = Math.random()*150;
      p.style.width = size+"px";
      p.style.height = size+"px";
      p.setAttribute('dir', Math.random()*6.282);
      p.setAttribute('speed',Math.random()*2);
      
      particles.appendChild(p);
      Intermate._particles[a] = p;
    }
    
    Intermate.particlesTick();
  }
}

/* Slaat de informatie over zichtbaarheid van blokken nog eens op */
Intermate.saveBlockVisibility = function() {
  for(var i in Intermate._blockVisibility) {
    Intermate.createCookie("block-"+i, Intermate._blockVisibility[i]?"yes":"no", 365);
  }
}

/* Fading */
Intermate.fadeOut = function(el, step) {
  if(!Intermate.getAnimationsEnabled()) {
    el.style.display = 'none';
  }
  else {
    try {
      if(el.style.opacity>0) {
        var o = Math.max(0.0,parseFloat(el.style.opacity)-parseFloat(step));
        el.style.opacity = o;
        el.style.mozOpacity = o;
        var nextFade = function() {
          Intermate.fadeOut(el,step);
        }
        el.timer = setTimeout(nextFade, 20);
      }
      else {
        el.style.display = 'none';
      }
    }
    catch(e) {
      el.style.display = 'none';
    }
  }
}

Intermate.fadeIn = function(el, step) {
  if(!Intermate.getAnimationsEnabled()) {
    el.style.display = 'block';
    el.style.opacity = 1.0;
  }
  else {
    try {
      if(el.style.opacity<1) {
        var o = Math.min(1.0,parseFloat(el.style.opacity)+parseFloat(step));
        el.style.opacity = o;
        el.style.mozOpacity = o;
        var nextFade = function() {
          Intermate.fadeIn(el, step);
        }
        el.timer = setTimeout(nextFade, 20);
      }
      else {
        el.style.position = '';
      }
    }
    catch(e) {
    }
  }
}

Intermate.toggleBlock = function(title, blockid) {
  var opacitySupported = (navigator.appName!="Microsoft Internet Explorer");
  var block = document.getElementById('block-'+blockid);
  
  if(block) {
     // Als er al gefade wordt, dat even cancellen
    if(block.timer) {
      clearTimeout(block.timer);
      block.timer = null;
    }
  
    if(block.style.display=='none') {
      block.style.display = 'block';
      if(opacitySupported) {
        block.style.position = '';
        block.style.opacity = 0.0;
        Intermate.fadeIn(block, 0.04);
      }
      Intermate._blockVisibility[blockid] = true;
    }
    else {
      if(opacitySupported) {
        block.style.zIndex = 100;
        block.style.opacity = 1.0;
        block.style.visibility = 'visible';
        block.style.position = 'absolute';
        Intermate.fadeOut(block, 0.03);
      }
      else {
        block.style.display = 'none';
      }
      Intermate._blockVisibility[blockid] = false;
    }
  }
  
  Intermate.saveBlockVisibility();
}
