/*************************************************
*
* (c)2009 domi, #colcode <domi@colcode.com>
*
* image helpers and gallery browser
* requires Gui.js
*
 ************************************************/

var Pic = new function()
{
   var _me  = this;
   var _lst = {};
   var _clk = {};
   
   this.gui = (typeof Gui != 'object')? false : Gui;
   
   this.makeClickable = function(cname)
   {
      var k = document.getElementsByTagName('img');
      
      if(typeof cname != 'string') cname = 'clk';
      
      for(var i in k)
      {
         var p = k[i];
         if(typeof p.src == 'undefined') continue;
         if(p.className.indexOf(cname) < 0) continue;
         if(typeof p.onclick == 'function') continue;
         
         p.clk = 'clk'+i;
         _clk[p.clk]     = new Image();
         _clk[p.clk].src = p.src.replace('.thb.','.');
         
         p.onclick = function(){ Pic.zoom(this); };
      }
   }
   
   this.zoom = function(img)
   {
      if(typeof _clk[img.clk] == 'undefined') return;
      
      var p = _clk[img.clk];
      
      if(p.width == 0 || p.height == 0) return;
      
      var tx = '<div class="ac">'+
               '<img src="'+ p.src +'" width="'+ p.width +'" height="'+ p.height +'" alt="" title="" />'+
               '</div>';
      
      var w   = (p.width < 200)? 210 : (p.width + 10);
      
      _me.gui.dialogOpen(tx, ' ', w, p.height + 30, 'c', 'c');
   }
   
   this.galleryShow = function(id)
   {
      var p, img;
      var gi = document.getElementById('_g_image');
      var gt = document.getElementById('_g_text');
      
      if(!gt || !gi || !id) return;
      
      if(typeof _lst[id] == 'undefined') return;
      
      p   = _lst[id];
      img = p.img;
      
      gt.innerHTML = p.descr;
      gi.innerHTML = '<img src="'+ p.img_path +'" class="g_img" />';
   }
   
   this.gallery = function(lst, name)
   {
      var buf, i, p, fp = false;
      var sw = 0;
      
      _lst = {};
      
      for(i in lst)
      {
         p = lst[i];
         
         var img = new Image();
         
         img.src = p.thb_path;
         
         _lst[p.id] = p;
         _lst[p.id].thb = img;
         
         if(!fp) fp = p.id;
         
         sw += p.thb_w;
         sw += 5;
      }
      
      buf = '<table border="0" cellspacing="0" cellpadding="0"><tr><td width="640" height="483" align="center" valign="middle" id="_g_image"></td></tr></table>'+
            '<div id="_g_text" style="width: 640px; height: 25px; line-height: 25px; font-weight: bold; color: #ddd; text-align: center;"></div>'+
            '<div style="width: 640px; height: 120px; overflow: auto;">'+
            '<div style="width: auto; height: 100px; white-space: nowrap;">';
      
      for(i in lst)
      {
         p = lst[i];
         
         var img = new Image();
         img.src = p.img_path;
         p.img = img;
         
         buf += '<img src="'+ p.thb_path +'" width="'+ p.thb_w +'" height="'+ p.thb_h +'" class="clk g_thb" onclick=\"Pic.galleryShow('+ p.id +');" />';
      }
      
      buf += '</div></div>';
      
      _me.gui.dialogOpen(buf, name, 650, 655, 'c', 'c', '#333');
      _me.galleryShow(fp);
   }
   
}();

/* eof */
