// JavaScript Document
var navConfig = {
	navInfo:navList,
	sectionTitle:'',
	columnLength:8,
	firstPoem:0,
	currentPoem:'',
	pList:[],
	navDiv:null,
	setFile:function(idx)
	/***********************************************
	 Calculates the first poem in the navigation bar
	************************************************/
	{
//		alert('idx='+idx)
		var tmp = this.firstPoem
		var f1 = Math.floor(idx/navConfig.columnLength) * navConfig.columnLength
//		alert('f1='+f1)
/*		var f2 = f1 + navConfig.columnLength
		if (f2 >= navConfig.pList.length)
		{
			this.firstPoem = navConfig.pList.length - navConfig.columnLength
		}
		else */
			this.firstPoem = f1
//		alert('this.firstPoem='+this.firstPoem)
		return tmp != this.firstPoem //there has been a change
		
	}
}

function navScroll(sc)
{
	if (navConfig.navInfo.length <= navConfig.columnLength) // nothing to scrol
	   return;
	var prevFirst = navConfig.firstPoem
	var prevLast = prevFirst + navConfig.columnLength - 1
	if (sc > 0)
		navConfig.setFile(navConfig.firstPoem+navConfig.columnLength+sc-1)
	else
		navConfig.setFile(navConfig.firstPoem+sc)
	var delta = navConfig.firstPoem - prevFirst
	if (delta == 0) // this should never happaned because the scrolling button does not exist
		return
	if (delta > 0)
	{
		var i = 0
		var f = function()
		{
			navConfig.navDiv.removeChild(navConfig.pList[prevFirst+i])
			if (prevLast+i+1 < navConfig.pList.length)
				navConfig.navDiv.insertBefore(navConfig.pList[prevLast+i+1],navConfig.downArr)
			if (++i < navConfig.columnLength)
				setTimeout(f,40)
			else
				setScrolling()

		}
		f()
	}
	else // backwards, the steps are always at the length of colomnLength as firstPoems are aligned
	{
		var len = navConfig.pList.length - prevFirst
		var i = delta
		var j = 0
		var f = function()
		{
			if (len-- > 0)
				navConfig.navDiv.removeChild(navConfig.downArr.previousSibling)
			navConfig.navDiv.insertBefore(navConfig.pList[navConfig.firstPoem-i-1],navConfig.upArr.nextSibling)
			++i
			if (++j < navConfig.columnLength)
				setTimeout(f,40)
			else
				setScrolling()
		}
		f()
	}
	//alert('took out')
	//drawRuler()
// ***	setScrolling()
}

function putNextButton(id)
{
	var a = document.getElementById(id)
	if (navConfig.nextPoem != null)
	{
		a.href = navConfig.navInfo[navConfig.nextPoem].url
		a.title = navConfig.navInfo[navConfig.nextPoem].title
		return 1
	}
	a.href=''
	return 0
}

function locatePoem(nm)
{ // find the list holding the poem
	for (i=0;i<navList_layers.length;i++)
	{
		for (j=0;j<navList_layers[i].list.length;j++)
		{
			if (navList_layers[i].list[j].title.match(nm))
			{
				navConfig.navInfo = navList_layers[i].list
				navConfig.sectionTitle = navList_layers[i].title
				return
			}
		}
	}
	alert('no match')
}

function navRuler(nm)
{ // this is the setup function at the beginning of the poem
	locatePoem(nm)
	navConfig.currentPoem = nm
	navConfig.navDiv = document.getElementById('navRuler')
	var cp=0
	for (var i=0;i<navConfig.navInfo.length;i++)
	{
		var t = document.createElement('div')
		var a = document.createElement('a')
		if (navConfig.currentPoem != navConfig.navInfo[i].title)
		{
			a.href = navConfig.navInfo[i].url
			a.title = navConfig.navInfo[i].title
		}
		else
		{
			t.id="current"
			cp=i
			navConfig.nextPoem = i+1
			if (navConfig.nextPoem == navConfig.navInfo.length)
				navConfig.nextPoem = 0
		}
		a.innerHTML = navConfig.navInfo[i].title
		t.appendChild(a)
		navConfig.pList.push(t)
	}
	navConfig.upArr = document.createElement('div')
	var a = document.createElement('a')
	a.innerHTML = 'тег...'
	a.style.direction='rtl'
	a.style.textAlign = 'left'
	navConfig.upArrow = a
	navConfig.upArr.appendChild(a)
	navConfig.downArrow = navConfig.upArrow.cloneNode(true)
	navConfig.downArr = document.createElement('div')
	navConfig.downArr.appendChild(navConfig.downArrow)
	navConfig.setFile(cp)
	drawRuler()
}

