
var id = "#overflow";
var id_foto = ".minipic";
var tam_foto = 145;
var cantPorPag = 5;

var cant;

var distancia = 0;

var movimiento = 0;
var movs = 0;
var load = 0;

var movsMax;
var loadMax;

$(window).load(function() {
			
		/***/
		
		$("#move_left").addClass("hidden");
		
		/***/
				
		cant = $('#img_cant').text();
		
		if(cant <= cantPorPag)
		{
			$("#move_right").addClass("hidden");
			
			return;
		}
		
		movimiento = tam_foto * cantPorPag;
		
		distanciaMaxima = tam_foto * cant;
		
		$(id).css("width", distanciaMaxima);
		
		distanciaMaxima = (-1 * distanciaMaxima) + movimiento;
		
		movsMax = Math.ceil(cant / cantPorPag) - 1;
		
		loadMax = movsMax - 3;
		
		$("#move_left").click(
													function()
													{
														if(movs == 0)
														{
															return;
														}
														else if(movs == 1)
														{
															$("#move_left").addClass("hidden");
															distancia = -1;								
														}
														else
														{
															distancia = (movs-1) * movimiento;
														}
														movs--;
														
														$(id).animate({marginLeft: -distancia}, "normal", "easeout");
														
														$("#move_right").removeClass("hidden");
													}
												);
		
		$("#move_right").click(
													function()
													{
														if(movs == movsMax)
														{
															return;
														}
														else
														{
															if(movs == (movsMax-1))
															{
																$("#move_right").addClass("hidden");
															}
															
															distancia = (movs+1) * movimiento;
														}
														
														movs++;
														
														loadCarrusel();
														
														$(id).animate({marginLeft: -distancia}, "normal", "easeout");
														
														$("#move_left").removeClass("hidden");
													}
												);
 });
 
 /***/

function loadImage(to)
{
	
	id_gallery = $('#gallery_tit').attr('class').substr(4);
	
	pag = -1;
	
	if(to == 'previous') {
		if(img_num > 0) {
			pag = img_num - 1;
		}
	} else if(to == 'next') {
		if(img_num < img_cant) {
			pag = img_num + 1;
		}
	}

	if(pag != -1) {
		
		img_num = pag;
		
		/**/
		
		movs = Math.ceil((img_num+1) / cantPorPag) - 1;
		
		if(movs && (movs != load)) {			
			loadCarrusel();
		}
		
		distancia = movs * movimiento;
		
		$(id).css({marginLeft: -distancia});
		
		/**/
		
		toggleButtons();
		
		$.ajax({
		async: false,
		type: 'POST',
		url: 'galleries/photo/'+pag,
		complete: _loadImage
		});
	}
	
}

function _loadImage(oxml)
{
	xml = oxml.responseXML;
	
	xmlitem = xml.getElementsByTagName('item').item(0);
	
	if(!xmlitem)
	{
		return;
	}

	src = 'img/photos/'+xmlitem.getAttribute('src');
	title = xml.getElementsByTagName('item').item(0).firstChild.nodeValue;
	
	showImage(src, title);
	
}

function loadCarrusel()
{

	if(load < loadMax)
	{														
		$('#carrusel_cont').load('galleries/temporal-photos/'+movs, null, function() {
		$('#overflow').append($('#carrusel_cont').html());
		$('#carrusel_cont').empty();
															
		carLinks();																
	});
										
	load++;
}
}
