/*******************************************************************************
* photogalery.js
* 
* ajaxova fotogalerie
* 
* pouzite funkce: send_xmlhttprequest()
*                 _gethttpobject()
* 
* autor: miroslav machalek
* datum: 2007-08-31
*******************************************************************************/

// inicializuje fotogalerii
function photogalery_construct(margin_top, div_cover_id, div_content_id, id_photogalery, server) {
  // attributes
  this.margin_top     = margin_top;
  this.div_cover      = document.getElementById(div_cover_id);
  this.div_content    = document.getElementById(div_content_id);
  this.server         = server;
  
  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);

  this.id_photogalery = id_photogalery;
  this.id_image       = 1;
  
  this.clientx        = '';
  this.clienty        = '';

  this.preview_type   = '';
  this.preview_width  = 0;
  this.preview_height = 0;
  this.nacten_obsah   = 0;

  // private methods
  this._gethttpobject        = _gethttpobject;
  this._hide_div             = _hide_div;
  this._hide_selects         = _hide_selects;
  this._cover_all            = _cover_all
  this._set_photogalery      = _set_photogalery;
  this._set_image            = _set_image;
  this._get_content          = _get_content;
  this._get_coordinates      = _get_coordinates;
  this._get_element_position = _get_element_position;
  this._move_preview         = _move_preview;

  // public methods
  this.show_photogalery   = show_photogalery;
  this.show_photogalery_from_string = show_photogalery_from_string;
  this.close_photogalery  = close_photogalery;
  this.show_preview       = show_preview;
  this.close_preview      = close_preview;

  // skryjeme div prekryti
  this._hide_div(this.div_cover, 1);
  
  // skryjeme div fotogalerie
  this._hide_div(this.div_content, 1);

  // divu fotogalerie nastavime, ze ma pri pohybu mysi ukladat jeji souradnice 
  this.div_content.onmousemove = _get_coordinates;
}

// zobrazuje nebo skryva div
function _hide_div(div, hide) {
  if (hide) {
    div.style.display = 'none';
  }
  else {
    div.style.display = '';
  }
}

// zobrazuje nebo skryva selecty
function _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 _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 fotogalerie
    this._hide_div(this.div_content, 1);
    
    // zobrazime selecty
    this._hide_selects(0);
  }
}

// nastavuje fotogalerii
function _set_photogalery(id_photogalery) {
  this.id_photogalery = id_photogalery; 
}

// nastavuje obrazek
function _set_image(id_image) {
  this.id_image = id_image; 
}

// uklada souradnice mysi
function _get_coordinates(e) {
  e = ((e) ? e : event);

  photogalery.clientx = e.clientX;
  photogalery.clienty = e.clientY;
}


// vraci pozici prvku
function _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 _get_content(xmlhttp, photogalery) {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var photogalery_response = xmlhttp.responseText;      

    parts = photogalery_response.split('#');

    photogalery.div_content.innerHTML = parts[2];

    if (parts[0] > photogalery.scroll_width) {
      sirka = parts[0];
      margin_left = '-' + (photogalery.scroll_width / 2) + 'px';
    }
    else {
      sirka = photogalery.scroll_width;
      margin_left = '-' + (parts[0] / 2) + 'px';
    }

    // prekryjeme vse
    photogalery._cover_all(1, sirka);

    if (parts[1] > photogalery.scroll_height) {
      photogalery.div_cover.style.height = (parseInt(parts[1]) + photogalery.margin_top + 70) + 'px';      
    }

    photogalery.div_content.style.top = photogalery.margin_top + 'px';
    photogalery.div_content.style.width = parts[0] + 'px';
    
    photogalery.div_content.style.marginLeft = margin_left;

    // odrolujeme nahoru
    if (document.documentElement.scrollTop) {
      document.documentElement.scrollTop = 0;
    }
    else {
      document.body.scrollTop = 0;
    }

    // zobrazime div fotogalerie
    photogalery._hide_div(photogalery.div_content, 0);
  }
}

