STMA.Vis = {
    lightBoxResize: function() {
        var viewportwidth;
        var viewportheight;
    
        if (typeof window.innerWidth != 'undefined') {
            viewportwidth = window.innerWidth,
            viewportheight = window.innerHeight
        } else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
            viewportwidth = document.documentElement.clientWidth,
            viewportheight = document.documentElement.clientHeight
        } else {
            viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
            viewportheight = document.getElementsByTagName('body')[0].clientHeight
        }    
        maxW = 1000;
        maxH = 750;
        indent = 150;
        if (viewportheight < (maxH + indent) || viewportwidth < (maxW + indent)) {           
            rt = maxW / maxH;
            h = viewportheight - indent;
            w = viewportwidth - indent;
            r = w/h;
            if (r > rt) {
                newH = h;
                newW = Math.floor(h * rt);
            } else {
                newW = w;
                newH = Math.floor(w /rt);             
            }
            var a;
            var item;
            a = document.getElementsByTagName('a');
            for(i=0;i<a.length;i++) {
                item = a[i];
                if (item.rel.search('lightbox') != '-1') {
                    var str = item.href;
                    str = str.replace('width='+maxW+'&height='+maxH,'width='+newW+'&height='+newH);
                    item.href = str;
                }
            }
        } 
        window.onresize = function() {
            STMA.Vis.lightBoxResize();
        } 
    },
    changeThumb: function(i,v) {
        o = $g('varThumb_'+i+'_'+v)
        p = o.parentNode;
        if (o != p.set) {
            if ($g('varSizes_'+i) != null) {
                s = $g('varSizes_'+i);
                if (!s.set) {
                    s.set = s.firstChild;
                }
                s.set.className = 'hidden';
                n = $g('varSize_'+v);
                n.className = '';
                s.set = n;
            }
            if (!p.set) {
                p.set = p.firstChild;
            }
            p.set.className = 'hidden';
            o.className = '';
            p.set = o;            
        } else {
            return false;
        }

    }
}
STMA.Vis.ObjectPath = {
    // checking id
    expId: 0,
    // real id
    realId: 0,
    // the whole div container
    obj: null,
    // active link
    focus: null,
    // div selector
    cont: null,
    // ajax flag; cancel callback if false
    onHold: false,
    init: function(id) {
        this.obj = $g(id);
        
        var links = this.obj.getElementsByTagName('a');
        var scope = this;
        var i;
        for(i=0;i<links.length;i++) {
            var item = links[i];
            if (item.rel) {
                item.onmouseover = function() {
                    scope.show(this);
                }
            }
        }

    },
    show: function(item) {
        var scope = this;
        this.focus = item;
        this.expId++;
        item.onmouseout = function() {
            scope.hide();
        }
        this.onHold = true;
        STMA.AJAX.init('sortiment.nav', {
            onComplete: function(r) {
                scope.realId++;
                if (scope.realId == scope.expId) {
                    scope.append(r);
                }
            }
        }, 'rel='+item.rel);
    },
    hide: function() {
        this.onHold = false;
        if (this.cont != null) {
            if (this.cont.parentNode === this.obj) {
                this.obj.removeChild(this.cont);
            }
        }
        this.focus.style.paddingRight = 0;
    },
    append: function(r) {
        if (this.onHold) {
            var scope = this;
            var par = this.obj;
            var d = document.createElement('div');
            this.focus.onmouseout = null;
            this.cont = d;
            d.className = 'extendBox';
            
            d.id = 'extendBox';
            
            d.innerHTML = r;

            d.onmouseout = function(e){
                if (!e) var e = window.event;
                var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
                while (reltg.tagName != 'BODY'){
                    if (reltg.id == this.id){return;}
                    reltg = reltg.parentNode;
                }
                scope.hide();
            }

            // set the correct x position
            var parX = par.nodePosition()[1];
            var linkX = this.focus.nodePosition()[1];
            var diff = linkX - parX;
            d.style.marginLeft = diff - 5 + 'px';
            d.style.marginTop = -this.focus.offsetHeight - 5 + 'px';
            this.focus.width = 300;

            // insert the new element
            par.insertAfter(d, this.focus);

            // set the correct width
            var tempW = this.focus.offsetWidth;
            if (d.offsetWidth > tempW) {
                // stretch the link
                this.focus.style.paddingRight = (d.offsetWidth-tempW - 4) + 'px';
            } else {
                // stretch the div
                d.style.paddingRight = (tempW - d.offsetWidth - 4) + 'px';
            }
        }

    }
}
