var NLTree = CatTree[0];
var ENTree = CatTree[1];
var RootNode;

var reInit;
var reInitSearch;

function reposition() {
	var main = document.getElementById("_main");
	main.style.left = Math.max(50, Math.floor(document.body.clientWidth / 2)-500);
}

function checkVersion(treeitemID) {
  var hasRequestedVersion = DetectFlashVer(9, 0, 28);
  if (hasRequestedVersion) {
    if (document.location.hash != "") {
      var theTagParts = document.location.hash.split("=");
      var theTagID = (theTagParts[0].toLowerCase() == "#tag") ? 1 : 2;

      document.location.href = "/index.php#&827f-nodeid=0&827f-"+escape(theTagParts[1])+"="+theTagID;
    } else {
      document.location.href = "/index.php#&827f-nodeid="+treeitemID;
    }
  }
}

function OpenPageByID_new(ID, params, tags) {
	if (params) {
		var newwin = window.open (FlatTree[ID].filename, "popup", params);
		newwin.focus();
	} else {
		var url = FlatTree[ID].filename;
		url = url.split("#");
		if ((url.length>4) && (url.substr(0,4) != 'http')) {
			url[0] = escape(url[0]);
		}
		url = url.join("#");
		if (tags == true) url += document.location.hash;
		document.location = url;
	}
	if (reInit != null) reInit();
}
if (!this.OpenPageByID) OpenPageByID = OpenPageByID_new;

function OpenPageByNode(aNode) {
	OpenPageByID(aNode.ID);
}

function getRootNode() {
	var theCurrentNode = thischild;
	while (theCurrentNode.parentRef != null) {
		theCurrentNode = theCurrentNode.parentRef; 
	}
	RootNode = theCurrentNode;
}

function doSearch() {
	var theSearchQuery = document.getElementById("_searchField").value;
	document.location.href=RootNode.childs[6].filename+"#q="+escape(theSearchQuery);
	if (reInitSearch != null) reInitSearch();
}

function keyEvent(e) {
	if (e.keyCode == 13) {
		doSearch()
	}
}

var rollOverPath = "";
function menuMouseOver(menupath, ID) {
  document.getElementById("menupath").style.display='none';
	rollOverPath = menupath;
	
	if (document.getElementById("item"+menupath).className.indexOf("_path") == -1)
		document.getElementById("item"+menupath).className = document.getElementById("item"+menupath).className+"_path";
	if (document.getElementById("menu"+menupath) != null) {
		document.getElementById("menu"+menupath).style.display = "block";
		var thePos = document.getElementById("menu"+menupath).style.left;
		if (thePos.indexOf("px") != -1) thePos = thePos.substr(0,thePos.length-2);
		if (thePos*1 < -1000) {
			document.getElementById("menu"+menupath).style.left = "170px";
		}
	}
}
function menuMouseOut(menupath, ID, classname) {
  document.getElementById("menupath").style.display='block';
	setTimeout('menuMouseOutReal("'+menupath+'", '+ID+', "'+classname+'");', 0);
}
function menuMouseOutReal(menupath, ID) {
	if ((rollOverPath.indexOf(menupath) != -1) && (rollOverPath != menupath)) return;
	if (rollOverPath == menupath) rollOverPath = "";

	if (document.getElementById("item"+menupath).className.indexOf("_path") != -1)
		document.getElementById("item"+menupath).className = document.getElementById("item"+menupath).className.split("_path").join("");
	if (document.getElementById("menu"+menupath) != null)
		document.getElementById("menu"+menupath).style.display = "none";
	
	var trimmedPath = menupath.split("_");
	trimmedPath.splice(trimmedPath.length - 1,1);
	
	var newMenuPath = trimmedPath.join("_");
	if ((newMenuPath != "_") && (newMenuPath != rollOverPath)) {
		menuMouseOutReal(newMenuPath, ID);
	}
}

function menuClick(menupath, ID) {
	OpenPageByID(ID);
}

