/*
Copyright (c) 2007-2008 Ylab, http://www.ylab.nl
*/
var current_page;
var current_code;
var dir_open = 1
var dir_close = -1;

function replace_headings(){
  //repace h1 text by a (background) image
  var span, items = document.getElementsByTagName('h1');
  for(var i=0, item; item=items[i]; i++){
    if(!!item.id && !item.style.backgroundImage){
	    item.innerHTML = '<span class="hidden">' + item.innerHTML + '</span>';
	    item.style.backgroundImage = 'url(images/caption-' + item.id +'.png)';
	  }
  }
}

function animate_images(){
  //repace h1 text by a (background) image
  var items = document.images;
  for(var i=0, item; item=items[i]; i++){
    if(!!item.className && item.className == 'hover'){
      imgHover(item);
	  }
  }
}

function replace_illustration(){
	$('illustration').src = 'images/illustration-' + current_code + '.jpg';
}

function replace_emailaddresses(){
	var parts, s_addr, s_repl = ' [at] ';	// put the @-replacement here
	var items = document.getElementsByClassName('email');
  for(var i=0, item; item=items[i]; i++){
    parts = item.innerHTML.split(s_repl);
		if (parts.length == 2) {
		  s_addr = (parts[0] + '@' + parts[1]);
		  item.innerHTML = '<a href="mailto:' + s_addr + '">' + s_addr + '</a>';
		}
	}
}

function resize_content(){
  //adjust height of 'page' element to the full height of the viewport
	var vp = new Viewport();
	var el = $('page');
	el.style.height = vp.y + 'px';
  el.style.overflow = 'scroll';
  el.style.overflowY = 'scroll';
	el.style.overflowX = 'hidden';

}

function init_page(){
  var items = $('mainmenu').getElementsByTagName('a');

  current_page = location.pathname;
  if(current_page.substring(0,1)=='/'){
    current_page = current_page.substring(1);
  }

  for(var i=0, item; item=items[i]; i++){
    item.onclick = show;
  }
  updatePage();
  resize_content();
  $('illustration').onerror = function(){
    this.src='images/illustration-404.jpg';
  };
}

function show(){
  //replace the content
  //this: a href
  var new_page = this.pathname, url_content, url_parts;
  if(new_page.substring(0,1)=='/'){
    new_page = new_page.substring(1);
  }
  if(current_page == new_page){
    return false;
  }
	current_page = new_page;
	url_parts = new_page.split('/');
	url_parts[url_parts.length-1] = 'data/' + url_parts[url_parts.length-1] + '.php';
	url_content = url_parts.join('/');
	for(i=1; i<url_parts.length; i++){
		url_content = '../' + url_content;
	}
	initSlideMovement(dir_close, false);

	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = updateContent;
	xmlHttp.open('GET', url_content, true);
	xmlHttp.send(null);
  return false;
}

function updateContent(){
  var response, obj;

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete'){
		response = xmlHttp.responseText;
		initSlideMovement(dir_close, response);
	}
}

function initSlideMovement(direction, response){
		var obj = $('content');
    //stop running animation
  	window.clearTimeout(obj.timeout);
		obj.direction = direction;
		slide(obj, response);
}

function slide(obj, response){
  var repeat, code;
  var ms_delay = 10; //delay in miliseconds;
  var i_steps  = obj.direction == dir_open ? 100 : 10;
  //calculate new height
	var newY = obj.offsetHeight + (obj.direction * obj.scrollHeight / i_steps);
	//verify min and max value
	newY = Math.min(newY, obj.scrollHeight);
	newY = Math.max(newY, 0);
	obj.style.overflow = 'hidden';//needed for IE to update
	obj.style.height = (newY) + 'px';
	//repeat if needed
	repeat = obj.direction == dir_open ? obj.offsetHeight < obj.scrollHeight : obj.offsetHeight > 0;
	if(repeat){
		code = function(){slide(obj, response);};
		obj.timeout = window.setTimeout(code, ms_delay);
	}
	else{
	  if(response){
	    //replace content
  		obj.innerHTML = response;
  		updatePage();
  		replace_illustration();
  		window.setTimeout('replace_headings();', 100);
  		//set direction
  		obj.direction = dir_open;
  		slide(obj, false);
	  }
	}
}

function expandInfo(sId, sMore, sLess){
	var divMore, divMoreDir = 1;
	var divMoreInfo=$(sId);
	if(divMoreInfo){
		divMore = document.createElement('div');
		divMore.style.cursor= 'pointer';
		divMore.className = 'morehandler';
		divMore.change = function(){
			this.innerHTML = '<a>' + (divMoreDir == -1 ? sLess : sMore) + '</a> &' + (divMoreDir == -1 ? 'l' : 'r') + 'aquo;';
		};
		divMore.onclick = function(){
			$('content').style.overflow = 'visible';
			divMoreInfo.direction = divMoreDir;
			slide(divMoreInfo, false);
			divMoreDir *= -1;
			this.change();
		};
		divMore.change();
		divMoreInfo.style.overflow = 'hidden';
		divMoreInfo.style.height = 0;
		divMoreInfo.parentNode.insertBefore(divMore, divMoreInfo);
	}
}

