$(document).ready( function() {

    //Switch prekladu
    var translateIt = true;
    //Je otevren translate box? pokud ano, neotvirej dalsi!
    var translateBox = false;
    //Pokud je preklad '' ukaz tyto znacky:
    var translationBlankString = '{}';
    //defaultni cesta EdgeFrame
    var EDGE_ROOT = '/admin';    
       
     
    showTranslationWindow = function () {            
        if (!translateBox) 
        {   
            translateBox = true;  
            var source =  $(this);          
            $("body").append('<div style="display: none; position: absolute; background-color: #4d4d4d; border: 1px solid #4d4d4d; width: 210px; height: 110px;" id="tBox">' +
                            '<div style="width:100%; text-align: right; padding-bottom: 10px;">'+
                            '<div id="tBoxSetup" style="float: left; padding-left: 8px; padding-top: 8px; text-align:left;">'+
                            '</div>'+
                            '<img id="tBoxClose" src="/admin/public/images/iko_user.gif" alt="x"></div>'+
                            '<div id="tBoxCont" style="text-align:center;">'+
                                '<input type="text" id="preklad" name="preklad" value="" class="medium" style="border-bottom: 1px dashed #555"/><br/>'+
                                '<input type="text" id="prekladInfo" name="info" value="" class="medium"/><br/><br/>'+
                                '<input type="submit" id="prekladOK" value="OK"/>'+ 
                            '</div>'+
                         '</div>');
                         
            var destinationBox = $('#tBox');
            var destinationContent = $('#tBoxCont');
            
            $('#tBoxClose').click(function() {
                $('span[class^="transl"]').unbind();
                $('span[class^="transl"]').hoverIntent( tConfig );
                 destinationBox.fadeOut('fast', function () {
                 $('#tBox').remove();     
                 translateBox = false;
                 });                                       
            });
            
            var localestr = source.attr('class');
            var locale = localestr.split('_');
            
            destinationBox.css({top: (source.offset().top + 20)+'px', left: (source.offset().left)+'px' });
            $('#preklad').val(source.html() == translationBlankString ? '' : source.html());      
            $('#prekladInfo').val(source.attr('alt'));      
            $('#tBoxSetup').html(locale[2]);   
            
            destinationBox.fadeIn('fast');
            
            $('#prekladOK').click(function(){               
                   var savedTranslation = '';                    
                   $.post('/admin/modules/system/edgeFrontTranslation.ajax.php', {
                                                preklad: $('#preklad').val(),
                                                prekladInfo: $('#prekladInfo').val(),
                                                setup: source.attr('class') }, 
                            function(savedTranslation) {
                               // alert(savedTranslation);      
                                   	var isError = savedTranslation.split(':'); 
                                    var foundError = false;  
                                    var errorMsg = '';
                                    if (isError.length > 0) {
                                       
                                        if (isError[0] == 'err') 
                                        {
                                            foundError = true;
                                            errorMsg = isError[1];                                            
                                        }                                        
                                    }                                             
                                    if (!foundError) 
                                    { 	                                  
                                        destinationContent.html('<span><br/>Uloženo</span>'); 
                                        if ( savedTranslation  == '') savedTranslation = translationBlankString;
                                        var changeAll = source.attr('class'); 
                                        $('span[class="'+changeAll+'"]').html(savedTranslation);                                                                                     
                                        setTimeout("$('#tBoxClose').click()", 1000);
                                    } else {
                                        destinationContent.html('<span style="color:red">Chyba:<br/>'+errorMsg+'</span>');                                    
                                        // alert('Nelze uložit');                   
                                        setTimeout("$('#tBoxClose').click()", 3000);
                                    }                                 
                            });
                    $('#tBoxSetup').html('');  
                    destinationBox.css({height: '100px'});                                         
                    destinationContent.html('<br/>Ukládám...');                                             
            });                       
        }     
    }    
    
    hideTranslationWindow =  function () {
       
    }
    
    var tConfig = {    
                sensitivity: 10,     
                interval: 200,     
                over: showTranslationWindow,     
                timeout: 500,    
                out: hideTranslationWindow    
            };
     
   //verze s klavesovou skratkou
   shortcut.add("Shift+X",function() {
          
      
    if (translateIt) {  
     
        $('span[class^="transl"]').css('border-bottom','1px dashed #cd0000'); 
        $('span[class^="transl"]').each( function (i,e) {    
                if ( $(this).html() == '') {
                        $(this).html(translationBlankString);
                    }               
            }); 
        $('span[class^="transl"]').hoverIntent( tConfig );        
        translateIt = false;
    } else {  
 
        $('span[class^="transl"]').unbind();   
        $('span[class^="transl"]').each( function (i,e) {    
                if ( $(this).html() == translationBlankString) {
                        $(this).html('');
                    }               
            });                                         
        $('span[class^="transl"]').css('border-bottom','medium none');
        translateIt = true;        
    }
   });
   
   //Verze bez ni 
   /*
   $('span[class^="transl"]').css('border-bottom','1px dashed #cd0000');                                           
        $('span[class^="transl"]').hoverIntent( tConfig );
   */
});