function myEvent(where, evt, handler, op)
{
    if (op == '+') {
        if (where.attachEvent) where.attachEvent(('on' + evt), handler)
        else if (where.addEventListener) where.addEventListener(evt, handler, false)
    } else {
        if (where.detachEvent) where.detachEvent(('on' + evt), handler)
        else if (where.removeEventListener) where.removeEventListener(evt, handler, false)
    }
}

function Browser() {
    var ua, s, i;
    window.isIE = false;
    window.isNS = false;
    window.version = null;
    ua = navigator.userAgent;
    s = 'MSIE';
    if ((i = ua.indexOf(s)) >= 0) {
        window.isIE = true;
        window.version = parseFloat(ua.substr(i + s.length));
        return;
    }
    s = 'Netscape6/';
    if ((i = ua.indexOf(s)) >= 0) {
        window.isNS = true;
        window.version = parseFloat(ua.substr(i + s.length));
        return;
    }
    s = 'Gecko';
    if ((i = ua.indexOf(s)) >= 0) {
        window.isNS = true;
        window.version = 6.1;
        return;
    }
    if (window.opera) { window.isIE = true; }
}

function HTMLElement(someInfo)
{
    var obj = null;
    if (typeof someInfo == 'object') {
        obj = someInfo;
    } else if (typeof someInfo == 'string') {
        obj = document.getElementById(someInfo);
    }
    if (obj == null) {
        alert('ListParser: input parameter is not an object and is not id of existing html element');
        return null;
    }
    
    obj.previous = {}; //for previous values of this element's properties
    obj.my = {};
    obj.my.tagName = obj.tagName.toLowerCase();
    
    //return boolean false if word doesn't exist inside of string
    //return string without word if this word existed inside
    //delimiter by default is ' '
    obj.cutWordFromString = function(word, str, delimiter) {
        if (typeof delimiter != 'string' || delimiter == '') {
            delimiter = ' ';
        }
        var list = str.split(delimiter);
        for (var i = 0; i < list.length; i++) {
            if (list[i] == word) {
                var newList = Array();
                newList = newList.concat(list.slice(0, i), list.slice((i + 1)));
                return newList.join(delimiter);
            }
        }
        return false;
    };
    
    obj.addClass = function(addClass) {
        var n = this.cutWordFromString(addClass, this.className);
        if (n === false) {
            this.className += ' ' + addClass;
            return true;
        } else {
            //this class already exists
            return false;
        }
    };
    
    obj.removeClass = function(removeClass) {
        var n = this.cutWordFromString(removeClass, this.className);
        if (n === false) {
            //this class wasn't exist
            return false;
        } else {
            //this class was existing and removed now
            this.className = n;
            return true;
        }
    };
    
    obj.changeVisibility = function() {
        if (typeof this.style.visibility == undefined) {
            this.style.visibility = 'hidden';
        } else if (this.style.visibility == 'hidden') {
            this.style.visibility = 'visible';
        } else {
            this.style.visibility = 'hidden';
        }
    };
    
    obj.changeDisplay = function(displayType) {
        if (typeof displayType != 'string' || displayType == '') {
            displayType = 'block'; //default value
        }

        if (typeof this.style.display == undefined) {
            this.style.display = 'none';
        } else if (this.style.display == 'none') {
            this.style.display = displayType;
        } else {
            this.style.display = 'none';
        }
    };
    
    return obj;
}

function opacity(obj)
{
    obj.opacityLevel = 100;
    var opaIE = function(level) {
        this.opacityLevel = level;
        this.style.filter = 'alpha(opacity=' + level.toString() + ')';
    };
    var opaDOM = function(level) {
        this.opacityLevel = level;
        level = level / 100;
        this.style.MozOpacity = level.toString();
        this.style.opacity = level.toString();
    };
    obj.setOpacity = function(level) {
        this.opacityLevel = level;
        level = level / 100;
        this.style.opacity = level.toString();
        this.style.MozOpacity = level.toString();
        this.style.filter = 'alpha(opacity=' + level.toString() + ')';
    };
    if ((window.isNS)||(window.opera)) { obj.setOpacity = opaDOM; }
    if (window.isIE && !window.opera) { obj.setOpacity = opaIE; }
    return obj;
}

