
IBox = {
    set : function(targetId, rel)
    {
        if (typeof(rel)=='undefined' || rel=='')
            rel = 'ibox';
        images = new Array();
        rList = document.getElementsByTagName('A');
        for (var i = 0; i < rList.length; i++)
        {
            if (typeof(rList[i].rel)!='undefined' && rList[i].rel==rel)
            {
                rList[i].targetId = targetId;
                rList[i].virtId = targetId+'_virt';
                rList[i].onclick = IBox.go;
                index = images.length;
                images[index] = rList[i];
                images[index].index = index+1;
                //getElement('info').innerHTML += images.length+'-'+i+'<br>';
            }
        }
        if (e=getElement(targetId))
        {
            e.style.visibility = 'visible';
            e.style.position = 'relative';
            e.style.zIndex = 2;
            e.onclick = IBox.popupImg;
            if (c=getElement('iboxcontrol'))
            {
                aprev = document.createElement('A');
                aprev.href = '#';
                aprev.targetId = targetId;
                aprev.onclick = IBox.prev;
                anext = document.createElement('A');
                anext.href = '#';
                anext.targetId = targetId;
                anext.onclick = IBox.next;
                cblock = document.createElement('UL');
                
                cleft = document.createElement('LI');
                cleft.className = 'left';
                cleft.appendChild(aprev);
                
                cinfo = document.createElement('LI');
                cinfo.className = 'info';
                
                cright = document.createElement('LI');
                cright.className = 'right';
                cright.appendChild(anext);
                
                cblock.appendChild(cleft);
                cblock.appendChild(cinfo);
                cblock.appendChild(cright);
                try {
                    c.appendChild(cblock);
                    c.ibox = {
                        left : cleft,
                        info : cinfo,
                        right : cright
                    }
                } catch(e) {alert(e)}
            }
            e.ibox = {
                img : (e.getElementsByTagName('img').length) ? e.getElementsByTagName('img')[0] : false,
                virtId : targetId+'_virt',
                imageWidth : (e.getElementsByTagName('img').length) ? e.getElementsByTagName('img')[0].width : 0,
                imageHeight : (e.getElementsByTagName('img').length) ? e.getElementsByTagName('img')[0].height : 0,
                control : c,
                images : images,
                index : 1
            }
            if (e.ibox.control)
                e.ibox.control.ibox.info.innerHTML = e.ibox.index+' / '+e.ibox.images.length;
            //href = URL+'foto.php?url='+e.href+'&title='+((typeof(e.ibox.img.title)!='undefined') ? e.ibox.img.title : '');
            if (typeof(e.parentNode)!='undefined')
            {
                a = document.createElement('A');
                a.id = targetId+'_virt';
                a.style.zIndex = 1;
                a.style.top = -e.offsetHeight+'px';
                a.style.marginTop = 0;
                //a.style.marginLeft = 0;
                //a.style.visibility = 'hidden';
                
                virt = new Image();
                virt.onload = IBox.onLoad;
                virt.targetId = targetId;
                virt.parentId = targetId+'_virt';
                a.appendChild(virt);
                a.ibox = {
                    img : virt,
                    virtId : targetId+'_virt',
                    status : 0
                }
                e.parentNode.appendChild(a);
            }
        }
    },
    go : function(e)
    {
        if ((virt=getElement(this.virtId)) && virt.ibox.status<2)
        {
            if (e=getElement(this.targetId))
            {
                e.ibox.index = (typeof(this.index)!='undefined') ? this.index : 1;
                if (e.ibox.control)
                    e.ibox.control.ibox.info.innerHTML = e.ibox.index+' / '+e.ibox.images.length;
            }
            if (l=getElement('iboxloading'))
            {
                l.src = l.src;
                l.style.visibility = 'visible';
            }
            virt.ibox.img.removeAttribute('width');
            virt.ibox.img.removeAttribute('height');
            virt.ibox.img.title = (this.getElementsByTagName('img').length) ? this.getElementsByTagName('img')[0].title : false,
            virt.ibox.img.alt = (this.getElementsByTagName('img').length) ? this.getElementsByTagName('img')[0].alt : false,
            virt.ibox.img.src = this.href;
            virt.ibox.status = 1;
        }
        return false;
    },
    onLoad : function()
    {
        res = trimRatio(this.width, this.height, this.parentNode.offsetWidth, this.parentNode.offsetHeight, true);
        try {
            xpos = Math.floor((this.parentNode.offsetWidth-res.width)/2);
            ypos = Math.floor((this.parentNode.offsetHeight-res.height)/2);
        } catch(e) {
        }
        if (virt=getElement(this.parentId))
        {
            virt.ibox.status = 2;
            virt.href = this.src;
            virt.ibox.realWidth = this.width;
            virt.ibox.realHeight = this.height;
        }
        this.style.marginTop = ypos+'px';
        this.style.marginLeft = xpos+'px';
        //getElement('info').innerHTML = this.nodeName+' . '+this.width+'x'+this.height+' =&gt; '+res.x+'x'+res.y+' | '+ypos+' | '+this.parentNode.offsetHeight;
        this.width = res.x;
        this.height = res.y;
        if (l=getElement('iboxloading'))
            l.style.visibility = 'hidden';
        IBoxBlends.go(this.targetId, 'blend', 1.0);
    },
    reset : function(targetId)
    {
        if (e=getElement(targetId))
        {
            if (virt=getElement(e.ibox.virtId))
            {
                e.onclick = IBox.popupImg;
                e.ibox.imageWidth = virt.ibox.realWidth;
                e.ibox.imageHeight = virt.ibox.realHeight;
                e.href = virt.href;
                e.ibox.img.width = virt.ibox.img.width;
                e.ibox.img.height = virt.ibox.img.height;
                e.ibox.img.title = virt.ibox.img.title;
                e.ibox.img.alt = virt.ibox.img.alt;
                e.ibox.img.style.marginTop = virt.ibox.img.style.marginTop;
                e.ibox.img.style.marginLeft = virt.ibox.img.style.marginLeft;
                e.ibox.img.src = virt.ibox.img.src;
                virt.ibox.status = 0;
                IBoxBlends.changeOpacity(e, 100);
            }
        }
    },
    prev : function()
    {
        if (typeof(this.targetId)!='undefined' && (e=getElement(this.targetId)) && e.ibox.index>0 && typeof(e.ibox.images[e.ibox.index-2])!='undefined')
        {
            if ((virt=getElement(e.ibox.virtId)) && virt.ibox.status<2)
            {
                if (l=getElement('iboxloading'))
                {
                    l.src = l.src;
                    l.style.visibility = 'visible';
                }
                e.ibox.index--;
                e.ibox.control.ibox.info.innerHTML = e.ibox.index+' / '+e.ibox.images.length;
                virt.ibox.img.removeAttribute('width');
                virt.ibox.img.removeAttribute('height');
                virt.ibox.img.title = (e.ibox.images[e.ibox.index-1].getElementsByTagName('img').length) ? e.ibox.images[e.ibox.index-1].getElementsByTagName('img')[0].title : false,
                virt.ibox.img.alt = (e.ibox.images[e.ibox.index-1].getElementsByTagName('img').length) ? e.ibox.images[e.ibox.index-1].getElementsByTagName('img')[0].alt : false,
                virt.ibox.img.src = e.ibox.images[e.ibox.index-1].href;
                virt.ibox.status = 1;
            }
        }
        return false;
    },
    next : function()
    {
        if (typeof(this.targetId)!='undefined' && (e=getElement(this.targetId)) && e.ibox.index<e.ibox.images.length && typeof(e.ibox.images[e.ibox.index])!='undefined')
        {
            if ((virt=getElement(e.ibox.virtId)) && virt.ibox.status<2)
            {
                if (l=getElement('iboxloading'))
                {
                    l.src = l.src;
                    l.style.visibility = 'visible';
                }
                e.ibox.index++;
                e.ibox.control.ibox.info.innerHTML = e.ibox.index+' / '+e.ibox.images.length;
                virt.ibox.img.removeAttribute('width');
                virt.ibox.img.removeAttribute('height');
                virt.ibox.img.title = (e.ibox.images[e.ibox.index-1].getElementsByTagName('img').length) ? e.ibox.images[e.ibox.index-1].getElementsByTagName('img')[0].title : false,
                virt.ibox.img.alt = (e.ibox.images[e.ibox.index-1].getElementsByTagName('img').length) ? e.ibox.images[e.ibox.index-1].getElementsByTagName('img')[0].alt : false,
                virt.ibox.img.src = e.ibox.images[e.ibox.index-1].href;
                virt.ibox.status = 1;
            }
        }
        return false;
    },
    popupImg : function()
    {
        imageX = (typeof(this.ibox.imageWidth)!='undefined') ? this.ibox.imageWidth+20 : 1024;
        imageY = (typeof(this.ibox.imageHeight)!='undefined') ? this.ibox.imageHeight+20 : 768;
        href = URL+'foto.php?url='+this.href+'&title='+((typeof(this.ibox.img.title)!='undefined') ? this.ibox.img.title : '');
        nw = window.open(href,'Foto','width='+imageX+', height='+imageY+', toolbar=0, scrollbars=yes, resizable');

        if ( typeof(nw) != 'undefined' )
            nw.focus();
        return false;
    }
}

