var index_increment = 5;
var memory_index = 0;
var len, width, width_mask;

gotoSlide = function(index) {
	$('#debug').html(index + ' ' + len + ' ' + index_increment);
	$('#slideshow .filmstrip').animate({
		left:(index * width * -1)
	},500, function() {
		// animation complete
	});
	
	if(index == 0) {
		$('#nav_left').find('img').removeClass('nav_left_enabled');
		$('#nav_left').find('img').removeClass('nav_left_enabled_over');
		$('#nav_left').find('a').css('cursor','default');
	}
	else {
		$('#nav_left').find('img').addClass('nav_left_enabled');
		$('#nav_left').find('a').css('cursor','pointer');
	}
	
	// 16 - 1 - 5
	
	// looping version
	$('#nav_right').find('img').addClass('nav_right_enabled');
	$('#nav_right').find('a').css('cursor','pointer');
	
	/* no loop version working 
	if(index >= (len - 1 - index_increment)) {
		$('#nav_right').find('img').removeClass('nav_right_enabled');
		$('#nav_right').find('img').removeClass('nav_right_enabled_over');
		$('#nav_right').find('a').css('cursor','default');
	}
	else {
		$('#nav_right').find('img').addClass('nav_right_enabled');
		$('#nav_right').find('a').css('cursor','pointer');
	}
	*/
	
}

prevSlide = function() {
	// --memory_index;
	memory_index -= index_increment;
	if(memory_index < 0) {
		// memory_index = (len - index_increment);
		memory_index = 0;
	}
	gotoSlide(memory_index);
}

nextSlide = function() {
	// ++memory_index;
	memory_index += index_increment;
	if(memory_index > (len - 1)) {
		memory_index = 0;
	}
	gotoSlide(memory_index);
}

lightboxSlides = function(index) {

	var ar_pics = [];
	$('#slideshow ul li a').each(function() {
		// ar_pics.push($(this).attr('href'));
		ar_pics.push($(this).attr('rel'));
	});
	
	// alert("ar_pics=" + ar_pics);

	$.fancybox(ar_pics, {
		'padding'			: 0,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'              : 'image',
		'changeFade'        : 0,
		'overlayOpacity'	: 0.6,
		'index'				: index
	});
	
}

$(document).ready(function() {

	len = $('#slideshow ul li').size();
	width = $('#slideshow ul li').width();
	width_mask = $('#slideshow .mask').width();
	$('#slideshow .filmstrip').css('width',(len * width) + 'px');

	if((width * len) <= width_mask) {
		$('#nav_left').remove();
	}

	$('.filmstrip ul li a img').each(function(index) {
		$(this).attr('title',index);
	});

	$('#nav_left a').bind('mouseover',function() {
		if(memory_index > 0) {
			$(this).find('img').addClass('nav_left_enabled_over');
		}
	});

	$('#nav_left a').bind('mouseout',function() {
		$(this).find('img').removeClass('nav_left_enabled_over');
	});

	$('#nav_right a').bind('mouseover',function() {
		if(memory_index <= len - 1 - index_increment) {
			$(this).find('img').addClass('nav_right_enabled_over');
		}
	});

	$('#nav_right a').bind('mouseout',function() {
		$(this).find('img').removeClass('nav_right_enabled_over');
	});
	
	positionButtonEnlarge = function() {
		$('#button_enlarge').position({
			of: $('#image_large'),// element to position against
			my: 'left top', // which position on the element being positioned to align with the target element
			at: 'left top', // which position on the target element to align the positioned element against
			offset: '10 10' // left-top values to the calculated position
		});
	}
	
	positionButtonEnlarge();
	
	$(window).scroll(function() {
		positionButtonEnlarge();
	});
	$(window).resize(function() {
		positionButtonEnlarge();
	});
	
	setTimeout('positionButtonEnlarge();',200);
	
	$('#slideshow ul li a').each(function(index) {
		$(this).click(function() {
			var href = $(this).attr('href');
			var title = $(this).attr('title');
			$('#image_large').attr('src',href);
			// $('#link_image_large').attr('href',href);
			// $('#link_button_enlarge').attr('href',href);
			
			// modify according to selected image
			$('#link_image_large').click(function() {
				lightboxSlides(index);
				return false;
			});
			$('#link_button_enlarge').click(function() {
				lightboxSlides(index);
				return false;
			});
			return false;
		});
	});
	
	// initialise
	$('#link_image_large').click(function() {
		lightboxSlides(0);
		return false;
	});
	$('#link_button_enlarge').click(function() {
		lightboxSlides(0);
		return false;
	});
	
	gotoSlide(0);
	// setInterval('nextSlide()',2000);
});

