function ABTOGallery()
{
    this.container = null;
    this.frame = null;
    this.label = null;
    this.overlays = new Array();

    this.labelAlpha = 100;
    this.labelStatus = 2;

    this.labelPeriod = 3;
    this.deltaLabel = 10;

    this.btrigger = null;
    this.ntrigger = null;

    this.triggerPeriod = 3;
    this.deltaTrigger = 10;

    this.BTStatus = 2;
    this.BTAlpha = 100;

    this.NTStatus = 2;
    this.NTAlpha = 100;

    this.images = new Array();
    this.current = 0;

    this.govnoBrowserMode = 0;

    // Rotation

    this.isRotating = 0;
    this.changeTimeoutId = null;
    this.changeDelay = 7000;
    this.rotDelay = 4000;

    
}

ABTOGallery.prototype.initByNode = function(node, args)
{
    if(node != null)
    {
        this.container = node;
        this.container.innerHTML = "";
        var frame = document.createElement('div');
        this.frame = frame;
        this.frame.className = "ABTOGallery";
        this.container.appendChild(this.frame);

        // triggers

        var btrigger = document.createElement('div');
        this.btrigger = btrigger;
        this.btrigger.id = "btrigger";
        this.btrigger.className = "breg";
        this.btrigger = btrigger;
        this.frame.appendChild(this.btrigger);

        var ntrigger = document.createElement('div');
        this.ntrigger = ntrigger;
        this.ntrigger.id = "ntrigger";
        this.ntrigger.className = "nreg";
        this.ntrigger = ntrigger;
        this.frame.appendChild(this.ntrigger);

        // Mouse Events

        var gal = this;

        var nb = this.ntrigger;
        this.ntrigger.onclick = function()
        {
            gal.nextImage();
        }
        this.ntrigger.onmouseover = function()
        {
            nb.className = "nover";
        }
        this.ntrigger.onmousedown = function()
        {
            nb.className = "npress";
        }
        this.ntrigger.onmouseup = function()
        {
            nb.className = "nover";
        }
        this.ntrigger.onmouseout = function()
        {
            nb.className = "nreg";
        }

        var bb = this.btrigger;
        this.btrigger.onclick = function()
        {
            gal.prevImage();
        }
        this.btrigger.onmouseover = function()
        {
            bb.className = "bover";
        }
        this.btrigger.onmousedown = function()
        {
            bb.className = "bpress";
        }
        this.btrigger.onmouseup = function()
        {
            bb.className = "bover";
        }
        this.btrigger.onmouseout = function()
        {
            bb.className = "breg";
        }


        // Frame Events

        this.frame.onmouseout = function()
        {
            gal.hideTriggers();
            gal.labelStatus = 1;
            gal.showingLabel();
            gal.startRotation();
        }

        this.frame.onmouseover = function()
        {
            gal.showTriggers();
            gal.labelStatus = 3;
            gal.hidingLabel();
            gal.stopRotation();
        }


        // Attributes

        if(args!=null)
        {
            if(args['label']!=null) this.setLabel(args['label']);

            if(args['width']!=null) this.setWidth(args['width']);
            if(args['height']!=null) this.setHeight(args['height']);

            //if(args['triggerPeriod']!=null) this.setTriggerPeriod(args['triggerPeriod']);
            //if(args['deltaTrigger']!=null) this.setDeltaTrigger(args['deltaTrigger']);

            //if(args['labelPeriod']!=null) this.setLabelPeriod(args['labelPeriod']);
            //if(args['deltaLabel']!=null) this.setDeltaLabel(args['deltaLabel']);

            //if(args['rotDelay']!=null) this.setRotDelay(args['rotDelay']);
            //if(args['changeDelay']!=null) this.setChangeDelay(args['changeDelay']);
            
            if(args['triggerspos']!=null) this.setTriggersPos(args['triggerspos']);

            if(args['images']!=null)
            {
               var images = args['images'];
               for (var i = 0;i < images.length;i++)
               {
                  this.addImage(images[i]);
               }
            }

            gal.hideTriggers();
            gal.labelStatus = 1;
            gal.showingLabel();
            this.startRotation();

        }
    }
    else
    {
        alert("Node you're trying to init with is null!");
    }
}

