var waitTime = 30000;
var stepDelay = 100;
var player = null;
var playing = 0;
var volume = 50;
var currentProject = "";
var currentProjectDiv = null;
var currentPhotoNode = null;
var debug = false;

function XBrowserAddHandler(target,eventName,handlerName) {
    if ( target.addEventListener )
      target.addEventListener(eventName, handlerName, false);
    else if ( target.attachEvent )
      target.attachEvent("on" + eventName, handlerName);
    else
      target["on" + eventName] = handlerName;
}

XBrowserAddHandler(window, "load", onWindowLoad);

function onWindowLoad() {
  contents = document.getElementById("text");
  timer    = document.getElementById("timer").getElementsByTagName("DIV")[0];
  if (debug) doPage(6, "&");
  var flashvars = {file: "music/Meatloaf - Paradise By The Dashboard Lights.mp3", as: playing};
  var params = {allowScriptAccess: "always"};
  var attributes = {};
  params.wmode = "transparent";
  swfobject.embedSWF("/niftyplayer.swf", "audioplayer", "1", "1", "8.0.0", "", flashvars, params, attributes);
  
  var e = document.getElementsByTagName("*");
  for (var i=0; i < e.length; i++) {
    if (e[i].tagName.toUpperCase() != "IMG") {
      disableSelection(e[i]); //Disable text selection on entire body
    }
  }
  document.getElementById("i3").oncontextmenu = function() { return false; }
  
  var pages = document.getElementById("logo").getElementsByTagName("DIV");
  for(var i=0; i < pages.length; i++) {
    var n = pages[i];
    pages[i].onmouseover = function() { onmouseovermenu(this); };
    pages[i].onmouseout  = function() { onmouseoutmenu(this); };
  }
  
  var songs = "";
  for (var i=0; i < playlist.length; i++) {
    songs += "<span onclick=\"loadurl("+i+");\">" + (i+1) + "</span>";
  }
  document.getElementById("songs").innerHTML = songs;
  loadurl(0);
}

function onmouseovermenu(node) {
  node.setAttribute("oldbackgroundImage", node.style.backgroundImage);
  node.style.backgroundImage = "url(\"/img/logo1.png\")";
}

function onmouseoutmenu(node) {
  node.style.backgroundImage = node.getAttribute("oldbackgroundImage");
}

function playerLoaded() {
  player = niftyplayer("audioplayer");
  player.obj.setVolume(volume);
}

var contents, icon, timer, timer_step, timer_count, timerid, currentpage = 0;

document.onmousedown = resetTimer;

function doPage(i, name, subpage) {
  if (currentpage != 0) {
    document.getElementById("c"+currentpage).style.backgroundImage = "";
    document.getElementById("i"+currentpage).style.display = "none";
  }
  timer.parentNode.style.display = "none";
  currentpage = i;
  
  var page = document.getElementById("c"+i);
  var icon = document.getElementById("i"+i);
  if (page) {
    page.setAttribute("oldbackgroundImage", "url(\"/img/logo1.png\")");
    if (i != 0) resetTimer();
    contents.innerHTML = doPost("/", {page: encodeURIComponent(name), subpage: encodeURIComponent(subpage), project: currentProject}).contents;
    icon.style.display = "none";
    icon.style.display = "block";
    var p = document.getElementsByName("project");
    for (var i2=0; i2 < p.length; i2++) {
      if (p[i2].getAttribute("class") == "selected") currentProjectDiv = p[i2];
    }
  } else {
    contents.innerHTML = "";
  }
  
  document.getElementById("selectsong").style.display = (i == 2 ? "block" : "none");
}

function resetTimer() {
  if (currentpage == 0) return;
  if (debug) return;
  timer.parentNode.style.display = "block";
  timer_count = timer_step = Math.floor(waitTime / stepDelay);
  clearInterval(timerid);
  timerid = setInterval("doTimer()", stepDelay);
}

function doTimer() {
  timer.style.width = (100 / timer_step * timer_count--) + "%";
  if (timer_count == 0) {
    clearInterval(timerid);
    doPage(0, "Start");
  }
}

function doPost(url, data) {
  var objXML = (typeof XMLHttpRequest != "undefined") ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
  objXML.open("POST", url, false);
  objXML.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF8");
  var postdata = "";
  for (property in data) {
    if (postdata != "") postdata += "&";
    postdata += property + "=" + encodeURIComponent(data[property]+"").replace(/&/ig, "%26");
  }
  objXML.send(postdata);
  return {status: objXML.status, contents: objXML.responseText};
}

function icon1() {
  doPage(1, "Wonen", "Wonen & Zo 2");
}

function icon2() {
  playing ? player.stop() : player.play();
  playing = !playing;
}

function loadurl(i) {
  if (!player) {
    setTimeout("loadurl(" + i + ")", 100);
    return;
  }
  if (playing) player.stop();
  player.load("/music/" + encodeURIComponent(playlist[i]));
  var s = document.getElementById("songs").getElementsByTagName("SPAN");
  if (s.length > 0) {
    for (var j=0; j < s.length; j++) {
      s[j].className = "";
    }
    s[i].className = "active";
  }
  if (playing) player.play();
}

