function replyToSenderImage(id, file_name, file_ext, size, popis, mass) {

    if(typeof mass == 'undefined') mass = false;

    var sender = $('#dialog_image').attr('value');
	if (sender.substr(0,2)=='PB')
    {
      var psArea = document.getElementById(sender.substr(2));
      if(psArea.addPhoto('../../userfiles/fotografie/nahledy/' + file_name + '_' + size + '.' + file_ext, id))
      { 
        if(mass == false){
            $("#dialog_image").dialog('close');
        }
      }
    }
    else if (sender == 'tiny')
    {
        $("#dialog_image").dialog('close');

        //var frame = window.frames['MCE_MyFrame'];
        var frames = window.frames;
        for (var i = 0; i < frames.length; i++){
            if(frames[i].document.formcallback != undefined){
                frames[i].document.formcallback.src.value = 'userfiles/fotografie/normal/'+file_name+'.'+file_ext;
                frames[i].document.formcallback.alt.value = popis;
                frames[i].document.formcallback.title.value = file_name;
                frames[i].ImageDialog.showPreviewImage('userfiles/fotografie/normal/'+file_name+'.'+file_ext, 1);
            }
        }
        
    }
   
	return false;
}

function replyToSenderImageMass(tableName, column) {
    $('#table_'+tableName+' input[name='+column+']:checkbox').each(function() {
        if($(this).attr('checked')) {
            var foto_id = $(this).attr('rel');
            var foto_info = $('#table_'+tableName).find('.foto_info[rel='+foto_id+']').attr('title');
            foto_info = foto_info.split(":::");
            replyToSenderImage(foto_id, foto_info[0], foto_info[1], foto_info[2], foto_info[3], true);  
        }
    });
    $("#dialog_image").dialog('close');
}

function replyToSenderDokument(id, file, mass){

    if(typeof mass == 'undefined') mass = false;

    var sender = $('#dialog_dokument').attr('value');
    if (sender.substr(0,2) == 'DB')
    {
      var dbArea = document.getElementById(sender.substr(2));
      if(dbArea.addRow(file, id)) 
      { 
          if(mass == false){
              $("#dialog_dokument").dialog('close');
          }
      }
    }
    else 
    {
        document.getElementById(sender).value = id;
        document.getElementById(sender+'_text').value = file;
        $("#dialog_dokument").dialog('close');
    }
    return false;
}

function replyToSenderDocumentMass(tableName, column) {
    $('#table_'+tableName+' input[name='+column+']:checkbox').each(function() {
        if($(this).attr('checked')) {
            var file_id = $(this).attr('rel');
            var file_info = $('#table_'+tableName).find('.file_info[rel='+file_id+']').attr('title');
            replyToSenderDokument(file_id, file_info, true);  
        }
    });
    $("#dialog_dokument").dialog('close');
}

function replyToSenderBanner(id, file, mass){

    if(typeof mass == 'undefined') mass = false;

    var sender = $('#dialog_banner').attr('value');

    if (sender.substr(0,2) == 'DB')
    {
      var dbArea = document.getElementById(sender.substr(2));
      if(dbArea.addRow(file, id)) 
      { 
          if(mass == false){
              $("#dialog_banner").dialog('close');
          }
      }
    }
    else 
    {
        document.getElementById(sender).value = id;
        document.getElementById(sender+'_text').value = file;
        $("#dialog_banner").dialog('close');
    }

    return false;
}

function replyToSenderBannerMass(tableName, column) {
    $('#table_'+tableName+' input[name='+column+']:checkbox').each(function() {
        if($(this).attr('checked')) {
            var file_id = $(this).attr('rel');
            var file_info = $('#table_'+tableName).find('.file_info[rel='+file_id+']').attr('title');
            replyToSenderBanner(file_id, file_info, true);  
        }
    });
    $("#dialog_banner").dialog('close');
}

