// ie, opera, misto 1x1 obrazku, fix tabulek
function FixTableHeights()
	{
	if (($.browser.msie) || ($.browser.opera) || ($.browser.webkit))
		{
		$('.vyrobky_radek td.styl_vypisu>table.vypis_item, table.pridruzene table.pridruzene_item').each(function(){
			$(this).css('height',$(this).parent().innerHeight()+'px');
			})
	   	}
	}
	
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
	}

function number_format(number, decimals, dec_point, thousands_sep) {
    number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}
	
function prepoctiPriplatky(t) {
	t = $(t).parents('div.tlacitka_vypis:first');
	var cc = t.find('.priplatky .cena_celkem');
	if (cc.length==0)
		return;

	var main = t.find('input[class=mnozstvi]');
	var ks = parseFloat(main.val());
	if (! (ks>1))
		{ks = 1;}
	t.find('.priplatky input.priplatek.povinna').val(ks);

	var total = parseFloat(main.attr('cena'))*ks;

	t.find('.priplatky input.priplatek').each(function(){
		var p = $(this);
		var pks = parseFloat(p.val());
		if (! (pks>0))
			{pks = 0;}
	    total += (parseFloat(p.attr('cena'))*pks);
		})
		                                         
	cc.text(number_format(total,parseFloat(cc.attr('desetiny')),',',' ')+' '+cc.attr('mena'));
	}


$(function(){

	// tabulky
    $("td.menu, td.kosik").find('.menu_nadpis_horni:even').addClass("licha");
    $(".tabulka tbody tr:even").addClass("licha");

	// kotvy pri base href
    $("a[ href ^= '#' ]").each(function() {
        var href = window.location + $(this).attr("href").replace("/#.*/i","");
        $(this).attr("href",href);
		});

	// ie7, del prazdnych param. divu, hodi se i ostatnim prohlizecum
	$("#vyrobky_radek div[class^='plus_']:empty").remove();

	// rozevrit podle susenek filtry
	if ($.cookie('filtrovani')!='0')
		{
		$('#filtrovani>table').removeClass('hidden');
		$('#filtrovani_content').show();
		}

	// filtrovani - odstranit z URL nepouzite prvky
    $("#filtrovani form").submit(function(){
		$(this).find('select,input[type!=checkbox]').each(function(){
			if ($(this).val() == '')
				{$(this).removeAttr('name');}
			})
		});

	// filtrovani - od/do
    $("#filtrovani .slider").each(function(){
		var t = $(this);
		t.slider({ animate: true, min: parseFloat(t.attr('min')), max: parseFloat(t.attr('max')), range: true, values: [parseFloat(t.attr('low')), parseFloat(t.attr('high'))],
			slide: function(event, ui)
				{
				var tp = $(this).parent();
				if (ui.Amin == undefined)
					{ui.Amin = tp.find('.slider').slider("option", "min")}
				if (ui.Amax == undefined)
					{ui.Amax = tp.find('.slider').slider("option", "max")}

				if (ui.Akurz == undefined)
					{ui.Akurz = parseFloat($(this).attr('kurz'));}
				if (ui.Adesetiny == undefined)
					{ui.Adesetiny = parseFloat($(this).attr('desetiny'));}
				
				tp.find('.slidermin span').text(roundNumber((ui.values[0]/ui.Akurz),ui.Adesetiny));
				tp.find('.slidermax span').text(roundNumber((ui.values[1]/ui.Akurz),ui.Adesetiny));
				tp.find('.slidermin').toggle((ui.values[0] != ui.Amin));
				tp.find('.slidermax').toggle((ui.values[1] != ui.Amax));
				tp.find('.slidertxt').toggle((ui.values[0] != ui.Amin) || (ui.values[1] != ui.Amax));

				tp.find('input').val(  ((ui.values[0] != ui.Amin) || (ui.values[1] != ui.Amax))  ? ui.values[0]+"-"+ui.values[1] :"");
				}
			});
		})
		
	// prepocitat priplatky
	$('.vyrobky_radek table.priplatky input.priplatek:first').change();

	FixTableHeights();
	});

$(window).load(function () {
	FixTableHeights();
	});

function toggleFiltrovani()
	{
	if ($('#filtrovani>table.hidden').length>0)
		{
		$.cookie('filtrovani','1');
		$('#filtrovani>table').removeClass('hidden');
		$('#filtrovani_content').fadeIn(500);
		} else {
		$.cookie('filtrovani','0');
		$('#filtrovani>table').addClass('hidden');
		$('#filtrovani_content').fadeOut(300);
		}
	}

function PresmerujSelect(t) {
	window.location = $(t).val();
	return true;
	}
	
