/*
 * SherwoodGallery     
 *  
 */
kff.widgets.WoodGallery = function(options)
{
	// this.$element = $elements.eq(0);
	this.options = $.extend({
		data: null,
		prefetchCount: 3,
		hashPrefix: 'kfbox-',
		pageSpan: 3,
		pageLeaders: ' … ',
		historyLoadCallback: null,
		afterShowCallback: null,
		slideshowSpeed: 4000,
		slideshowFastFactor: 2,
		speed: 1600,
		easing: 'easeOutQuart',
		domHooks: {
			title: null,
			image: null,
			desc: null,
			next: null,
			prev: null,
			next2: null,
			prev2: null,
			pageActive: null,
			pageTotal: 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;
};

kff.widgets.WoodGallery.prototype.init = function()
{
	this.render();
	this.showImage(this.activeIndex);	
}

kff.widgets.WoodGallery.prototype.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;
};

kff.widgets.WoodGallery.prototype.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);
				that.stopSlideshow();
			}
			else that.showNext();
		};
		this.options.domHooks.slideshowButton.bind('click', function()
		{
			that.toggleSlideshow();
			return false;
		});
		
		this.options.domHooks.image.bind('click', function()
		{
			that.toggleSlideshow();
			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)
			{
				that.startSlideshow();
			}
			return false;
		});
	}

};

kff.widgets.WoodGallery.prototype.slide = function()
{
	if(this.activeIndex + 1 >= this.options.data.length)
	{
		this.showImage(0);
		this.stopSlideshow();
	}
	else this.showNext();
};

kff.widgets.WoodGallery.prototype.startSlideshow = function()
{
	var that = this;
	this.slideshowInterval = window.setInterval(function(){ that.slide(); }, this.options.slideshowSpeed);					
	this.options.domHooks.slideshowButton.html('Zastavit slideshow').removeClass('slideshow-stopped').addClass('slideshow-running');
	this.slide();
	this.slideshow = true;
};

kff.widgets.WoodGallery.prototype.stopSlideshow = function()
{
	window.clearInterval(this.slideshowInterval);
	this.options.domHooks.slideshowButton.html('Spustit slideshow').removeClass('slideshow-running').addClass('slideshow-stopped');
	this.slideshowInterval = null;
	this.slideshow = false;
};

kff.widgets.WoodGallery.prototype.toggleSlideshow = function()
{
	if(this.slideshow) this.stopSlideshow();
	else this.startSlideshow();
};



kff.widgets.WoodGallery.prototype.showNext = function()
{
	this.showImage(this.activeIndex + 1);
};

kff.widgets.WoodGallery.prototype.showPrev = function()
{
	this.showImage(this.activeIndex - 1);
};

kff.widgets.WoodGallery.prototype.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 );
		}
	}
};

kff.widgets.WoodGallery.prototype.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);
	}
};

kff.widgets.WoodGallery.prototype.transitionTo = function(imageIndex)
{	
	var that = this;
	var parentSize = {x: this.options.domHooks.image.width(), y: this.options.domHooks.image.height() };
	
	var $oldImg = $('img', this.options.domHooks.image);

	var image = $(document.createElement('img'))
		.bind('load', function(){
			$(this)
				.css({ 
					position: 'absolute',
					/*
					left: (parentSize.x - this.width) / 2, 
					top: (parentSize.y - this.height) / 2,
					*/ 
					left: 0, 
					top: 0,
					opacity: 0 })
				.appendTo(that.options.domHooks.image);
			$(this).animate({opacity: 1}, that.options.speed, that.options.easing);
			$oldImg.animate({opacity: 0}, that.options.speed, that.options.easing, function(){ $(this).remove(); });	
		})
		.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]);
		});
		
		if(that.options.domHooks.pageActive) that.options.domHooks.pageActive.html(imageIndex + 1);
		if(that.options.domHooks.pageTotal) that.options.domHooks.pageTotal.html(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();		
		
		if($('.product-detail .product-images a').size() < 2) $('.product-detail a.zoom').hide();
		else $('.product-detail a.zoom').show();
		
	}, 0);
	
	
	
	this.activeIndex = imageIndex;
};

