/*
  handleBodyLoad
*/

function handleBodyLoad(){

	if(runJavascriptVersion == true){
		
		var uri = window.location.href.split(window.location.host)[1];
		if(window.location.search){
			uri = uri.split('?')[0];
		}
		
		var doInit = true;
		if(uri.match('/sitemap/') || uri.match('/quick-links/')) doInit = false;

		if(doInit == true){
			var sW = document.getElementById('site');
			sW.className = sW.className.replace('jsOff', 'jsOn');

			if((uri == '/en/home/') || (uri == '/nl/home/')){
				initPft();
			} else {
				redirectHome();
			}
		}
	}
}


 /*
	getElementsByClassName
*/
document.getElementsByClassName = function (needle){
    var s = [document.documentElement || document.body], i = 0, r = [], l = 0, e;
    var re = new RegExp('(^|\\s)' + needle + '(\\s|$)');

    do{
        e = s[i];

        while (e){
            if (e.nodeType == 1){
                if (e.className && re.test(e.className)) r[l++] = e;

                s[i++] = e.firstChild;
            }

            e = e.nextSibling;
        }
    }
	
    while (i--);

    return r;
} /*
initPft
*/
var pftState = 'bigOverview';

function initPft(){
	siteWrapper = document.getElementById('site');
	introWrapper = document.getElementsByClassName('intro')[0];
	caseWrapper = document.getElementById('casesWrapper');
	passionContainer = caseWrapper.getElementsByTagName('div')[0];
	passionUL = caseWrapper.getElementsByTagName('ul')[0];
	listItems = passionUL.getElementsByTagName('li');
	
	introWrapper.style.margin = '40px 0px 0px 0px';
	
	/* start constants */
	imgWidth = 163;
	imgOffsetLeft = 0;
	imgOffsetBetween = 14;
	listItemsPerPage = 4;
	pageSize = 4 * (imgWidth + imgOffsetBetween);
	
	containerWidth = pageSize;
	containerHeight= 240;
	currentPage = 0;
	totalPages = Math.ceil(listItems.length  / 4);
	
	strongHeight = 56;
	listMarginTop = 28;
	
	/**/
	caseWrapper.style.position= 'absolute';
	caseWrapper.style.top = '300px';
	caseWrapper.style.left = '36px';
	caseWrapper.style.width = 711+ 'px'; //783
	caseWrapper.style.backgroundImage = 'url(/img/themeSiteTemplate/bigListBackground.gif)';
	caseWrapper.style.height = containerHeight+ 'px';

	/**/
	passionContainer.style.top = '0px';
	passionContainer.style.left = '8px';
	passionContainer.style.overflowX = 'hidden';
	passionContainer.style.overflowY = 'hidden';
	passionContainer.style.width = (containerWidth-imgOffsetBetween) + 'px';
	passionContainer.style.height = containerHeight+ 'px';
	passionContainer.style.position= 'absolute';


	/**/
	passionUL.style.listStyleType = 'none';
	passionUL.style.margin = '0';
	passionUL.style.padding = '0';
	passionUL.style.position= 'relative';

	strongs = passionContainer.getElementsByTagName('strong');		
	for (var i=0; i <strongs.length; i++){
		strongs[i].style.fontFamily = 'Verdana, arial';
		strongs[i].style.fontWeight = 'normal';
		strongs[i].style.fontSize = '12px';
		strongs[i].style.color = '#fff';

/*		strongs[i].style.backgroundImage = 'url(/img/themeSiteTemplate/bigTnBackground.gif)';*/
               strongs[i].style.backgroundColor = '#003581';
		strongs[i].style.height = '0px';
		strongs[i].style.width = '163px';
		strongs[i].style.overflow = 'hidden';
		strongs[i].style.display = 'block';
		strongs[i].innerHTML = '<em>'+strongs[i].innerHTML+'</em>';
               strongs[i].style.position = 'absolute';
	}
	ems = passionContainer.getElementsByTagName('em');
	for (var i=0; i <ems.length; i++){
		ems[i].style.display = 'block';
		ems[i].style.fontStyle = 'normal';
		ems[i].style.padding = '4px';
	}	
	
	for (var i=0; i <listItems.length; i++) {
		listItems[i].style.position = 'absolute';
		listItems[i].style.fontSize = '0px';
		listItems[i].style.marginTop = '28px';
		listItems[i].style.marginLeft = (i*(imgWidth + imgOffsetBetween))+ imgOffsetLeft+'px';
		listItems[i].getElementsByTagName('a')[0].onclick = handleSelection;
		listItems[i].id = 'pftImage-' + i;
	}
		images = passionContainer.getElementsByTagName('img');	
	for (var i=0; i <images.length; i++) {
		images[i].style.border = 'none';
		images[i].onmouseover = function(){

			var thisLi = this.parentNode.parentNode;
			thisLi.className = 'mouseover';

			setTimeout('openListItem("'+thisLi.id+'");',200);
		}
		images[i].onmouseout = function(){

			var thisLi = this.parentNode.parentNode;
			thisLi.className = '';
			var thisStrong = this.parentNode.parentNode.getElementsByTagName('strong')[0];
			new Effect.Morph(thisStrong , {style: 'height:'+ 0 +'px', duration: 0.3});
			new Effect.Morph(thisLi , {style: 'margin-top:'+ listMarginTop +'px', duration: 0.3});
                      var thisIMG = thisLi.getElementsByTagName('img')[0];
            		new Effect.Morph(thisIMG , {style: 'margin-top:'+ 0 +'px', duration: 0.3});

			
			if(pftState != 'bigOverview'){
				var liSpan = thisLi.getElementsByTagName('span')[0];
				new Effect.Morph(liSpan , {style: 'height:'+0+'px', duration: 0.4});
			}
		}
	}

	/* list anchors: initover, initout, initspan*/
	anchors = passionContainer.getElementsByTagName('a');
	for (var i=0; i <anchors.length; i++) {
		anchors[i].style.textDecoration = 'none';
		var listSpan = document.createElement('span');
		listSpan.style.display= 'block';

		listSpan.style.width = imgWidth+'px';
		listSpan.style.height = '14px';
		listSpan.style.overflow = 'hidden';
		listSpan.style.position = 'relative';
		listSpan.style.backgroundColor = '#009cdf';
		
		anchors[i].appendChild(listSpan);
		
		var spanImg = document.createElement('img');
		spanImg.style.display = 'block';
		spanImg.style.width = '10px';
		spanImg.style.height = '11px';
		spanImg.style.position = 'absolute';
		spanImg.style.bottom = '1px';
		spanImg.style.right = '1px';
                spanImg.style.border='0';
		spanImg.src = '/img/themeSiteTemplate/list-item-arrow.png';
		
		listSpan.appendChild(spanImg);			
	}
	spans = passionContainer.getElementsByTagName('span');	
	

	
	/* generate next/prev buttons*/
	var prevButton = document.createElement('a');
	prevButton.id = 'prevButton';
	prevButton.style.href= '#';
	prevButton.style.display= 'block';
	prevButton.style.width = '36px';
	prevButton.style.height = '212px';
	prevButton.style.position = 'absolute';
	prevButton.style.top = '14px';
	prevButton.style.left = '-36px';
	prevButton.style.backgroundColor = '#009cdf';
	prevButton.style.visibility = 'hidden';
	prevButton.onclick = handlePrev;
	
	var nextButton = document.createElement('a');
	nextButton.id = 'nextButton';
	nextButton.style.href= '#';
	nextButton.style.display= 'block';
	nextButton.style.width = '36px';
	nextButton.style.height = '212px';
	nextButton.style.position = 'absolute';
	nextButton.style.top = '14px';
	nextButton.style.right = '-36px';
	nextButton.style.backgroundColor = '#009cdf';
	nextButton.onclick = handleNext;

	caseWrapper.appendChild(prevButton);
	caseWrapper.appendChild(nextButton);

	var nextImg = document.createElement('img');
	nextImg.style.width = '10px';
	nextImg.style.height = '11px';
	nextImg.style.position = 'absolute';
	nextImg.style.bottom = '3px';
	nextImg.style.left = '14px';
	nextImg.src = '/img/themeSiteTemplate/list-item-arrow.png';
	
	nextButton.appendChild(nextImg);

	var prevImg = document.createElement('img');
	prevImg.style.width = '10px';
	prevImg.style.height = '11px';
	prevImg.style.position = 'absolute';
	prevImg.style.bottom = '3px';
	prevImg.style.left = '14px';
	prevImg.src = '/img/themeSiteTemplate/list-item-arrow-left.png';
	
	prevButton.appendChild(prevImg);


	/* generate paging buttons*/
	var pagingWrapper = document.createElement('div');
	pagingWrapper.id = 'paging';
	pagingWrapper.style.display= 'block';
	pagingWrapper.style.position = 'absolute';
	pagingWrapper.style.top = '210px';
	pagingWrapper.style.left = '0px';
	
	siteWrapper.appendChild(pagingWrapper);

	for (var i=0; i < totalPages; i++) {
		var pageNr = i+1;
		var pagingButton = document.createElement('a');
		pagingButton.id = 'pageBtn'+pageNr;
		pagingButton.style.display= 'block';
		pagingButton.style.width = '20px';
		pagingButton.style.height = '20px';
		pagingButton.style.position = 'absolute';
		pagingButton.style.marginLeft = 40 + (28*i) +'px';
		pagingButton.style.border = '2px #009cdd solid';
		pagingButton.style.color = '#fff';
		pagingButton.style.fontSize = '11px';
		pagingButton.style.lineHeight = '20px';
		pagingButton.style.fontFamily = 'Verdana';
		pagingButton.style.textDecoration = 'none';
		pagingButton.style.textAlign = 'center';
		pagingButton.innerHTML = pageNr;
		if(i==0){
			pagingButton.style.backgroundColor = '#0061a6';
		} else {
			pagingButton.style.backgroundColor = '#009cdd';
		}
		pagingButton.href = 'javascript:gotoPage(' + i + ');'
		
		pagingWrapper.appendChild(pagingButton);
	}		
	
}

