﻿function TopButton(iBottom, iRight, sImage, sImageOver)
{
    this._Position = 0;
    this._Bottom = iBottom;
    this._Right = iRight;
    this._ImageSrc = sImage;
    this._ImageOverSrc = sImageOver;
    
    this._Image = null;
    this._Screen = new Screen();
    
    this._Callwrapper;
    
    var o = this;
    
    this.CreateButton = function()
    {
        this._Image = document.createElement("img")
        this._Image.src = this._ImageSrc;
        this._Image.style.position = "absolute";
        
        this._Screen.Current();        
        
        this._Image.style.top = (this._Screen.Top() + this._Screen.Height()) + "px";
        this._Image.style.left = (this._Screen.Width() - this._Right - this._Image.width + this._Screen.Left()) + "px"; 
        this._Image.style.display = "none";
        
        this._Position = this._Image.height + this._Bottom;

        this._Image.onclick = function()
        {
            window.scroll(0, 0);
        }
        
        this._Image.onmouseover = function()
        {
            o._Image.src = o._ImageOverSrc;
            o._Image.style.cursor = "pointer";            
        }
        
        this._Image.onmouseout = function()
        {
            o._Image.src = o._ImageSrc;
            o._Image.style.cursor = "default"; 
        }
        
        document.getElementsByTagName('body')[0].appendChild(this._Image);                 
    }
    
    this.MoveButton = function(iTo)
    {
       if ((parseInt(this._Image.style.top) - 5) > iTo)
       {
            this._Image.style.top = (parseInt(this._Image.style.top) - 5) + "px";
            this._Callwrapper = new CCallWrapper(this, 10, 'MoveButton', iTo);
            CCallWrapper.asyncExecute(this._Callwrapper);
       }
       else
       {
            this._Image.style.top = iTo + "px";
       }     
    }
    
    this.ShowButton = function(iLast)
    {
        this._Screen.Current();
        
        if (this._Screen.Top() == iLast)
        {        
            if ((this._Screen.Width() > (this._Image.width + this._Right)) && (this._Screen.Height() > (this._Image.height + this._Bottom)))  
            {
                var iStart = this._Screen.Top() + this._Screen.Height();               
              
                if (iStart >= (document.getElementsByTagName('body')[0].offsetHeight - this._Image.height))
                {
                    this._Image.style.top = (document.getElementsByTagName('body')[0].offsetHeight - this._Image.height) + "px";
                }
                else
                {
                    this._Image.style.top = iStart + "px";               
                }
                
                this._Image.style.left = (this._Screen.Width() - this._Right - this._Image.width + this._Screen.Left()) + "px";
                
                this._Image.style.display = "block";
                this.MoveButton(iStart - this._Position);
            }            
        }        
    }
    
    
    this.OnScroll = function()
    {
        if (o._Image != null)
        {
            o._Image.style.display = "none";
        }
        
        o._Screen.Current();
        
        if (o._Screen.Top() != 0)
        {
            if (o._Image == null)
            {
                o.CreateButton();
            }            
            
            o._Callwrapper = new CCallWrapper(o, 1000, 'ShowButton', o._Screen.Top());
            CCallWrapper.asyncExecute(o._Callwrapper);           
        }        
    }
}