function genMenu(menupath, rootNode) {
  if (menupath == null) {
    var theArray = new Array();
    var section = thischild;

    if (document.location.hash.substr(0,4) == "#tag") {
      var tag = document.location.hash;
      tag = tag.split("=")[1];
      tag = tag.split("%20").join(" ");

      theArray = genTagNodes(tag, RootNode.childs[2].childs[1]);
      section = theArray[0];
    }
    
    if (document.location.hash.substr(0,10) == "#specialty") {
      var tag = document.location.hash;
      tag = tag.split("=")[1];
      tag = tag.split("%20").join(" ");

      theArray = genTagNodes(tag, RootNode.childs[2].childs[2]);
      section = theArray[0];
    }


    theArray.unshift(section);
    while (section != RootNode) {
      theArray.unshift(section);
      section = section.parentRef;
    }

    document.write('<div id="menupath">');

    var ypos = 0;
    for (var i = 0; i < RootNode.childs.length; i++) {
      if (theArray[0] == RootNode.childs[i]) ypos = i;
    }

    if ((theArray[0] != RootNode) && (ypos < 3)) {
    
      document.write('<div class="item_path" style="left:0px;top:'+(ypos*24)+'px;">&nbsp;</div>');

      if (theArray[1] != null)
      if (theArray[1] != theArray[0])
      if ((theArray[0] == RootNode.childs[2])) {
        ypos = 0;
        for (var i = 0; i < RootNode.childs[2].childs.length; i++) {
          if (theArray[1] == RootNode.childs[2].childs[i]) ypos = i;
        }
        document.write('<div class="item_path" style="left:170px;top:'+(ypos*24)+'px;">&nbsp;</div>');
        for (var i = 0; i < RootNode.childs[2].childs.length; i++) {
          document.write('<div class="itemmed" style="'+((RootNode.childs[2].childs[i] == theArray[1]) ? "" : "color:#888888")+';left:170px;top:'+(i*24)+'px;" onmouseover="menuMouseOver('+"'"+'__2'+"'"+', '+RootNode.childs[2].childs[i].ID+');">'+RootNode.childs[2].childs[i].label+'</div>');
        }
        if (theArray[2] != null) {
          document.write('<div class="item_path_end" style="left:340px;top:'+(ypos*24)+'px;" onmouseover="menuMouseOver('+"'"+'__2_'+ypos+"'"+', '+theArray[2].ID+');menuMouseOver('+"'"+'__2'+"'"+', '+theArray[2].ID+');">'+theArray[2].label+'</div>');
        }
      } else {
        if (theArray[1] != null) {
          document.write('<div class="item_path_end" style="left:170px;top:'+(ypos*24)+'px;" onmouseover="menuMouseOver('+"'"+'__'+ypos+"'"+', '+theArray[1].ID+');">'+theArray[1].label+'</div>');
        }
      }
      
    }
    document.write('</div>');
  }
  
	var spacing = 18;
	var itemclass = "subitem";
	if (menupath == null) menupath = "__";
	if ((menupath == "__") || (menupath == "__2_")) {
		spacing = 24;
		if (menupath == "__") {
      itemclass = "item";
    } else {
      itemclass = "itemmed";
    }
	}
	if (rootNode == null) rootNode = RootNode;
	for (var i = 0; i < ((menupath == "__") ? 3 : rootNode.childs.length); i++) {
		document.write('<div class="'+itemclass+'" style="top:'+(i*spacing)+'px;" id="item'+menupath+i+'" onMouseOver="menuMouseOver('+"'"+menupath+i+"'"+', '+rootNode.childs[i].ID+');" onMouseOut="menuMouseOut('+"'"+menupath+i+"'"+', '+rootNode.childs[i].ID+');" onClick="menuClick('+"'"+menupath+i+"'"+', '+rootNode.childs[i].ID+');">'+rootNode.childs[i].label+'</div>');
		if (itemclass != "subitem"){
			document.write('<div class="'+(((menupath == "__") && (i==2)) ? "endmenu" : "submenu")+'" style="left:-3000px; top:'+(((menupath == "__") && (i==2)) ? 0 : (i*spacing+2))+'px; width:171px; height:'+(rootNode.childs[i].childs.length*18+2)+'px" id="menu'+menupath+i+'">');
			genMenu(menupath+i+"_", rootNode.childs[i]);
			document.write('</div>');
		}
	}
}

