var imgclose = "/web/jsp/img/imgclose.gif";
var maxImages = 16;

function findTopParent()
{
    var depth = 64;
    var p = window;

    while ((p.parent != p) && (p.parent != null) && (--depth > 0))
    {
        try
        {
          var l = p.parent.document.location;
        }
        catch (e)
        {
          break;
        }
        p = p.parent;
    }

    return p;
}


function getEventPos(ev)
{
    var posx = 0;
    var posy = 0;
    if (!ev) var e = window.event;
    
    if (!ev) return [0, 0];
    
    if (ev.pageX || ev.pageY)
    {
	    posx = ev.pageX;
	    posy = ev.pageY;
    }
    else if (ev.clientX || ev.clientY)
    {
	    posx = ev.clientX + document.body.scrollLeft;
	    posy = ev.clientY + document.body.scrollTop;
    }
    return [posx, posy];
}

function getWindowSize()
{
    var width = 0;
    var height = 0;
  
    if (typeof(window.innerWidth) == 'number')
    {
        width = window.innerWidth;
        height = window.innerHeight;
    }
    else if (window.document.body && typeof(window.document.body.clientWidth) == 'number')
    {
        width = window.document.body.clientWidth;  
        height = window.document.body.clientHeight;        
    }

    return [width, height];
}


if (document.all)
{
  xbGetElementById = 
  function (id, windowRef) 
  { 
    if (!windowRef) 
    {
      windowRef = window; 
    }
    var elm = windowRef.document.all[id]; 
    if (!elm) 
    {
      elm = null; 
    }
    return elm; 
  };
}
else if (document.getElementById)
{
  xbGetElementById = 
  function (id, windowRef) 
  { 
    if (!windowRef) 
    {
      windowRef = window; 
    }
    return windowRef.document.getElementById(id); 
  };
}
else 
{
  xbGetElementById = 
  function (id, windowRef) 
  { 
    return null; 
  };
}

var iwinCtrl = null;

function baseInit()
{
    iwinCtrl = new ImageWinCtrl();
}

function ImageWinCtrl()
{
    this.handles = new Array();
    this.urls = new Array();

    this.imgBin = xbGetElementById("iwincontainer");

    if (this.imgBin == null)
    {
        var el = document.createElement('span');
        el.setAttribute('id', 'iwincontainer');

        document.body.appendChild(el);
        
        this.imgBin = el;
    }
    
    
    this.activeWin = null;
    this.zIndex = 20;
    this.winCount = 0;
    
    var html = '';
    var i = 0;
    while (i < maxImages)
    {
        html = html + '<div class="iwinpop" id="iwin_'+i+'"></div>';
        i++;
    }
    
    this.imgBin.innerHTML = html;
}


ImageWinCtrl.prototype.createWin = function (url, pos)  {

    var id = this.winCount;
    
    if (this.urls[url] != null)
    {
        this.urls[url].show();
        return;
    }

    if (id == maxImages)
        alert("This script is configured for a maxium of " + maxImages + " images.\nPlease adjust maxImages in imgwin.js");


    var div = xbGetElementById("iwin_" + id);

    var iw = new ImageWin(div, id, url, pos);
    
    this.handles[id] = iw;
    this.urls[url] = iw;
    
    this.winCount++;

};

ImageWinCtrl.prototype.getZ = function ()  {
    return ++this.zIndex;
};

ImageWinCtrl.prototype.getWin = function (id)  {
    return this.handles[id];
};


function _mup(ev) { return mup(ev); } 
function _mmove(ev) { return mmove(ev); }

function mup (ev) {

    if (iwinCtrl.activeWin != null)
        iwinCtrl.activeWin.passivate();
        
    document.onmouseup = null;
    document.onmousemove = null;

    return false;
}

var evskip = 0;
function mmove (ev) {

    if ((++evskip % 3)!=0) return false;

    if (! ev) var ev = window.event;

    var iwin = iwinCtrl.activeWin;

    if (iwin == null)
        return false;

    iwin.didMove = 1;
    
    if (iwin.resizing == 1)
    {
        var evPos = getEventPos(ev);
        iwinresize(iwin.img, Math.max(evPos[0] - iwin.x, 20), Math.max(evPos[1] - iwin.y, 20));
    }
    else
    {
        iwin.x = iwin.x + ev.screenX - iwin.lastX;
        iwin.y = iwin.y + ev.screenY - iwin.lastY;

        iwin.css.left = iwin.x;
        iwin.css.top = iwin.y;

        iwin.lastX = ev.screenX;
        iwin.lastY = ev.screenY;
    }



    return false;
}