function sectionList(l)
{
	var i
	var sec = document.createElement('div')
	sec.className = "galerylink"
	sec.style.position = 'absolute'
	sec.style.left= '-96px'
	sec.style.padding='5px'
	sec.style.border='1px gray solid'
	sec.style.backgroundColor= '#DDDCBD'
/*	sec.style.opacity="0.6"
	sec.style.filter="alpha(opacity=60)"
					  */
	for (i=0;i<l.length;i++)
	{
		var a = document.createElement('a')
		a.href = l[i].url
//		a.title = l[i].title
		a.innerHTML = l[i].title
		var t= document.createElement('div')
		t.appendChild(a)
		sec.appendChild(t)
	}
	sec.style.visibility = "hidden"
	sec.style.zIndex=1000 
/*	sec.onmouseover = function()
	{
		sec.style.opacity="1"
		sec.style.filter="alpha(opacity=100)"		
	}
	sec.onmouseout = function()
	{
		sec.style.opacity="0.6"
		sec.style.filter="alpha(opacity=60)"		
	}
			    */
	return sec
}

function sectionsRuler()
{
	var loc = document.getElementById('navRuler')
	for (var i=0;i<navList_layers.length;i++)
	{
		var t = document.createElement('div')
		var a = document.createElement('a')
		t.style.position ="relative"
		if (navConfig.currentPoem != navList_layers[i].title)
		{
			a.href = navList_layers[i].list[0].url // first poem in the list
//			a.title = navList_layers[i].title
		}
		t.appendChild(a)
		var f = function()
		{
			var hiddnMenu = sectionList(navList_layers[i].list)
			a.appendChild(hiddnMenu)
			a.appendChild(document.createTextNode(navList_layers[i].title))
			t.onmouseover = function(){hiddnMenu.style.visibility = "visible"}
			t.onmouseout = function(){hiddnMenu.style.visibility = "hidden"}
		}
		f()
		loc.appendChild(t)
	}
}

function sectionTitle()
{
	var div = document.createElement("div")
	var a = document.createElement("a")
	div.style.borderBottom = "1px solid black"
	div.style.marginBottom = "6px"
	a.style.backgroundColor= '#DDDCBD'
	a.style.fontSize= '120%'
	a.innerHTML = navConfig.sectionTitle
	div.appendChild(a)
	return div
}

function drawRuler()
{
	navConfig.navDiv.appendChild(sectionTitle())
	navConfig.navDiv.appendChild(navConfig.upArr)
	var endI = navConfig.firstPoem + navConfig.columnLength
	if (endI > navConfig.navInfo.length)
		endI = navConfig.navInfo.length
	for (var i=navConfig.firstPoem;i<endI;i++)
	{
		navConfig.navDiv.appendChild(navConfig.pList[i])
	}
	navConfig.navDiv.appendChild(navConfig.downArr)
	setScrolling()
}

function setScrolling()
{
	if (navConfig.firstPoem > 0)
	{
		navConfig.upArrow.href = 'javascript:navScroll(-1)'
		navConfig.upArrow.style.display='block'
	}
	else
	{
		navConfig.upArrow.href='javascript:void(0)'
		navConfig.upArrow.style.display='none'
	}
	//alert('first=' + navConfig.firstPoem + ' length=' + navConfig.navInfo.length)
	if ((navConfig.firstPoem + navConfig.columnLength) < navConfig.navInfo.length)
	{
		navConfig.downArrow.href = 'javascript:navScroll(1)'
		navConfig.downArrow.style.display='block'
	}
	else
	{
		navConfig.downArrow.href='javascript:void(0)'
		navConfig.downArrow.style.display='none'
	}

}