function writeStyles()
{
	var gray = "#d9dade";
	var bggray = "#6c6d6f";
	var colors = ["#6699cc","#ce7831","#d5ac00","#599ab7","#9e9400","#009ddc","#bd0673"];
	var bgcolors = ["#334c66","#673c18","#6a5600","#2c4d5b","#4f4a00","#004e6e","#5e0339"];
	var textColor = "#ffffff";
	var homeColor = "#000000";
	
	var theColor = Math.floor(Math.random()*colors.length);
	var backColor = colors[theColor]; 
	var bgColor = bgcolors[theColor];
	
	if ((RootNode == thischild) || (thischild.parentRef == RootNode)) {
	} else {
		backColor = gray;
		textColor = "#444444";
		homeColor = "#444444";
		bgColor = bggray;
	}
	document.write("<style>");
	document.write("body { background:"+backColor+"; }");
	document.write(".homeblock { color:"+backColor+"; }");
	document.write(".dividerblock { background:"+backColor+"; }");
	document.write(".submenu { background:"+backColor+"; }");
	document.write(".home { color:"+homeColor+"; }");
	document.write(".item { color:"+textColor+"; }");
	document.write(".subitem { color:"+textColor+"; }");
	document.write(".itemmed { color:"+textColor+"; }");
	document.write(".item_path_end { color:"+textColor+"; }");
	document.write(".subitem_path { background:"+bgColor+"; }");
	document.write("</style>");
}


var tagList = new Array();
var newNodeID = 100000;
function sortNodes(a, b)
{
	return (a.label > b.label) ? 1 : -1;
}
function catalogue(startNodes, attachNode, property, baseurl)
{
	for (var i = 0; i < startNodes.length; i++) {
		if (startNodes[i].obj != null) {
			if (startNodes[i].obj[property] != null) {
				var tags = startNodes[i].obj[property].split(",");
				for (var j = 0; j < tags.length; j++) {
          if (baseurl == null) {
            if (tagList[tags[j]] == null) {
              attachNode.childs[attachNode.childs.length] = new TreeItem(newNodeID++, attachNode.ID, tags[j], startNodes[i].filename+"#tag="+tags[j], startNodes[i].filename+"#tag="+tags[j], attachNode, {ID:startNodes[i].ID});
              tagList[tags[j]] = attachNode.childs[attachNode.childs.length-1];
            } else {
              tagList[tags[j]].childs[tagList[tags[j]].childs.length] = new TreeItem(newNodeID++, tagList[tags[j]].ID, tags[j], startNodes[i].filename+"#tag="+tags[j], startNodes[i].filename+"#tag="+tags[j], tagList[tags[j]], {ID:startNodes[i].ID});
            }
          } else {
            if (tagList[tags[j]] == null) {
              attachNode.childs[attachNode.childs.length] = new TreeItem(newNodeID++, attachNode.ID, tags[j], baseurl+tags[j], baseurl+tags[j], attachNode, {ID:startNodes[i].ID});
              tagList[tags[j]] = attachNode.childs[attachNode.childs.length-1];
            } else {
              tagList[tags[j]].childs[tagList[tags[j]].childs.length] = new TreeItem(newNodeID++, tagList[tags[j]].ID, tags[j], baseurl+tags[j], baseurl+tags[j], tagList[tags[j]], {ID:startNodes[i].ID});
            }
          }
				}
			}
		}
		catalogue(startNodes[i].childs, attachNode, property, baseurl);	
	}
	attachNode.childs.sort(sortNodes);
}