IBoxBlends = {

    go : function(elToBlend, mode, speed) {
        if (!(e=getElement(elToBlend)))
            return false;
        
        if (!speed)
            speed = 2.0; // sekund
        switch (mode)
        {
            case 'blend':
                    this.blend_e2e(e, speed);
                break;
            default:
        }
    },
    
    blend_e2e : function(e, speed) {
        var timer = 0;
        var speed = Math.round(((speed)?(speed*1000):2000) / 100);
        for(i=100; i>=0; i--)
        {
            setTimeout("IBoxBlends.changeOpacity('" + e.id + "', " + i + ")",(timer * speed));
            timer++;
        }
        setTimeout("IBox.reset('" + e.id + "')",(timer * speed));
    },
    
    resetTab : function(eId) {
        if (e=getElement(eId))
        {
            e.style.zIndex = 0;
            e.style.visibility = 'visible';
            elblends.changeOpacity(e, 100);
        }
    },
    
    changeOpacity : function(element, opacity) {
        var object = (typeof(element)=='string') ? document.getElementById(element).style : element.style;
        //if (opacity>0)
            //object.visibility = 'visible';
        //else
            //object.visibility = 'hidden';
        object.opacity = (opacity / 100);
        object.MozOpacity = (opacity / 100);
        object.KhtmlOpacity = (opacity / 100);
        object.filter = "alpha(opacity=" + opacity + ")";
    },
    
    src2bg : function src2bg(src, id) {
    if ((img=getElement(src)) && (cont=getElement(id)))
        cont.style.backgroundImage = "url("+img.src+")";
    }

}

function trimRatio(x1, y1, x2, y2, over)
{
    if (typeof(over)==undefined)
        over = false;
    if (!x1 || !x2)
        return {x:false, y:false, width:false, height:false};
    
    if (x1>x2 || over)
    {
        X = x2;
        Y = (y1/x1)*x2;
        
        if (Y>y2 && !over)
        {
            Y = y2;
            X = (x1/y1)*y2;
        }
        else if (over && Y<y2)
        {
            Y = y2;
            X = (x1/y1)*y2;
        }
        if (X<x2 && over)
        {
            X = x2;
            y = (y1/x1)*X;
        }
    }
    else if (y1>y2)
    {
        Y = y2;
        X = (x1/y1)*y2;
    }
    else
    {
        X = x1;
        Y = y1;
    }
    X = Math.floor(X);
    Y = Math.floor(Y);
    return {
        x : X,
        y : Y,
        width : X,
        height : Y
    };
}

