var BASE_HREF = BASE_HREF || '../';
var defaultMotive = defaultMotive || 'Ložnice';
var isHP = isHP || false;
var hledanyVyraz = hledanyVyraz || 'Hledaný výraz';

$.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);
	});
}

switchTab = function(selector)
{
	$(selector).parent('ul').find('li.active').removeClass('active').end().end().addClass('active');
};

$(document).ready(function()
{
	var langCode = $('html').attr('xml:lang') || 'cs';

	$.fn.kfBox && $('.lightbox').kfBox();

	$.fn.kfBox && $('.lightbox-artikl').kfBox();

	$('table tr:nth-child(even)').addClass('even');

	$('#q').inputDefaultText({ text: hledanyVyraz});

	$('a.external').click(function(){window.open(this.href);return false;});

	/* Motives: */
	if(typeof motiveSets != 'undefined') (function()
	{
		$('#motive-box').append('<a href="#" class="motive-prev"></a><a href="#" class="motive-next"></a>');
		var runMotiveSet = function(motiveSet)
		{
			$('#motives').addClass('loading').empty();
			var l = motiveSet.length;
			var imgs = [];
			$.each(motiveSet, function(i, val)
			{
				var $img = $('<img width="960" height="361"/>').bind('load', function()
				{
					if(--l <= 0)
					{
						$('#motives').append('<ul />');
						$.each(imgs, function(i, val){
							val.wrap('<li></li>').parent().appendTo('#motives ul');
						});

						$.fn.kfBox && $('#motives .lightbox').kfBox();

						$("#motives").removeClass('loading').jCarouselLite({
	    					btnNext: '.motive-next',
				    		btnPrev: '.motive-prev',
				    		easing: "easeInOutCubic",
							speed: 1000,
							visible: 1,
							scroll: 1,
							circural: true
						});


					}
				});
				if(val.link) imgs.push($img.wrap('<a href="'+ val.link +'"></a>').parent());
				else imgs.push($img.wrap('<a href="'+ val.filename +'" class="lightbox"></a>').parent());
				$img.attr('src', val.filename);
			});
		};

		runMotiveSet(motiveSets[defaultMotive]);

		/* Motive sliding menu: */
		if(isHP){
			$('#motive-box').append('<div id="motive-menu"><a href="#" class="btn-toggle btn-toggle-down"></a><img src="' + BASE_HREF + 'img/txt-kolekce-' + langCode + '.png" height="47" /><ul></ul></div>');
			var $motiveMenu = $('#motive-menu');
			var $motiveUl = $('#motive-menu ul');
			$.each(motiveSets, function(i)
			{
				$motiveUl.append('<li><a href="#">' + i + '</a></li>');
			})

			var motiveMenuSlideHeight = $motiveMenu.outerHeight() - $('a.btn-toggle').outerHeight();
			$motiveMenu.find('a.btn-toggle').bind('click', function()
			{
				var $this = $(this);
				var offset = $this.hasClass('.btn-toggle-down') ? -motiveMenuSlideHeight : 0;
				$motiveMenu.animate({ bottom: offset }, 500, 'easeOutCubic', function(){ $this.toggleClass('btn-toggle-down'); });
				return false;
			});

			$motiveUl.find('a').bind('click', function(){
				runMotiveSet(motiveSets[$(this).text()]);
				return false;
			});
		};
	})();

	if($.fn.flash){
		$("#prodejci-kraje-flash").flash({
			src: BASE_HREF+'flash/mapa.swf',
			width: 670,
			height: 360,
			wmode: 'opaque',
			version: '7',
			expressInstall: true
		});
	};


});