function getTagNodes()
{
	RootNode.childs[2].childs[0].filename = RootNode.childs[2].filename;
	RootNode.childs[2].childs[0].filenameencoded = RootNode.childs[2].filenameencoded;

	var keywordNode = RootNode.childs[2].childs[1];
	RootNode.childs[2].childs[1] = new TreeItem(newNodeID++, keywordNode.parentID, keywordNode.label, RootNode.childs[2].filename, RootNode.childs[2].filenameencoded, RootNode.childs[2]);

	var specialtyNode = RootNode.childs[2].childs[2];
	RootNode.childs[2].childs[2] = new TreeItem(newNodeID++, specialtyNode.parentID, specialtyNode.label, RootNode.childs[2].filename, RootNode.childs[2].filenameencoded, RootNode.childs[2]);
	
	tagList = new Array();
	catalogue(RootNode.childs[2].childs[0].childs, RootNode.childs[2].childs[1], "tags");
	tagList = new Array();
	catalogue(RootNode.childs[2].childs[0].childs, RootNode.childs[2].childs[2], "specialty", specialtyNode.filename+"#specialty=");
}

function genSubAPages()
{
  document.write("<div class=\"subpages\">");
  var section = thischild;
  if (section.childs.length == 0) section = section.parentRef;
  
  isNews = false;
  if  (section.childs[0].label.length > 9) {
    var date = section.childs[0].label.substr(0,8) * 1;
    if (date.toString() == section.childs[0].label.substr(0,8)) {
      isNews = true;
    }
  }

  if (section != RootNode)
  for (var i = 0; i < section.childs.length; i++) {
    var node = section.childs[isNews ? section.childs.length-i-1 : i];
    var label = node.label.toUpperCase();
    if (isNews) {
      var date = node.label.substr(0,8);
      label = label.substr(9) + " ("+date.substr(6,2)+"-"+date.substr(4,2)+"-"+date.substr(0,4)+")";
    }
    document.write("<a href=\"javascript:void(0);\" onclick=\"OpenPageByID("+node.ID+");\">"+label+"</a><br/>");
  }
  
  document.write("</div>");
}

function genSubBPages()
{
  document.write("<div class=\"brandguidance\" style=\"padding:0px 0px 60px 0px;\">Brand guidance &amp; Design</div>");

  var section = thischild;
  if (section.parentRef != RootNode.childs[2].childs[0]) {
    section = section.parentRef;
  }
  
  if (section.childs.length > 0) {
    var theArray;
    var index = 0;
    var isFound = false;
    var prevChild = null;
    var nextChild = null;
    
    if (document.location.hash.substr(0,4) == "#tag") {
      var tag = document.location.hash;
      tag = tag.split("=")[1];
      tag = tag.split("%20").join(" ");

      theArray = genTagNodes(tag, RootNode.childs[2].childs[1]);
      for (var i = 0; i < theArray.length; i++) {
        if (isFound && nextChild == null) {
          nextChild = FlatTree[theArray[i].obj.ID];
        }
        theArray[i] = FlatTree[theArray[i].obj.ID];
        if (theArray[i] == thischild) {
          prevChild = theArray[i-1];
          isFound = true;
          index = i;
        }
      }
    } else {
      theArray = new Array();
      theArray.push(section);
      if (section == thischild) isFound = true;
      for (var i = 0; i < section.childs.length; i++) {
        if (isFound && nextChild == null) {
          nextChild = section.childs[i];
        }
        theArray.push(section.childs[i]);
        if (section.childs[i] == thischild) {
          prevChild = theArray[i];
          isFound = true;
          index = i+1;
        }
      }
    }
    if (theArray.length > 1) {
      if (prevChild != null) {
        document.write("<a href=\"javascript:void(0);\" onclick=\"OpenPageByID("+prevChild.ID+", null, true);\"><img src=\"/cms/images/img-0f154b12a99aaf301a109aaf3ed6a362.jpg\"></a>");
      } else {
        document.write("<img src=\"/cms/images/img-a38a1d3f2e3ad9ad2fbd9fb3d8be761a.jpg\">");
      }

      for (var i = 0; i < theArray.length; i++) {
        if (i == index) {
          document.write("&nbsp; "+(i+1));
        } else {
          document.write("&nbsp; <a href=\"javascript:void(0);\" onclick=\"OpenPageByID("+theArray[i].ID+", null, true);\">"+(i+1)+"</a>");
        }
      }
      
      if (nextChild != null) {
        document.write("&nbsp; <a href=\"javascript:void(0);\" onclick=\"OpenPageByID("+nextChild.ID+", null, true);\"><img src=\"/cms/images/img-81d861f9881aef1d82c5bb5cc252bf41.jpg\"></a>");
      } else {
        document.write("&nbsp; <img src=\"/cms/images/img-cf3266e59a9bc708518d832dfbb5e8d6.jpg\">");
      }
    }
  }
}

