var loc = document.location + '';
var ingles = loc.match( /english/ );
var imgpath = ingles ? "../" : "";
var ie=document.all;
var nn6=document.getElementById&&!document.all;
var nn6off = nn6 ? 2 : 0;

function $( s ) { return document.getElementById( s ); }
function ce( t ) { return document.createElement( t ); }

function pullleft()
{
	var w = document.body.clientWidth;
	var woff = (w-780)/2;

	loff1 = ingles ? 10 : 0;
	loff2 = ingles ? 3 : 0;
	o = $( 'imgmenu' );
	o.style.left = woff + 128 + loff1;
	o = $( 'livromenu' );
	o.style.left = woff + 228 + loff2;
}
function rpar()
{
	return '?' + Math.random();
}

function debugclose()
{
	var _log = $('log');
	document.body.removeChild( _log );
	_log = null;
}

function debug()
{
	var i;
	var list = [];

	if( $('log') == null )
	{
		log = ce( 'div' );
		log.id = 'log';
		log.className = 'debug';
		log.innerHTML = '<nobr><b>Debug Window</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:debugclose()">close</a></nobr>';
		document.body.appendChild( log );
	}

	for( i = 0 ; i < arguments.length ; i++ )
	{
		list[i] = arguments[i];
	}
	$('log').innerHTML += '<br>' + list.join( ', ' );
	$('log').style.zIndex = 1000000;
}


function fotoover( i ) {  }
function fotoout( i ) {  }
function fotosel( i ) 
{
	var tit = tlist[i].titulo != '' ? '<br><b>' + tlist[i].titulo + '</b><br>' : '';
//	var leg = '<br>' + txtsection  + tit + '<br><font size=+1><b>' + tlist[i].legenda.charAt(0) + '</b></font>' + tlist[i].legenda.substring(1);
	var leg = '<br><font class=texto_cinza_claro>' + txtsection + '</font>' + tit + '</font><div class="texto_legenda"><p>' + tlist[i].legenda + '</p></div>';
	$('imgmed').innerHTML = '<img id="fotomed" src="' + imgpath + 'fotos/med/' + tlist[i].src + '.jpg" border="0">'
	$('legenda').innerHTML = leg;
	
	if( ingles )
		$('titulo').innerHTML = '<b>Image Code:</b> ' + tlist[i].codigo + '<br><br><b>Location:</b> ' + tlist[i].local + '<br><b>Date:</b> ' + tlist[i].data;
	else
		$('titulo').innerHTML = '<b>Código de Imagem:</b> ' + tlist[i].codigo + '<br><br><b>Local:</b> ' + tlist[i].local + '<br><b>Data:</b> ' + tlist[i].data;

	next = i + 1; prev = i - 1;
	$('anterior').innerHTML = prev >= 0 ? '<a href="javascript:fotosel(' + prev + ')"><img src="bot_anterior.gif" width=75 height=19 border="0"></a>' : '<img src="bot_anteriorb.gif" width=75 height=19 border="0">';
	$('proxima').innerHTML = next < tlist.length ? '<a href="javascript:fotosel(' + next + ')"><img src="bot_proxima.gif" width=75 height=19 border="0"></a>' : '<img src="bot_proximab.gif" width=75 height=19 border="0">';
}

function mostra( session )
{
	mostrasecao( session );
	thumbbar( session );
}

function secover( idx )
{
	$('titsub').innerHTML = slist[idx].titulo;
}

function secout( idx )
{
	$('titsub').innerHTML = $('titsub').subdefault;
}



function navegadorlivro()
{
	var urlbase;
	var i, j, w, sectit, subsel, subdefault;
	var outtxt = '', sel;

	sectit = arguments[0];

	url = document.location + '';
	j = url.lastIndexOf( '/' );
	urlbase = url.substring( j + 1 );
	subsel = parseInt(urlbase.charAt( urlbase.length - 6 ))-1;
	urlbase = urlbase.substr( 0, urlbase.length - 6 );

	subdefault = ingles ? '(choose one section on the left)' : '(escolha uma sub-seção à esquerda)';
	slist = new Array;
	offset = urlbase == 'p_livro_capitulos0' ? -1 : 0;
	if( offset == -1 ) subsel++;

	for( i = 1, j = 0 ; i < arguments.length ; i++, j++ )
	{
		if( j == subsel ) 
		{
			subdefault = arguments[i];
		}
		slist[j] = new Object;
		slist[j].titulo = arguments[i];
		slist[j].link = urlbase + (j+1+offset) + '.html';
	}
	base = 1 + offset;
	if( slist.length == 0 ) subdefault = '';
	$('titsecao').innerHTML = '<b><a href="' + urlbase + base + '.html" onmouseover="secover(0)"  onmouseout="secout(0)">' + sectit + '</a> »</b>';
	$('titsub').innerHTML = subdefault;
	$('titsub').subdefault = subdefault;

	outtxt += '<img src="esp.gif" width=5 height=1 border="0">';
	for( i = 0, j = 1+offset ; i < slist.length ; i++, j++ )
	{
		sel = i == subsel ? 'b' : '';
		outtxt += '<a href="' + slist[i].link + '" onmouseover="secover(' + i + ')"  onmouseout="secout(' + i + ')"><img src="box_0' + j + sel + '.gif" width=27 height=19 border="0" vspace="9">';
	}
	$('linksub').innerHTML = outtxt;
	w = slist.length*27 + 5;
	$('linksub').style.width = w;
	$('titsecao').style.width = 780 - w - 319;
	$('linksub').style.display = slist.length > 0 ? 'block' : 'none';
}



