var IE = false;  // IE bitch?

function Setup() {
    reg = /^.*Microsoft.*$/;
    if (reg.test(navigator.appName))
        IE = true;
}

Setup();

/**
 * @return element
 */ 
function GetElem(_id) {
    return document.getElementById(_id);
}

/**
 * vezme styl elementu z vlozeneho stylu (ne z <div style="...">)
 * @return styl elementu 
 */ 
function GetStyle(_element) {
    if (!IE)
        return document.defaultView.getComputedStyle(_element, "");
    else
        return _element.currentStyle;
}

/**
 * classa
 */ 
function Scroller(_sbID, _sbInID, _speed, _px, _firstID, _secondID) {
    this.sb = GetElem(_sbID);  // vnejsi box
    this.sbIn = GetElem(_sbInID);  // vnitrni box
    this.sbFirst = GetElem(_firstID);  // prvni posouvatko (leve/nahoru)
    this.sbSecond = GetElem(_secondID);  // druhe posouvatko (prave/dolu)
    this.sbFirstClassName = "";
    this.sbSecondClassName = "";
    if ((this.sb == null) || (this.sbIn == null)) {
        alert("Scroller error: there's no such id");
        return;
    }
    this.sbStyle = GetStyle(this.sb);
    this.sbInStyle = GetStyle(this.sbIn);
    this.boxWidth = 0;  // sirka boxu
    this.boxHeight = 0;  // vyska boxu
    this.boxInWidth = 0;  // sirka vnitrniho boxu
    this.boxInHeight = 0;  // vyska vnitrniho boxu
    this.dx = 0;  // rozdil sirky vnejsiho a vnitrniho boxu
    this.dy = 0;  // rozdil vysky vnejsiho a vnitrniho boxu
    this.boxLT = 0;  // x-ova/y-ova pozice leveho/horniho okraje vnitrniho boxu
    this.timer = null;
    this.speed = (_speed != undefined) ? _speed : 10;  // rychlost 1 tiku v milisekundach
    this.pxPerTick = (_px != undefined) ? _px : 1;  // posun o pixly za 1 tik
    this.direction = 0;  // smer posouvani
    this.boxes = new Array();
    this.sb.style.overflow = 'hidden';

    this.Init = function () {
        // toto je kvuli fci setInterval, bo jinak se k this v teto fci nedostanu :(
        if (Scroller.instances == undefined)
            Scroller.instances = new Array();
        this.id = Scroller.instances.length;
        Scroller.instances[this.id] = this;
        
        if ((this.sb == null) || (this.sbIn == null)) {
            this.sbEnabled = false;
            alert("Scroller error: there's no such id");
        } else { 
            var nodes = this.sbIn.childNodes;
            for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].nodeType == 1) {
                    this.boxes.push(nodes[i]);
                    this.boxInWidth += nodes[i].offsetWidth;
                    this.boxInHeight += nodes[i].offsetHeight;
                }
            }

            this.boxWidth = parseInt(this.sbStyle.width);
            this.boxHeight = parseInt(this.sbStyle.height);
            this.dx = this.boxInWidth - this.boxWidth;
            this.dy = this.boxInHeight - this.boxHeight;
          
            if (this.sbFirst != null) {
                this.sbFirst.ref = this;
                this.sbFirst.onmouseover = function () {
                    this.ref.Scroll(1);
                }
                this.sbFirst.onmouseout = function () {
                    this.ref.StopScrolling();
                }
                this.sbFirstClassName = this.sbFirst.className;

/*                if (this.boxLT >= 0)
                    this.sbFirst.className = "g_left_on";*/
            }
    
            if (this.sbSecond != null) {
                this.sbSecond.ref = this;
                this.sbSecond.onmouseover = function () {
                    this.ref.Scroll(-1);
                }
                this.sbSecond.onmouseout = function () {
                    this.ref.StopScrolling();
                }
                this.sbSecondClassName = this.sbSecond.className;

                if (this.dx > 0)
                    this.sbSecond.className = "g_right_on";  // je to natvrdo, na...
            }
        }
    }
    
    this.Scroll = function (_direction) {
        if (!this.sbEnabled)
            return;
            
        this.direction = _direction;

        if (this.timer == null)
            this.timer = setInterval("Scroller.instances["+this.id+"].MoveBox()", this.speed)
    }
    
    this.StopScrolling = function () {
        if (!this.sbEnabled)
            return;
            
        clearInterval(this.timer);
        this.timer = null;
    }
    
    this.MoveBox = function () {}
}

/**
 * horizontalni scroller
 */ 
function HScroll(_sbID, _sbInID, _speed, _px, _firstID, _secondID) {
    this.scroller = new Scroller(_sbID, _sbInID, _speed, _px, _firstID, _secondID);

    this.scroller.Init();
    this.scroller.sbEnabled = (this.scroller.dx > 0);  // jestli je vnitrni box mensi jak vnejsi, serem na vsechno
   
    this.scroller.MoveBox = function () {
        if (this.direction == -1) {  // leva
            if (this.boxLT <= -this.dx) {
                this.sbIn.style.left = -this.dx;
                this.sbSecond.className = this.sbSecondClassName;
            } else {
                this.boxLT -= this.pxPerTick;
                this.sbIn.style.left = this.boxLT + "px";
                this.sbFirst.className = "g_left_on";
                this.sbSecond.className = "g_right_on";
            } 
        } else if (this.direction == 1) {  // prava
            if (this.boxLT >= 0) {
                this.sbIn.style.left = 0;
                this.sbFirst.className = this.sbFirstClassName;
            } else {
                this.boxLT += this.pxPerTick;
                this.sbIn.style.left = this.boxLT + "px";
                this.sbFirst.className = "g_left_on";
                this.sbSecond.className = "g_right_on";
            } 
        }
    }    
}
