/*
 * RockGallery     
 *  
 */

(function($){

$.fn.rockGallery = function(options)
{  
	new rockGallery(options);
	return this;
};

var rockGallery = function(options)
{
	// this.$element = $elements.eq(0);
	this.options = $.extend({
		data: null,
		prefetchCount: 3,
		hashPrefix: 'kfbox-',
		pageSpan: 3,
		pageLeaders: ' … ',
		historyLoadCallback: null,
		afterShowCallback: null,
		slideshowSpeed: 5000,
		slideshowFastFactor: 2,
		
		domHooks: {
			title: null,
			image: null,
			description: null,
			rating: null,
			next: null,
			prev: null,
			slideshowButton: null,
			slideshowFastButton: null
		}
		
	}, options);
	
	this.langCode = this.options.langCode || $('html').attr('xml:lang') || 'cs';
	this.oldHash = '';
	this.activeIndex = 0;
	this.slideshow = false;
	this.slideshowInterval = null;
	
	this.render();
	this.showImage(this.activeIndex);
};

rockGallery.prototype = new function()
{
	this.showByHash = function(hash)
	{
		if(hash.indexOf(this.options.hashPrefix) !== 0) return false;
		var activeIndex = parseInt(hash.replace(this.options.hashPrefix, '')) - 1;
		this.showImage(activeIndex);
		return true;
	};
	
	this.render = function()
	{
		var that = this;
		this.options.domHooks.next.bind('click', function(){ that.showNext(); return false; });
		this.options.domHooks.prev.bind('click', function(){ that.showPrev(); return false; });
		
		// Slideshow control:
		if(this.options.slideshowSpeed && this.options.domHooks.slideshowButton)
		{
			var slide = function(){
				if(that.activeIndex + 1 >= that.options.data.length) that.showImage(0);
				else that.showNext();
			};
			this.options.domHooks.slideshowButton.bind('click', function()
			{
				if(that.slideshow)
				{
					window.clearInterval(that.slideshowInterval);
					that.options.domHooks.slideshowButton.html('Spustit slideshow').removeClass('slideshow-running').addClass('slideshow-stopped');
					that.slideshowInterval = null;
				}
				else
				{
					
					that.slideshowInterval = window.setInterval(slide, that.options.slideshowSpeed);					
					that.options.domHooks.slideshowButton.html('Zastavit slideshow').removeClass('slideshow-stopped').addClass('slideshow-running');
					slide();
				}
				that.slideshow = !that.slideshow;
				return false;
			});
			
			this.options.domHooks.slideshowFastButton.bind('click', function()
			{
				if($(this).hasClass('slideshow-fast'))
				{
					$(this).html('Zrychlit').removeClass('slideshow-fast');
					that.options.slideshowSpeed *= that.options.slideshowFastFactor;
				}
				else
				{
					$(this).html('Zpomalit').addClass('slideshow-fast');
					that.options.slideshowSpeed = Math.floor(that.options.slideshowSpeed / that.options.slideshowFastFactor);
				}
				
				if(that.slideshowInterval)
				{
					window.clearInterval(that.slideshowInterval);
					that.slideshowInterval = window.setInterval(slide, that.options.slideshowSpeed);
					slide();
				}
				return false;
			});
		}
	};

	this.showNext = function()
	{
		this.showImage(this.activeIndex + 1);
	};

	this.showPrev = function()
	{
		this.showImage(this.activeIndex - 1);
	};

	this.preloadSiblings = function(index)
	{
		var prefetchCount = this.options.prefetchCount;
		for(var i = 1; i <= prefetchCount; i++)
		{
			if(this.options.data[index + i])
			{
				$(document.createElement('img')).attr('src', this.options.data[index + i].imageUrlFull );
				$(document.createElement('img')).attr('src', this.options.data[index + i].imageUrlThumb );
			}
			if(this.options.data[index - i])
			{
				$(document.createElement('img')).attr('src', this.options.data[index - i].imageUrlFull );
				$(document.createElement('img')).attr('src', this.options.data[index - i].imageUrlThumb );
			}
		}
	};

	this.showImage = function(imageIndex)
	{
		if(typeof this.options.data[imageIndex] !== 'undefined')
		{
			var that = this;
			var image = $(document.createElement('img'))
				.bind('load', function(e)
					{
						that.transitionTo(imageIndex);
						that.preloadSiblings(imageIndex);
					})
				.attr('src', that.options.data[imageIndex].imageUrlFull);
		}
	};

	this.transitionTo = function(imageIndex)
	{	
		var that = this;
		var parentSize = {x: this.options.domHooks.image.width(), y: this.options.domHooks.image.height() };		

		$('img', this.options.domHooks.image).fadeOut(1000, function(){
			$(this).remove();
		});	

		var image = $(document.createElement('img'))
			.bind('load', function(){
				$(this)
					.css({ 
						left: (parentSize.x - this.width) / 2, 
						top: (parentSize.y - this.height) / 2, 
						display: 'none' })
					.appendTo(that.options.domHooks.image)
					.fadeIn(1000);
			})
			.attr('src', that.options.data[imageIndex].imageUrlFull);
			
		setTimeout(function()
		{
			$.each(that.options.domHooks, function(key, value)
			{
				if(key == 'image' || key == 'next' || key == 'prev' || key == "slideshowButton" || key == "slideshowFastButton") return;
				if(that.options.data[imageIndex][key] == 'undefined') return;
				if(value instanceof jQuery) value.empty().append(that.options.data[imageIndex][key]);
				else if(typeof value == 'function') value(that.options.data[imageIndex][key]);
			});
			
			that.options.domHooks.paging.empty().append((imageIndex + 1) + ' / ' + that.options.data.length);
			
			/* Thumbnails next-prev */
			$('img', that.options.domHooks.prev).remove();
			$('img', that.options.domHooks.next).remove();
			if(that.options.data[imageIndex + 1])
			{
				that.options.domHooks.next.append('<img src="'+ that.options.data[imageIndex + 1].imageUrlThumb +'" />').show();
			}
			else that.options.domHooks.next.hide();
			if(that.options.data[imageIndex - 1])
			{				
				that.options.domHooks.prev.append('<img src="'+ that.options.data[imageIndex - 1].imageUrlThumb +'" />').show();
			}
			else that.options.domHooks.prev.hide();					
		}, 0);

		this.activeIndex = imageIndex;
	};
};

$.rockGallery = rockGallery;

})(jQuery)
