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);
	});
}

jQuery.cookie = function (key, value, options) {
    
    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }
        
        value = String(value);
        
        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

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;
			});
		};
	})();
	
	$('#fake-thickbox').each(function(){
	   if($.cookie("fakeThickbox") != 'set'){
    	    $.cookie('fakeThickbox', 'set', { expires: 7 });
        	var $box = $(this).show();
        	var $overlay = $('#fake-thickbox-overlay').show();
        	$overlay.add($box.find('.close')).bind('click', function(){
        		$overlay.remove();
        		$box.remove();
        		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
		});
	};


});

