/*******************************************************************************
* photogalery.js
* 
* ajaxova fotogalerie
* 
* pouzite funkce: send_xmlhttprequest()
* 
* autor: miroslav machalek
* datum: 2007-08-31
*******************************************************************************/

// inicializuje fotogalerii
function photogalery_construct(margin_top, div_cover_id, div_content_id, id_photogalery) {
  // attributes
  this.margin_top     = margin_top;
  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);

  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.previw_is_load  = 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;
  this._save_rating          = _save_rating;
  this._save_comment         = _save_comment;
  this._get_add_form         = _get_add_form;
  this._get_control_add_form = _get_control_add_form;
  this._get_add_photogalery  = _get_add_photogalery;

  // 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;
  this.rate_image           = rate_image;
  this.add_comment          = add_comment;
  this.show_comments        = show_comments;
  this.show_add_form        = show_add_form;
  this.show_address_input   = show_address_input;
  this.control_add_form     = control_add_form;
  this.upload_images        = upload_images;
  this.change_description   = change_description;
  this.delete_image         = delete_image;

  // 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;
}

// vytvari http object
function _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 _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 (selects[i].id != 'rating' && selects[i].id != 'type_photogalery') {
      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.height = 'auto';
    
    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);

  if (document.documentElement.scrollTop) {
    var scroll_top = document.documentElement.scrollTop;
  }
  else {
    var scroll_top = document.body.scrollTop;
  }

  // predchozi
  if (photogalery.preview_type == 1) {
    var top_position = (photogalery.clienty - photogalery.preview_height + scroll_top - 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 - photogalery.preview_height + scroll_top - 20) + 'px';
    var left_position = (photogalery.clientx - div_content_position.x - photogalery.preview_width - 15) + 'px';
  }
  
  // zobrazime div nahledu
  if (photogalery.previw_is_load) {
    document.getElementById('photogalery_preview').style.top = top_position;
    document.getElementById('photogalery_preview').style.left = left_position;
  
    document.getElementById('photogalery_preview').style.display = '';
  }
}

// zobrazuje fotogalerii
function show_photogalery(id_image, show_comments, enquiry, order, number) {
  if (isNaN(parseInt(show_comments))) {
    show_comments = 0;
  }

  if (isNaN(parseInt(enquiry))) {
    enquiry = 0;
  }

  if (isNaN(parseInt(order))) {
    order = 0;
  }

  if (isNaN(parseInt(number))) {
    number = 0;
  }

  // vynulujeme informaci o tom, zda je nahled nacten
  this.previw_is_load = 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', '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 +
               '&show_comments=' + show_comments +
               '&enquiry=' + enquiry +
               '&order=' + order +
               '&number=' + number);

  return false;
}

// zobrazuje fotogalerii z retezce obsahujiciho identifikator fotogalerie a obrazku
function show_photogalery_from_string(id_photogalery_id_image, show_comments, enquiry, order, number) {
  if (id_photogalery_id_image != 0) { 
    parts = id_photogalery_id_image.split('_');
    photogalery._set_photogalery(parts[0]);
    photogalery.show_photogalery(parts[1], show_comments, enquiry, order, number);
  }

  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[2];

    // nastavime informaci o tom, zda je nahled nacten
    photogalery.previw_is_load = 1;

    // posuneme nahled
    _move_preview(photogalery);
  }
}

// zobrazuje nahled fotky
function show_preview(type, id_image, enquiry) {
  // 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.previw_is_load = 0;

    // vytvorime si httpobject
    var xmlhttp = _gethttpobject();
  
    var photogalery = this;
  
    xmlhttp.open('POST', '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 + '&enquiry=' + enquiry);

    return false;
  }

  // posuneme nahled
  _move_preview(this);
}

// skryva nahled fotky
function close_preview() {
  // skryjeme div nahledu
  document.getElementById('photogalery_preview').style.display = 'none';
}

// zpracovava navrat po volani ajaxove funkce pro hodnoceni obrazku
function _save_rating(xmlhttp) {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var rate_response = xmlhttp.responseText;

    // pokud se podarilo ulozit hodnoceni obrazku
    if (rate_response != '0') {
      document.getElementById('photogalery_stars').innerHTML = rate_response;
    }
  }
}

// zpracovava hodnoceni obrazku
function rate_image(id_image, rating, enquiry) {
  if (isNaN(parseInt(enquiry))) {
    enquiry = 0;
  }

  // vytvorime si httpobject
  var xmlhttp = _gethttpobject();

  xmlhttp.open('POST', 'ajax/photogalery_save_rating.php', true);
    
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

  xmlhttp.onreadystatechange = function () { _save_rating(xmlhttp); };
  
  xmlhttp.send('id_image=' + id_image + '&rating=' + rating + '&enquiry=' + enquiry);

  return false;
}

