var currentImg;
var currentLink;
var currentLinkIndex;
var galleryControl = 'pause';
var linklist = new Array();
var imageslist = new Array();
var neko_home_image_box;
var neko_intern_image_box;

function fade( tohide, toshow)
{
	tohide.css({ 'opacity' : 1});
	var tohide_id = parseInt(tohide.attr('rel'));
	var toshow_id = parseInt(toshow.attr('rel'));
	
	if(toshow_id > tohide_id)
	{
		// presentaré una imagen mayor en la lista
		
		toshow.css({ 'opacity' : 0 , 'display': 'none', 'position': 'relative', 'top' : '-335px'});
		tohide.css({'top' : '0px'});
	}else
	{
		// la imagen es menor
		toshow.css({ 'opacity' : 0 , 'display': 'none', 'position': 'relative', 'top' : '0px'});
		tohide.css({'top' : '-335px'});
	}
	
	tohide.animate({ opacity: 0}, 500, function(){
		tohide.css({ 'display' : 'none'});
		toshow.css({ 'top' : '0px'});
	});
	
	toshow.css({ 'display' : 'block'});
	toshow.animate({ opacity: 1}, 500 , function(){
		
	});
}
function slide( index)
{
	$('.slider').css({ 'position' : 'relative'});
	
	neko_intern_image_box.css({'overflow': 'hidden', 'position': 'relative'});
	var i = 0 - (index * 520);
	$('.slider').animate({ 'left': i}, 500, 'easeOutExpo' , function(){
		
	});	
}

function slidehome( index)
{
	$('.slider').css({ 'position' : 'relative'});
	neko_home_image_box.css({'overflow': 'hidden', 'position': 'relative'});
	var i = 0 - (index * 740);
	$('.slider').animate({ 'left': i}, 740, 'easeOutExpo' , function(){
		
	});
	neko_home_image_box.css({'overflow': 'hidden'});
}

function fadeGallery( container_id)
{
	
	var seconds_time = 3000;
	if($('#' + container_id).length > 0)
	{
		var linklist = new Array();
		var imageslist = new Array();
		var imagesbox = $('#' + container_id+' .box');
		neko_intern_image_box = imagesbox;
		
		
		$('#' + container_id+' #navimages ul li').each(function(){
			var link = $(this).find('a');
			linklist.push(link);
		});
		//alert('#' + container_id+' .box a');
		$('#' + container_id+' .box a').each(function(){
			var atag = $(this);
			atag.css({ 'display' : 'none'});
			imageslist.push(atag);
		});
		$.each(linklist, function(index, obj){
			obj.click(function(){
				
				if(currentLink)
				currentLink.removeClass('current');
				
				fade( currentImg, imageslist[index]);	 // realizar animacion
				currentImg = imageslist[index];
				
				currentLink = $(this);
				currentLink.addClass('current');
				return false;
			});
		});
		currentImg = imageslist[0].css({ 'display' : 'block'});
		currentLinkIndex = 0;
		currentLink = linklist[0];
		currentLink.addClass('current');
		
	}else{
		alert('objeto no encontrado');
	}
}
function slideGallery( container_id)
{
	$('#btnpause').hide();
	var seconds_time = 3000;
	if($('#' + container_id).length > 0)
	{
		
		var imagesbox = $('#' + container_id+' .box');
		neko_intern_image_box = imagesbox;
		$('#' + container_id+' #navimages ul li').each(function(){
			var link = $(this).find('a');
			linklist.push(link);
		});
		$('.slider').css({'width' : (520 * linklist.length)})
		//alert('#' + container_id+' .box a');
		$('#' + container_id+' .box img').each(function(){
			var atag = $(this);
			//atag.css({ 'display' : 'none'});
			imageslist.push(atag);
		});
		$.each(linklist, function(index, obj){
			obj.click(function(){
				
				currentLinkIndex = index;
				if(currentLink)
					currentLink.removeClass('current');
				slide( index);
				currentImg = imageslist[index];
				currentLink = $(this);
				currentLink.addClass('current');
				return false;
			});
		});
		if(linklist.length == 1)
		$('#btnplay').hide();
		
		currentLinkIndex = 0;
		currentLink = linklist[0];
		currentLink.addClass('current');
		
		currentImg = imageslist[0];
		galleryControl = 'play';
		currentLinkIndex++;
		$('#btnpause').click(function(){ 
			galleryControl = 'pause';
			$('#btnplay').show();
			$('#btnpause').hide();
			return false;
		});
		$('#btnplay').click(function(){
			galleryControl = 'play';
			$('#btnplay').hide();
			$('#btnpause').show();
			return false;
		});
		
		//autoplay(imageslist, linklist);
		
	}else{
		alert('objeto no encontrado');
	}
}

