var Images = [];
var $links = $('.styleNav-marks a');
var total  = $links.size()-1;
Images[activeIndex] = $('#slideshow img');
load(activeIndex);

$links.click(function(e) {
	e.preventDefault();
	var $a = $(this);
	var index = $links.index(this);
	if( activeIndex == index ) return false;
	setCurrent(index, $a);
});

function setCurrent(index, $a) {
	if( !existImg(index) ) 
		createImg(index, $a.attr('href'));
	
	$('#slideshow a').append(Images[index]);
	$('#slideshow img:last').css({opacity:0.0}).fadeTo('normal', 1, function(){
		if( $('.styleFoto img').size()>1 ) $('.styleFoto img:first').remove();
	});
		
	var hrefVal = $('.styleText a').eq(index).attr('href');
	$('.styleFoto a').attr('href', hrefVal);
	
	$('.styleText a').eq(activeIndex).attr('class', 'linkPasive');
	$('.styleText a').eq(index).attr('class', 'linkActive');
	
	$links.eq(activeIndex).attr('class','markNoActive');
	$a.attr('class','markActive');

	if( index == total ) {
		$('#styleNav-next').attr('class','inActive-next');
		$('#styleNav-prev').attr('class', 'active');

	}
	else if( !index ) {
		$('#styleNav-prev').attr('class','inActive-prev');
		$('#styleNav-next').attr('class', 'active');
	}
	else {
		if( $('#styleNav-next').attr('class') == 'inActive-next' ) $('#styleNav-next').attr('class', 'active');
		if( $('#styleNav-prev').attr('class') == 'inActive-prev' ) $('#styleNav-prev').attr('class', 'active');
	}

	activeIndex = index;
	load(activeIndex);
}

function setClick(index) {
	$a = $links.eq(index);
	setCurrent(index, $a);	
}

function existImg(index){
	if(Images[index]==undefined)
		return false;
	return true;
}

function createImg(index, href){
	var img = new Image;
	img.src = href;
	Images[index] = img;
}

function getHref(index){
	return $links.eq(index).attr("href");
}

function load(index) {
	var nextIndex = index+1;
	if( existImg(nextIndex) ) {
		load(nextIndex);
	}
	else {
		if ( total >= nextIndex )
			createImg(nextIndex, getHref(nextIndex));
	}
}

function anterior(){
	if( activeIndex > 0 ) {
		setClick(activeIndex-1);
	}
}

function siguiente(){
	if( activeIndex < total ) {
		setClick(activeIndex+1);
	}
}

