var gallery = {
   debug: false,
   load: 1,

   setup: function() {
      if(gallery.load == 0)
         return;

      var divarr = gallery.container.getElementsByTagName('div');
      gallery.divarr = new Array();
      for(i=0; i<divarr.length; i++) {
         if(divarr[i].className == "optionimgbox" || divarr[i].className == "optionsmall" || divarr[i].className == "albumitem" || divarr[i].className == "albumitem2") {
            gallery.divarr[gallery.divarr.length] = divarr[i];
            var links = divarr[i].getElementsByTagName('a');
            gallery.debugit(links[0].href);
            links[0].href = "javascript:gallery.popup('"+links[0].href+"', '"+divarr[i].id+"')";
            divarr[i].style.display = "block";
            divarr[i].style.float = "left";
         }
      }

      gallery.loading = 0;
   },

   popup: function(img, id) {
      if(!gallery.loading)
         gallery.loading = 1;
      else
         return;

      if(gallery.imagecontainer)
         gallery.imagecontainer.style.visibility = "visible";

      gallery.imagecontainer = document.getElementById(id);

      gallery.closeit();

      gallery.debugit(img);
      gallery.img = new Image();
      gallery.img.src = img;

      var loadingbox = document.createElement('div');
      loadingbox.setAttribute("id", "loading");
      var loadingimg = document.createElement('img');
      loadingimg.setAttribute("src", (gallery.baseurl+gallery.imgdir)+"ajax-loader-black2.gif");
      loadingbox.appendChild(loadingimg);

      gallery.imagecontainer.appendChild(loadingbox);

      var imgs = gallery.imagecontainer.getElementsByTagName('img');
      gallery.thumbw = imgs[0].getAttribute('width');
      gallery.thumbh = imgs[0].getAttribute('height');
      gallery.debugit("thumb size: "+gallery.thumbw+" x "+gallery.thumbh);

      document.getElementById('loading').style.top = Math.round((gallery.thumbh-50)/2)+"px";
      document.getElementById('loading').style.left = Math.round((gallery.thumbw-50)/2)+"px";

      gallery.loadimg();
   },

   loadimg: function() {
      if(gallery.img.complete) {
         gallery.debugit("img size: "+gallery.img.width+" x "+gallery.img.height);
         var loading = document.getElementById("loading");
         gallery.imagecontainer.removeChild(loading);
         if(gallery.img.width == 0 || gallery.img.height == 0) {
            alert("Image not found");
            gallery.loading = 0;
            gallery.closeit();
            return;
         }
         gallery.popbox();
      }
      else {
         gallery.debugit("loading...");
         setTimeout("gallery.loadimg()", 1000);
      }
   },

   popbox: function() {
      gallery.debugit("popbox");

      var coors = gallery.getcoors(gallery.imagecontainer);
      gallery.debugit("pos of thumb: "+coors.x+" "+coors.y);

      gallery.debugit("clientwidth: "+document.documentElement.clientWidth);
      gallery.debugit("clientheight: "+document.documentElement.clientHeight);

      gallery.targetwidth = gallery.img.width + 20;
      gallery.targetheight = gallery.img.height + 30;

      gallery.debugit("targetsize: "+gallery.targetwidth+" x "+gallery.targetheight);

      if(gallery.targetwidth > document.documentElement.clientWidth) {
         gallery.targetheight = (gallery.targetheight * (document.documentElement.clientWidth - 40))/gallery.targetwidth;
         gallery.targetheight = Math.round(gallery.targetheight);
         gallery.targetwidth = document.documentElement.clientWidth - 40;
      }
      if(gallery.targetheight > document.documentElement.clientHeight) {
         gallery.targetwidth = (gallery.targetwidth * (document.documentElement.clientHeight - 60))/gallery.targetheight;
         gallery.targetwidth = Math.round(gallery.targetwidth);
         gallery.targetheight = document.documentElement.clientHeight - 60;
      }

      gallery.debugit("targetsize: "+gallery.targetwidth+" x "+gallery.targetheight);

      gallery.targetleft = Math.round((document.documentElement.clientWidth - gallery.targetwidth-20) / 2);

      var topspace = document.documentElement.scrollTop>document.body.scrollTop?document.documentElement.scrollTop:document.body.scrollTop;
      gallery.targettop = Math.round((document.documentElement.clientHeight - gallery.targetheight-20) / 2);
      gallery.targettop += topspace;

      gallery.debugit("target pos: "+gallery.targetleft+" "+gallery.targettop);

      gallery.imgpopup.style.top = coors.y+"px";
      gallery.imgpopup.style.left = coors.x+"px";
      gallery.imgpopup.style.width = gallery.thumbw+"px";
      gallery.imgpopup.style.height = gallery.thumbh+"px";
      gallery.imgpopup.style.display = "block";

      gallery.imagecontainer.style.visibility = "hidden";

      gallery.imgpopup.innerHTML = "<a href='javascript:gallery.closeit()'><img src='"+gallery.img.src+"' width='"+gallery.targetwidth+"' height='"+gallery.targetheight+"' alt='"+gallery.id+"' id='popupimg' style='border:0px' /></a><div id='popname'>"+gallery.imagecontainer.id.replace(/_/g,' ')+"</div><div id='popclose'><a href='javascript:gallery.closeit()'>close</a></div>";

      gallery.expand();

   },

   counter: 0,

   expand: function() {
      if(++gallery.counter >= 200)
         return;
      var currtoppos = parseInt(gallery.imgpopup.style.top);
      var currleftpos = parseInt(gallery.imgpopup.style.left);

      gallery.debugit("current: "+currleftpos+" "+currtoppos);

      var difference = Math.abs(gallery.targettop - currtoppos)/2;
      difference = Math.ceil(difference);
      gallery.debugit("difference: "+difference);
      if(gallery.targettop > currtoppos)
         var newtoppos = currtoppos + difference;
      else
         var newtoppos = currtoppos - difference;

      difference = Math.abs(gallery.targetleft - currleftpos)/2;
      difference = Math.ceil(difference);
      gallery.debugit("difference: "+difference);
      if(gallery.targetleft > currleftpos)
         var newleftpos = currleftpos + difference;
      else
         var newleftpos = currleftpos - difference;

      gallery.imgpopup.style.top = newtoppos+"px";
      gallery.imgpopup.style.left = newleftpos+"px";

      currtoppos = parseInt(gallery.imgpopup.style.top);
      currleftpos = parseInt(gallery.imgpopup.style.left);

      gallery.debugit("current before size: "+currleftpos+" "+currtoppos);



      var currwidth = parseInt(gallery.imgpopup.style.width);
      var currheight = parseInt(gallery.imgpopup.style.height);

      gallery.debugit("current size: "+currwidth+" "+currheight);

      difference = Math.round((gallery.targetwidth+20 - currwidth)/2);
      var newwidth = currwidth + difference;
      gallery.imgpopup.style.width = newwidth+"px";
      document.getElementById('popupimg').style.width = (newwidth-10)+"px";

      difference = Math.round((gallery.targetheight+30 - currheight)/2);
      var newheight = currheight + difference;
      gallery.imgpopup.style.height = newheight+"px";
      var newimgheight = ((newwidth-20)*gallery.targetheight)/gallery.targetwidth;
      document.getElementById('popupimg').style.height = newimgheight+"px";

      gallery.debugit("expand to: "+newwidth+" "+newheight);




      currtoppos = parseInt(gallery.imgpopup.style.top);
      currleftpos = parseInt(gallery.imgpopup.style.left);



      gallery.debugit("moved to: "+currleftpos+" "+currtoppos);

      if(currtoppos == gallery.targettop && currleftpos == gallery.targetleft && currwidth == (gallery.targetwidth+20) && currheight == (gallery.targetheight+30)) {
         gallery.loading = 0;
         return;
      }
      else
         setTimeout("gallery.expand()", 50);
   },

   closeit: function() {
      gallery.imgpopup.innerHTML = '';
      gallery.imgpopup.style.display = 'none';
      gallery.imagecontainer.style.visibility = "visible";
      document.getElementById('debugbox').innerHTML = '';
   },

   getcoors: function(ele) {
      var posX = 0;
      var posY = 0;

      while(ele != null){
         posX += ele.offsetLeft;
         posY += ele.offsetTop;
         ele = ele.offsetParent;
      }

      return {x:posX, y:posY};
   },





   getid: function(e) {
      var idname = "";
      if(!e)
         if(window.event)
            e = window.event;
         else
            return;
      if(document.layers)
         idname = e.target.id;
      else if(window.event && window.event.srcElement)
         idname = window.event.srcElement.id;
      else if(e && e.stopPropagation && !window.opera)
         if(e.target.nodeType == 1)
            idname = e.target.id;
         else
            if(e.target.parentNode != null)
               idname = e.target.parentNode.id;
      else if (window.opera && e)
         idname = e.target.id;

      return idname;
   },

   debugit: function(msg) {
      if(gallery.debug)
         document.getElementById("debugbox").innerHTML += msg+"<br />";
   }
};
