var BASE_HREF = BASE_HREF || '../';

$(document).ready(function()
{
	var langCode = $("html").attr("xml:lang") || 'cs';
	var lang = {
		'cs': {
			'Hledaný výraz': 'Hledaný výraz'
		},
		'en': {
			'Hledaný výraz': 'Search term'
		}
	};

	$('a.lightbox').kfBox();			
	$('a.external').click(function(){return !window.open($(this).attr("href"))});
	$("a.print").bind("click", function() { window.print(); return false; });
	$('table tr:nth-child(odd)').addClass('even');
	$('#q').inputDefaultText({ text: lang[langCode]['Hledaný výraz']});


	/* Inicializace scrollovátka referencí */
	$('.ref-cycle-box').append('<div class="prevnext"><a href="#" class="btn-prev">Předchozí</a><a href="#" class="btn-next">Další</a></div>');
	$('.ref-cycle-box .frames').wrap('<div class="hscroll-box"></div>');
    var refScroller = new kff.widgets.Scroller('.ref-cycle-box .hscroll-box', {
		carousel: '.frames',
		items: '.frame',
		prev: '.btn-prev',
		next: '.btn-next',
		scrollWidth: 230,
		carouselWidth: 230,
		speed: 500
	});
	refScroller.activate();

	var interval;	
	var bindAutoscroll = function(){
		clearInterval(interval);
		interval = setInterval(function(){
			if(refScroller.scrollPosition >= $('.ref-cycle-box .hscroll-box .frame').size() - 1) refScroller.scrollFirst(); 
			else refScroller.scrollNext();
		}, 5000);
	};	
	bindAutoscroll();
	
});

$(window).bind('load', function()
{
	fontSizeListener('#main', equalizeHeights);
});

$.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; });
};

$.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; });
};

$.fn.kfEqualizeColumns = function(options)
{
	options = $.extend({
		column: '>li'
	}, options);

	return this.each(function(i)
	{
		var $columns = $(options.column, this);
		var maxHeight = 0;
		$columns
			.height('auto')
			.each(function(){
				var h = $(this).height();
				if(h > maxHeight) maxHeight = h;
			})
			.height(maxHeight);
	});
}

function fontSizeListener($el, callback)
{
	$el = $($el);
	var h = 0;
	var interval = setInterval(function()
	{
		if($el.height() != h){
			h = parseInt($el.height());
			callback();
		}
	}, 200);
};

function equalizeHeights()
{
	$('.gallery>ul').kfEqualizeColumns({ column: '>li>a' });
	$('.signpost>ul').kfEqualizeColumns({ column: '>li>.eq-ref' });
};