/* psr animation  */
var psr = null;
function buildPSR()
{
    var psrBoxObj = document.getElementById('psr_box');
    if (psrBoxObj != null) {
        psr = {};

        var box = document.createElement('table');
        box.width = '100%';
        box.height = '13';
        box.cellSpacing = '0';
        box.cellPadding = '0';
        box.border = '0';
        var tr = box.appendChild(document.createElement('tbody')).appendChild(document.createElement('tr'));
        tr.valign = 'bottom';
        var td1 = tr.appendChild(document.createElement('td'));
        td1.width = '3';
        td1.align = 'right';
        psr.objLeft = td1.appendChild(document.createElement('img'));
        psr.objLeft.id = 'psr_left';
        psr.objLeft.src = '/images/0.gif';
        psr.objLeft.width = '3';
        psr.objLeft.height = '13';
        psr.objLeft.border = '0';
        var td2 = tr.appendChild(document.createElement('td'));
        psr.obj = td2.appendChild(document.createElement('img'));
        psr.obj.id = 'psr';
        psr.obj.src = '/images/0.gif';
        psr.obj.width = '100%';
        psr.obj.height = '13';
        psr.obj.style.width = '100%';
        psr.obj.border = '0';
        var td3 = tr.appendChild(document.createElement('td'));
        td3.width = '3';
        psr.objRight = td3.appendChild(document.createElement('img'));
        psr.objRight.id = 'psr_right';
        psr.objRight.src = '/images/0.gif';
        psr.objRight.width = '3';
        psr.objRight.height = '13';
        psr.objRight.border = '0';
        psrBoxObj.appendChild(box);
        
        var img11 = new Image(); img11.src = '/images/psr_bg.gif';
        psr.bgPassive = 'url(' + img11.src + ')';
        var img12 = new Image(); img12.src = '/images/psr_bg_active.gif';
        psr.bgActive = 'url(' + img12.src + ')';
        var img21 = new Image(); img21.src = '/images/psr_bg_left.gif';
        psr.bgLeftPassive = 'url(' + img21.src + ')';
        var img22 = new Image(); img22.src = '/images/psr_bg_left_active.gif';
        psr.bgLeftActive = 'url(' + img22.src + ')';
        var img31 = new Image(); img31.src = '/images/psr_bg_right.gif';
        psr.bgRightPassive = 'url(' + img31.src + ')';
        var img32 = new Image(); img32.src = '/images/psr_bg_right_active.gif';
        psr.bgRightActive = 'url(' + img32.src + ')';
        psr.bgPosXStart = -30;
        psr.bgPosXEnd = -12;
        psr.bgPosX = psr.bgPosXStart;
        psr.timer = null;
        psr.stepX = 1;
        psr.interval = 80;
        psr.stoped = true;
        psr.animate = function() {
            if (! this.stoped) {
                if (this.bgPosX >= this.bgPosXEnd) {
                    this.bgPosX = this.bgPosXStart;
                }
                this.bgPosX += this.stepX;
                this.obj.style.backgroundPosition = this.bgPosX.toString() + 'px center';
            }
            this.timer = setTimeout("psr.animate("+this.stepX.toString()+", "+this.interval.toString()+")", this.interval);
        };
        psr.setBg = function(mode) {
            if (mode == 'active') {
                psr.obj.style.backgroundImage = psr.bgActive;
                psr.objLeft.style.backgroundImage = psr.bgLeftActive;
                psr.objRight.style.backgroundImage = psr.bgRightActive;
            } else {
                psr.obj.style.backgroundImage = psr.bgPassive;
                psr.objLeft.style.backgroundImage = psr.bgLeftPassive;
                psr.objRight.style.backgroundImage = psr.bgRightPassive;
            }
        };
        psr.onmouseover = function() {
            psr.setBg('active');
            psr.stoped = true;
        };
        psr.onmouseout = function() {
            psr.setBg('passive');
            psr.stoped = false;
        };
        myEvent(psr.obj, 'mouseover', psr.onmouseover, '+');
        myEvent(psr.obj, 'mouseout', psr.onmouseout, '+');
        
        psr.onLinkMouseover = function() {
            psr.setBg('active');
            psr.interval = 20;
        };
        psr.onLinkMouseout = function() {
            psr.setBg('passive');
            psr.interval = 100;
        };
        psr.onElevatorMouseover = function() {
            psr.setBg('active');
            psr.interval = 10;
        };
        psr.onElevatorMouseout = function() {
            psr.setBg('passive');
            psr.interval = 100;
        };
        psr.stoped = false;
        psr.animate();
    }
}