ABTOGallery.prototype.setTriggersPos = function(top)
{
    if(this.btrigger!=null && this.ntrigger!=null)
    {
        this.btrigger.style.top = top+'px';
        this.ntrigger.style.top = top+'px';
    }
    else
    {
        alert("There are no triggers, so can't move them!");
    }
}

ABTOGallery.prototype.init = function(id, args)
{
    var node = document.getElementById(id);
    if(node !=null)
    {
        this.initByNode(node, args);
    }
    else
    {
        alert("Node with id \""+id+"\" you're trying to init with is null!");
    }
}

ABTOGallery.prototype.setLabel = function(id)
{
    var node = document.getElementById(id);
    if(node !=null)
    {
        this.setLabelByNode(node);
    }
    else
    {
        alert("Node with id \""+id+"\" you're trying to set as label is null!");
    }
}

ABTOGallery.prototype.setLabelByNode = function(node)
{
    if(node != null)
    {
        this.label = node;
    }
    else
    {
        alert("Node you're trying to set as label is null!");
    }
}

ABTOGallery.prototype.addOverlay = function(id)
{
    var node = document.getElementById(id);
    if(node !=null)
    {
        this.addOverlayByNode(node);
    }
    else
    {
        alert("Node with id \""+id+"\" you're trying to add as overlay is null!");
    }
}

ABTOGallery.prototype.addOverlayByNode = function(node)
{
    if(node != null)
    {
        this.overlays.push(node);
        var gal = this;
        
        node.onmouseover = function()
        {
            gal.showTriggers();
            gal.labelStatus = 3;
            gal.hidingLabel();
            gal.stopRotation();
        }

        node.onmouseout = function()
        {
            gal.hideTriggers();
            gal.labelStatus = 1;
            gal.showingLabel();
            gal.startRotation();
        }

    }
    else
    {
        alert("Node you're trying to add as overlay is null!");
    }
}

ABTOGallery.prototype.setGovnoBrowserMode = function(value)
{
    this.govnoBrowserMode = value;
}

ABTOGallery.prototype.setWidth = function(value)
{
    this.frame.style.width = value+"px";
}

ABTOGallery.prototype.setHeight = function(value)
{
    this.frame.style.height = value+"px";
}

ABTOGallery.prototype.setSize = function(width, height)
{
    this.frame.style.width = width+"px";
    this.frame.style.height = height+"px";
}

ABTOGallery.prototype.addImage = function(src, alt)
{
    var newimg = document.createElement('img');
    newimg.src = src;
    newimg.alt = alt;
    prependChild(newimg, this.frame);

    var AGI = new ABTOGalleryImage();
    AGI.setNode(newimg);
    if (this.images.length != 0)
    AGI.hide();
    this.images.push(AGI);
}

ABTOGallery.prototype.nextImage = function()
{
    this.current++;
    if(this.current >= this.images.length) this.current = 0;
    this.showImage(this.current);
}

ABTOGallery.prototype.prevImage = function()
{
    this.current--;
    if(this.current < 0) this.current = this.images.length-1;
    this.showImage(this.current);
}

ABTOGallery.prototype.showImage = function(index)
{
    this.current = index;
    for (var i=0; i < this.images.length; i++)
    {
        if(i==index)
        {
            this.images[i].status = 1;
            this.images[i].showing();
        }
        else
        {
            this.images[i].status = 3;
            this.images[i].hiding();
        }
    }
}

ABTOGallery.prototype.hidingLabel = function()
{
    if (this.label == null) return;
    var AG = this;
    if(this.labelStatus == 3)
    {
        this.labelAlpha -= this.deltaLabel;
        if (this.labelAlpha <= 0)
        {
            this.labelAlpha = 0;
            this.labelStatus = 0;
        }
        else
        {
            setTimeout(function(){AG.hidingLabel()}, this.labelPeriod);
        }
        this.setLabelAlpha(this.labelAlpha);
    }
}

ABTOGallery.prototype.showingLabel = function()
{
    if (this.label == null) return;
    var AG = this;
    if(this.labelStatus == 1)
    {
        this.labelAlpha += this.deltaLabel;
        if (this.labelAlpha >= 100)
        {
            this.labelAlpha = 100;
            this.labelStatus = 2;
        }
        else
        {
            setTimeout(function(){AG.showingLabel()}, this.labelPeriod);
        }
        this.setLabelAlpha(this.labelAlpha);
    }
}