var slist;
function mostrasecao( session )
{
	var i, j, w, sectit, subsel, subdefault;
	var sec = session.substring( 0, 2 );
	var mainsec = sec + '00';
	var outtxt = '', sel;
		
	subsel = -1;
	subdefault = ingles ? '(choose one section on the left)' : '(escolha uma sub-seção à esquerda)';
	slist = new Array;
	for( i = 0 ; i < secoes.length ; i++ )
	{
		if( secoes[i].codigo.indexOf( sec ) == 0 )
		{
			if( secoes[i].codigo == mainsec )
			{
				sectit = secoes[i].titulo;
				continue;
			}
			if( secoes[i].codigo == session ) 
			{
				subsel = slist.length;
				subdefault = secoes[i].titulo;
			}
			slist[slist.length] = secoes[i];
		}
	}
	if( slist.length == 0 ) subdefault = '';
	$('titsecao').innerHTML = '<b><a href="javascript:mostra(\''+ sec +'\')">' + sectit + '</a> »</b>';
	$('titsub').innerHTML = subdefault;
	$('titsub').subdefault = subdefault;

	outtxt += '<img src="esp.gif" width=5 height=1 border="0">';
	for( i = 0, j = 1 ; i < slist.length ; i++, j++ )
	{
		sel = i == subsel ? 'b' : '';
		outtxt += '<a href="javascript:mostra(\''+slist[i].codigo +'\')" onmouseover="secover(' + i + ')"  onmouseout="secout(' + i + ')"><img src="box_0' + j + sel + '.gif" width=27 height=19 border="0" vspace="9">';
	}
	$('linksub').innerHTML = outtxt;
	w = slist.length*27 + 5;
	$('linksub').style.width = w;
	$('titsecao').style.width = 780 - w - 319;
	$('linksub').style.display = slist.length > 0 ? 'block' : 'none';
	imagens = ingles ? "Images" : "Imagens";
	txtsection = '' + imagens + ' » ' + sectit + '<br>';
	if( subsel >= 0 ) txtsection += '' + subdefault + '<br>';
}

var jamove = false;
var tlist;
function thumbbar( session )
{
	var i;
	var o = $('thumbnails');
	var out = '<table id="thumbtab" cellpadding=0 cellspacing=0 border=0><tr>';
	var tab, len;

	tlist = new Array;
	for( i = 0 ; i < fotos.length ; i++ )
	{
		if( fotos[i].session.indexOf( session ) == 0 )
		{
			tlist[tlist.length] = fotos[i];
		}
	}

	len = tlist.length;
	while( len < 6 ) len *= 2;
	for( i = 0 ; i < len ; i++ )
	{
		j = i % tlist.length;
		out += '<td><a href="javascript:fotosel(' + j + ')" onmouseover="fotoover(' + j + ')" onmouseout="fotoout(' + j + ')"><img src="' + imgpath + 'fotos/peq/' + tlist[j].src + '.jpg" width=123 height=83></a></td>';
	}
	for( i = 0 ; i < len ; i++ )
	{
		j = i % tlist.length;
		out += '<td><a href="javascript:fotosel(' + j + ')" onmouseover="fotoover(' + j + ')" onmouseout="fotoout(' + j + ')"><img src="' + imgpath + 'fotos/peq/' + tlist[j].src + '.jpg" width=123 height=83></a></td>';
	}
	out += '</tr></table>';
	out += '<div class=setavolta onmousedown="thumbgo(this, 1)" onmouseup="thumbgo(this, 1)"></div>';
	out += '<div class=setavai onmousedown="thumbgo(this, -1)" onmouseup="thumbgo(this, -1)"></div>';
	o.innerHTML = out;
	tab =  $('thumbtab');
	tab.v = 0.25;
//	if( !jamove ) tab.tid = setTimeout( 'thumbbarmove(1)', 50 );
//    document.onmousemove=thumbmove;
	jamove = 1;
	tab.left = 0;
	tab.half = tab.clientWidth/2;
	fotosel( 0 );
}

