/*
This file is part of the BibleInfoSite system
Author: David van Grootheest
Copyright: Nederlands Bijbelgenootschap (Netherlands Bible Society)

Description: functions for BibleInfoSite system
Last updated: 2009-11-02
*/


function initialize() {

	var i;
	var found;

	var topUrl = top.location.toString();
	var url = window.location.toString();

	var fileName;
	if (url.indexOf('?') > -1) {
		fileName = url.substring(url.lastIndexOf('/')+ 1, url.indexOf('?'));
	}
	else {
		fileName = url.substring(url.lastIndexOf('/')+ 1);
	}

	var query;
	var queryArray = new Array();
	var id;
	var idArray = new Array();
	var subId;
	var subIdArray = new Array();

	if (url.indexOf('?') > -1) {
		query = url.substring(url.indexOf('?')+1);
		queryArray = query.split('&');
		if (queryArray[0].indexOf('id=')==0) {
			id = queryArray[0].substring(queryArray[0].indexOf('id=')+3);
			idArray = id.split('.');
		}
		if (queryArray[1]){
			if (queryArray[1].indexOf('sub=')==0) {
				subId = queryArray[1].substring(queryArray[1].indexOf('sub=')+4);
				subIdArray = subId.split('.');
			}
		}
	}


	if(id && id != '') {
		if(document.getElementById('sub').children.length > 1) {
			/*
			if(document.styleSheets[0].insertRule){
				document.styleSheets[0].insertRule('#sub .itemcontent { display: none; }', 1);
			}else{
				document.styleSheets[0].addRule('#sub .itemcontent', 'display: none');
			}
			*/
			for(i=0; i<document.getElementById('sub').children.length; i++){
				document.getElementById('sub').children[i].children[1].style.display = 'none';
			}

		}else if(document.getElementById('sub').children.length == 1){
			/*
			if(document.styleSheets[0].insertRule){
				document.styleSheets[0].insertRule('#sub .expandSymbol { display: none; }', 1);
			}else{
				document.styleSheets[0].addRule('#sub .expandSymbol', 'display: none');
			}
			*/
			document.getElementById('sub').children[0].children[1].style.display = 'block';
			document.getElementById('sub').children[0].children[0].children[0].children[0].style.display = 'none';
		}

		if(subId){
			/*
			if(subId.indexOf('ann.') == 0){
				var cvArray = new Array();
				cvArray = subIdArray[2].split('_');
				var verseId = subIdArray[1] + '.' + cvArray[0] + '.' +  cvArray[1];
				var mainId = 'ref.' + subId;
				displayItem(subId, mainId);

			}else if(id.indexOf('art.') == 0 && idArray[4]){
				displayItem(subId, 'main');
			}
			*/
			var mainId = 'ref.' + subId;
			displayItem(subId, mainId);
		}
	}

	if(!subId && document.getElementById('tocref.' + id)) {
		document.getElementById('tocref.' + id).focus();
	}


/* Set status-bar behavior for links */

	/*
	for (i = 0; i < document.links.length; i++) {

		if (document.links[i].href.indexOf(':display') > -1) {
			document.links[i].onmouseover = function() { window.status = this.title + ' opzoeken op Biblija.net/BijbelOnline'; return true; };
			document.links[i].onmouseout = function() { window.status = '' };
		}
		else if (document.links[i].href.indexOf('spec.') > 0) {
			var info1 = 'Speciaal artikel bekijken (midden)';
			document.links[i].onmouseover = function() { window.status = info1; return true; };
			document.links[i].onmouseout = function() { window.status = '' };
		}
		else if (document.links[i].href.indexOf('#note.') > 0) {
			var info2a = document.links[i].getAttribute('title');
			document.links[i].onmouseover = function() { window.status = info2a; return true; };
			document.links[i].onmouseout = function() { window.status = '' };
		}
		else if (document.links[i].parentNode.className == 'noteRef') {
			var info2b = document.links[i].getAttribute('title');
			document.links[i].onmouseover = function() { window.status = info2b; return true; };
			document.links[i].onmouseout = function() { window.status = '' };
		}
		else if (document.links[i].parentNode.className == 'textRef') {
			var info2c = document.links[i].getAttribute('title');
			document.links[i].onmouseover = function() { window.status = info2c; return true; };
			document.links[i].onmouseout = function() { window.status = '' };
		}
		else if (document.links[i].getAttribute('title') == 'Naar beginpagina (met uitleg)') {
			var info3a = 'Naar beginpagina (met uitleg)';
			document.links[i].onmouseover = function() { window.status = info3a; return true; };
			document.links[i].onmouseout = function() { window.status = '' };
		}
		else if (document.links[i].href.indexOf('toc.php') > 0) {
			var info3b = 'Inhoudsopgave tonen (links)';
			document.links[i].onmouseover = function() { window.status = info3b; return true; };
			document.links[i].onmouseout = function() { window.status = '' };
		}
		else if (document.links[i].target == 'main') {
			var info4 = 'Hoofdartikel bekijken (midden)';
			document.links[i].onmouseover = function() { window.status = info4; return true; };
			document.links[i].onmouseout = function() { window.status = '' };
		}
		else if (document.links[i].target == 'sub') {
			var info5 = 'Sub-artikel bekijken (rechts)';
			document.links[i].onmouseover = function() { window.status = info5; return true; };
			document.links[i].onmouseout = function() { window.status = '' };
		}
		else if (document.links[i].target == '_blank') {
			var info6 = 'Externe link (nieuw venster)';
			document.links[i].onmouseover = function() { window.status = info6; return true; };
			document.links[i].onmouseout = function() { window.status = '' };
		}
		else if (window.name=='main' && document.links[i].href.substring(document.links[i].href.length - 1) != '/') {
			var info7 = 'Hoofdartikel bekijken (midden)';
			document.links[i].onmouseover = function() { window.status = info7; return true; };
			document.links[i].onmouseout = function() { window.status = '' };
		}
		else {
			document.links[i].onmouseover = function() { window.status = ''; return true; };
			document.links[i].onmouseout = function() { window.status = '' };
		}
	}
	*/

}