function handlePrev() {
	if (currentPage > 0) {
		currentPage--;
	}
	new Effect.Morph(passionUL , {style: 'margin-left:-'+ (currentPage * (pageSize))+'px', duration: 0.8});
	fadeBtnBorders();
	return false;
}

function handleNext() {
	if ((currentPage+1) < totalPages) {
		currentPage++;
	}
	new Effect.Morph(passionUL , {style: 'margin-left:-'+ (currentPage * (pageSize))+'px', duration: 0.8});
	fadeBtnBorders();
	return false;
}

function gotoPage(goPage) {
	currentPage = goPage;
	new Effect.Morph(passionUL , {style: 'margin-left:-'+ (goPage * (pageSize))+'px', duration: 0.8});
	fadeBtnBorders();
}

function fadeBtnBorders(){
	var pagingBtns = document.getElementById('paging').getElementsByTagName('a');
	var lastBtn = pagingBtns[pagingBtns.length-1];
	var firstBtn = pagingBtns[0];
	var currentBtn = pagingBtns[currentPage];
	
	for (var i=0; i <pagingBtns.length; i++) {
		if(i == currentPage){
			new Effect.Morph(pagingBtns[i] , {style: 'background-color:#0061a6', duration: 0.6});
		} else {
			new Effect.Morph(pagingBtns[i] , {style: 'background-color:#009cdd', duration: 0.6});
		}
	}
	if(currentPage == totalPages-1){
		document.getElementById('nextButton').style.visibility = 'hidden';
		document.getElementById('prevButton').style.visibility = 'visible';
	} else if(currentPage == 0) {
		document.getElementById('nextButton').style.visibility = 'visible';
		document.getElementById('prevButton').style.visibility = 'hidden';
	} else {
		document.getElementById('nextButton').style.visibility = 'visible';
		document.getElementById('prevButton').style.visibility = 'visible';
	}
}

