/*******************************************************************************
* show_image.js
* 
* ajaxove zobrazeni obrazku
* 
* pouzite funkce: send_xmlhttprequest()
* 
* autor: miroslav machalek
* datum: 2007-09-16
*******************************************************************************/

// inicializuje zobrazeni obrazku
function show_image_construct(margin_top, div_cover_id, div_content_id) {
  // attributes
  this.margin_top     = margin_top + 'px';
  this.div_cover      = document.getElementById(div_cover_id);
  this.div_content    = document.getElementById(div_content_id);
  
  if (document.documentElement.clientWidth) {
    this.scroll_width = document.documentElement.clientWidth;
  }
  else {
    this.scroll_width = document.body.clientWidth;
  }

  var scroll_height_body = 0;
  var scroll_height_de = 0;

  if (document.body.scrollHeight) {
    scroll_height_body = document.body.scrollHeight;
  }
  
  if (document.documentElement.scrollHeight) {
    scroll_height_de = document.documentElement.scrollHeight;
  }

  this.scroll_height = ((scroll_height_body > scroll_height_de) ? scroll_height_body : scroll_height_de) + 67;

  this.image_src      = '';

  // private methods
  this._gethttpobject        = _si_gethttpobject;
  this._hide_div             = _si_hide_div;
  this._hide_selects         = _si_hide_selects;
  this._cover_all            = _si_cover_all
  this._get_content          = _si_get_content;
  this._get_element_position = _si_get_element_position;

  // public methods
  this.show_image  = si_show_image;
  this.close_image = si_close_image;

  // skryjeme div prekryti
  this._hide_div(this.div_cover, 1);
  
  // skryjeme div obrazku
  this._hide_div(this.div_content, 1);
}

// vytvari http object
function _si_gethttpobject() {
  var xmlhttp = null;

  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest()
  }
  else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP')
  }
  
  return xmlhttp;
}

// zobrazuje nebo skryva div
function _si_hide_div(div, hide) {
  if (hide) {
    div.style.display = 'none';
  }
  else {
    div.style.display = '';
  }
}

// zobrazuje nebo skryva selecty
function _si_hide_selects(hide) {
  var selects = document.getElementsByTagName('select');

  for (i = 0; i < selects.length; i++) {
    if (hide) {
      selects[i].style.display = 'none';
    }
    else {
      selects[i].style.display = '';
    }
  }
}

// prekryva nebo odkryva vse, co potrebuje
function _si_cover_all(hide_all, sirka) {
  // mame vse prekryt
  if (hide_all) {
    // zobrazime prekryti
    this._hide_div(this.div_cover, 0);
    this.div_cover.style.height = this.scroll_height + 'px';
    this.div_cover.style.width  = sirka + 'px';

    // schovame selecty
    this._hide_selects(1);
  }
  else {
    // skryjeme div prekryti
    this._hide_div(this.div_cover, 1);
    
    // skryjeme div obrazku
    this._hide_div(this.div_content, 1);
    
    // zobrazime selecty
    this._hide_selects(0);
  }
}

// vraci pozici prvku
function _si_get_element_position(element) {
  var x = 0, y = 0;
  
  while (element != null) {  
    x += element.offsetLeft;
    y += element.offsetTop;
    
    element = element.offsetParent;
  }
  
  return {x:x, y:y};
} 

// zpracovava navrat po volani ajaxove funkce pri zobrazeni obrazku
function _si_get_content(xmlhttp, image) {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var image_response = xmlhttp.responseText;      

    parts = image_response.split('#');

    image.div_content.innerHTML = parts[1];

    if (parts[0] > image.scroll_width) {
      sirka = parts[0];
      margin_left = '-' + (image.scroll_width / 2) + 'px';
    }
    else {
      sirka = image.scroll_width;
      margin_left = '-' + (parts[0] / 2) + 'px';
    }

    // prekryjeme vse
    image._cover_all(1, sirka);

    image.div_content.style.top = image.margin_top;
    image.div_content.style.width = parts[0] + 'px';
    
    image.div_content.style.marginLeft = margin_left;

    // odrolujeme nahoru
    document.documentElement.scrollTop = 0;

    // zobrazime div obrazku
    image._hide_div(image.div_content, 0);
  }
}

// zobrazuje obrazek
function si_show_image(image_src) {
  // vytvorime si httpobject
  var xmlhttp = this._gethttpobject();
  
  var image = this;

  // nastavime obrazek
  this.image_src = image_src;

  xmlhttp.open('POST', 'ajax/show_image_get_content.php', true);
  
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

  xmlhttp.onreadystatechange = function () { _si_get_content(xmlhttp, image); };

  xmlhttp.send('image_src=' + image_src);

  return false;
}

// schovava obrazek
function si_close_image() {
  this._cover_all(0);
}