function v() { };
function thumbgo(o, s)
{
	var tab =  $('thumbtab');
	tab.left += 70*s;
	o.href = 'javascript:v()';
	thumbbarmove(o);
}

function thumbbarmove(e)
{
	var tab =  $('thumbtab');

	x = tab.left;
	x *= 1;
	x += tab.v*1;
//	tab.tid = setTimeout( 'thumbbarmove()', 50 );
	if( x < -tab.half ) x += tab.half;
	if( x > 0 ) x -= tab.half;
	tab.left = x;
	tab.style.left = Math.floor( tab.left );
}

function thumbmove( e )
{
	var x = nn6 ? e.clientX : event.clientX;
	var fobj = nn6 ? e.target : event.srcElement;
	var tab =  $('thumbtab');
	var mid = document.body.clientWidth/2;
	var v;
	while (fobj.tagName != 'BODY' )
	{
		if( fobj.id == 'thumbnails' )
		{
			v = -(x - mid)/25;
			tab.v = (tab.v*9 + v)/10;
			return;
		}
		fobj = nn6 ? fobj.parentNode : fobj.parentElement;
	}
	v = 0.25 * tab.v > 0 ? 1 : -1;
	tab.v = (tab.v*9 + v)/10;
}



var secoes = new Array;
function responsesecoes( txt )
{
	var linhas = txt.split( '\n' );
	var i, j, tmp;

	for( i = 2, j = k = 0 ; i < linhas.length ; i++ )
	{
		tmp = linhas[i].split( "\t" );
		secao = new Object;
		k = 0;
		if( tmp.length > 2 )
		{
			secao.tituloport = tmp[k++];
			secao.tituloingl = tmp[k++];
			secao.codigo = tmp[k++].substring( 0, 4 );
			if( ingles )
				secao.titulo = secao.tituloingl;
			else
				secao.titulo = secao.tituloport;

			if( secao.codigo.length < 3 ) continue;
			secoes[j++] = secao;
		}
	}
	ajaxrequest('catalogacao.txt?27', 'responsecatalogacao', null, 0 );
}



var fotos = new Array;
function responsecatalogacao( txt )
{
	var linhas;
	var i, j, tmp;

	txt = unescape( txt );
	linhas = txt.split( '\n' );
	for( i = 2, j = k = 0 ; i < linhas.length ; i++ )
	{
		tmp = linhas[i].split( "\t" );
		foto = new Object;
		k = 0;
		if( tmp.length > 3 )
		{
			foto.src = tmp[k++];
			l = foto.src.length;
			if( l == 4 ) foto.src = foto.src.substring( 1 );
//			debug( foto.src, l );
			foto.codigo = tmp[k];
			foto.session = tmp[k++].substring( 0, 4 );
			foto.data = tmp[k++];
			foto.tituloport = tmp[k++];
			foto.tituloingl = tmp[k++];

			foto.localport = tmp[k++];
			foto.localingl = tmp[k++];
			foto.legendaport = tmp[k++];
			foto.legendaingl = tmp[k++];
			foto.infoport = tmp[k++];
			foto.infoingl = tmp[k++];

			if( ingles )
			{
				foto.titulo = foto.tituloingl;
				foto.local = foto.localingl;
				foto.legenda = foto.legendaingl;
				foto.info = foto.infoingl;
			}
			else
			{
				foto.titulo = foto.tituloport;
				foto.local = foto.localport;
				foto.legenda = foto.legendaport;
				foto.info = foto.infoport;
			}
			fotos[j++] = foto;
		}
	}
	if( psession != 0 )
	{
		mostra( psession );
	}
}

function loaddata( session )
{
	psession = session;
	ajaxrequest('secoes.txt?3', 'responsesecoes', null, 0 );
}

menuatual = false;

function menuon( mid )
{
	var m = $(mid);

	pullleft();
	if( menuatual != m && menuatual != false ) menuoff( menuatual.id );
	m.style.display = 'block';
	m.style.zIndex = 1000;
	if( m.tid != 0 ) clearTimeout( m.tid );
	m.tid = 0;
	menuatual = m;
}

function menuoff( mid )
{
	var m = $(mid);
	m.style.display = 'none';
}

function menuout( mid )
{
	var m = $(mid);
	m.tid = setTimeout( 'menuoff("' + m.id + '")', 200 );
}

function menuover( mid )
{
	var m = $(mid);
	if( m.tid != 0 ) clearTimeout( m.tid );
	m.tid = 0;
}


function preload()
{
	var i, img;
	for( i = 0 ; i < arguments.length ; i++ )
	{
		img = new Image;
		img.src = arguments[i];
	}
}


var psession;


