var srcA = new Array();
var altA = new Array();
var creA = new Array();
var pos = null;
var _w;
var _h;

function gup(name, elem){
	var img = elem;
	
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(img);

	return results[1].replace(/\[CODCERQ\]/gi, "#").replace(/\[CODASSIM\]/gi, "'").replace(/\[CODASDUP\]/gi, "\"").replace(/\[CODECOM\]/gi, "&");
}

$(document).ready(function(){
});



function hideNav(pos){
	var posFinal = (imgA.length - 1); 
	pos == 1 ? $('.navegacaoEsq').hide() : $('.navegacaoEsq').show();
	pos == posFinal ? $('.navegacaoDir').hide() : $('.navegacaoDir').show();
}

function resgataImg(elem){
    var img = gup("Arquivo", elem);
	pos = gup("Index", elem);
	$('#imgGrande').attr({'src' : img})
}

function resgataLegenda(pos){
	var leg = gup('Legenda', imgA[pos]);
	var credito = gup('Credito', imgA[pos]);
	
	$('#legenda').html(leg + "<span></span>");
    $('#legenda span').html("<br/>" + credito);
}

function navegacao(dir){
    
    $('.ModalLoading').removeClass('DisplayNone');
    $('.ModalLoading').css({'width' : '100%', 'height' : '100%'});
    
	if (dir == 'ante') {
		(pos > 0) ? pos-- : (pos = pos);
	}else if (dir == 'prox'){
		(pos < (imgA.length)) ? pos++ : (pos = pos);
	}
	
	resgataImg(imgA[pos]);
	resgataLegenda(pos)
	hideNav(pos);
	
	//resgatar o indice da foto
	$('.counter').html(pos + " ");
		
	_w = parseFloat(gup("OriginalWidth", imgA[pos]));
	_h = parseFloat(gup("OriginalHeight", imgA[pos]));	
		
	$('#imgGrande').css('width', _w);
	$('#imgGrande').css('height', _h);
		
	_w = _w + 138;
	(_w < 500)? _w = 500 : _w = _w;
	
	$('.blockMsg').css({width: _w+'px', marginLeft: '-'+(_w/2)+'px'})
	$('.modal p').css({width: (_w -130) +'px'})
	$('.ModalLoading').addClass('DisplayNone');
}

function abreModal(elem){
	var img = elem.getElementsByTagName('img')[0].src;
	
	resgataImg(img);
	resgataLegenda(pos);
	hideNav(pos)
	
	//resgatar o indice da foto
	var counter = parseFloat(gup("Index", img));
	$('.counter').html(counter + " ");
	
	_w = parseFloat(gup("OriginalWidth", img));
	_h = parseFloat(gup("OriginalHeight", img));
	$('#imgGrande').css('width', _w);
	$('#imgGrande').css('height', _h);
	
	_w =  _w + 138;
	(_w < 500)? _w = 500 : _w = _w;

	$.blockUI({ message: $('.modal'), css: { border: 'none', width: _w + 'px', position: 'absolute', top: '55px', marginLeft: '-' + (_w / 2) + 'px', left: '50%'} });
	$('.modal p').css({ width: (_w - 130) + 'px' })
}

function fechaModal(){
	$.unblockUI();
}