var thePortfolioHighlightID = -1;
function portfolioOver(aID)
{
  thePortfolioHighlightID = aID;
  document.getElementById("portfolio").src = FlatTree[aID].obj.pagefoto;
}

function portfolioOut(aID)
{
  if (aID == thePortfolioHighlightID) {
    document.getElementById("portfolio").src = "/cms/images/img-ccec1cc8fd06ae7203cc7d6255a855ee.jpg";
  }
}

function genTagCloud()
{
  document.write("<img src=\"/cms/images/img-ccec1cc8fd06ae7203cc7d6255a855ee.jpg\" id=\"portfolio\" /><br/>");
  document.write("<div style=\"height:10px;overflow:hidden;\"></div>");
  
  for (var i = 0; i < RootNode.childs[2].childs[0].childs.length; i++) {
    var theNode = RootNode.childs[2].childs[0].childs[i];
    
    document.write("<a href=\"javascript:void(0);\" onclick=\"OpenPageByID("+theNode.ID+");\" onmouseover=\"portfolioOver("+theNode.ID+");\" onmouseout=\"portfolioOut("+theNode.ID+");\">"+theNode.label.toUpperCase()+"</a> ");
  }
}

function genKeywordGallery()
{
  document.write("<div id=\"galleryarea\"></div>");
  reInit = fillGallery;
  
  reInit();
}

function genTagNodes(specialty, baseNode)
{
  var tagNode = null;
  for (var i = 0; i < baseNode.childs.length; i++) {
    var theNode = baseNode.childs[i];
    if (theNode.label == specialty) {
      tagNode = theNode;
    }
  }
  if (tagNode == null) return null;
  
  var theArray = new Array();
  theArray.push(tagNode);
  
  for (var i = 0; i < tagNode.childs.length; i++) {
    theArray.push(tagNode.childs[i]);
  }
  
  return theArray;
}

function fillGallery()
{
  var specialty = document.location.hash;
  specialty = specialty.split("=")[1];
  specialty = specialty.split("%20").join(" ");
  
  var theArray = genTagNodes(specialty, RootNode.childs[2].childs[2]);
  var theHTML = "<img src=\"/cms/images/img-4c556a673f1567fa681d56428d0efd6c.gif\" width=\"40\" height=\"1\">";
  for (var i = 0; i < theArray.length; i++) {
    var thePage = FlatTree[theArray[i].obj.ID];
    if (i != 0) theHTML += "<img src=\"/cms/images/img-4c556a673f1567fa681d56428d0efd6c.gif\" width=\"20\" height=\"1\">";
    theHTML += "<a href=\"javascript:void(0);\" onclick=\"OpenPageByID("+thePage.ID+");\">";
    theHTML += "<img src=\""+thePage.obj.galleryfoto+"\" style=\"filter: dropshadow(color=#000000,offX=0,offY=5)\">";
    theHTML += "</a>";
  }
  theHTML += "<img src=\"/cms/images/img-4c556a673f1567fa681d56428d0efd6c.gif\" width=\"40\" height=\"1\">";
  document.getElementById("galleryarea").innerHTML = theHTML;
  document.getElementById("galleryarea").style.width = (theArray.length * 225 + 60)+"px";
}