// zpracovava navrat po volani ajaxove funkce pro pridani komentare k obrazku
function _save_comment(xmlhttp) {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var comment_response = xmlhttp.responseText;

    // pokud se podarilo ulozit komentar
    if (comment_response != '0') {
      document.getElementById('photogalery_comments').innerHTML = comment_response;
    }
    
    document.getElementById('name').value = '';
    document.getElementById('comment').value = '';
  }
}

// zpracovava pridani komentare k obrazku
function add_comment(id_image, enquiry) {
  if (isNaN(parseInt(enquiry))) {
    enquiry = 0;
  }

  var name = document.getElementById('name');
  var comment = document.getElementById('comment');

  // pokud mame jmeno a komentar
  if (name.value && comment.value) {
    // vytvorime si httpobject
    var xmlhttp = _gethttpobject();

    xmlhttp.open('POST', 'ajax/photogalery_save_comment.php', true);

    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    xmlhttp.onreadystatechange = function () { _save_comment(xmlhttp); };

    xmlhttp.send('id_image=' + id_image + '&enquiry=' + enquiry +
                 '&name=' + name.value + '&comment=' + comment.value);
  }

  return false;
}

// zpracovava navrat po volani ajaxove funkce pro zobrazeni komentaru k obrazku
function _get_comments(xmlhttp) {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var comments_response = xmlhttp.responseText;

    // pokud se podarilo nacist komentare
    if (comments_response != '0') {
      document.getElementById('photogalery_comments').innerHTML = comments_response;
    }
  }
}

// zobrazuje komentare k obrazku na urcite strance
function show_comments(id_image, page, enquiry) {
  if (isNaN(parseInt(enquiry))) {
    enquiry = 0;
  }

  // vytvorime si httpobject
  var xmlhttp = _gethttpobject();

  xmlhttp.open('POST', 'ajax/photogalery_get_comments.php', true);

  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

  xmlhttp.onreadystatechange = function () { _get_comments(xmlhttp); };

  xmlhttp.send('id_image=' + id_image + '&page=' + page + '&enquiry=' + enquiry);

  return false;
}

// zpracovava navrat po volani ajaxove funkce pri zobrazeni formulare pro pridani fotogalerie
function _get_add_form(xmlhttp, photogalery) {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var photogalery_response = xmlhttp.responseText;      

    photogalery.div_content.innerHTML = photogalery_response;

    if (640 > photogalery.scroll_width) {
      sirka = 640;
      margin_left = '-' + (photogalery.scroll_width / 2) + 'px';
    }
    else {
      sirka = photogalery.scroll_width;
      margin_left = '-' + (640 / 2) + 'px';
    }

    // prekryjeme vse
    photogalery._cover_all(1, sirka);

    photogalery.div_content.style.top = photogalery.margin_top + 'px';
    photogalery.div_content.style.width = 640 + 'px';
    
    photogalery.div_content.style.marginLeft = margin_left;

    photogalery.div_content.style.height = '480px';

    // 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);
  }
}

// zobrazuje formular pro pridani fotogalerie
function show_add_form(step, enquiry) {
  if (isNaN(parseInt(enquiry))) {
    enquiry = 0;
  }

  if (step == 2) {
    var photogalery_code = document.getElementById('photogalery_code').value;
  }

  // vytvorime si httpobject
  var xmlhttp = _gethttpobject();

  var photogalery = this;

  xmlhttp.open('POST', 'ajax/photogalery_get_add_form.php', true);
  
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

  xmlhttp.onreadystatechange = function () { _get_add_form(xmlhttp, photogalery); };

  xmlhttp.send('step=' + step + '&enquiry=' + enquiry +
               ((step == 2) ? '&photogalery_code=' + photogalery_code : ''));

  return false;
}

// zobrazuje nebo skryva pole pro zadani odkazu na fotogalerii
function show_address_input(type_photogalery) {
  // fotogalerie typu odkaz
  if (type_photogalery == 2) {
    document.getElementById('address_input').style.display = 'inline';
  }
  else {
    document.getElementById('address_input').style.display = 'none';
  }
}