ABTOGallery.prototype.hideLabel = function()
{
    if (this.label == null) return;
    this.labelStatus = 0;
    this.labelAlpha = 0;
    this.setLabelAlpha(this.labelAlpha);
}

ABTOGallery.prototype.showLabel = function()
{
    if (this.label == null) return;
    this.labelStatus = 2;
    this.labelAlpha = 100;
    this.setLabelAlpha(this.labelAlpha);
}

ABTOGallery.prototype.hidingBTrigger = function()
{
    var AG = this;
    if(this.BTStatus == 3)
    {
        this.BTAlpha -= this.deltaTrigger;
        if (this.BTAlpha <= 0)
        {
            this.BTAlpha = 0;
            this.BTStatus = 0;
        }
        else
        {
            setTimeout(function(){AG.hidingBTrigger()}, this.triggerPeriod);
        }
        this.setBTAlpha(this.BTAlpha);
    }
}

ABTOGallery.prototype.showingBTrigger = function()
{
    var AG = this;
    if(this.BTStatus == 1)
    {
        this.BTAlpha += this.deltaTrigger;
        if (this.BTAlpha >= 100)
        {
            this.BTAlpha = 100;
            this.BTStatus = 2;
        }
        else
        {
            setTimeout(function(){AG.showingBTrigger()}, this.triggerPeriod);
        }
        this.setBTAlpha(this.BTAlpha);
    }
}

ABTOGallery.prototype.hideBTrigger = function()
{
    this.BTStatus = 0;
    this.BTAlpha = 0;
    this.setBTAlpha(this.BTAlpha);
}

ABTOGallery.prototype.showBTrigger = function()
{
    this.BTStatus = 2;
    this.BTAlpha = 100;
    this.setBTAlpha(this.BTAlpha);
}

ABTOGallery.prototype.setBTAlpha = function(value)
{
    setAlpha(this.btrigger, value,this.govnoBrowserMode)
}


ABTOGallery.prototype.setLabelAlpha = function(value)
{
    setAlpha(this.label, value,this.govnoBrowserMode)
}

ABTOGallery.prototype.hidingNTrigger = function()
{
    var AG = this;
    if(this.NTStatus == 3)
    {
        this.NTAlpha -= this.deltaTrigger;
        if (this.NTAlpha <= 0)
        {
            this.NTAlpha = 0;
            this.NTStatus = 0;
        }
        else
        {
            setTimeout(function(){AG.hidingNTrigger()}, this.triggerPeriod);
        }
        this.setNTAlpha(this.NTAlpha);
    }
}

ABTOGallery.prototype.showingNTrigger = function()
{
    var AG = this;
    if(this.NTStatus == 1)
    {
        this.NTAlpha += this.deltaTrigger;
        if (this.NTAlpha >= 100)
        {
            this.NTAlpha = 100;
            this.NTStatus = 2;
        }
        else
        {
            setTimeout(function(){AG.showingNTrigger()}, this.triggerPeriod);
        }
        this.setNTAlpha(this.NTAlpha);
    }
}

ABTOGallery.prototype.hideNTrigger = function()
{
    this.NTStatus = 0;
    this.NTAlpha = 0;
    this.setNTAlpha(this.NTAlpha);
}

ABTOGallery.prototype.showNTrigger = function()
{
    this.NTStatus = 2;
    this.NTAlpha = 100;
    this.setNTAlpha(this.NTAlpha);
}

ABTOGallery.prototype.setNTAlpha = function(value)
{
    setAlpha(this.ntrigger, value,this.govnoBrowserMode)
}

ABTOGallery.prototype.setTriggerPeriod = function(value)
{
    this.triggerPeriod = value;
}

ABTOGallery.prototype.setDeltaTrigger = function(value)
{
    this.deltaTrigger = value;
}

ABTOGallery.prototype.setLabelPeriod = function(value)
{
    this.labelPeriod = value;
}

ABTOGallery.prototype.setDeltaLabel = function(value)
{
    this.deltaLabel = value;
}

