var BASE_HREF = BASE_HREF || '../';

$.fn.inputDefaultText = function(options)
{
	options = $.extend({
		text: 'Hledany vyraz'
	}, options);

	return this
		.val(options.text)
		.bind('focus', function(){ if(this.value == options.text) this.value = ''; })
		.bind('blur', function(){ if(this.value == '') this.value = options.text; });
};

$(document).ready(function()
{
	$.fn.kfBox && $('.lightbox').kfBox();
	$.fn.kfBox && $('.velikosti').kfBox();
	$.fn.kfBox && $('.related-images a').kfBox();

	$('table tr:nth-child(even)').addClass('even');

	$('.col-product-desc a').filter(function(){return this.hostname && this.hostname !== location.hostname; }).click(function(){window.open(this.href);return false;});
	$('.brand-list .text-box a').click(function(){window.open(this.href);return false;});

	$('#q').inputDefaultText({ text: 'Hledaný výraz'});

	$('a.external').click(function(){window.open(this.href);return false;});

	$('#shipping-address-switch').bind('click', function(){ $('.shipping-address-box').toggleClass('hidden'); return false });

	if($.fn.cycle)
	{
		$('#motive-box .motive-bar').append('<div class="motive-paging"></div>');
		$('#motive-box .motives').cycle({
			fx: 'fade',
			speed: 1000,
			timeout: 3000,
			pause: true,
			pager: '#motive-box .motive-paging'
		});
	}

	/* Scrollovátko dalších helem */
	(function(){
		var $prevnext = $('<div class="prevnext"><a href="#" class="btn-prev">předchozí</a> <a href="#" class="btn-next">další</a></div>').css('display', 'none');
	    $('.hscroll-box').before($prevnext);
	    var helmyScroller = new KFScroller('.hscroll-box', { scrollWidth: 885, prev: '.product-list .btn-prev', next: '.product-list .btn-next', name: $('.hscroll-box').attr('id') || null });
	    var $helmySwitch = $('<a class="helmy-switch">Vypsat v řadě</a>');
	    $('.hscroll-box').before($helmySwitch);
	    $helmySwitch.bind('click', function()
		{
			var $this = $(this);
			if($this.hasClass('helmy-switch-active'))
			{
				$this.html('Vypsat v řadě');
				$prevnext.css('display', 'none');
				helmyScroller.deactivate();
				$.cookie('helmyScroller', 'collapsed', { expires: 7, path: '/' });
			}
			else
			{
				$this.html('Rozbalit výpis');
				$prevnext.css('display', 'block');
				helmyScroller.activate();
				$.cookie('helmyScroller', 'expanded', { expires: 7, path: '/' });
			}
			$this.toggleClass('helmy-switch-active');
			return false;
		});
		var status = $.cookie('helmyScroller') || 'expanded';
		if(status == 'expanded') $helmySwitch.click();
	})();

	/* Scrollovátko podmenu */
	var $prevnext2 = $('<div class="prevnext"><a href="#" class="btn-prev">předchozí</a> <a href="#" class="btn-next">další</a></div>');
    $('.submenu-hscroll-box').before($prevnext2);
    var submenuScroller = new KFScroller('.submenu-hscroll-box', { scrollWidth: 760, carouselWidth: 760, prev: '#submenu .btn-prev', next: '#submenu .btn-next', name: $('.submenu-hscroll-box').attr('id') || null });
	submenuScroller.activate();

	/* Rozklikávání obrázku */
	(function(){
		var $img = null;
		var $loader = $('<div class="loader"></div>');
		var urlMiddle = null, urlLarge = null;
		var productMidSuffix = 'productMid';
		if($('.product-detail-narrow').size()) productMidSuffix = 'productMidVystroj';

		if($('.col-product-image .more-images a').size())
		{
			$('.col-product-image .more-images a').bind('click', function(event, kfbox)
			{
				if(kfbox || $('#kfb-window').size()) return false;

				urlLarge = this.href;
				urlMiddle = this.href.replace('productBig', productMidSuffix);

				if($img instanceof jQuery)
				{
					$img.unbind();
					$loader.remove();
				}
				$('.col-product-image .img-box').append($loader);
				var $that = $(this);
				$img = $(document.createElement('img'))
					.bind('load', function(e){
						$loader.remove();
						$a = $('<a href="' + urlLarge + '"></a>');
						$('.col-product-image .img-box .inner').empty().append($a);

						$a.append(this).bind('click', function(event){
							$that.trigger('click', [true]);
							return false;
						});
					})
					.attr('src', urlMiddle);
				event.stopImmediatePropagation();
				return false;
			}).kfBox();

			$('.col-product-image .img-box a').bind('click', function(event){
				$('.col-product-image .more-images a:first-child').trigger('click', [true]);
				return false;
			});
		}
		else $('.col-product-image .img-box a').kfBox();

	})();
});