function animateLinks()
{
    buildPSR();
    if (psr != null) {
        var ids = new Array('logo', 'logo_text');
        var classes = new Array('menu', 'menu_left');
        var obj;
        for (var i = 0; i < ids.length; i++) {
            obj = document.getElementById(ids[i]);
            if (obj != null) {
                if (obj.alt != '') obj.alt = '';
                if (obj.title != '') obj.title = '';
                myEvent(obj, 'mouseover', psr.onLinkMouseover, '+');
                myEvent(obj, 'mouseout', psr.onLinkMouseout, '+');
            }
        }
        objs = document.getElementsByTagName('a');
        if (objs != null) {
            for (var i = 0; i < objs.length; i++) {
                switch (objs[i].className) {
                case 'menu':
                    break;
                case 'menu_left':
                default:
                    myEvent(objs[i], 'mouseover', psr.onLinkMouseover, '+');
                    myEvent(objs[i], 'mouseout', psr.onLinkMouseout, '+');
                    break;
                }
            }
        }
        objs = document.getElementsByTagName('span');
        if (objs != null) {
            var elevatorsIndex = 0;
            var elevBg = new Image(); elevBg.src = '/images/btn_bg_on.png';
            for (var i = 0; i < objs.length; i++) {
                switch (objs[i].className) {
                case 'menu_box_in':
                    elevatorsIndex++;
                    var elevLogin = '_' + elevatorsIndex.toString();
                    elevators[elevLogin] = elevator(objs[i], elevLogin, elevBg.src);
                    
                    myEvent(objs[i], 'mouseover', psr.onLinkMouseover, '+');
                    myEvent(objs[i], 'mouseout', psr.onLinkMouseout, '+');
                    break;
                }
            }
        }
    }
}

