
/* zoomify start */

window.onload = Zoomify;
window.onresize = RePositionZoomifyPanel;   

function Zoomify()
{
    if(Zoom == 'True')
    { 
        var e = document.getElementById("content");
        
        if(e != '')
        {
            var aImages = e.getElementsByTagName("IMG");
            
            var aSrc = new Array(aImages.length);
            
            for(var i = 0; i < aImages.length; i++)
            {
                aSrc[i] = src(aImages[i].src);
                //alert(aImages[i].src);
            }
            
            if(aImages.length != 0) 
                GE.Web.Ajax.AjaxFunctions.GetZoomable(aSrc, location.href, GetZoomable_Callback);
        }
    }   
}

function GetZoomable_Callback(res)
{
    window.status = "Fetching Zoomifyable images...";
    
    if(typeof(res) == 'object')
    {
        ApplyZoomify(res.value, document.getElementById("content"));
    }
    
    window.status = "Done";
}

function ApplyZoomify(s, e)
{
    if(s)
    { 
        for(var i = 0; i < e.childNodes.length; i++)
        {
            if(e.childNodes[i].tagName == 'IMG') 
            {
                if(s.indexOf(src(e.childNodes[i].src)) != -1)
                {
                    AddZoomifyLink(e.childNodes[i]);
                }
            }
            
            if(e.childNodes[i].className != 'zoomlink')
            {
                ApplyZoomify(s, e.childNodes[i]);
            }
        }
    }
}

function AddZoomifyLink(e)
{ 
    if      ( e.src.indexOf("x?") >= 0 ) e.src = e.src + "&watermark";
    else if ( e.src.indexOf("?" ) >= 0 ) e.src = e.src.replace("?","x?") + "&watermark";
    else                                 e.src = e.src + "x?watermark";
    
    e.className = "zoomify";
    
    e.title = "Click to zoomify this image."
    
    e.onclick = function()
    { 
            OpenZoomifyPanel(e); return false;
    }
}

function OpenZoomifyPanel(e)
{
    /* creation */
    
    var panel = document.getElementById('panel');
    
    if(!panel)
    {
        panel = document.createElement("DIV");
        
        panel.id = 'panel';
        
        document.body.appendChild(panel);

        panel.style.position = 'absolute';
        
        panel.style.visible = 'hidden'; 
    }
        
    /* content adding */
    //alert(location.href);
    
    p = location.href.substring('http://'.length);
    p = p.substring(p.indexOf('/'));
    p = (p.indexOf('.aspx') != -1) ? p.substr(0, p.lastIndexOf('/')) : p;
    p = p.replace('/Home/', '/');
    n = name(e.src);

    var i = n;

    var is_chapter = p.indexOf("/library/radiology/chapter") == 0;
    //alert(p);

    // if image is textbook, remove sub directorys until we have "/library/radiology/chapterXX" (where the zoomify images are)
    if ( is_chapter )
    {
        var pp,ppi;
        for ( ppi=0,pp=0; ppi<3 && pp>=0; pp=p.indexOf('/',pp+1),++ppi )
        {
            ;
        }
        //alert(p);
        //alert(pp);
        if(pp >= 0)
        {
            p = p.substr(0,pp);
        }
    }

    //alert(p);
    //alert(n);
    //alert(i);
    if ( is_chapter /*i.substring(0,4) == "nic_"*/ )
    {
        i = "_inversed/" + i;
    }
    //alert(p+"/"+n);
    
    panel.innerHTML = "<div id='zoomifypanel'><a href='#' onclick='CloseZoomifyPanel();return false'>Close window</a><br/>" + GetFlashString('/swf/viewer.swf', 380 ,335, 'imageSource=/zoomify/' + p + '/' + n + '&inverseSource=/zoomify/' + p + i) + "</div>"
    //alert(panel.innerHTML);
    
    var panel = document.getElementById('panel');
    var c = document.getElementById('content');
    
    if(panel && c)
    {
        //panel.style.top = (findPosY(e) - 20) + 'px';
        panel.style.top = (findPosY(e) - (465 - e.height)    ) + 'px';      
        
        //panel.style.left = (findPosX(c) + c.offsetWidth - 380) + 'px';
        panel.style.left = (findPosX(e) - (380 - e.width) ) + 'px';
    }
}

function RePositionZoomifyPanel()
{
        var panel = document.getElementById('panel');
        var c = document.getElementById('content');
        
        if(panel && c)
        {
            panel.style.left = (findPosX(c) + c.offsetWidth - 337) + 'px';
        }
}

function CloseZoomifyPanel()
{
  var panel = document.getElementById('panel');
  var parent = panel.parentNode;
  parent.removeChild(panel);
}   
