var foundWords = 0;
var currentIdx = 1;

function document_initialize() {
	fix_location_hash(window.location);
	var idx = 1;
	var doBackScroll = true;
	currentIdx = 1;
	if (typeof(top.banner) != 'undefined' && top.banner != null) {
		if (typeof(top.banner.get_highlight_object) == 'undefined') {
			window.setTimeout('document_initialize();', 250);
			return;
		}
		var h = top.banner.get_highlight_object();
		var o = gsdlGetElement('textContent');
		if (h != null && o != null && (h.doHighlightTerms || h.phrases.length > 0)) {
			o.innerHTML = h.highlight(o.innerHTML);
		}
	}

	if(document.layers) {
		idx = 0;
		var  highlightReg = new RegExp('^_hla[0-9]+$');
		for (i = 0; i < document.anchors.length; ++i)
		{
			var el = document.anchors[i].name;
			if (el.match(highlightReg))
				++idx;
		}
	} else {
		var docfirsthighlight = null;
		while (true)
		{
			obj = gsdlGetElement('hl' + idx);
			if (obj == null || typeof(obj) == 'undefined') {
				if (idx == 1)
					idx = 0;
				else
					--idx;
				break;
			}
			if (idx == 1)
				docfirsthighlight = obj;
			++idx;
		}
		if (docfirsthighlight != null && idx > 0) {
			if (window.location.hash != null && window.location.hash == '#docfirsthighlight') {
				gotoHighlight(currentIdx);
				doBackScroll = false;
			}
		}
	}

	if (doBackScroll && window.location.hash != null) {
		doBackScroll = window.location.hash.indexOf('#RefToc') != 0;
	}


	foundWords = idx;
	updateBannerButtons('doc', currentIdx, foundWords, cgiarg.d);
	updateDocTree();
	if (doBackScroll)
		window.setTimeout('window.scrollBy(0,-70);',400);
	documentUpdateResultIndex();
}

function documentUpdateResultIndex() {
	if (typeof(top.banner) != 'undefined' && top.banner != null) {
		if (typeof(top.banner.updateResultIndex) == 'undefined') {
			window.setTimeout('documentUpdateResultIndex();', 250);
			return;
		}
		top.banner.updateResultIndex(cgiarg.d);
	}
}

function updateDocTree() {
	if (cgiarg.d != '' && typeof(top.doctree) != 'undefined' && typeof(top.doctree.synchPage) != 'undefined') {
		var bookmark = documentId;
		if (window.location.hash != null && window.location.hash.indexOf('#RefToc') == 0) {
			bookmark = cgiarg.d + '__' + window.location.hash.substr(1);
		}
		top.doctree.synchPage(bookmark);
	} else {
		window.setTimeout('updateDocTree();', 400);
		return;
	}
}

function gotoHighlight(index) {
	if (index == null) index = 1;
	if(document.layers) {
		var currentURL = location.href;
		var highlightReg = new RegExp('(#.*)$', '');
		var results = currentURL.match(highlightReg);
		if (results != null)
			currentURL = currentURL.replace(highlightReg, "#_hla" + index);
		else
			currentURL += "#_hla" + index;
		location.href = currentURL;
	} else {
		var obj = null;
		if(document.getElementById)
			obj = document.getElementById('hl' + index);
		else if(document.all)
			obj = document.all['hl' + index];
		if (obj != null) {
			obj.className = 'hla';
			obj.scrollIntoView(true);
		}
	}
}

function gotonext(value) {
	if (foundWords != 0) {
		var obj = null;
		if(document.getElementById)
			obj = document.getElementById('hl' + currentIdx);
		else if(document.all)
			obj = document.all['hl' + currentIdx];
		if (obj != null) {
			obj.className = 'hl';
		}
		currentIdx += value;
		if (value < 0 && currentIdx < 1)
			currentIdx = 1;
		else if (value > 0 && currentIdx > foundWords)
			currentIdx = foundWords;
		gotoHighlight(currentIdx);
		updateBannerButtons('doc',currentIdx, foundWords, cgiarg.d);
	}
}

function openSection(selectObject) {
	var doc = gsdlGetValue(selectObject).trim();
	if (doc.length) window.location = gsdlURL + cgiarg.l + '/d/' + doc + '/';
}

function getDocOID() { return cgiarg.d; }

