// JavaScript Document

window.addEvent('domready', function(){

var slideshow = $('thumbnails');
var images = $$("#thumbnails .thumbnail img");
var imageWidth = 135;
var imageCount = -1;


images.each(function(image, i) {
	imageCount++;
});

var visibleSlides = 3;

imageCount = imageCount-visibleSlides;

var gallerySlides = $$("#gallery .image");


var currentSlide = 0; //Main Image
var currentPos = 0 //Position of menu

var maxWidth = (-imageCount*imageWidth); //This determines where the right-end of the menu is. 0 = left-hand end  

var nextButton = $('clickNext');
var prevButton = $('clickPrev');

var marginFx = new Fx.Style(slideshow, 'margin-left', {duration:800, transition: Fx.Transitions.Quad.easeOut});

prevButton.addEvent("click", function(event){
								  
	marginFx.stop();
	if(currentPos != 0){
		marginFx.start(currentPos+imageWidth);
		currentPos += imageWidth;
		var removeButtonFX = new Fx.Style(nextButton, 'opacity');
		removeButtonFX.set(1);
	}
	else
	{
		var removeButtonFX = new Fx.Style(this, 'opacity');
		removeButtonFX.set(.5);
	}
});

nextButton.addEvent("click", function(event){
	marginFx.stop();
		if(currentPos != maxWidth){
			marginFx.start(currentPos-imageWidth);
			currentPos -= imageWidth;
			var removeButtonFX = new Fx.Style(prevButton, 'opacity');
			removeButtonFX.set(1);
		}
		else
		{
			var removeButtonFX = new Fx.Style(this, 'opacity');
			removeButtonFX.set(.5);
		}
});


images.each(function(thumb, i) {
	thumb.addEvent("click", function(){
			showLarge(thumb);
			
		});
	});
});

function showLarge (thumbnail) {
		var hideFx = new Fx.Style('largeImage', 'opacity', {duration:250});
		hideFx.start(0);
		/*var loadingImage = new Asset.image('../../userfiles/image/ajax-loader.gif', {id: 'loader'}); 
		loadingImage.injectInside('imageHolder');
		(function(){$('largeImage').remove()}).delay(340);*/
						
		(function(){
		var imageToShow = new Asset.image(thumbnail.src.replace("width=130", "width=529"),{id: 'newImage'}); 
		
		var showfx = new Fx.Style(imageToShow, 'opacity', {duration: 250});
		showfx.set(0);
		imageToShow.injectInside('imageHolder');
		$('largeImage').remove();
		showfx.start(1);
		$('newImage').id = 'largeImage';

		}).delay(200);
	
	//alert(thumbnail.src.replace("_thumb", "");
		
		//alert("make me bigger damn it!!!");
		
		
	

}