maggot.load("move.js");

function init_lightbox(lightboxParent,init_class) {
  init_class = init_class || 'lightbox';
  var lightbox_moving;
  var img_loader = maggot.html('img');
  var win_loader = maggot.html('img',{src:'/coding/inc/window-new.png',title:'Open In New Window'},{cursor:'pointer',display:'none',position:'absolute',zIndex:1001});
  var lightbox_img = maggot.html('img',{},{display:'none',border:'solid 5px #f1f1f1',position:'absolute',zIndex:1000,cursor:'move'});
  var lightbox_bg = maggot.html('div',{},{backgroundColor:'#000',backgroundImage:'url(/coding/inc/ajax-loader.gif)',backgroundRepeat:'no-repeat',backgroundPosition:'center',display:'none',position:'absolute',left:'0px',top:'0px',display:'none',zIndex:999,opacity:0.75,filter:'alpha(opacity=75)'});
  var img_desc = maggot.html('div',{},{color:'#000',background:'#fff',fontSize:'12px',display:'none',textAlign:'center',position:'absolute',zIndex:1000,padding:'5px'});
  document.body.appendChild(win_loader);
  document.body.appendChild(lightbox_bg);
  document.body.appendChild(lightbox_img);
  document.body.appendChild(img_desc);
  img_loader.onload = function(e) {
    var imgsize = compute_size(img_loader);
    if(imgsize[0] == 0 || imgsize[1] == 0) {
      setTimeout(img_loader.onload, 1000);
      return;
    }
    lightbox_img.style.top = document.documentElement.scrollTop + (document.documentElement.clientHeight - imgsize[1]) / 2 - 25 + 'px';
    lightbox_img.style.left = document.documentElement.scrollLeft + (document.documentElement.offsetWidth - imgsize[0]) / 2 - 5 + 'px';
    lightbox_img.width = imgsize[0];
    lightbox_img.height = imgsize[1];
    if(imgsize[0] < img_loader.width || imgsize[1] < img_loader.height) {
      win_loader.style.top = maggot.int(lightbox_img.style.top,0) + 10 + 'px';
      win_loader.style.left = maggot.int(lightbox_img.style.left,0) + 10 + 'px';
      win_loader.onclick = function(){
        window.open('/webgear/lib/image.htm?' + lightbox_img.src,'wndpreview','resizable,statusbar').focus();
        win_loader.style.display = lightbox_bg.style.display = lightbox_img.style.display = img_desc.style.display = 'none';
      }
      win_loader.style.display = 'block';
    }
    lightbox_bg.style.backgroundPosition = '-55px -55px';
    lightbox_img.style.display = 'block';
    img_loader.src = 'about:blank';
    if(img_desc.innerHTML) {
      img_desc.style.top = parseInt(lightbox_img.style.top) + imgsize[1] + 10 + 'px';
      img_desc.style.left = lightbox_img.style.left;
      img_desc.style.width = parseInt(lightbox_img.width) + 'px';
      img_desc.style.display = 'block';
    }
  }
  lightbox_bg.onclick = lightbox_img.onclick = function(e) {
    if(lightbox_moving) {
      lightbox_moving = false;
      return;
    }
    win_loader.style.display = lightbox_bg.style.display = lightbox_img.style.display = img_desc.style.display = 'none';
  }
  var mw, mh, titleText;
  function compute_size(img) {
    var w = img.width, h = img.height, _w = mw - 40, _h = mh - 60;
    if(w > _w	|| h > _h) {
	    if(h / w > _h	/	_w)
		    return [(_h	*	w) / h,	_h];
	    else
		    return [_w,	(_w	*	h) / w];
    }	else {
	    return [w, h];
    }
  }
  function lightbox(src) {
    lightbox_img.style.display = 'none';
    mw = document.documentElement.offsetWidth, mh = document.documentElement.clientHeight;
    lightbox_bg.innerHTML = src;
    lightbox_bg.style.left = '0px';
    lightbox_bg.style.top = '0px';
    lightbox_bg.style.width = document.documentElement.scrollWidth + 'px';
    lightbox_bg.style.height = document.documentElement.scrollHeight + 'px';
    lightbox_bg.style.backgroundPosition = '50% ' + (document.documentElement.scrollTop + (mh - 55) / 2) + 'px';
    lightbox_bg.style.display = 'block';
    img_loader.src = lightbox_img.src = src;
  }
  var arr = [lightbox_bg, lightbox_img, img_loader, win_loader];
  maggot('a',lightboxParent).map(function(el){
    if(maggot.findclass(el, init_class)) {
      arr[arr.length] = el;
      if(maggot('img',el).length)
        var altText = maggot('img',el)[0].alt || "";
      else
        var altText = el.title || el.innerHTML || "";
      maggot.attach(el,'onclick',function(e){
        img_desc.innerHTML = altText;
        lightbox(el.getAttribute("href"));
        maggot.event(e).stop();
        return false;
      });
    }
  });
  maggot.attach(lightbox_img,'onmousewheel',function(e){
    e = maggot.event(e);
    if(lightbox_img.style.display != 'none') {
      var nwidth = 0, nheight = 0, ratio = 0.2;
      if(e.wheelDelta > 0) {
        nwidth = parseInt(lightbox_img.width * ratio, 10);
        nheight = parseInt(lightbox_img.height * ratio, 10);
      } else {
        ratio *= -1;
        nwidth = parseInt(lightbox_img.width * ratio, 10);
        nheight = parseInt(lightbox_img.height * ratio, 10);
        if(Math.abs(nwidth) < 10 || Math.abs(nheight) < 10) return;
      }
      var oxy = [maggot.int(lightbox_img.style.left,0), maggot.int(lightbox_img.style.top,0)];
      var cxy = [oxy[0] + (lightbox_img.width / 2),oxy[1] + (lightbox_img.height / 2)];
      var xy = [e.pageX - cxy[0], e.pageY - cxy[1]];
      
      var nleft = parseInt(oxy[0] - nwidth / 2), ntop = parseInt(oxy[1] - nheight / 2);
      nleft -= parseInt(xy[0] * ratio), ntop -= parseInt(xy[1] * ratio);
      
      lightbox_img.width += nwidth;
      lightbox_img.height += nheight;
      lightbox_img.style.left = nleft + 'px';
      lightbox_img.style.top = ntop + 'px';
      
      img_desc.style.left = lightbox_img.style.left;
      img_desc.style.width = lightbox_img.width + "px";
      img_desc.style.top = parseInt(lightbox_img.style.top,10) + lightbox_img.height + 10 + 'px';
      if(win_loader.style.display != 'none') {
        win_loader.style.top = maggot.int(lightbox_img.style.top,0) + 10 + 'px';
        win_loader.style.left = maggot.int(lightbox_img.style.left,0) + 10 + 'px';
      }
      return e.stop();
    }
  });
  maggot.attach(document,'onkeydown',function(e){
    win_loader.style.display = lightbox_bg.style.display = lightbox_img.style.display = img_desc.style.display = 'none';
  });
  maggot.attach(window,'onbeforeunload',function(e){
    arr.map(function(elem){
      elem.onclick = elem.onload = null;
    });
  });
  maggot.attach(lightbox_img,'ondragstart',function(e){
    lightbox_moving = true;
    maggot.event(e).stop();
    maggot.move.capture(lightbox_img,win_loader,img_desc);
    return false;
  });
}
