var BASE_HREF = BASE_HREF || '../';

$(document).ready(function()
{
	$('a.lightbox').kfBox();
	$('a.lightbox-text').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: 'Hledaný výraz'});
	
	$('input.btn-order').each(function(){
		var $input = $(this);
		var $a = $('<a href="#" class="btn-order">' + $input.val() + '</a>');
		if($input.hasClass('btn-order-small')) $a.addClass('btn-order-small');
		$input.after($a);
		$input.hide();
		$a.bind('click', function(event){
			$input.trigger('click');
			event.preventDefault();
		});
	});

	
	// INTRO
	var homeTimer = null;
	var setHomeTimer = function(){
        homeTimer = clearTimeout(homeTimer)
    	homeTimer = setTimeout(function(){
    		var $active = $('.intro-tabs li.active').next();
    		if($active.size() == 0) $active = $('.intro-tabs li').eq(0);
    		$active.find('a').trigger('mousedown');
    		setHomeTimer();
    	}, 8000);
    }
    setHomeTimer();
	
	$(document).delegate('.theme-compugroup .intro-tabs a', 'mousedown', function(event){
		$e = $(event.currentTarget);		
		$($e.attr('href')).show().stop().fadeTo(500, 1);
		$e.closest('li').addClass('active');
		$e.parents('.intro-tabs').find('a').not($e).each(function()
		{ 
			$($(this).attr('href')).stop().fadeTo(500, 0, function(){ $(this).hide(); }); 
		}).closest('li').removeClass('active');
		setHomeTimer();
		return false;
	}).delegate('.intro-tabs a', 'click', function(event){ return false; });
	$('.intro-tabs a').eq(0).trigger('mousedown');

	
	/* Změna stránkování */
	$('.paging-box .itemsPerPage').bind('change', function(){
		$(this).parent().submit();
	});
    
	
	// Flash player
	$.fn.flashembed && $('.flash-player').each(function(i){
		var a = $(this).find('a');
 		$(this).flashembed(
          {
               src: BASE_HREF + 'player.swf',
               width: a.attr('class') == 'small' ? 280 : 610,
               height: a.attr('class') == 'small' ? 183 : 365,
               wmode: 'opaque',
               allowfullscreen: 'true',
               allowscriptaccess: 'always',
               id: 'flash-player' + i,
               name: 'flash-player' + i
          },
          {file: a.attr('href'), searchbar: 'false', autostart: a.attr('class') == 'small'? 'true' : 'false', image: $(this).find('img').attr('src')}
     	);
	});
});

$(window).bind('load', function()
{
	/* Srovnávání boxů na titulce */
	if($('body').hasClass('homepage'))
	{
		fontSizeListener('#footer p', function()
		{
			$('body').kfEqualizeColumns({ column: '.eq1' });
			$('body').kfEqualizeColumns({ column: '.eq3' });
			var $eq2 = $('.eq2').height('auto');
			var h1 = parseInt($eq2.eq(0).height());
			var h2 = parseInt($eq2.eq(1).height()) - 30;
			if(h1 < h2)  $eq2.eq(0).height(h2);
			else $eq2.eq(0).height('auto');
		});	
	}
	
	/* Srovnávání boxů na podstránce */
	if($('body').hasClass('subpage'))
	{
		fontSizeListener('#footer p', function()
		{
			var $col1content = $('.col-main .content-box .mid').eq(0).height('auto');
			var $cols = $('.col-main, .col-side');			
			var h1 = parseInt($cols.eq(0).height());
			var h2 = parseInt($cols.eq(1).height());
			if(h1 < h2)  $col1content.height(h2 - 40);
			else $col1content.height('auto');			
		});	
	}
	
	$('.product-list>ul').kfEqualizeColumns({ column: 'h3' });

});

$.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).eq(0);
	var h = 0;
	var interval = setInterval(function()
	{
		var currentH = parseInt($el.height());
		if(currentH != h){
			h = currentH;
			callback();
		}
	}, 200);
};