function fadeActiveCase(activeId){
	for (var i=0; i <listItems.length; i++) {
		if(listItems[i].id == activeId){
			listItems[i].getElementsByTagName('img')[0].style.opacity = 0.4;
			listItems[i].getElementsByTagName('img')[0].style.MozOpacity = 0.4;
			listItems[i].getElementsByTagName('img')[0].style.filter = 'alpha(opacity=40)';
		} else {
			listItems[i].getElementsByTagName('img')[0].style.opacity = 1;
			listItems[i].getElementsByTagName('img')[0].style.MozOpacity = 1;
			listItems[i].getElementsByTagName('img')[0].style.filter = 'alpha(opacity=100)';
		}
	}
}

function openListItem(id) {
	var thisLi = document.getElementById(id);
	if (thisLi.className == 'mouseover') {
		var thisStrong = thisLi.getElementsByTagName('strong')[0];
		
		new Effect.Morph(thisStrong , {style: 'height:'+ strongHeight +'px', duration: 0.3});
		new Effect.Morph(thisLi , {style: 'margin-top:'+ 0 +'px', duration: 0.3});

               var thisIMG = thisLi.getElementsByTagName('img')[0];
		new Effect.Morph(thisIMG , {style: 'margin-top:'+ strongHeight +'px', duration: 0.3});

		if(pftState != 'bigOverview'){
			var liSpan = thisLi.getElementsByTagName('span')[0];
			new Effect.Morph(liSpan , {style: 'height:'+14+'px', duration: 0.4});
		}
	}
}