function elevator(obj, login, bgSrc)
{
    obj = HTMLElement(obj);
    obj.login = login;
    obj.bgPosUnit = 'em';
    obj.bgPosStart = 1.6;
    obj.bgPosEnd = -0.8;
    obj.bgPos = obj.bgPosStart;
    obj.bgStepForward = 0.15;
    obj.bgStepBack = 0.25;
    
    obj.setAttribute('mouseover', '0');
    obj.setAttribute('processing', '0');
    
    obj.animate = function() {
        if (this.getAttribute('processing') == '1') {
            psr.onElevatorMouseover();
            if (this.getAttribute('mouseover') == '1') {
                //make ON
                if (this.bgPos > this.bgPosEnd) {
                    this.bgPos -= this.bgStepForward;
                    this.style.backgroundPosition = '0 ' + this.bgPos.toString() + this.bgPosUnit;
                    setTimeout("elevators['"+this.login.toString()+"'].animate()", 100);
                } else {
                    psr.onElevatorMouseout();
                    this.setAttribute('processing', '0');
                }
            } else {
                //make OFF
                if (this.bgPos < this.bgPosStart) {
                    this.bgPos += this.bgStepBack;
                    this.style.backgroundPosition = '0 ' + this.bgPos.toString() + this.bgPosUnit;
                    setTimeout("elevators['"+this.login.toString()+"'].animate()", 100);
                } else {
                    this.removeClass('menu_box_on');
                    this.removeClass('menu_box_on');
                    psr.onElevatorMouseout();
                    this.setAttribute('processing', '0');
                }
            }
        }
    };

    var onmouseover = function() {
        this.setAttribute('mouseover', '1');
        if (this.getAttribute('processing') == '0') {
            this.setAttribute('processing', '1');
            psr.onElevatorMouseover();
            this.addClass('menu_box_on');
            this.addClass('menu_box_on');
            this.animate();
        }
    };
    var onmouseout = function() {
        this.setAttribute('mouseover', '0');
        if (this.getAttribute('processing') == '0') {
            this.setAttribute('processing', '1');
            this.animate();
        }
    };
    obj.onmouseover = onmouseover;
    obj.onmouseout = onmouseout;
    //myEvent(obj, 'mouseover', onmouseover, '+');
    //myEvent(obj, 'mouseout', onmouseout, '+');
    
    //styles
    obj.style.backgroundImage = 'url(' + bgSrc + ')';
    obj.style.backgroundRepeat = 'repeat-x';
    obj.style.backgroundPosition = '0 ' + obj.bgPosStart + obj.bgPosUnit;
    
    /*
    var spanT = obj.insertBefore(document.createElement('span'), obj.firstChild);
    spanT.className = 'bct';
    var spanTL = spanT.appendChild(document.createElement('span'));
    spanTL.className = 'bcl';
    var spanB = obj.appendChild(document.createElement('span'));
    spanB.className = 'bcb';
    var spanBL = spanB.appendChild(document.createElement('span'));
    spanBL.className = 'bcl';
    */
    return obj;
}

var elevators = {};
var animateIntervals = {};

 

/*  slider  */
var slider = {};
slider.moveSlideFromLeft = function(slideId, step, timer) {
    if (typeof animateIntervals[slideId] != 'undefined') {
        clearInterval(animateIntervals[slideId]);
    }
    if (slider.stop == true) {
        animateIntervals[slideId] = setInterval("clearInterval(animateIntervals["+slideId.toString()+"]); slider.moveSlideFromLeft(" + slideId.toString() + ", " + step.toString() + ", " + timer.toString() + ");", timer);
        return;
    }
    if (this.getActiveRema().slide.id == slideId) {
        this.getActiveRema().slide.pos += step;
        if (this.getActiveRema().slide.pos < (this.getActiveRema().slide.w - this.width)) {
            this.getActiveRema().style.backgroundPosition = '-' + this.getActiveRema().slide.pos + 'px center';
            animateIntervals[slideId] = setInterval("clearInterval(animateIntervals["+slideId.toString()+"]); slider.moveSlideFromLeft(" + slideId.toString() + ", " + step.toString() + ", " + timer.toString() + ");", timer);
        } else {
            this.getActiveRema().slide.pos = 0;
            this.getActiveRema().style.backgroundPosition = 'right center';
            slider.showNextSlide()
        }
    } else {
        this.getActiveRema().slide.pos = 0;
        slider.showNextSlide()
    }
};

slider.moveSlideFromRight = function(slideId, step, timer) {
    if (typeof animateIntervals[slideId] != 'undefined') {
        clearInterval(animateIntervals[slideId]);
    }
    if (slider.stop == true) {
        animateIntervals[slideId] = setInterval("clearInterval(animateIntervals["+slideId.toString()+"]); slider.moveSlideFromRight(" + slideId.toString() + ", " + step.toString() + ", " + timer.toString() + ");", timer);
        return;
    }
    if (this.getActiveRema().slide.id == slideId) {
        this.getActiveRema().slide.pos += step;
        var dif = this.getActiveRema().slide.w - this.width;
        if (this.getActiveRema().slide.pos < dif) {
            this.getActiveRema().style.backgroundPosition = '-' + (dif - this.getActiveRema().slide.pos) + 'px center';
            animateIntervals[slideId] = setInterval("clearInterval(animateIntervals["+slideId.toString()+"]); slider.moveSlideFromRight(" + slideId.toString() + ", " + step.toString() + ", " + timer.toString() + ");", timer);
        } else {
            this.getActiveRema().slide.pos = 0;
            this.getActiveRema().style.backgroundPosition = 'left center';
            slider.showNextSlide();
        }
    } else {
        this.getActiveRema().slide.pos = 0;
        slider.showNextSlide()
    }
};