function replyToSenderVideo(id, file, url, width, height, youtube, mass){

    if(typeof mass == 'undefined') mass = false;

    var sender = $('#dialog_video').attr('value');

    if (sender.substr(0,2) == 'DB')
    {
      var dbArea = document.getElementById(sender.substr(2));
      if(dbArea.addRow(file, id)) 
      { 
          if(mass == false){
              $("#dialog_video").dialog('close');
          }
      }
    }
    else if (sender == 'tiny')
    {
        $("#dialog_video").dialog('close');
        //var frame = window.frames['MCE_MyFrame'];
        var frames = window.frames;
        for (var i = 0; i < frames.length; i++){
            if(frames[i].document.video_form_callback != undefined){
                if(youtube == 1){
                    frames[i].document.video_form_callback.src.value = url;
                    frames[i].document.video_form_callback.width.value = width;
                    frames[i].document.video_form_callback.height.value = height;
                } else {
                    frames[i].document.video_form_callback.src.value = '../../userfiles/videa/'+file;
                }
            }
        }
        
    }
    else 
    {
        document.getElementById(sender).value = id;
        document.getElementById(sender+'_text').value = file;
        $("#dialog_video").dialog('close');
    }

    return false;
}

function replyToSenderVideoMass(tableName, column) {
    $('#table_'+tableName+' input[name='+column+']:checkbox').each(function() {
        if($(this).attr('checked')) {
            var file_id = $(this).attr('rel');
            var file_info = $('#table_'+tableName).find('.file_info[rel='+file_id+']').attr('title');
            file_info = file_info.split(":::");
            replyToSenderVideo(file_id, file_info[0], file_info[1], file_info[2], file_info[3], file_info[4], true);  
        }
    });
    $("#dialog_video").dialog('close');
}

function isUserFriendlyChar(val) {
  // Backspace, Tab, Enter, Insert, and Delete
  if(val == 8 || val == 9 || val == 13 || val == 45 || val == 46)
    return true;

  // Ctrl, Alt, CapsLock, Home, End, and Arrows
  if((val > 16 && val < 21) || (val > 34 && val < 41))
    return true;

  // The rest
  return false;
}
function switchCodebook(cbId, typeId)
{
    cb_box = document.getElementById("type_box" + typeId);
        for (i=0; i<cb_box.childNodes.length; i++)
        {
            node = cb_box.childNodes[i];
            if (node.nodeName=="DIV" && node.className != 'form_item')
            {
                node.style.display = 'none';
            }
        }
    document.getElementById('cis' + cbId).style.display = 'block';
}

function selectAll(cbBox)
{
    cb_box = document.getElementById(cbBox);
  
      for (i=0; i<cb_box.getElementsByTagName('input').length; i++)
      {
         node = cb_box.getElementsByTagName('input')[i];

            node.checked = 'checked';
      }
}
function cancelAll(cbBox)
{
  cb_box = document.getElementById(cbBox);
  
      for (i=0; i<cb_box.getElementsByTagName('input').length; i++)
      {
         node = cb_box.getElementsByTagName('input')[i];
            node.checked = false;
      }
}
function showLoader(loader_id, element, location, size)
{
  if(typeof size == 'undefined') size = "large";
  var w, h, src;
  switch(size)
  {
    case "small":
      w = 16;
      h = 16;
      src = '../files/images/indicator_small_trans.gif';
    break;
    default:
      w = 32;
      h = 32;
      src = '../files/images/indicator_big_trans.gif';
    break;
  }
  
  var loader = document.getElementById(loader_id);    
  if(!loader)
  {            
    loader = document.createElement('img');
    loader.style.position = 'absolute';
    loader.style.display='none';
    loader.id=loader_id;
    loader.src = src;
    loader.width=w;
    loader.height=h;
    loader.style.zIndex = 100000000000;
    document.getElementsByTagName('body')[0].appendChild(loader);    
  }
  
  var pos = absolutePosition(element);
  if(typeof location == 'undefined') location = "center center";
  var loc = location.split(' ');
  if(loc[0] == 'outer')
  {
    if(loc[1] == 'left')
      loader.style.left = (pos.x - 1 - w) + 'px';
    else if(loc[1] == 'right')
      loader.style.left = (pos.x + element.clientWidth+1) + 'px';
    else
      loader.style.left = Math.round(pos.x + element.clientWidth/2 - w/2) + 'px';
    
    if(loc[2] == 'top')
      loader.style.top = (pos.y - 1 - w) + 'px';
    else if(loc[2] == 'bottom')
      loader.style.top = (pos.y + element.clientHeight+1+w) + 'px';
    else
      loader.style.top = Math.round(pos.y + element.clientHeight/2 - w/2) + 'px';
  }
  else
  {
    if(loc[0] == 'left')
      loader.style.left = (pos.x + 1) + 'px';
    else if(loc[0] == 'right')
      loader.style.left = (pos.x + element.clientWidth-1-w) + 'px';
    else
      loader.style.left = Math.round(pos.x + element.clientWidth/2 - w/2) + 'px';
    
    if(loc[1] == 'top')
      loader.style.top = (pos.y + 1) + 'px';
    else if(loc[1] == 'bottom')
      loader.style.top = (pos.y + element.clientHeight-1-w) + 'px';
    else
      loader.style.top = Math.round(pos.y + element.clientHeight/2 - w/2) + 'px';
  }
  
  loader.style.display = 'block';
}