// posouva nahled
function _move_preview(photogalery) {
  var div_content_position = photogalery._get_element_position(photogalery.div_content);
    
  // predchozi
  if (photogalery.preview_type == 1) {
    var top_position = (photogalery.clienty - div_content_position.y + 20) + 'px';
    var left_position = (photogalery.clientx - div_content_position.x + 15) + 'px';
  }
  // nasledujici
  else if (photogalery.preview_type == 2) {
    var top_position = (photogalery.clienty - div_content_position.y + 20) + 'px';
    var left_position = (photogalery.clientx - div_content_position.x - photogalery.preview_width - 15) + 'px';
  }
    
  document.getElementById('photogalery_preview').style.top = top_position;
  document.getElementById('photogalery_preview').style.left = left_position;

  // zobrazime div nahledu
  if (photogalery.nacten_obsah) {
    document.getElementById('photogalery_preview').style.display = '';
  }
}

// zobrazuje fotogalerii
function show_photogalery(id_image) {
  // vynulujeme informaci o tom, zda je nahled nacten
  this.nacten_obsah = 0;

  // vynulujeme informaci o typu nahledu
  this.preview_type = '';

  // vytvorime si httpobject
  var xmlhttp = _gethttpobject();
  
  var photogalery = this;

    // nastavime obrazek
  this._set_image(id_image);

  xmlhttp.open('POST', this.server + 'ajax/photogalery_get_content.php', true);
  
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

  xmlhttp.onreadystatechange = function () { _get_content(xmlhttp, photogalery); };

  xmlhttp.send('id_photogalery=' + this.id_photogalery + '&id_image=' + this.id_image);

  return false;
}

// zobrazuje fotogalerii z retezce obsahujiciho identifikator fotogalerie a obrazku
function show_photogalery_from_string(id_photogalery_id_image) {
  if (id_photogalery_id_image != 0) {
    parts = id_photogalery_id_image.split('_');
    photogalery._set_photogalery(parts[0]);
    photogalery.show_photogalery(parts[1]);
  }
  
  return false;
}

// schovava fotogalerii
function close_photogalery() {
  this._cover_all(0);
  
  return false;
}

// zpracovava navrat po volani ajaxove funkce pri zobrazeni nahledu
function _get_preview(xmlhttp, photogalery, link_type) {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var photogalery_response = xmlhttp.responseText;      

    parts = photogalery_response.split('#');

    photogalery.preview_width = parts[0];
    photogalery.preview_height = parts[1];

    document.getElementById('photogalery_preview').innerHTML = parts[1];

    // nastavime informaci o tom, zda je nahled nacten
    photogalery.nacten_obsah = 1;

    // posuneme nahled
    _move_preview(photogalery);
  }
}

// zobrazuje nahled fotky
function show_preview(type, id_image) {
  // pokud neni nahled zobrazen a zmenil se typ nahledu, tak jej nacteme
  if (document.getElementById('photogalery_preview').style.display != '' &&
      this.preview_type != type) {
    // nastavime typ nahledu
    this.preview_type = type;

    // vynulujeme informaci o tom, zda je nahled nacten
    this.nacten_obsah = 0;

    // vytvorime si httpobject
    var xmlhttp = _gethttpobject();
  
    var photogalery = this;
  
    xmlhttp.open('POST', this.server + 'ajax/photogalery_get_preview.php', true);
    
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  
    xmlhttp.onreadystatechange = function () { _get_preview(xmlhttp, photogalery); };
  
    xmlhttp.send('id_photogalery=' + this.id_photogalery + '&id_image=' + id_image);

    return false;
  }

  // posuneme nahled
  _move_preview(this);
}

// skryva nahled fotky
function close_preview() {
  // skryjeme div nahledu
  document.getElementById('photogalery_preview').style.display = 'none';
}