function icon3(e) {
  var keycode;
  
  if (e && e.stopPropagation) {
    e.stopPropagation();
  }
  if (window.event && window.event.cancelbubble ) {
    window.event.cancelbubble = true;
  }
  if (e && e.preventDefault) {
    e.preventDefault();
  }
  if (window.event) {
    window.event.returnValue = false;
  }

	if (!e) var e = window.event;
  if (e.ctrlKey)
    volume -= 10;
  else
    volume += 10;
  player.obj.setVolume(volume);
  
}

function icon4() {
  doPage(4, "Erwin Stoepker", "Erwin Stoepker 2");
}

function icon5() {
  doPage(5, "Naast ons ontwerpcentrum", "Naast ons ontwerpcentrum 2");
}

function icon6() {
}

function icon7() {
  doPage(7, "Zomaar een greep", "Zomaar een greep 2");
}

function icon8() {
  doPage(8, "Onze visie", "Onze visie 2");
}

function icon9() {
  doPage(0, "Start");
}

function doProjectsScroll(i) {
  var p = document.getElementById("projects").getElementsByTagName("DIV")[0];
  if (parseInt(p.style.top) + i <= 0) {
    p.style.top = parseInt(p.style.top) + i + "px";
  }
}

function doProject(title, node) {
  if (currentProjectDiv != null) currentProjectDiv.className = "";
  var x = doPost("/", {page: "projecten", project: title});
  document.getElementById("project").innerHTML = x.contents;
  currentProject = title;
  currentProjectDiv = node;
  currentProjectDiv.className = "selected";
}

function doProjectPhotos(id) {
  contents.innerHTML = doPost("/", {page: "fotos", project: id}).contents;
}

function doProjectPhoto(id, photo, node) {
  if (currentPhotoNode != null) currentPhotoNode.className = "";
  document.getElementById("photo").innerHTML = doPost("/", {page: "foto", project: id, photo: photo}).contents;
  currentPhotoNode = node;
  currentPhotoNode.className = "selected";
}

function doPhotosScroll(i) {
  var p = document.getElementById("photos").getElementsByTagName("DIV")[0];
  if ( (i > 0 && parseInt(p.style.left) + i <= 0) || (i < 0 && p.offsetWidth+parseInt(p.style.left) > p.parentNode.offsetWidth) ) {
    p.style.left = parseInt(p.style.left) + i + "px";
  }
}

function disableSelection(target) {
  if (typeof target.onselectstart!="undefined") //IE route
    target.onselectstart=function(){return false}
  else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
    target.style.MozUserSelect="none"
  else //All other route (ie: Opera)
    target.onmousedown=function(){return false}
  target.style.cursor = "default"
}

function enlarge(a, s, fbid) {
  document.getElementById("prev").style.cursor = 'pointer';
  document.getElementById("next").style.cursor = 'pointer';
  var oc = document.getElementById("overlay_content");
  var i = document.getElementById("image");
  document.body.style.cursor = "wait";
  i.onload = function () {
    oc.style.marginLeft = -this.width / 2 + 'px';
    oc.style.top = (getPageSize()[3] - this.height) / 2 + getPageScroll()[1] + 'px';
    document.getElementById("prev").style.marginTop = ((this.height/2) - 13) + 'px'
    document.getElementById("next").style.marginTop = ((this.height/2) - 13) + 'px'
    var o = document.getElementById("overlay");
    o.style.display = "block";
    window.onresize = onresize;
    document.body.style.cursor = "";
  }
  i.setAttribute("overlay", oc);
  i.src = '/loading.gif';
  i.src = '/thumbs.php?w=650&h=650&s=' + s;
/*  
  var p = document.getElementById("photobook").getElementsByTagName("IMG");
  for (var j=0; j < p.length; j++) {
    if (p[j].src == i.src) {
      document.getElementById("prev").style.visibility =  (j > 1) ? '' : 'hidden';
      document.getElementById("next").style.visibility =  (j < p.length) ? '' : 'hidden';
    }
  }
*/
}

function onresize() {
}

function hideoverlay() {
  document.getElementById("overlay").style.display = "";
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function cancelevent(e) {
  if (e && e.stopPropagation) {
    e.stopPropagation();
  }
  if (window.event && window.event.cancelbubble ) {
    window.event.cancelbubble = true;
  }
  if (e && e.preventDefault) {
    e.preventDefault();
  }
  if (window.event) {
    window.event.returnValue = false;
  }
}

function doPrev() {
  var s = document.getElementById("image");
  var p = document.getElementById("photobook").getElementsByTagName("IMG");
  for (var i=0; i < p.length; i++) {
    if (p[i].src == s.src) {
      if (i == 0) i = p.length;
      s.src = p[i-1].src;
      //document.getElementById("prev").style.visibility =  (i > 1) ? '' : 'hidden';
      //document.getElementById("next").style.visibility =  '';
      return;
    }
  }
}

function doNext() {
  var s = document.getElementById("image");
  var p = document.getElementById("photobook").getElementsByTagName("IMG");
  for (var i=0; i < p.length; i++) {
    if (p[i].src == s.src) {
      if (i == p.length-1) i = -1;
      s.src = p[i+1].src;
      //document.getElementById("prev").style.visibility =  '';
      //document.getElementById("next").style.visibility =  (i < p.length-2) ? '' : 'hidden';
      return;
    }
  }
}