function hideLoader(loader_id)
{  
  var loader = document.getElementById(loader_id);  
  if(loader)
  {
    loader.style.display='none';   
  }    
}

absolutePosition = function(el) {
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(el.tagName);
	if (is_div && el.scrollLeft)
		SL = el.scrollLeft;
	if (is_div && el.scrollTop)
		ST = el.scrollTop;
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) {
		var tmp = absolutePosition(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

// vrací true pro IE < 8
var _isIE = -1;
function isIE()
{
  if(_isIE == -1)
  {
    var userAgent=navigator.userAgent.toLowerCase();
    
    var re = /msie ([0-9]{1,}[\.0-9]{0,})/;
    var version = 100;
    var m = re.exec(userAgent); 
    if (m != null)
      version = parseFloat(m[1]);
      
    if(version < 8 && !window.opera)
    {
      _isIE = true;
      return true;
    }
    else
    {
      _isIE = false;
      return false;
    }
  }
  else
  {
    return _isIE;
  }
}

//vrací aktuální šířku a výšku pracovní plochy(bez menu)
//@menu   int   kolik procent zabírá menu
//@pocet_zalozek int   počet záložek na stránce
function page_size(menu, pocet_zalozek) {
 var myWidth = 0, myHeight = 0;

 var sirka_zalozky = 151;

  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  myWidth = myWidth * ((98 - menu) / 100);

  if(myWidth < (sirka_zalozky * pocet_zalozek) + 50){
      
      var possitive_width = ((myWidth - 20) / pocet_zalozek) - 10;

      $('#zahlavi').find('a.zalozka').each(function() {
          if(possitive_width < 109) {
              $(this).width(possitive_width);
          } else {
              $(this).width(109);
              $(this).css('background-image', 'url(../../octopus/files/web_images/zalozka_small.jpg)');
          }

          if($(this).text().length > 12) {
              $(this).text($(this).text().substring(0, 12) + '...');
          }
      });
  }

}

function pridat_jazyk(id_jazyk, id_page, autor) {
    var http_request = false;
    
    var request = "id_jazyk="+id_jazyk;
    request = request+"&id_page="+id_page;
    request = request+"&autor="+autor;
    
    if (window.XMLHttpRequest) { 
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { 
        try {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (eror) {
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    
    http_request.onreadystatechange = function() { 
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                window.location.href=http_request.responseText;
            } else {
                alert('Během vykonávání příkazu se vyskytla chyba.');
            }
        } 
    };

    http_request.open('POST', 'scripts/pridat_jazyk.php', true);
    http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http_request.send(request);
}

function pridat_jazyk_static(id_jazyk, id_page, autor) {
    var http_request = false;
    
    var request = "id_jazyk="+id_jazyk;
    request = request+"&id_page="+id_page;
    request = request+"&autor="+autor;
    
    if (window.XMLHttpRequest) { 
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { 
        try {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (eror) {
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    
    http_request.onreadystatechange = function() { 
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                window.location.href=http_request.responseText;
            } else {
                alert('Během vykonávání příkazu se vyskytla chyba.');
            }
        } 
    };

    http_request.open('POST', 'scripts/pridat_jazyk_static.php', true);
    http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http_request.send(request);
}