function displayStatus1() {
	window.status = 'Opzoeken op Biblija.net/BijbelOnline';
	return true;
}


function displayStatus2() {
	window.status = 'Bekijken op Voederbak.nl';
	return true;
}


function clearStatus() {
	window.status = '';
	return true;
}



function toggleDisplayAnnot(id) {

	node = document.getElementById(id);

	if(node.children[1].style){
		if(node.children[1].style.display=='block'){
			node.children[1].style.display='none';
			node.children[0].children[0].children[0].style.display='block';
			pageTracker._trackEvent('Sub', 'toggleOff', id);
		}else{
			node.children[1].style.display='block';
			node.children[0].children[0].children[0].style.display='none';
			pageTracker._trackEvent('Sub', 'toggleOn', id);

			var idArray = new Array();
			idArray = id.split('.');
			var cvArray = new Array();
			cvArray = idArray[2].split('_');
			var verseId = idArray[1] + '.' + cvArray[0] + '.' +  cvArray[1];
			var mainId = 'ref.' + id;
			displayMainAndSubItem(mainId, id);

			/* node.scrollIntoView(); */
		}
	}
}



function displayItem(id, id0) {
	if(document.getElementById(id)){
		pageTracker._trackEvent('Main', 'clickToSub', id0 + '_' + id);
		mainObject = document.getElementById(id0);
		subObject = document.getElementById(id);
		subObject.children[1].style.display='block';
		subObject.children[0].children[0].children[0].style.display='none';
		displayMainAndSubItem(id0, id)
	}else{
		var href='index.php?id=' + id;
		if(id.indexOf('spec.')==0){
			href = href + '&toc=' + document.getElementById('toc').children[0].getAttribute('id');
		}
		window.location = href;
	}
}