var currentOpacity = 0;
function fadeNext()
{
  if (currentOpacity > 0.5) {
    fadeOut();
  } else {
    fadeIn();
  }
}

function fadeOut() {
  if (currentOpacity > 0) {
    currentOpacity -= 0.02;
    setOpacity(currentOpacity);
    setTimeout("fadeOut()", 10);
  } else {
    setTimeout("fadeNext()", 5000);
  }
}

function fadeIn() {
  if (currentOpacity < 1) {
    currentOpacity += 0.02;
    setOpacity(currentOpacity);
    setTimeout("fadeIn()", 10);
  } else {
    setTimeout("fadeNext()", 5000);
  }
}

function setOpacity(value)
{
  var object = document.getElementById("fadefoto1");
  object.style.opacity = value;
  object.style.filter = "alpha(opacity="+Math.round(value*100)+")";
}

function rotateImage(containerID, url)
{
  var container = document.getElementById(containerID);
  
  var theSecondImage = "<div class=\"fotowide\" style=\"top:10px;left:10px;\">"
  theSecondImage    += "<div id=\"fadefoto1\" class=\"foto\" style=\"width:830px;height:205px;opacity:0;filter:alpha(opacity=0);\">";
  theSecondImage    += "<img border=\"0\" ondragstart=\"return false;\" id=\"imgfadefoto1\" src=\""+url+"\" width=\"830\" height=\"205\" />";
  theSecondImage    += "</div>";
  theSecondImage    += "</div>";
  
  container.innerHTML += theSecondImage;
  
  setTimeout("fadeNext()", 3000);
}

function loadXMLDoc(url) {
	request = false;
  if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    // branch for native XMLHttpRequest object
    try {
      request = new XMLHttpRequest();
    } catch(e) {
      request = false;
    }
  } else if(window.ActiveXObject) {
    // branch for IE/Windows ActiveX version
    try {
      request= new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        request = false;
      }
    }
  }
	if(request) {
		request.open("GET", url, false);
		request.send("");
		return request.responseText;
	}
	
	return "";
}

function reInitSearch_new()
{
  setTimeout("document.location.reload();", 100);
}

function genSearchResults(query)
{
  query = document.location.hash;
  query = query.split("=");
  query = query[1].split("%20").join(" ");
  document.write("<div class=\"searchlist\">");
 
  var langcode = (RootNode == NLTree) ? "1043" : "1044";
  var theXML = loadXMLDoc("sites/cmp67/search.php?lang="+langcode+"&q="+escape(query));
  if (theXML == null) {
    theXML = "<search></search>";
  }
  
  theXML = theXML.split("<item");
  var theNodes = new Array();
  for (var i = 1; i < theXML.length; i++) {
    if (theXML[i].length > 0) {
      theNodeID = theXML[i].substr(theXML[i].indexOf("id=\"")+4);
      theNodeID = theNodeID.substr(0, theNodeID.indexOf("\""));
      
      theNodes.push(theNodeID);
    }
  }
  
  if (RootNode == NLTree) {
    document.write("<div class=\"searchtitle\">Zoekresultaat '"+query+"' | "+theNodes.length+" gevonden</div>");
  } else {
    document.write("<div class=\"searchtitle\">Searchresult '"+query+"' | "+theNodes.length+" found</div>");
  }
  
  for (var i = 0; i < theNodes.length; i++) {
    document.write("<div class=\"searchresult\"><a href=\"javascript:void(0);\" onclick=\"OpenPageByID("+theNodes[i]+");\">");
    var label = FlatTree[theNodes[i]].label;
    if (label.length > 9) {
      var date = label.substr(0,8);
      var isNews = ((date*1).toString() == label.substr(0,8));
      if (isNews) {
        label = label.substr(9) + " ("+date.substr(6,2)+"-"+date.substr(4,2)+"-"+date.substr(0,4)+")";
      }
    }

    document.write(label);

    document.write("</a></div>");
  }
  
  document.write("</div>");
  
  reInitSearch = reInitSearch_new;
}

getRootNode();
getTagNodes();
writeStyles();
