var galleryId;
var previewId;
var galleryImages;
var selectedImage;

function Gallery() {}

Gallery.create = function(gallery, preview) {
	galleryId = '#'+gallery;
	previewId = '#'+preview;
	//position = 0;
	/*
	var buttonPrev = new Image();
	buttonPrev.src = '/img/arrow_left.png';
	buttonPrev.onclick = function() {Gallery.prev() };
	
	var buttonNext = new Image();
	buttonNext.src = '/img/arrow_right.png';
	buttonNext.onclick = 'alert("ahoj")';
	*/
	
	$(galleryId+' a').attr('class', 'thumbnail');
	galleryImages = $(galleryId+' a');
	
	$('<a id="gallery-prev" href="javascript:void(0);" onclick="Gallery.prev(); return false;"><img src="/img/arrow_left.png" /></a>').prependTo(galleryId);
	$('<a id="gallery-next" href="javascript:void(0);" onclick="Gallery.next(); return false;"><img src="/img/arrow_right.png" /></a>').appendTo(galleryId);
	
	Gallery.select(0);
}

Gallery.select = function(imageIndex) {
	$('#gallery-prev').removeClass('disabled');
	$('#gallery-next').removeClass('disabled');
	$(previewId+' img').attr('src', galleryImages[imageIndex].href);
	$(galleryId+' a.thumbnail').removeClass('selected');
	$(galleryId+' a.thumbnail:eq('+imageIndex+')').addClass('selected');
	selectedImage = imageIndex;

	Gallery.position(imageIndex);
	if (imageIndex <= 0) $('#gallery-prev').attr('class', 'disabled');
	if (imageIndex >= galleryImages.length-1) $('#gallery-next').attr('class', 'disabled');
	return false;
}

/*Gallery.move = function(imageIndex) {
//	if (galleryImages.length-1 > 3) {
//alert($('#gallery-view').width() +" "+ $('#gallery-content').width());
	if ($('#gallery-view').width() < $('#gallery-content').width()) {
		if(imageIndex > selectedImage && imageIndex > 2) position -= $('#gallery-content a:eq('+imageIndex+')').width();
		else if (imageIndex < selectedImage && imageIndex > 1) position += $('#gallery-content a:eq('+imageIndex+')').width();
		$('#gallery-content').animate({left: position+'px'});
	}
}*/

Gallery.position = function (image) {
	var i = 0;
	var place = 0;
	if ($('#gallery-view').width() < $('#gallery-content').width()) {
		if (image > 2)
		$('#gallery-content a').each(function(){
			if (i == image-2) return false;
			place += $(this).width();
			i++;
		});
		$('#gallery-content').animate({left: -place+'px'});
	}
}


Gallery.next = function() {
	//Gallery.move(selectedImage+1);
	Gallery.select(selectedImage+1);
}

Gallery.prev = function() {
	//Gallery.move(selectedImage-1);
	Gallery.select(selectedImage-1);
}