ABTOGallery.prototype.setChangeDelay = function(value)
{
    this.changeDelay = value;
}

ABTOGallery.prototype.setRotDelay = function(value)
{
    this.rotDelay = value;
}

ABTOGallery.prototype.showTriggers = function()
{
    this.BTStatus = 1;
    this.showingBTrigger();

    this.NTStatus = 1;
    this.showingNTrigger();
}

ABTOGallery.prototype.hideTriggers = function()
{
    this.BTStatus = 3;
    this.hidingBTrigger();

    this.NTStatus = 3;
    this.hidingNTrigger();
}

ABTOGallery.prototype.hideTriggersImmediately = function()
{
    this.hideBTrigger();
    this.hideNTrigger();
}

ABTOGallery.prototype.showTriggersImmediately = function()
{
    this.showBTrigger();
    this.showNTrigger();
}

// Rotation

ABTOGallery.prototype.startRotation = function()
{
    if(!this.isRotating)
    {
        this.isRotating = 1;
        if(this.changeTimeoutId!=null) clearTimeout(this.changeTimeoutId);
        AG = this;
        this.changeTimeoutId = setTimeout(function(){AG.change()}, this.rotDelay);
    }
}

ABTOGallery.prototype.stopRotation = function()
{
    this.isRotating = 0;
}


ABTOGallery.prototype.change = function()
{
    if(this.isRotating)
    {
        this.isChanging = 1;
        this.nextImage();
        AG = this;
        if(this.changeTimeoutId!=null) clearTimeout(this.changeTimeoutId);
        this.changeTimeoutId = setTimeout(function(){AG.change()}, this.changeDelay);
    }
}

function ABTOGalleryImage()
{
    this.node = null;
    this.status = 2;
    this.alphaPeriod = 10;
    this.deltaAlpha = 6;
    this.alpha = 100;
    // Statuses:
    // 0 - invisible
    // 1 - appearing
    // 2 - appeared
    // 3 - disappearing
}

ABTOGalleryImage.prototype.setNode = function(node)
{
    this.node = node;
}

ABTOGalleryImage.prototype.setAlphaPeriod = function(value)
{
    this.alphaPeriod = value;
}

ABTOGalleryImage.prototype.setDeltaAlpha = function(value)
{
    this.deltaAlpha = value;
}

ABTOGalleryImage.prototype.hiding = function()
{
    var AGI = this;
    if(this.status == 3)
    {
        this.alpha -= this.deltaAlpha;
        if (this.alpha <= 0)
        {
            this.alpha = 0;
            this.status = 0;
        }
        else
        {
            setTimeout(function(){AGI.hiding()}, this.alphaPeriod);
        }
        this.setAlpha(this.alpha);
    }
}

ABTOGalleryImage.prototype.showing = function()
{
    var AGI = this;
    if(this.status == 1)
    {
        this.alpha += this.deltaAlpha;
        if (this.alpha >= 100)
        {
            this.alpha = 100;
            this.status = 2;
        }
        else
        {
            setTimeout(function(){AGI.showing()}, this.alphaPeriod);
        }
        this.setAlpha(this.alpha);
    }
}

ABTOGalleryImage.prototype.hide = function()
{
    this.alpha = 0;
    this.setAlpha(this.alpha);
    this.status = 0;
}

ABTOGalleryImage.prototype.show = function()
{
    this.alpha = 100;
    this.setAlpha(this.alpha);
    this.status = 2;
}

ABTOGalleryImage.prototype.setAlpha = function(value)
{
    setAlpha(this.node, value)
}

function setAlpha(node, value, govnoBrowserMode)
{
    if(!govnoBrowserMode)
    {
        if(value == 0) {
            node.style.display = 'none';
        } else {
            node.style.display = 'block';
        }

        node.style.opacity = (value / 100);
        node.style.MozOpacity = (value / 100);
        node.style.KhtmlOpacity = (value / 100);
    }
    else
    {
        if(value < 50)
        {
            node.style.display = 'none';
        } else {
            node.style.display = 'block';
        }
    }
}

function prependChild(chto, kuda)
{
    var first = kuda.firstChild;
    kuda.insertBefore(chto, first);
}