function slideGalleryHome( container_id)
{
	$('#btnpause').hide();
	var seconds_time = 3000;
	if($('#' + container_id).length > 0)
	{
		
		var imagesbox = $('#' + container_id+' .box');
		neko_home_image_box = imagesbox;
		$('#' + container_id+' #navimages ul li').each(function(){
			var link = $(this).find('a');
			linklist.push(link);
		});
		$('.slider').css({'width' : (740 * linklist.length)})
		//alert('#' + container_id+' .box a');
		$('#' + container_id+' .box a').each(function(){
			var atag = $(this);
			//atag.css({ 'display' : 'none'});
			imageslist.push(atag);
		});
		$.each(linklist, function(index, obj){
			obj.click(function(){
				//alert(index);
				//alert(imageslist[index]);
				//imageslist[index].css({'top': String( 0 - (335 * index)) + 'px'});
				currentLinkIndex = index;
				if(currentLink)
					currentLink.removeClass('current');
				slidehome( index);
				currentImg = imageslist[index];
				currentLink = $(this);
				currentLink.addClass('current');
				return false;
			});
		});
		if(linklist.length == 1)
		$('#btnplay').hide();
		
		currentLinkIndex = 0;
		currentLink = linklist[0];
		currentLink.addClass('current');
		
		currentImg = imageslist[0];
		galleryControl = 'play';
		currentLinkIndex++;
		$('#btnpause').click(function(){ 
			galleryControl = 'pause';
			$('#btnplay').show();
			$('#btnpause').hide();
			return false;
		});
		$('#btnplay').click(function(){
			galleryControl = 'play';
			$('#btnplay').hide();
			$('#btnpause').show();
			return false;
		});
		
		//autoplay(imageslist, linklist);
		
	}else{
		alert('objeto no encontrado');
	}
}


function autoplay()
{
	if(galleryControl == 'pause'){
		//alert('pausado');
		return;
	}
	$('#btnplay').hide();
	$('#btnpause').show();
	
	var current = currentLinkIndex;
	
	if(current >= (linklist.length - 1))
	{
		current = 0 - 1;
	}
	
	//setTimeout(function(){
		
	if(currentLink)
		currentLink.removeClass('current');
		
	slide( currentLinkIndex);
	currentImg = imageslist[currentLinkIndex];
	currentLink = linklist[currentLinkIndex];
	currentLink.addClass('current');
	currentLinkIndex = current + 1;
	//autoplay(imageslist, linklist); // loop
	//}, 3000);
}


function autoplayhome()
{
	if(galleryControl == 'pause'){
		//alert('pausado');
		return;
	}
	$('#btnplay').hide();
	$('#btnpause').show();
	//alert(galleryControl + ' : ' + currentLinkIndex + ' :: ' + linklist.length);
	
	var current = currentLinkIndex;
	
	if(current >= (linklist.length - 1))
	{
		current = 0 - 1;
	}
	
	//setTimeout(function(){
		
	if(currentLink)
		currentLink.removeClass('current');
		
	slidehome( currentLinkIndex);
	currentImg = imageslist[currentLinkIndex];
	currentLink = linklist[currentLinkIndex];
	currentLink.addClass('current');
	currentLinkIndex = current + 1;
	//autoplay(imageslist, linklist); // loop
	//}, 3000);
}