var currTrailer;
var timeTrailer;
var photos = ['dxf2008-brendan-dawes.jpg',
              'dxf2008-gerthans-berghuis.jpg',
              'dxf2008-joe-fletcher.jpg',
              'dxf2008-jonathan-arnowitz.jpg',
              'dxf2008-thomas-kueber.jpg',
              'dxf2008-marthijn-pool.jpg',
              'dxf2008-thomas-castro.jpg'
              ];
var captions = ['Brendan Dawes',
                'Gerthans Berghuis',
                'Joe Fletcher',
                'Jonathan Arnowitz',
                'Thomas Kueber and Julian Masuhr',
                'Marthijn Pool',
                'Thomas Castro'
                ];

function trailer(){
  var i, btns =[], divTrailer = $('trailer'), imgTrailer = $('imgTrailer');
  imgTrailer.onmousemove = mouseOnTrailer;
  imgTrailer.onmouseout = autoTrailer;
	//divTrailer.style.position = 'relative';
  for(i=0; i<captions.length; i++){
    if(imgTrailer.alt == captions[i]){
      currTrailer = i;
    }
		new Image().src = 'images/' + photos[i];
  }
  autoTrailer();
}

function nextTrailer(n){
  window.clearTimeout(timeTrailer);
  var imgTrailer = $('imgTrailer');
  if(imgTrailer == undefined){
  	return;
  }
  var i = (n == undefined) ? currTrailer+1 : n;
  if(i != currTrailer){
    if(i == photos.length){
      i=0;
    }
    imgTrailer.alt = captions[i]
    imgTrailer.src = 'images/' + photos[i];
    currTrailer = i;
  }
}

function autoTrailer(b){
  window.clearTimeout(timeTrailer);
  if(b===true){nextTrailer();}
  timeTrailer = window.setTimeout('autoTrailer(true);', 2000);
}

function mouseOnTrailer(e) {
  var c = getCoordinates(e, this);
  if(c.y < 100){
    autoTrailer();
    return;
  }
  var n = parseInt((c.x + 30)/63);
  n = Math.min(n,6);
  n = Math.max(n,0);
  nextTrailer(n);
}

function updatePage(){
  window.clearTimeout(timeTrailer);
	url_parts = current_page.split('/');
	current_code = url_parts[url_parts.length-1];
  replace_headings();
  replace_emailaddresses();
  animate_images();
  switch(current_code){
  	case '':
  	case 'index':
  	  trailer();
  		expandInfo('more-yled', 'show participating organizations', 'hide participating organizations');
  		expandInfo('more-praise', 'show more', 'show less');
  		break;
  	case 'sessions':
			expandInfo('more-brendan', 'About Brendan', 'Hide r&eacute;sum&eacute;');
			expandInfo('more-august', 'About August', 'Hide r&eacute;sum&eacute;');
			expandInfo('more-joe', 'About Joe', 'Hide r&eacute;sum&eacute;');
			expandInfo('more-gerthans', 'About Gert Hans', 'Hide r&eacute;sum&eacute;');
			expandInfo('more-dlabs', 'About Thomas and Julian', 'Hide r&eacute;sum&eacute;');
			expandInfo('more-marthijn', 'About Marthijn', 'Hide r&eacute;sum&eacute;');
			expandInfo('more-jonathan', 'About Jonathan', 'Hide r&eacute;sum&eacute;');
			expandInfo('more-thomas', 'About Thomas', 'Hide r&eacute;sum&eacute;');
  		break;
  }
}

function getCoordinates(e, el) {
	var posx = 0, posy = 0;
	if (!e){
	  e = window.event;
	}
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
  el = new Dimension(el);
  return {x:posx-el.x, y:posy-el.y};
}

function Dimension(element){
  this.x=-1;
  this.y=-1;
  this.w=0;
  this.h=0;
  if (element==document){
    this.x=element.body.scrollLeft;
    this.y=element.body.scrollTop;
    this.w=element.body.clientWidth;
    this.h=element.body.clientHeight;
  }else if (element!=null){
    var e=element;
    var left=e.offsetLeft;
    while ((e=e.offsetParent)!=null) {
      left+=e.offsetLeft;
    }
    var e=element;
    var top=e.offsetTop;
    while((e=e.offsetParent)!=null) {
      top+=e.offsetTop;
    }
    this.x=left;
    this.y=top;
    this.w=element.offsetWidth;
    this.h=element.offsetHeight;
  }
}