function morphLastStyles() {
	document.getElementById('nextButton').getElementsByTagName('img')[0].style.left = '6px';
	document.getElementById('prevButton').getElementsByTagName('img')[0].style.left = '6px';

	for (var i=0; i <strongs.length; i++){
		strongs[i].style.fontSize = '9px';
		strongs[i].getElementsByTagName('em')[0].style.padding = '2px';
	}
}


function handleSelection() {
        
	if (pftState == 'bigOverview') {
	pftState = 'smallOverview';
	
	nextBtn = document.getElementById('nextButton');
	prevBtn = document.getElementById('prevButton');
	pagingWrapper = document.getElementById('paging');
	
	imgWidth = 80;
	imgHeight = 83;
	imgOffsetLeft = 0;
	imgOffsetBetween = 10;
	listItemsPerPage = 4;
	pageSize = 4 * (imgWidth + imgOffsetBetween);
	
	containerWidth = pageSize; //360
	containerHeight = 130;
	
	strongHeight = 28;
	listMarginTop = 14;
	
	moveDuration = 2.0; //1.0
	moveDurationSlow = 3.0; //1.6
	
	//var pftState = 'smallList';
		
	/* morph container elements */
	new Effect.Morph(passionUL , {style: 'margin-left:-'+ (currentPage * (pageSize))+'px', duration: moveDuration});
	new Effect.Morph(passionContainer , {style: 'width:'+ (containerWidth-imgOffsetBetween) +'px;height:'+containerHeight+'px;left:'+20+'px', duration: moveDuration});
	new Effect.Morph(caseWrapper , {style: 'height:'+containerHeight+'px;', duration: moveDuration});
	new Effect.Morph(caseWrapper , {style: 'top:'+80+'px;left:'+ 370 +'px;width:'+ 390 +'px;', duration: moveDurationSlow});
	new Effect.Morph(nextBtn , {style: 'width:'+20+'px;height:'+83+'px;right:0px;', duration: moveDuration});
	new Effect.Morph(prevBtn , {style: 'width:'+20+'px;height:'+83+'px;left:0px', duration: moveDuration});
	new Effect.Morph(pagingWrapper , {style: 'top:'+153+'px', duration: moveDurationSlow});
	new Effect.Fade(introWrapper.getElementsByTagName('p')[0]);

       /*for (var i=(listItems.length-1); i>=0; i--) {
		listStrong = listItems[i].getElementsByTagName('strong')[0];
              listStrong.parentNode.removeChild(listStrong);
              listStrong.style.display='none';
      }*/


	/* morph elements in all li */
	for (var i=0; i <listItems.length; i++) {
		new Effect.Morph(listItems[i] , {style: 'margin-left:'+ ((i*(imgWidth + imgOffsetBetween))+ imgOffsetLeft)+'px; width:'+imgWidth+'px;height:'+containerHeight+'px;margin-top:'+listMarginTop+'px', duration: moveDuration});
		
		listImage = listItems[i].getElementsByTagName('img')[0];
		new Effect.Morph(listImage , {style: 'width:'+imgWidth+'px;height:'+imgHeight+'px', duration: moveDuration});
		
		listSpan = listItems[i].getElementsByTagName('span')[0];
		new Effect.Morph(listSpan , {style: 'width:'+imgWidth+'px;height:'+0+'px;', duration: moveDuration});
		listStrong = listItems[i].getElementsByTagName('strong')[0];
		new Effect.Morph(listStrong , {style: 'width:'+imgWidth+'px', duration: moveDuration});
	}

	/* overig */
	caseWrapper.style.backgroundImage = 'none';
	setTimeout('morphLastStyles();',400);
	grabFile('/postoffice/passionInfo.cfc?method=getDocumentItemByDocumentURI&documentURI='+this.href.replace('http://'+location.hostname, ''));
	} else {
grabFile('/postoffice/passionInfo.cfc?method=getDocumentItemByDocumentURI&documentURI='+this.href.replace('http://'+location.hostname, ''));
	}

	fadeActiveCase(this.parentNode.id);

	this.blur();

	return false;
}

 /*
	redirectHome
*/

