/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


/* GALLERY */

var myConn = new XHConn();
var image = new Image();
var src,evt,html;

function gallery(XML)
{
  html = XML.responseText;
  preload(src);
}

function getpicture(path,file,e){
  if(e){
    evt = e.target;
    if(!evt){evt = window.event;}
  }else{
    evt = '';
  }

  src = path + file;
  move('loading',evt,-60,22);
  show('loading');
  myConn.connect("/gallery.php", "POST","file=" + file +"&path=" + path,gallery);
}

function preload(src){

  image.src = src;
  image.onload = preloaded;
}

function preloaded(){

  move('pop',evt,-(image.width + 20) / 2,20);
  document.getElementById('pop').style.width = image.width + 22;
  show('pop',html);
  document.getElementById('img').src = image.src;
  hide('loading');
}

/* BASIC */


function show(id,data){
  var obj  = document.getElementById(id);
  if(obj){
    obj.style.display = "block";
    if(data) obj.innerHTML = data;
  }
}  

function hide(id){
  var obj  = document.getElementById(id);
  if(obj){
    obj.style.display = "none";
  }
}  

function move(id,e,left,top){
  var obj = document.getElementById(id);
  if(obj){
    obj.style.left = document.body.scrollLeft + (document.body.clientWidth) / 2 + left;
    if(e && e.height) obj.style.top = e.offsetTop + e.height + top;
    if(e && !e.height) obj.style.top = e.clientY  + document.body.scrollTop + top + 90;
  }
}

function load(id){
  var obj  = document.getElementById(id); 
  if(obj){
   show(id);
   obj.innerHTML = "<tr><td><strong> Loading... </strong></td></tr>";
  }
}
