// Requires : ajax_lite.js
// Requires : functions_lite.js
// Requires : JS var -> vLng

document.write('<div id="level1_over" style="position:absolute;z-index:98;background-color:#000000;visibility:hidden;opacity:.80;-moz-opacity: 0.5;filter: alpha(opacity=80);left:0px;top:0px;"></div>');
document.write('<div id="gallery" style="position:absolute;z-index:99;background-color:#FFFFFF;visibility:hidden;padding:15px"></div><div id="galtemp" style="position:absolute;visibility:hidden"></div>');

last_img_x = 0;
last_img_y = 0;
last_tot_y = 0;
cur_img_x = 0;
cur_img_y = 0;
cur_tot_y = 0;
res_inc_top = 1;
res_inc_left = 1;
res_inc_width = 1;
res_inc_height = 1;
res_steps = 20;
res_speed = 20;
cur_left = 0;
cur_top = 0;
cur_width = 0;
cur_height = 0;
cur_step = 0;
control_offset = 28;
base_txt_offset = 28;
last_txt_offset = 0;
if(document.all) {
  xtra_x = 30;
  xtra_y = 30;
} else {
  xtra_x = 0;
  xtra_y = 0;
}

function popBdPhoto(vId,vCat,vNot,vChilds,vNoDiapo) {
  if(vNoDiapo!=1){vNoDiapo=0;}
  document.getElementById('level1_over').style.width = "100%";
  document.getElementById('level1_over').style.height = getDocHeight()+"px";
  document.getElementById('level1_over').style.visibility = "visible";
  centerElement("gallery",0,0);
  getPhp("../../cgi/gallery.php","gallery","id="+vId+"&lng="+vLng+"&cat="+vCat+"&not="+vNot+"&childs="+vChilds+"&nodiapo="+vNoDiapo+"&firstpic="+vId+"&curcnt=1","get","placeGallery();");
  document.getElementById('gallery').style.visibility = "visible";
}

function placeGallery() {
  var img_x = parseInt(document.getElementById("photo_x").value);
  var img_y = parseInt(document.getElementById("photo_y").value);
  document.getElementById('gallery').style.width = img_x+xtra_x+"px";
  txt_offset = document.getElementById("gal_txt").offsetHeight;
  if(txt_offset < base_txt_offset) {
    txt_offset = base_txt_offset;
  }
  document.getElementById('gallery').style.height = img_y+control_offset+txt_offset+xtra_y+"px";
  centerElement("gallery",(img_x/2),((img_y+control_offset+txt_offset)/2));
}

function initGalResize() {
  cur_step = 0;
  txt_offset = document.getElementById("gal_txt").offsetHeight;
  if(txt_offset < base_txt_offset) {
    txt_offset = base_txt_offset;
  }
  cur_img_x = document.getElementById("photo_x").value;
  cur_img_y = document.getElementById("photo_y").value;
  cur_left = parseInt(document.getElementById('gallery').style.left.replace("px",""));
  cur_top = parseInt(document.getElementById('gallery').style.top.replace("px",""));
  cur_width = parseInt(last_img_x)+xtra_x;
  cur_height = parseInt(last_img_y)+control_offset+last_txt_offset+xtra_y;
  offset_x = parseInt(last_img_x)-parseInt(cur_img_x);
  offset_y = (parseInt(last_img_y)+last_txt_offset)-(parseInt(cur_img_y)+txt_offset);
  if(offset_x==0) {
    diff_x = 0;
  } else {
    diff_x = offset_x/2/res_steps;
  }
  if(offset_y==0) {
    diff_y = 0;
  } else {
    diff_y = offset_y/2/res_steps;
  }
  if(diff_x>0) {
    res_inc_left = diff_x;
    res_inc_width = diff_x-(diff_x*3);
  } else {
    res_inc_left = diff_x;
    res_inc_width = diff_x-(diff_x*3);
  }
  if(diff_y<0) {
    res_inc_top = diff_y;
    res_inc_height = diff_y-(diff_y*3);
  } else {
    res_inc_top = diff_y;
    res_inc_height = diff_y-(diff_y*3);
  }
  //alert(res_inc_left+" "+res_inc_width+" "+res_inc_top+" "+res_inc_height+" "+cur_width+" "+cur_height+" "+last_img_x+" "+cur_img_x+" "+last_img_y+" "+cur_img_y+" "+last_txt_offset+" "+txt_offset);
  window.setTimeout("loopGalResize()",res_speed);
}

function loopGalResize() {
  //alert(cur_left+" "+cur_top+" "+cur_width+" "+cur_height);
  cur_left += res_inc_left;
  cur_top += res_inc_top;
  cur_width += res_inc_width;
  cur_height += res_inc_height;
  document.getElementById('gallery').style.left = Math.ceil(cur_left)+"px";
  document.getElementById('gallery').style.top = Math.ceil(cur_top)+"px";
  document.getElementById('gallery').style.width = Math.ceil(cur_width)+"px";
  document.getElementById('gallery').style.height = Math.ceil(cur_height)+"px";
  cur_step++;
  if(cur_step<res_steps) {
    window.setTimeout("loopGalResize()",res_speed);
  } else {
    endGalResize();
  }
}

function endGalResize() {
  document.getElementById("gallery").innerHTML = document.getElementById("galtemp").innerHTML;
  document.getElementById("galtemp").innerHTML = "";
}

function showPhoto(vId,vCat,vNot,vChilds,vNoDiapo,vFirst,vCurCnt) {
  if(vNoDiapo!=1){vNoDiapo=0;}
  last_img_x = document.getElementById("photo_x").value;
  last_img_y = document.getElementById("photo_y").value;
  last_txt_offset = txt_offset;
  document.getElementById("gallery").innerHTML = loading_msg;
  getPhp("../../cgi/gallery.php","galtemp","id="+vId+"&lng="+vLng+"&cat="+vCat+"&not="+vNot+"&childs="+vChilds+"&nodiapo="+vNoDiapo+"&firstpic="+vFirst+"&curcnt="+vCurCnt,"get","initGalResize();");
}

function closeGallery() {
  document.getElementById('gallery').style.visibility = "hidden";
  document.getElementById('gallery').innerHTML = "";
  document.getElementById('level1_over').style.visibility = "hidden";
}

function getDocHeight() {
  var D = document;
  return Math.max(
      Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
      Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
      Math.max(D.body.clientHeight, D.documentElement.clientHeight)
  );
}

function popBdVideo(vId,vAutoPlay) {
  document.getElementById('level1_over').style.width = "100%";
  document.getElementById('level1_over').style.height = getDocHeight()+"px";
  document.getElementById('level1_over').style.visibility = "visible";
  centerElement("gallery",0,0);
  getPhp("../../cgi/video.php","gallery","id="+vId+"&lng="+vLng+"&autoplay="+vAutoPlay,"get","placeVideo();");
  document.getElementById('gallery').style.visibility = "visible";
}

function placeVideo() {
  var img_x = parseInt(document.getElementById("video_x").value);
  var img_y = parseInt(document.getElementById("video_y").value);
  document.getElementById('gallery').style.width = img_x+"px";
  centerElement("gallery",(img_x/2),(img_y/2)+30);
}