// zpracovava navrat po volani ajaxove funkce pri kontrole formulare pro pridani fotogalerie
function _get_control_add_form(xmlhttp, photogalery, step) {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var control_response = xmlhttp.responseText;      

    parts = control_response.split('#');

    if (step == 1) {
      // doslo k chybe
      if (parts[0] == 1) {
        document.getElementById('photogalery_error_messages').innerHTML = parts[1];
      }
      // vse probehlo v poradku
      else {
        photogalery.div_content.style.height = '480px';

        photogalery.div_content.innerHTML = parts[1];
      }
    }
    else if (step == 2) {
      photogalery.div_content.style.height = '480px';

      photogalery.div_content.innerHTML = parts[1];
    }
  }
}

// kontroluje formular pro pridani fotogalerie
function control_add_form(step, enquiry) {
  if (step == 1) {
    // vytvorime si httpobject
    var xmlhttp = _gethttpobject();
    
    var _step = step;
    
    var photogalery = this;
    
    xmlhttp.open('POST', 'ajax/photogalery_control_add_form.php', true);
    
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  
    xmlhttp.onreadystatechange = function () { _get_control_add_form(xmlhttp, photogalery, _step); };

    xmlhttp.send('step=' + step + '&enquiry=' + enquiry +
                 '&name=' + document.getElementById('name').value +
                 '&author=' + document.getElementById('photogalery_author').value +
                 '&type_photogalery=' + document.getElementById('type_photogalery').value +
                 '&address=' + document.getElementById('address').value);
  
    return false;
  }
  else if (step == 2) {
    // vytvorime si httpobject
    var xmlhttp = _gethttpobject();
    
    var _step = step;
    
    var photogalery = this;
    
    xmlhttp.open('POST', 'ajax/photogalery_control_add_form.php', true);
    
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  
    xmlhttp.onreadystatechange = function () { _get_control_add_form(xmlhttp, photogalery, _step); };
  
    xmlhttp.send('step=' + step);
  
    return false;
  }
}

// zpracovava navrat po volani ajaxove funkce pri zakladani fotogalerie
function _get_add_photogalery(xmlhttp) {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var photogalery_response = xmlhttp.responseText;      

    // fotogalerii se podarilo zalozit
    if (photogalery_response) {
      document.getElementById('photogalery_code').value = photogalery_response;

      // skryjeme tlacitko pro nahrati fotek
      document.getElementById('button_upload_images').style.display = 'none';

      var iframes = document.getElementsByTagName('iframe');

      for (i = 0; i < iframes.length; i++) {
        var iframe = document.getElementById(iframes[i].id);
        var iframe_document = iframe.contentWindow || iframe.contentDocument;

        if (iframe_document.document) {
          iframe_document = iframe_document.document;
        }

        iframe_document.getElementById('photogalery').value = photogalery_response;
        iframe_document.getElementById('photogalery_upload_form').submit();
      }
    }
  }
}

// nahrava fotky fotogalerie
function upload_images(enquiry) {
  // vytvorime si httpobject
  var xmlhttp = _gethttpobject();
  
  xmlhttp.open('POST', 'ajax/photogalery_add_photogalery.php', true);
  
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

  xmlhttp.onreadystatechange = function () { _get_add_photogalery(xmlhttp); };

  xmlhttp.send('enquiry=' + enquiry);

  return false;
}

// zpracovava navrat po volani ajaxove funkce pri zmene popiu fotky a funkce pri
// mazani fotky
function _get_change_informations(xmlhttp, image_code, type) {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var change_response = xmlhttp.responseText;      

    // zmena popisu fotky
    if (type == 1) {
    }
    // mazani fotky
    else if (type == 2) {
      document.getElementById('change_informations_' + image_code).style.display = 'none';
    }
  }
}

// meni popis fotky
function change_description(image_code, enquiry) {
  // vytvorime si httpobject
  var xmlhttp = _gethttpobject();

  var _image_code = image_code;

  xmlhttp.open('POST', 'ajax/photogalery_change_image.php', true);
  
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

  xmlhttp.onreadystatechange = function () { _get_change_informations(xmlhttp, _image_code, 1); };

  xmlhttp.send('image_code=' + image_code + '&enquiry=' + enquiry +
               '&description=' + document.getElementById('description_' + image_code).value +
               '&type=1');

  return false;

}

// maze fotku
function delete_image(image_code, enquiry) {
  // vytvorime si httpobject
  var xmlhttp = _gethttpobject();
  
  var _image_code = image_code;
  
  xmlhttp.open('POST', 'ajax/photogalery_change_image.php', true);
  
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

  xmlhttp.onreadystatechange = function () { _get_change_informations(xmlhttp, _image_code, 2); };

  xmlhttp.send('image_code=' + image_code + '&enquiry=' + enquiry + '&type=2');

  return false;

}
