var no_hide_image = false;
var last_width = '';
var last_height = '';
var Images = Array();

function show_image(sender_href, width, height){    
  if ((obj1 = document.getElementById('ImagePanel')) && (obj2 = document.getElementById('ImagePanelBG'))){
    last_width = width;
    last_height = height;    
    
    t = 1;
    
    // delete http://..../files/ from sender_href
    f = sender_href.indexOf('files/') >= 0 ? sender_href.indexOf('files/') : 0;
    sender_href = sender_href.substr(f, sender_href.length);

    onc_left = '';
    onc_right = ''; 
    image_title = '';
    
    for(var a=0; a<Images.length; a++){
      if (Images[a][0] != sender_href) {                        
        x = 'hide_image(); return show_image(\'' + Images[a][0] + '\', ' + Images[a][1] + ', ' + Images[a][2] + ');';
        if (t == 1) onc_left = x;
        else {
          onc_right = x;
          break;
        }        
      }
      else {
        t = 2;
        image_title = Images[a][3]; 
      }
    }
                         
    // navigation & caption                      
    obj1.style.display = 'block'; 
    obj1.style.width = (width + 20) + 'px';
    t = '<img src="'+sender_href+'" onclick="no_hide_image = false; hide_image();" width="'+width+'" height="'+height+'"><div class="navigator">';    
    if (onc_left != '') t += '<img src="/cms/images/left.jpg" width="20" height="20" alt="Predchádzajúci obrázok" align="left" onclick="'+onc_left+'"/>';
    if (onc_right != '') t += '<img src="/cms/images/right.jpg" width="20" height="20" alt="Ďalší obrázok" align="right" onclick="'+onc_right+'"/>';
    t += '<strong>'+image_title+'</strong>';        
    t += '</div>';    
    
    obj1.innerHTML = t;
    
    window_resize();         
    
    no_hide_image = true;
    
    return false;        
  }
  else window.open(sender_href, '_blank');    
}

function window_resize(){
  if (no_hide_image || ((last_width == '') && (last_height == ''))) return;
  
  if ((obj1 = document.getElementById('ImagePanel')) && (obj2 = document.getElementById('ImagePanelBG')) && (obj3 = document.getElementById('bottom_page'))){
    var objs = document.getElementsByTagName('object');
    for(a = 0; a < objs.length; a++ ){
      objs[a].style.visibility = 'hidden';
    }
    
    obj2.style.display = 'block';
    obj2.style.width = Math.max(document.documentElement.clientWidth, document.body.clientWidth) + 'px';
    obj2.style.height = Math.max(document.documentElement.clientHeight, obj3.offsetTop) + 0 + 'px';

    obj1.style.top = (window.pageYOffset != undefined ? window.pageYOffset + Math.floor((window.innerHeight - last_height) / 2) : document.body.scrollTop  + Math.floor((document.body.clientHeight - last_height - 20) / 2)) + 'px';
    obj1.style.left = (window.pageXOffset != undefined ? window.pageXOffset + Math.floor((window.innerWidth - last_width) / 2) : document.body.scrollLeft  + Math.floor((document.body.clientWidth - last_width - 20) / 2)) + 'px';

    if (parseInt(obj1.style.top) + last_height + 70 > obj3.offsetTop) obj1.style.top = (obj3.offsetTop - last_height - 50) + 'px';
    if (parseInt(obj1.style.top) < 0) obj1.style.top = '15px';          
  }
}

function show_eb(sender, iwidth, iheight){
  if ((obj1 = document.getElementById('effects_browser')) && (obj2 = document.getElementById('ImagePanelBG')) && (obj3 = document.getElementById('bottom_page'))){
    no_hide_image = true;
  
    width  = 800;
    height = 390;
      
    obj2.style.display = 'block';
    obj2.style.width = Math.max(document.documentElement.clientWidth, document.body.clientWidth) + 'px';
    obj2.style.height = Math.max(document.documentElement.clientHeight, obj3.offsetTop) + 0 + 'px';
    
    obj1.style.display = 'block';    
    obj1.style.top = (window.pageYOffset != undefined ? window.pageYOffset + Math.floor((window.innerHeight - height) / 2) : document.body.scrollTop  + Math.floor((document.body.clientHeight - height - 20) / 2)) + 'px';
    obj1.style.left = (window.pageXOffset != undefined ? window.pageXOffset + Math.floor((window.innerWidth - width) / 2) : document.body.scrollLeft  + Math.floor((document.body.clientWidth - width - 20) / 2)) + 'px';
    
    show_eb_image(sender.href, sender.firstChild.alt, iwidth, iheight);
  }
  
  return false;
}

function show_eb_image(src, alt, width, height){
  if ((obj1 = document.getElementById('effects_browser_image'))){
    $('#eb_title').html(alt);
    
    obj1.src = src;
    obj1.width = width;
    obj1.height = height;
    obj1.style.marginLeft = ((530 - 265 - width) / 2) + 'px';
    obj1.style.marginTop = ((440 - height) / 2) + 'px';
  }
  
  return false;
}

function hide_eb(){
  if ((obj1 = document.getElementById('effects_browser')) && (obj2 = document.getElementById('ImagePanelBG'))){
    obj1.style.display = 'none';
    obj2.style.display = 'none';
    
    no_hide_image = false;
  }
  
  return false;
}

function hide_image(){ 
  if (no_hide_image) {    
    no_hide_image = false;
    
    return;
  } 
  
  if ((obj1 = document.getElementById('ImagePanel')) && (obj2 = document.getElementById('ImagePanelBG'))){
    last_width = '';
    last_height = '';
    obj1.style.display = 'none';  
    obj2.style.display = 'none';
    
    var objs = document.getElementsByTagName('object');
    for(a = 0; a < objs.length; a++ ){
      objs[a].style.visibility = 'visible';
    }
  }
  
   hide_eb();
}

window.onresize = window_resize;