function displayMainAndSubItem(mainId, subId) {

	var mainObject = document.getElementById(mainId);
	if(!mainObject){
		mainObject = document.getElementById('main');
	}
	var subObject = document.getElementById(subId);

	var mainOffset = mainObject.offsetTop;
	var subOffset = subObject.offsetTop;
	var stop = 0;
	var node;
	var i;
	if (mainOffset > subOffset) {
		if (mainOffset - subOffset > 500) {
			for (i = 0; i < document.getElementById('sub').children.length && stop == 0; i++) {
				node = document.getElementById('sub').children[i];
				node.children[1].style.display='block';
				node.children[0].children[0].children[0].style.display='none';
				mainOffset = mainObject.offsetTop;
				subOffset = subObject.offsetTop;
				if (mainOffset - subOffset <= 200 || node.getAttribute('id') == subId) {
					stop = 1;
				}
			}
		}
		/* mainObject.scrollIntoView(); */
		mainObject.focus();
		/* if(mainOffset > 700 || subOffset > 500) { */
		if(subOffset < mainOffset && document.documentElement.scrollTop > 0) {
			subObject.scrollIntoView();
		}
	}else if(subOffset > mainOffset) {
		for (i = 0; i < document.getElementById('sub').children.length && stop == 0; i++) {
			node = document.getElementById('sub').children[i];
			if (node.getAttribute('id') != subId) {
				node.children[1].style.display='none';
				node.children[0].children[0].children[0].style.display='block';
				mainOffset = mainObject.offsetTop;
				subOffset = subObject.offsetTop;
			}
			if (subOffset - mainOffset <= 0 || node.getAttribute('id') == subId) {
				stop = 1;
			}
		}
		/* if(mainOffset > 700 || subOffset > 500) { */
		if(subOffset < mainOffset && document.documentElement.scrollTop > 0) {
			subObject.scrollIntoView();
		}
		/* mainObject.scrollIntoView(); */
		mainObject.focus();
	}
}



function displayScr(id, versions) {
	var target;
	target = 'http://www.biblija.net/biblija.cgi?l=nl&m='

	if (id.indexOf('-') > -1) {
		target = target + convertOsisRef2(id);
	}
	else {
		target = target + convertOsisRef1(id);
	}

	if (versions) {
		/* target = target + '&compact=1'; */
		if (versions == 'SV_NBV') {
			target = target + '&id18=1&id37=1&pos=1&set=10&idp0=38&idp1=19';
		}
		else if (versions == 'NBG51_NBV') {
			target = target + '&id18=1&id16=1&pos=1&set=10&idp0=17&idp1=19';
		}
		else if (versions == 'GNB_NBV') {
			target = target + '&id18=1&id17=1&pos=1&set=10&idp0=18&idp1=19';
		}
		else if (versions == 'WV_NBV') {
			target = target + '&id18=1&id35=1&pos=1&set=10&idp0=36&idp1=19';
		}
	}

	window.open(target);
}



function convertOsisRef1(id) {
	var testId = id + '.';
	var book = id.substring(0, testId.indexOf('.'));
	var cv = id.substring(testId.indexOf('.') + 1);
	var testCv = cv + '.';
	var chapter = cv.substring(0, testCv.indexOf('.'));
	var verseId = cv.substring(testCv.indexOf('.') + 1);
	var testVerse = verseId + '!';
	var verse = verseId.substring(0, testVerse.indexOf('!'));
	var ref;
	ref = book + '+' + chapter;
	if (verse != '') {
		ref = ref + ':' + verse;
	}

	return ref;
}



function convertOsisRef2(id) {
	var id1 = id.substring(0, id.indexOf('-'));
	var testId1 = id1 + '.';
	var book1 = id1.substring(0, testId1.indexOf('.'));
	var cv1 = id1.substring(testId1.indexOf('.') + 1);
	var testCv1 = cv1 + '.';
	var chapter1 = cv1.substring(0, testCv1.indexOf('.'));
	var verseId1 = cv1.substring(testCv1.indexOf('.') + 1);
	var testVerse1 = verseId1 + '!';
	var verse1 = verseId1.substring(0, testVerse1.indexOf('!'));

	var id2 = id.substring(id.indexOf('-') + 1);
	var testId2 = id2 + '.';
	var book2 = testId2.substring(0, testId2.indexOf('.'));
	var cv2 = id2.substring(testId2.indexOf('.') + 1);
	var testCv2 = cv2 + '.';
	var chapter2 = cv2.substring(0, testCv2.indexOf('.'));
	var verseId2 = cv2.substring(testCv2.indexOf('.') + 1);
	var testVerse2 = verseId2 + '!';
	var verse2 = verseId2.substring(0, testVerse2.indexOf('!'));

	var ref;
	ref = book1 + '+' + chapter1;
	if (verse1 != '') {
		ref = ref + ':' + verse1;
	}

	ref = ref	+ '-';

	if (book2 != book1) {
		ref = ref + book2 + chapter2;
		if (verse2 != '') {
			ref = ref + ':' + verse2;
		}
	}
	else if (chapter2 != chapter1) {
		ref = ref + chapter2;
		if (verse2 != '') {
			ref = ref + ':' + verse2;
		}
	}
	else {
		ref = ref + verse2;
	}

	return ref;
}