function mdown(ev) {

    if (! ev) var ev = window.event;

    this.iwin.activate(ev);
    
    document.onmousemove = _mmove;
    document.onmouseup = _mup;
    
    return false;
}

function iwinresize(i, neww, newh)
{
    var r = newh / neww;
    
    var h = 0;
    var w = 0;

    if (r < i.iwin.ratio)
    {
        h = newh;
        w = newh / i.iwin.ratio;
    }
    else
    {
        w = neww;
        h = neww * i.iwin.ratio;
    }
    
    i.iwin.css.width = Math.max(w, neww) + 2;
    i.iwin.css.height = Math.max(h, newh) + 20;
    
    i.width = w;
    i.height = h;
    i.iwin.width = w;
    i.iwin.height = h;
}


function iwinimgloaded(i)
{
    if (i.iwin.loaded == 1)
    {
        return;
    }
    else
    {
        i.iwin.loaded = 1;
    }
    
    var h = i.height;
    var w = i.width;
    
    i.iwin.ratio = h/w;
    
    var winSize = getWindowSize();

    if (w > (winSize[0] * 0.9))
    {
        w = winSize[0] * 0.9;
        h = i.iwin.ratio * w;
    }

    if (h > (winSize[1] * 0.9))
    {
        h = winSize[1] * 0.9;
        w = h / i.iwin.ratio;
    }

    i.width = w;
    i.height = h

    i.iwin.width = w;
    i.iwin.height = h;

    i.iwin.css.width = w + 4;
    
    i.iwin.x = Math.max(i.iwin.x - w/2, 10);
    i.iwin.y = Math.max(i.iwin.y - h/2, 10);
    
    i.iwin.css.left = i.iwin.x
    i.iwin.css.top = i.iwin.y;
    
    i.iwin.css.zIndex = iwinCtrl.getZ();
}

function ImageWin (div, id, iurl, pos) {

    this.url = iurl;
    
    this.div = div;
    this.css = div.style;

    this.div.iwin = this;

    this.x = pos[0];
    this.y = pos[1];
    this.imageId = id;
    this.divId = 'iwin' + id;
    this.imgId = 'iwinimg' + id;
    this.width = 0;
    this.height = 0;
    this.ratio = 0;

    this.loaded = 0;
    this.active = 0;
    this.didMove = 0;
    this.lastX = 0;
    this.lastY = 0;
    this.resizing = 0;
    
    var closehtml = '<div class="iwinclosebar"><img onClick="hideimg('+this.imageId+')" class="iwinclose" src="'+imgclose+'"></div>';
    var imghtml = '<img onClick="hideimg('+this.imageId+')" onLoad="iwinimgloaded(this)" id="'+this.imgId+'" class="iwinimg" src="'+ this.url +'">';

    this.div.innerHTML = closehtml + imghtml;

    this.img = xbGetElementById(this.imgId);
    this.img.iwin = this;

    this.help = xbGetElementById("help_" + this.imgId);

    this.css.left = this.x;
    this.css.top = this.y;
    this.css.zIndex = -50;
    
    this.div.onmousedown = mdown;

    this.css.visibility = "inherit";
}

ImageWin.prototype.activate = function (ev)
{
    iwinCtrl.activeWin = this;

    
    this.active = 1;
    this.didMove = 0;
    this.lastX = ev.screenX;
    this.lastY = ev.screenY;

    
    var maxx = this.x + this.width + 2;
    var maxy = this.y + this.height + 20;

    var evPos = getEventPos(ev);
    if ((evPos[0] - 10 < maxx) && (evPos[0] + 10 > maxx) &&
        (evPos[1] - 10 < maxy) && (evPos[1] + 12 > maxy))
    {
        this.resizing = 1;
    }

    this.css.zIndex = iwinCtrl.getZ();
};

ImageWin.prototype.passivate = function ()
{
    iwinCtrl.activeWin = null;
    this.active = 0;
    
    if (this.resizing == 1)
    {
        this.css.width = this.width + 2;
        this.css.height = this.height + 20;    
    }
    
    this.resizing = 0;
};

ImageWin.prototype.show = function ()
{
    this.css.visibility = "inherit";
    this.css.zIndex = iwinCtrl.getZ();
    
};

ImageWin.prototype.hide = function ()
{
    if (this.didMove)
    {
        this.didMove = 0;
        return;
    }

    this.css.visibility = "hidden";
};


function popimg(url, ev)
{
    if (iwinCtrl == null)
        iwinCtrl = new ImageWinCtrl();
    
    iwinCtrl.createWin(url, getEventPos(ev));
}

function hideimg(id)
{
    iwinCtrl.getWin(id).hide();
}

