$(document).ready(function(){

    /**
     * razeni
     *
     */
    $("form#frazeni select").change(function(){
        $("form#frazeni").submit();
    });
    
    $("form#frazeni input[type=checkbox]").click(function(){
        $("form#frazeni").submit();
    });    
    
    /**
     * formatovani ceny
     * @param String nStr
     */
    function addCommas(nStr){
        nStr += '';
        x = nStr.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ' ' + '$2');
        }
        return x1 + x2;
    }
    
    function roundNumber(num, dec){
        var result = Math.round(Math.round(num * Math.pow(10, dec + 1)) / Math.pow(10, 1)) / Math.pow(10, dec);
        return result;
    }
    
    /**
     *  prepocet a formatovani ceny pri zmene varianty
     *  zmena obrazku varianty
     */
    $("input[name=varianta]").click(function(){
    
        var $rpc_varianta = $base_path + "/core/exec/varianta-rpc.php";
        var $polozka_foto = $("img#polozka-foto");
        var $polozka_foto_url = $polozka_foto.attr("src");
        var $polozka_foto_link = $("a.polozka-foto-url");
        
        if ($(this).attr("checked")) {
            // zmena ceny
            var varianta = $(this).val();
            var cena = $("input#" + varianta + "").val();
            if (cena != '') {
				cena = cena.replace(",",".");
                num = parseFloat(cena);
                result = addCommas(roundNumber(num,$desetinna_mista));
                result = result + '&nbsp;Kč';
                $("#produkt-cena").html("" + result + "");
            }
            // zmena obrazku	
            var $foto_id = $polozka_foto.attr("id");
            var $foto_css = $polozka_foto.attr("class");
            
            $.ajax({
                type: "POST",
                url: $rpc_varianta,
                data: "produkt-id=" + varianta + "",
                dataType: "json",
                success: function(json){
                    // nahrada fotky
                    if (json['foto'] != '' && json['foto_tn'] != '') {
                        if (json['foto_tn'] != $polozka_foto_url) {
                            // vymena odkazu
                            $polozka_foto_link.each(function(){
                                $(this).attr("href", json['foto']);
                            });
                            // vymena nahledu
                            var img = new Image();
                            $(img).load(function(){
                                $(this).hide();
                                $(this).fadeIn();
                                $($polozka_foto).replaceWith(img);
                            }).attr('id', $foto_id).attr('class', $foto_css).attr('src', json['foto_tn']);
                        }
                    }
                }
            });
        }
    });
    
    /**
     * vymazani pole mnozstvi
     */
    $("input[name=produkt-mnozstvi]").focus(function(){
        $(this).val("");
    });
	
	$("div.filtr input[type=checkbox]").click(function(){
        $("form#fsekce").submit();
    });
	
	$("div.filtr input[type=image]").hide();
	$("div.filtr input[type=submit]").hide();
    
});

