// Barra de rolagem de imagens

var roll_pos = 0;
var roll_fim;
var velo_roll = 50; // velocidade da rolagem
var pixel_roll = 1; // quantidade de saltos entre os pixel
var roll_timout = 0;
var rolando = false;
var mostra = false;
var rolagem_ultimo=1;
var rolagem_max;
var min_img = 5; // Número mínimo de imagens para acontecer a rolagem

function move_left()
{		
	if(rolando)
	{		
		roll_pos += pixel_roll;
		if (roll_pos > 0 ) roll_pos  = - roll_fim;
		move();
		roll_timout = setTimeout("move_left()", velo_roll);
	}
}
function move_right()
{
	if(rolando)
	{
		roll_pos -= pixel_roll;		
		if (roll_pos < - roll_fim) roll_pos  = 0;
		move();
		roll_timout = setTimeout("move_right()", velo_roll);
	}
}

function add_msg(msg)
{
	document.getElementById('mensagem').innerHTML += msg;
}


function move() {
	if (rolagem_max > min_img)
		document.getElementById('barra_rolagem').style.left =  roll_pos + 'px';
	num_img_pos = parseInt(((roll_pos-40)/160 -2)*-1);
	if (pos_certa((roll_pos - 90) %160) && mostra)
	{			
		abre_img(num_img_pos);
	}
	load_thumbs(num_img_pos);
}

function move_to(elemento) {
	
	if (elemento==1 || elemento > rolagem_max)
	{
		roll_pos = ((rolagem_max)*160*(-1)) + 90;
		elemento=1;
	}
	else
		roll_pos = ((elemento-1)*160*(-1)) + 90;
	if (rolagem_max > min_img) 
		document.getElementById('barra_rolagem').style.left =  roll_pos + 'px';
	abre_img(elemento);	
}

function pos_certa(pos){
	if (pos <= 0 && pos > (pixel_roll*-1))
		return true;
	else
		return false;
}

function abre_img(index)
{
	if (index > rolagem_max) index = 1;
	document.getElementById('img_full').src = imgs[index];
	document.getElementById('img_' + rolagem_ultimo).className = "ImgBorda";
	if (rolagem_ultimo > 0 && rolagem_ultimo < 7 && rolagem_max > min_img)
		document.getElementById('img2_' + rolagem_ultimo).className = "ImgBorda";
	rolagem_ultimo = index;
	if (index > 0 && index < 7 && rolagem_max > min_img)
		document.getElementById('img2_' + index).className = "ImgBordaOver";
	document.getElementById('img_' + index).className = "ImgBordaOver";		
	load_thumbs(index);
}

function load_thumbs(index)
{
	// Load nos thumbnails	
	if (rolagem_max < index && imgs_load[1] == '') return false;
	if (index == 1)
		i = Math.max(rolagem_max - 1,1);
	else
	{
		if (index == 2)
			i = rolagem_max;
		else
			i = index-2; 
	}
	for (j = 1; j <= 8 ; j++)
	{			
		if (imgs_load[i] != '')
		{
			document.getElementById('img_' + i).src = imgs_load[i];			
			if (i < 7 && rolagem_max >  min_img)
				document.getElementById('img2_' + i).src = imgs_load[i];			
			imgs_load[i] = '';
		}
		i = (i%rolagem_max)+1;
	}
}

function muda_tempo(acelera)
{
	if (acelera) pixel_roll = acelera;
	else pixel_roll = 1;	
}


function noMove() 
{
	botoes_img_click('stop');
	clearTimeout(roll_timout);
	rolando = false;
}

function rolagem_slow()
{	
	botoes_img_click('play');
	rolando=true;
	mostra=true;
	muda_tempo(false);
	clearTimeout(roll_timout);
	move_right();	
}

function rolagem_fast()
{	
	botoes_img_click('fast');
	rolando=true;
	mostra=true;
	muda_tempo(5);
	clearTimeout(roll_timout);
	move_right();
}


function click_left()
{
	if (opcao_atual != 'stop') noMove();
	mostra=false;
	rolando=true;
	move_left();
}

function click_right()
{
	if (opcao_atual != 'stop') {noMove();muda_tempo(false);}
	mostra=false;
	rolando=true;
	move_right();
}

function get_url_param( name )
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}

function abre_img_url()
{
	noMove();
	
	cod_img_abrir = get_url_param('img');
	if (cod_img_abrir != "") 
	{
		move_to(imgs_cod_abrir[cod_img_abrir]);
		return true;
	}
	
	cod = get_url_param('cod_img');
	
	if (cod == "")
	{
		cod = get_url_param('data');		
		if (cod == "")
			abre_img('1');
		else
		{
			if ( typeof(datas[cod]) == "undefined" )
				abre_img('1');
			else
				move_to(datas[cod]);
		}
	}
	else
		move_to(cod);
}

var opcao_atual = 'stop';

var opcao_texto = Array();

opcao_texto['stop'] = "parar";
opcao_texto['play'] = "rolar devagar";
opcao_texto['fast'] = "rolar rápido";

function botoes_img_click(opcao)
{
	document.getElementById('img_' + opcao_atual).className = 'img_'  + opcao_atual ;
	opcao_atual = opcao;
	document.getElementById('img_' + opcao_atual).className = 'img_'  + opcao_atual +  '_on' ;
	document.getElementById('div_img_texto').innerHTML = opcao_texto[opcao_atual];
}

function botoes_img_over(opcao)
{
	switch (opcao)
	{
		case "stop":					
			document.getElementById('img_stop').className='img_stop_on'
			//document.getElementById('div_img_texto').innerHTML =  'parar';
			break;
		case "play":					
			document.getElementById('img_play').className='img_play_on'
			//document.getElementById('div_img_texto').innerHTML = 'rolar devagar';
			break;
		case "fast":					
			document.getElementById('img_fast').className='img_fast_on'
			//document.getElementById('div_img_texto').innerHTML = 'rolar rápido';
			break;		
	}
	document.getElementById('div_img_texto').innerHTML = opcao_texto[opcao];
}

function botoes_img_out(opcao)
{
	if (opcao!= opcao_atual)
	{
		switch (opcao)
		{
			case "stop":					
				document.getElementById('img_stop').className='img_stop'
				break;
			case "play":					
				document.getElementById('img_play').className='img_play'
				break;
			case "fast":					
				document.getElementById('img_fast').className='img_fast'
				break;		
		}
	}
	document.getElementById('div_img_texto').innerHTML = opcao_texto[opcao_atual];
}