// JavaScript Document
function showInfoMessage(message, title) {
    var dialogDiv = document.createElement('div');
    var divID = Math.round(Math.random() * 1235);
    var dialogDivID = 'dialog_'+divID;
    dialogDiv.id = dialogDivID;
    dialogDiv.innerHTML = '<p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>'+message+'</p>';
    document.body.appendChild(dialogDiv);
    $("#"+dialogDivID).dialog({
        bgiframe: true,
        modal: true,
        title: title,
        buttons: {
                'Ok': function() {
                        $(this).dialog('close');
                }
        }
    });
}

function showOmluvyType(id) {
    $("#omluva_"+id).css("display", 'inline');
}

function hideOmluvyType(id) {
    $("#omluva_"+id).css("display", 'none');
}

function nl2br (str, is_xhtml) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli Þór
    // +   bugfixed by: Onno Marsman
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');
    // *     returns 1: 'Kevin<br />\nvan<br />\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '<br />\nOne<br />\nTwo<br />\n<br />\nThree<br />\n'

    breakTag = '<br />';
    if (typeof is_xhtml != 'undefined' && !is_xhtml) {
        breakTag = '<br>';
    }

    //return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
    return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag);
}

function bezdiak(txt) {
    var sdiak = "áäčďéěíĺľňóôöŕšťúůüýřžÁÄČĎÉĚÍĹĽŇÓÔÖŔŠŤÚŮÜÝŘŽ ";
    var bdiak = "aacdeeillnooorstuuuyrzAACDEEILLNOOORSTUUUYRZ.";
    var tx = '';
    for(p = 0; p < txt.length; p++) {
        if (sdiak.indexOf(txt.charAt(p)) != -1) {
          tx += bdiak.charAt(sdiak.indexOf(txt.charAt(p)));
        } else {
            tx += txt.charAt(p);
        }
    }
    return tx;
}

$(document).ready(function(){
    $(".login").fancylogin();
    
    if(document.getElementById('OnLineTest'))
    {
       onLineTestOnLoad();
    }
    
    if(document.getElementById('Profil'))
    {
       profileOnLoad();
    }

    if(document.getElementById('Posta'))
    {
       postaOnLoad();
    }

    
    if(document.getElementById('DiskuzeList'))
    {
       diskuzeListOnLoad(); 
    }
    
    if(document.getElementById('DiskuzePrispevky'))
    {
       diskuzePrispevkyOnLoad(); 
    }
    if(document.getElementById('materials'))
    {
       materialyFormOnLoad(); 
    }

    if(document.getElementById('new_login')) {
        $('#new_login').click(function() {          
          if( this.value == '' ) {
            this.value = bezdiak($('#new_name').val()).toLowerCase();
            if($('#new_surname').val() != '' && $('#new_surname').val() != 'undefined' && $('#new_surname').val() != null) {
               this.value += '.'+bezdiak($('#new_surname').val()).toLowerCase();
            }
           
            
           } 
        });
        
        $('#new_login').blur(function() {
            $.ajax({
                url: _SERVER_PATH+"inc/ajax/users.php",
                global: false,
                type: "POST",
                data: "action=checkLoginAvaibility&login="+this.value,
                dataType: "json",
                cache: "false",
                async: true,
                success: function(msg){
                    if(!msg.avaible) {
                        $('#new_login').val('');
                        alert('Tento login je již obsazený, zvolte jiný.\nPravděpodobně bude tento uživatel již v systému.');
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    // typically only one of textStatus or errorThrown
                    // will have info
                    alert(errorThrown +': '+textStatus);
                }
            });
            //this.value = bezdiak($('#new_name').val()).toLowerCase()+'.'+bezdiak($('#new_surname').val()).toLowerCase();
        }); 
        
        
    }

    //linky s atributem out budou odkazovat ven..
   $("a[rel='out']").click(function(){
      window.open($(this).attr("href"),'blank');
      return false;
   });

  
});



/**
 * Validace telefonu pomoci JQuery validatoru
 */ 
  $.validator.addMethod("phone", function(value) {  
    
    var reg_mobil1 = "^[1-9]{1}[0-9]{8}$";
    var reg_mobil2 = "^00[0-9]{12}$";
    var reg_mobil3 = "^[\+][0-9]{12}$";
    
    value = value.replace(/ /g,"");
    if(value.length){
    return ( value.match(reg_mobil1) || value.match(reg_mobil2) || value.match(reg_mobil3));
    }
    return true;
  }, "Telefon");


/**
 * Zobrazi pruhlednou vrstvu nad formularem ci jinym prvkem s loading obrazkem
 */
function showLoaderBox( boxElID, elID)
{
   $("#"+boxElID).append('<div id="'+elID+'"></div>');
    
    boxWidth  = $("#"+boxElID).width()+'px';
    boxHeight = $("#"+boxElID).height()+'px';
    
    $("#"+elID).css('position','absolute')
                                          .css('top','70px')
                                          .css('left','30px')
                                          .css('width', boxWidth)
                                          .css('height',boxHeight)
                                          .css('text-align','center')
                                          .css('line-height',boxHeight)
                                          .css('vertical-align','middle')
                                          .css('z-index','99')
                                          .css('background-color','#f2f2f2')
                                          .css('opacity',0.9);
                                                  
    $("#"+elID).html(_LOADING_IMAGE);
}

/**
 * Schova pruhlednou vrstvu nad formularem ci jinym prvkem s loading obrazkem
 */ 
function hideLoaderBox(elID){
  $("#"+elID).remove();
}  
        function Bind(h,o)
        {
            $(h + ' p.addnode').click(function () {
                o.kvaTree.AddNode(o,'node');
            });
            
            $(h + ' p.addleaf').click(function () {
                o.kvaTree.AddNode(o,'leaf');
            });
            
            $(h + ' p.edit').click(function () {
                o.kvaTree.EditNode(o);
            });
            
            $(h + ' a.materialy_delete_file').click(function () {
                o.kvaTree.DeleteNode(o);
            });
        }//Bind  

