var BASE_HREF = BASE_HREF || '../';

var novex = window.novex = window.novex || {};

Cufon.replace('h1, h2, #main-menu a, .home-bottom h3', { hover: true });

$(document).ready(function()
{

	if($.fn.cycle) $('#motive-box').cycle({
		fx: 'fade',
		pause: 1,
		speed: 1000,
		timeout: 4000
		}
	);

	$.fn.kfBox && $('.lightbox').kfBox();

	$.fn.kfBox && $('.lightbox-1').kfBox();
	$.fn.kfBox && $('.lightbox-2').kfBox();
	$.fn.kfBox && $('.lightbox-3').kfBox();
	$.fn.kfBox && $('.lightbox-4').kfBox();
	$.fn.kfBox && $('.lightbox-5').kfBox();
	$.fn.kfBox && $('.lightbox-6').kfBox();


	$.fn.kfBox && $('.related-images a').kfBox();
	$.fn.kfBox && $('.slider-box .list a').kfBox();

	$('table tr:nth-child(even)').addClass('even');

	$('#q').inputDefaultText({ text: 'Hledaný výraz'});

	$('a.external').click(function(){return !window.open($(this).attr("href"))});

	var $prevnext = $('<div class="prevnext"><a href="#" class="btn-prev">předchozí</a> <a href="#" class="btn-next">další</a></div>');
    $('.slider-box').append($prevnext);
    var panelScroller = new KFScroller('.slider-box .inner', {
		scrollWidth: 620,
		carouselWidth: 610,
		prev: '.slider-box .btn-prev',
		next: '.slider-box .btn-next',
		carousel: '.list',
		items: 'img',
		speed: 800,
		easing: 'easeOutCubic'
	});
	panelScroller.activate();

	$('.panely a').bind('click',function(){
		$('.slider-box .list').load('/ajax/attachedImages/'+$(this).attr('rel')+'/',function(){
			$.fn.kfBox && $('.slider-box .list a').kfBox();
		});
		return false;
	});

	$('#pm select').bind('change',function(){
		var $form=$(this).closest('form');
		$form.submit();
	});

	/* vkládání flashe */
	$('.flash-banner').each(function(){
		flashsize = $(this).find('a').attr('data-flashsize');
		flashsize = flashsize.split("x");
		$(this).flash(
		{
			src: $(this).find('a').attr('href'),
			width: flashsize[0],
			height: flashsize[1],
			wmode: 'opaque',
			flashvars: {}
		});
		$(this).find('.alt').css("display","none");
	});
	/* vkládání mp3 */
	$('.flash-player').each(function(){
 		$(this).flash(
          {
               src: BASE_HREF + 'flash/player.swf',
               width: 350,
               height: 283,
               wmode: 'opaque',
               allowfullscreen: 'true',
               flashvars: {file: $(this).find('a').attr('href'), searchbar: 'false', autostart: 'false', frontcolor: '0x666666', image: $(this).find('img').attr('src')}
          }
     	);
     	// nebudeme zobrazovat alt
     	$(this).find('.alt').css("display","none");
	});

	if($('#map').size())
	{
		var map = new novex.MapController('#map-svg', mapOptions);
		map.init();
	}


});

$.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; });
};

var KFScroller = function(element, options)
{
	this.options = $.extend({
		carousel: 'ul',
		items: 'li',
		prev: '.btn-prev',
		next: '.btn-next',
		scrollWidth: 100,
		carouselWidth: null,
		name: null,
		saveCookie: false,
		easing: null,
		speed: 1500
	}, options);
	this.$element = $(element);
	this.scrollPosition = 0;
	this.realScrollPosition = 0;
	this.instanceNumber = ++KFScroller.instanceCount;
	this.animating = false;
	this.ulWidth = this.carouselWidth = 0;
};

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;
		this.carouselWidth = $carousel.width();
		if(options.carouselWidth) $carousel.width(options.carouselWidth);

		$carousel.find(options.items).each(function(){
			that.ulWidth += $(this).outerWidth(true);
		});

		if(that.ulWidth > that.carouselWidth)
		{
			$carousel.addClass('kfscroller-active');
			$carousel.find(options.carousel).css('width', that.ulWidth);

			var animate = function(position, callback)
			{
				that.animating = true;
			    that.scrollPosition = position;
			    that.realScrollPosition = that.scrollPosition * that.options.scrollWidth;
			    if(that.realScrollPosition > that.ulWidth - that.carouselWidth)
			    {
					that.realScrollPosition = that.ulWidth - that.carouselWidth;
					that.scrollPosition = Math.floor(that.realScrollPosition / that.options.scrollWidth);
				}
			    else if(that.realScrollPosition < 0)
				{
					that.scrollPosition = that.realScrollPosition = 0;
				}
	            $carousel.stop().animate({ scrollLeft: that.realScrollPosition }, that.options.speed, that.options.easing, function(){
					that.savePosition();
					that.animating = false;
					that.updateButtonStates();
				});
			}

			$(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();
			that.scrollPosition = Math.floor(that.realScrollPosition / that.options.scrollWidth);
			$carousel.scrollLeft(that.realScrollPosition);
			that.updateButtonStates();
		}
		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()
{
	if(this.options.saveCookie && $.fn.cookie) $.cookie(this.getInstanceName(), this.realScrollPosition, { expires: 7, path: '/' });
	else return 0;
}

KFScroller.prototype.loadPosition = function()
{
	if(this.options.saveCookie && $.fn.cookie) return $.cookie(this.getInstanceName()) || 0;
	else return 0;
}

KFScroller.prototype.updateButtonStates = function()
{
	if(this.realScrollPosition >= this.ulWidth - this.carouselWidth) $(this.options.next).addClass('kfscroller-btn-disabled');
	else $(this.options.next).removeClass('kfscroller-btn-disabled');
	if(this.realScrollPosition <= 0) $(this.options.prev).addClass('kfscroller-btn-disabled');
	else $(this.options.prev).removeClass('kfscroller-btn-disabled');
}

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');
}