function createSlideShow()
{
    var boxObj = document.getElementById('screen');
    if (boxObj != null) {
    
        var a = boxObj.appendChild(document.createElement('a'));
        a.href = paslaugosLink;
        var b = a.appendChild(document.createElement('div'));
        b.id = 'slides_box';
        slider.stop = false;
        slider.maxOpacity = 80;
        b.onmouseover = function() {
            slider.maxOpacity = 100;
            slider.getActiveRema().setOpacity(slider.maxOpacity);
            slider.stop = true;
        };
        b.onmouseout = function() {
            slider.maxOpacity = 80;
            slider.getActiveRema().setOpacity(slider.maxOpacity);
            slider.stop = false;
        };

        slider.remai = new Array();
        slider.remai[0] = opacity(b.appendChild(document.createElement('div')));
        slider.remai[0].id = 'slides_rema_1';
        slider.remai[0].setOpacity(slider.maxOpacity);
        slider.remai[1] = opacity(b.appendChild(document.createElement('div')));
        slider.remai[1].id = 'slides_rema_2';
        slider.remai[1].setOpacity(0);
        
        slider.activeRemaId = 0;
        slider.passiveRemaId = 1;
        slider.getActiveRema = function() {
            return this.remai[this.activeRemaId];
        }
        slider.getPassiveRema = function() {
            return this.remai[this.passiveRemaId];
        }
        slider.invertRemai = function() {
            var temp = this.activeRemaId;
            this.activeRemaId = this.passiveRemaId;
            this.passiveRemaId = temp;
        }
        
        slider.showSlide = function(slide) {
            this.invertRemai();
            slide = this.getActiveRema().setSlide(slide);
            slider.smoothOn();
        };
        
        slider.smoothOn = function() {
            if (this.getPassiveRema().opacityLevel > 0) {
                if (typeof this.getPassiveRema().slide.animate != 'undefined') {
                    this.getActiveRema().setOpacity((this.getActiveRema().opacityLevel + 2));
                    this.getPassiveRema().setOpacity((this.getPassiveRema().opacityLevel - 2));
                    setTimeout("slider.smoothOn()", 40);
                } else {
                    //it means that we had wait image before
                    this.getActiveRema().setOpacity(this.maxOpacity);
                    this.getPassiveRema().setOpacity(0);
                    
                    var duration = this.getActiveRema().slide.animate();
                }
            } else {
                var duration = this.getActiveRema().slide.animate();
            }
        };
        
        var setBgPosition = function(align) {
            this.style.backgroundPosition = align + ' center';
        };
        slider.remai[0].setBgPosition = setBgPosition;
        slider.remai[1].setBgPosition = setBgPosition;
        var setBgImage = function(src) {
            this.style.backgroundImage = 'url(' + src + ')';
        };
        slider.remai[0].setBgImage = setBgImage;
        slider.remai[1].setBgImage = setBgImage;
        var setSlide = function(slide) {
            slide.rema = this;
            this.slide = slide;
            this.setBgImage(slide.src);
            slide.pos = 0;
            this.setBgPosition(slide.align);
            return slide;
        };
        slider.remai[0].setSlide = setSlide;
        slider.remai[1].setSlide = setSlide;
        
        slider.slides = new Array();
        
        slider.wait = {img: '', src: '/images/slide_wait.gif', w: 128, h: 128, align: 'center'};
        slider.wait.img = new Image();
        slider.wait.img.src = slider.wait.src;

        slider.width = 128;
        slider.height = 128;

        slider.slideCur = -1;
        slider.getNextSlide = function() {
            if (slider.slides.length > 0) {
                this.slideCur++;
                if (this.slideCur >= slider.slides.length) {
                    this.slideCur = 0;
                }
                return this.slides[this.slideCur];
            } else {
                return false;
            }
        };
        
        slider.showNextSlide = function() {
            if (slider.stop == true) {
                setTimeout("slider.showNextSlide()", 100);
            } else {
                var slide = this.getNextSlide();
                if (slide === false) {
                    //need wait
                    this.getActiveRema().setSlide(this.wait);
                    setTimeout("slider.showNextSlide()", 100);
                } else {
                    this.showSlide(slide);
                }
            }
        };
        slider.showNextSlide();
        
        
        //slides objects
        var animateStatic = function() {
            animateIntervals[this.id] = setInterval('clearInterval(animateIntervals[' + this.id.toString() + ']); slider.showNextSlide();', this.duration);
            return this.duration; // duration of static frame
        };
        var animateMoving = function() {
            var stepDuration = Math.round(100 / this.speed);
            var stepMove = 2;
            if (typeof animateIntervals[this.id] != 'undefined') {
                clearInterval(animateIntervals[this.id]);
            }
            animateIntervals[this.id] = setInterval('clearInterval(animateIntervals[' + this.id.toString() + ']); slider.moveSlideFrom' + ((this.align == 'left') ? 'Left' : 'Right') + '(' + this.id + ', ' + stepMove.toString() + ', ' + stepDuration.toString() + ');', stepDuration);
            var stepsNum = (this.w - slider.width) / stepMove;
            var stepsNumRound = Math.round(stepsNum);
            stepsNumRound = (stepsNumRound > stepsNum) ? stepsNumRound : (stepsNumRound + 1);
            return (stepsNumRound * stepDuration + 1000); // duration of dinamic frame
        };
        //slides info:
        var tempSlides = new Array();
        tempSlides[0] = {src: '/images/slides/05_01.jpg', w: 246, h: 128, align: 'left', speed: 2};
        tempSlides[1] = {src: '/images/slides/01_01.jpg', w: 560, h: 128, align: 'right', speed: 2};
        tempSlides[2] = {src: '/images/slides/01_02.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[3] = {src: '/images/slides/01_03.jpg', w: 514, h: 128, align: 'left', speed: 2};
        tempSlides[4] = {src: '/images/slides/01_04.jpg', w: 460, h: 128, align: 'right', speed: 2};
        tempSlides[5] = {src: '/images/slides/01_05.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[6] = {src: '/images/slides/01_06.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[7] = {src: '/images/slides/02_01.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[8] = {src: '/images/slides/02_02.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[9] = {src: '/images/slides/04_01.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[10] = {src: '/images/slides/04_02.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[11] = {src: '/images/slides/04_03.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[12] = {src: '/images/slides/04_04.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[13] = {src: '/images/slides/04_05.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[14] = {src: '/images/slides/04_06.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[15] = {src: '/images/slides/04_07.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[16] = {src: '/images/slides/04_08.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        tempSlides[17] = {src: '/images/slides/04_09.jpg', w: 128, h: 128, align: 'left', duration: 2000};
        //slides' preload
        for (var i = 0; i < tempSlides.length; i++) {
            tempSlides[i].img = new Image();
            tempSlides[i].img.src = tempSlides[i].src;
            slider.slides[i] = tempSlides[i];
            
            slider.slides[i].id = i;
            
            slider.slides[i].rema = null;
            if (slider.slides[i].w > slider.width) {
                //dynamic
                slider.slides[i].animate = animateMoving;
            } else {
                //static
                slider.slides[i].animate = animateStatic;
            }
        }
    }
}

function onLoad()
{
    Browser();
    animateLinks();
    createSlideShow();
}

if (document.getElementById && document.appendChild && document.createElement) {
    myEvent(window, 'load', onLoad, '+');
}

