/*
 * presbeton.PhotoGalleryController
 */

var presbeton = window.presbeton = window.presbeton || {}; 
 
presbeton.PhotoGalleryController = function(options)
{
	this.options = $.extend({
		data: null
	}, options);

};

presbeton.PhotoGalleryController.prototype.init = function()
{	
	this.active = 0;

	/* Thumbnails Scroller */
	$('.photogallery .thumbnails').wrapInner('<div class="hscroll-box"></div>');
	var $prevnext2 = $('<div class="prevnext"><a href="#" class="prev">předchozí</a> <a href="#" class="next">další</a></div>');
    $('.photogallery .thumbnails .hscroll-box').before($prevnext2);
    this.thumbnailsScroller = new kff.widgets.Scroller('.photogallery .thumbnails .hscroll-box', { 
		scrollWidth: 594, 
		carouselWidth: 579, 
		prev: '.photogallery .thumbnails .prev', 
		next: '.photogallery .thumbnails .next' 
	});
	this.thumbnailsScroller.init();
	
	this.$prevnext = $('<div class="prevnext"><a href="#" class="prev">předchozí</a> <a href="#" class="next">další</a></div>');
	$('.photogallery .preview').append(this.$prevnext);
	this.$thumbnails = $('.photogallery .thumbnails ul li a').bind('click', $.proxy(this.thumbnailClick, this));
	this.$prevnext.find('.prev').bind('click', $.proxy(this.prevClick, this));
	this.$prevnext.find('.next').bind('click', $.proxy(this.nextClick, this));
	this.$loader = $('<div class="loader"></div>');
	this.showImage(0);
}

presbeton.PhotoGalleryController.prototype.showImage = function(i)
{
	if(this.active != i)
	{
		var newImgUrl = this.$thumbnails.eq(i).attr('data-midimg');
		var $newImg = $('<img/>');
		var $oldImg = $('.photogallery .preview img');
		$('.photogallery .preview').append(this.$loader);
		$newImg.bind('load', $.proxy(function(){
			this.$loader.remove();
			$oldImg.remove();
			$('.photogallery .preview').append($newImg);
			$('.photogallery .preview .desc .inner').html(this.$thumbnails.eq(i).find('img').attr('alt'));
			
		}, this));
		
		$newImg.attr('src', newImgUrl);

		this.$thumbnails.removeClass('active').eq(i).addClass('active');
		this.active = i;
		
		this.thumbnailsScroller.animate(Math.floor(this.active / 6));
	}
}

presbeton.PhotoGalleryController.prototype.thumbnailClick = function(event)
{
	var i = this.$thumbnails.index(event.currentTarget);
	this.showImage(i);
	return false;
}

presbeton.PhotoGalleryController.prototype.prevClick = function(event)
{
	var i = (this.active - 1 + this.$thumbnails.size()) % this.$thumbnails.size();
	this.showImage(i);
	return false;
}

presbeton.PhotoGalleryController.prototype.nextClick = function(event)
{
	var i = (this.active + 1) % this.$thumbnails.size();
	this.showImage(i);
	return false;
}

