function char2Diacritic(transDiacritic)
{
    var charDiacritic = "ÁČĎÉÍĹĽŇÓÔŔŘŠŤÚŮÝŽ";
    var numDiacritic = "ACDEILLNOORRSTUUYZ";
    var tmpDiacritic = "";
    var newDiacritic = "";
    transDiacritic = transDiacritic.toUpperCase();
    for(i=0;i<transDiacritic.length;i++)
    {
        if (charDiacritic.indexOf(transDiacritic.charAt(i))!=-1)
            tmpDiacritic += numDiacritic.charAt(charDiacritic.indexOf(transDiacritic.charAt(i)))+'|';
        else
            tmpDiacritic += transDiacritic.charAt(i);
    }
    return tmpDiacritic;
} 

var myTextExtraction = function(node)  
{  
    var text = node.innerHTML;
   
    if (typeof(node.attributes['rel']) != "undefined") {
        return node.attributes['rel'].value;
    }

    if (text.match(/^[0-9]+.*$/))
    {
        var result = text.replace(/[^0-9.,]/g, '').replace(/,/g, '.');
        return result;
    }
    else
    {
        return char2Diacritic(text);
    }
} 
 

$(document).ready(function() { 
/*    $.tablesorter.defaults.widgets = ['zebra']; 
    $("#flexgrid").tablesorter({textExtraction: myTextExtraction});
*/
    $("#column_selector").click(function() {
        var selector  = $("#column_selector");
        var pos   = selector.offset();
        var width = selector.width();
        
        $(".vs-context-menu").css({
            'left': pos.left + width - 170 + "px", 
            "top" : pos.top + "px"})

        $(".vs-context-menu").show();
    });

    $("#column_selector_bottom").click(function() {
        var grid  = $("#flexgrid");
        var pos   = grid.offset();
        var width = grid.width();
        
        $(".vs-context-menu").css({
            'left': pos.left + width - 170 + "px", 
            'top' : pos.top + grid.height() - $(this).height() + "px"})

       var grid  = $("#column_selector");
        var pos   = grid.offset();
        var width = grid.width();
        
        $(".vs-context-menu").css({
            'left': pos.left + width - 255 + "px", 
            "top" : pos.top + "px"})

        $(".vs-context-menu").show();
    });

    $("#column_selector_bottom").click(function() {
        var grid  = $("#flexgrid");
        var pos   = grid.offset();
        var width = grid.width();
        
	$(".vs-context-menu").css({
            'left': pos.left + width - 255 + "px", 
            'top' : pos.top + grid.height() - $(this).height() + "px"})

        $(".vs-context-menu").show();
    });
     
    $("input.check_all").change(function() {
        if ($(this).attr('checked')) {
            $(".column_check").attr('checked', "CHECKED");
        } else {
            $(".column_check").attr('checked', "");
        }

    })
    
    $(".column_check_disabled").change(function() {
        $(this).attr('checked', false);
        tb_show('Přihlašte se, prosím.', '#TB_inline?height=200&width=300&inlineId=login_please')
    });
    
/*    $(".xcolumn_check_disabled_pay").change(function() {
        $(this).attr('checked', false);
	tb_show('Pouze pro platící uživatele.', '#TB_inline?height=250&width=300&inlineId=pay_by_sms');
    });
*/

    $(".column_check").change(function() {
        var checked = $(this).attr('checked');
        
        // Zobrazovani sloupcu - pokud uz existuje (skryty), pak ho jen zobrazi, jinak nacte znova stranku 
        // s tabulkou vcetne noveho sloupce
        if (checked) {
            var cells = $("." + $(this).attr('id'));
            if (cells.length > 0)
            {
                cells.show();
                $.ajax({
                    type: "POST",
                    data: {'show': $(this).attr('id')},
                    url: "/products/updatelisting/"});
            } 
            else 
            {
                $("body").append("<div id='TB_load'><img src='/static/images/loadingAnimation.gif'/></div>");
                $('#TB_load').show();
                var urlcommand = "show";
                window.location.href = "?" + urlcommand + "=" + $(this).attr('id') + "#product_table_head"; 
            }
        
        // Skryvani se resi ciste javascriptem bez odesilani pozadavku na server
        } else {
            $("." + $(this).attr('id')).hide();
                $.ajax({
                    type: "POST",
                    data: {'hide': $(this).attr('id')},
                    url: "/products/updatelisting/"});
        }
        
        // $(".vs-context-menu").hide();
    });

    $("#close_menu").click(function(){ $(".vs-context-menu").hide();});

    $("#custom_rate").change(function(){ 
        if ($(this).attr('checked')) {
            $("#custom_rate_value").show();
        } else {
            $("#custom_rate_value").hide();
        }});

    $(".column_check_disabled_pay").fancybox({
	'modal': true,
	'content': "<form action='/calculator/15/' method='post'><input type='hidden' name='i_doba_spor' value='" + $("#id_i_doba_spor").val() + "'/><input type='hidden' name='i_prav_inv' value='" + $("#id_i_prav_inv").val() + "'/><input type='hidden' name='i_poc_vklad' value='" + $("#id_i_poc_vklad").val() + "'/><input type='hidden' name='i_portfolio' value='" + $("#id_i_portfolio").val() + "'/><p><b>Chcete-li zobrazit informace o všech poplatcích a další údaje, pošlete SMS ve tvaru <span style='text-decoration: underline'>BP</span> na číslo <span style='text-decoration: underline'>90206</span> a získáte přístup k veškerým informacím na 60 minut. Cena SMS je <span style='text-decoration: underline'>80,- Kč</span>. Poté vám přijde na mobil kód, který zadáte do políčka níže.</b><br/>Služba není k dispozici pro zákazníky operátora Telefonica O2.<br/>Technicky zajišťuje ERIKA a.s. (<a href='http://www.erika-as.cz'>www.erika-as.cz</a>)</p><p>Potvrzovací kód: <input type='text' name='sms_code'/> <input type='submit' value='Potvrdit'/> nebo <a href='#' onclick='$.fancybox.close(); return false;'>zrušit</a></p></form>",
	'transitionIn'	: 'elastic',
	'transitionOut'	: 'elastic',
	'titleShow': false,
	'onComplete': function() {$(this).attr('checked', false);}
        });
}); 


function expand(id) {
   var div  = $("#descr_" + id);
   var cdb  = $("#cdb_" + id);
   var link = $("#link_" + id);

   if (div.css('height') == "auto")
   {
        div.css('height', div.attr("rel") + "px");
        cdb.css('height', cdb.attr("rel") + "px");
        link.html('více');
        $("#index_center").css('height', cdb.height() + 'px');
   }
   else
   {
        div.css('height', "auto");
        cdb.css('height', "auto");
        link.html('sbalit');
        $("#index_center").css('height', cdb.height() + 'px');
   }
}