function redirectHome(){
	if(document.getElementsByClassName('template-11').length) return false;

	var getUri = location.href.replace(location.hostname,'').replace('http://','');
            getUri = getUri.replace('?runJavascriptVersion=true', '');              
	var redirectedFrom = '';

	if (getUri.split('?').length > 1) {
		getUri = getUri.split('?')[0];
	}

	if(getUri == '/en/home/' || getUri == '/nl/home/'){
		var getUriSplit = location.href.replace(location.hostname,'').replace('http://','');
                    getUriSplit = getUriSplit.replace('?runJavascriptVersion=true', '');              
		if (getUriSplit.split('?').length > 1) {
			getUriSplit = getUriSplit.split('?');
			for (var i=0; i <getUriSplit.length; i++){
				if(getUriSplit[i].match('redirectedFrom')){
					redirectedFrom = getUriSplit[i].split('=')[1];
				}
			}
			
			//alert(redirectedFrom);
			var liJump = document.getElementById('casesWrapper').getElementsByTagName('li');
			for (var i=0; i <liJump.length; i++){
				if(liJump[i].getElementsByTagName('a')[0].href.match(redirectedFrom)){
					
					setTimeout('document.getElementById("'+ liJump[i].id +'").getElementsByTagName("a")[0].onclick();', 2000);
					//liJump[i].getElementsByTagName('a')[0].onclick();

					var jumpPage = Math.floor(i/4);
					gotoPage(jumpPage)
				}
			}
		}

	} else {
		if(getUri.match('/en/')){
			window.location.href = '/en/home/';
			window.location.href = '/en/home/?redirectedFrom='+getUri;
		} else if(window.location.href.match('/nl/')){
			window.location.href = '/nl/home/?redirectedFrom='+getUri;
		} else {
			window.location.href = '/en/home/?redirectedFrom='+getUri;
		}
	}
}
 /*

update content via AJAX
*/


function getHTTPObject() {
	var xhr = false;
	if (window.XMLHttpRequest) {
	     xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
	     try {
	           xhr = new ActiveXObject("Msxml2.XMLHTTP");
	      } catch(e) {
	                 try {
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	                   } catch(e) {
		xhr = false;
	                  }
	}
     }
     return xhr;
}
	
function grabFile(file) {
		var request = getHTTPObject();

		if (request) {
			request.onreadystatechange = function() {
				displayResponse(request);
			};
			request.open("GET", file, true);
			request.send(null);
		}
	}
	
	function displayResponse(request) {
		var documentItemContent = '';

		if (request.readyState == 4) {

			if (request.status == 200 || request.status == 304) {
				documentItemContent = request.responseText;
				
				// process response
			                       if (!document.getElementById('passionItem')) {
				                      var div = document.createElement('div');
				                      div.id = 'passionItem';
			                     
			                              div.style.width = '698px';
			                             div.style.height = '354px';
			                             div.style.position = 'absolute';
                                                    div.style.top = '210px';
                                                    div.style.left = '42px';  
			                      
			                            div.style.opacity = 0;
  			                            div.style.MozOpacity = 0;
			                            div.style.filter = 'alpha(opacity=0)';

			                            document.getElementById('site').appendChild(div);
			                      	   document.getElementById('passionItem').innerHTML = documentItemContent;
			                      window.setTimeout('fadeUpPassionItem()',1000);

                                            } else {
  				                  document.getElementById('passionItem').innerHTML = documentItemContent;
                                  	   }
							
			}
		}
	}

function fadeUpPassionItem() {
   div= document.getElementById('passionItem');
   new Effect.Opacity(div, { from: 0.0, to: 1.0, duration:3 });
   div.style.display = 'block';
}
 // Popups

var aPopups = new Array();
var aModalPopups = new Array();

window.onfocus = function()
{
	if(aModalPopups.length != 0)
	{
		aModalPopups[0].focus();
	}
}

var sDefaultFeatures = 'channelmode=0, fullscreen=0, location=0, menubar=0, resizable=0, scrollbars=0, status=0, titlebar=0, toolbar=0';

// create popup
function createPopup(sURL, sName, sFeatures, iWidth, iHeight)
{
	var iLeft	= (screen.availWidth - iWidth) / 2;
	var iTop	= (screen.availHeight - iHeight) / 2;
	
	return window.open(sURL, sName, sFeatures + ', width=' + iWidth + ', height=' + iHeight + ', left=' + iLeft + ', top=' + iTop);
}