var KFScroller = function(element, options)
{
	this.options = $.extend({
		carousel: 'ul',
		items: 'li',
		prev: '.btn-prev',
		next: '.btn-next',
		scrollWidth: 100,
		carouselWidth: null,
		name: null
	}, options);
	this.$element = $(element);
	this.scrollPosition = 0;
	this.realScrollPosition = 0;
	this.instanceNumber = ++KFScroller.instanceCount;
	this.animating = false;
};

KFScroller.instanceCount = 0;

KFScroller.prototype.activate = function()
{
	var $carousel = this.$element;
	var options = this.options;
	var that = this;

	if($carousel.size())
	{
		var scrollWidth = options.scrollWidth;
		var ulWidth = 0;
		this.carouselWidth = $carousel.width();
		if(options.carouselWidth) $carousel.width(options.carouselWidth);
		var carouselWidth = $carousel.width();

		$carousel.find(options.items).each(function(){
			ulWidth += $(this).outerWidth(true);
		});

		if(ulWidth > carouselWidth)
		{
			$carousel.addClass('kfscroller-active');
			$carousel.find(options.carousel).css('width', ulWidth);

			var animate = function(position, callback){
				that.animating = true;
			    that.scrollPosition = position;
			    that.realScrollPosition = that.scrollPosition * scrollWidth;
			    if(that.realScrollPosition > ulWidth - carouselWidth)
			    {
					that.realScrollPosition = ulWidth - carouselWidth;
					that.scrollPosition = Math.floor(that.realScrollPosition / scrollWidth);
				}
			    else if(that.realScrollPosition < 0)
				{
					that.scrollPosition = that.realScrollPosition = 0;
				}
	            $carousel.stop().animate({ scrollLeft: that.realScrollPosition }, 1600, "easeOutCubic", function(){
					that.savePosition();
					that.animating = false;
				});
			}

			$(options.next).bind('click', function(){
			    animate(that.scrollPosition + 1);
			    return false;

			});
			$(options.prev).bind('click', function(){
			    animate(that.scrollPosition - 1);
			    return false;
			});

			that.bindScroll();

			that.realScrollPosition = that.loadPosition();
			if(that.realScrollPosition != 0)
			{
				that.scrollPosition = Math.floor(that.realScrollPosition / scrollWidth);
				$carousel.scrollLeft(that.realScrollPosition);
			}
		}
		else
		{
			$(options.next).hide();
			$(options.prev).hide();
		}
	}
}

KFScroller.prototype.deactivate = function()
{
	this.unbindScroll();
	this.$element.stop().scrollLeft(0).css({width: this.carouselWidth}).removeClass('kfscroller-active');
	this.$element.find(this.options.carousel).css('width', 'auto');
	$(this.options.next).unbind('click');
	$(this.options.prev).unbind('click');
	this.animating = false;
}

KFScroller.prototype.getInstanceName = function()
{
	return this.options.name || ('KFScroller' + this.instanceNumber);
}

KFScroller.prototype.savePosition = function()
{
	$.cookie(this.getInstanceName(), this.realScrollPosition, { expires: 7, path: '/' });
}

KFScroller.prototype.loadPosition = function()
{
	return $.cookie(this.getInstanceName()) || 0;
}

KFScroller.prototype.bindScroll = function()
{
	var that = this;
	var timeout;
	that.$element.bind('scroll', function(){
		if(!that.animating)
		{
			var oldScrollPosition = that.realScrollPosition;
			that.realScrollPosition = that.$element.scrollLeft();
			that.scrollPosition = Math.floor(that.realScrollPosition / that.options.scrollWidth);
			clearTimeout(timeout);
			timeout = setTimeout(function(){ that.savePosition(); }, 500);
		}
	});
}

KFScroller.prototype.unbindScroll = function()
{
	this.$element.unbind('scroll');
}

