﻿function ScrollBooth(Name,HolderWidth,HolderHeight,HolderID,NavID,NextID,BackID,ContentFunction)
{    
    if(Name == null || HolderWidth == null || HolderHeight == null || HolderID == null || NavID == null || NextID == null || BackID == null || ContentFunction == null)
    {
        return false;
    }    
    
    this.Name = Name;    
    this.HolderWidth = HolderWidth;
    this.HolderHeight = HolderHeight;
    this.HolderID = HolderID;
    this.NavID = NavID;
    this.NextID = NextID;
    this.BackID = BackID;    
    this.PageCount = 0;
    this.ContentFunction = ContentFunction;
    this.onscrollboothload = null;
    this.onupdatenav = null;
    this.onaddpage = null;
    this.onscrolltopage = null;       
    this.onscrollbootherror = null;
    this.Error = null;                                                   
}

ScrollBooth.prototype.addError = function(error)
{
    this.Error = error;
    
    if(this.onscrollbootherror != null)
    {
        this.onscrollbootherror(this.Name,this.Error);
    }
};

ScrollBooth.prototype.updateNav = function(PageNumber)
{
    if(document.getElementById(this.NavID) != null)
    {
        var NavLink = document.createElement("div");
        NavLink.id = new String(this.Name + "NavPage" + PageNumber);
        NavLink.className = "ScrollBoothPageNumberButton";
        NavLink.data = {name:this.Name,page:PageNumber};
        NavLink.onclick = ScrollBoothPageNav;
        NavLink.onmouseover = function()
        {
            this.className = "ScrollBoothPageNumberButtonOver";
        };
        NavLink.onmouseout = function()
        {
            this.className = "ScrollBoothPageNumberButton";
        };        
        NavLink.innerHTML = PageNumber;
        
        document.getElementById(this.NavID).appendChild(NavLink);
        
        if(this.onupdatenav != null)
        {
            this.onupdatenav(PageNumber,NavLink.id);
        }
    }
    else
    {
        this.addError({id:6,text:"The Nav ID is invalid or it has not loaded in the DOM"});
        return;
    }
};

ScrollBooth.prototype.addPage = function(html)
{    
    if(document.getElementById(this.Name+"Scroll") != null)
    {
        this.PageCount++;    
        var PageNumber = this.PageCount;        
        
        var NewPage = document.createElement("div");
        NewPage.id = new String(this.Name+"Page"+PageNumber);
        NewPage.style.width = new String(this.HolderWidth+"px");
        NewPage.style.height = new String(this.HolderHeight+"px");    
        NewPage.style.left = new String(((PageNumber-1) * this.HolderWidth)+"px");
        NewPage.className = "ScrollBoothPage";      
        NewPage.innerHTML = html;
        
        var ScrollShell = document.getElementById(this.Name+"Scroll");
        ScrollShell.data.total = this.PageCount;        
        ScrollShell.style.width = new String((PageNumber * this.HolderWidth)+"px");       
        ScrollShell.appendChild(NewPage);        
        
        this.updateNav(PageNumber);
        
        if(this.onaddpage != null)
        {
            this.onaddpage(PageNumber,NewPage.id);
        }
    }
    else
    {
        this.addError({id:5,text:"The scroll element is null"});
        return;
    }      
};

ScrollBooth.prototype.load = function(Content)
{   
    if(document.getElementById(this.HolderID) != null)
    {
        document.getElementById(this.HolderID).innerHTML = "<div id='"+this.Name+"Shell' class='ScrollBoothShell' style='width:"+this.HolderWidth+"px; height:"+this.HolderHeight+"px;'>" +
                                                           "<div id='"+this.Name+"Scroll' class='ScrollBoothHolder' style='width:"+this.HolderWidth+"px; height:"+this.HolderHeight+"px;'>" + 
                                                           "</div></div>";
        
        document.getElementById(this.Name+"Scroll").data = {name:this.Name,current:1,total:this.PageCount,width:this.HolderWidth,onscrolltopage:this.onscrolltopage};
       
             
        if(Content != null)
        {
            this.ContentFunction(Content);
            
            if(document.getElementById(this.Name + "NavPage1") != null)
            {
                var CurrentLink = document.getElementById(this.Name + "NavPage1");
                CurrentLink.className = "ScrollBoothPageNumberButtonCurrent";
                CurrentLink.onmouseover = function()
                {
                    this.className = "ScrollBoothPageNumberButtonCurrentOver";
                };
                CurrentLink.onmouseout = function()
                {
                    this.className = "ScrollBoothPageNumberButtonCurrent";
                }; 
            }
            
            if(document.getElementById(this.NextID) != null)
            {        
                var NextButton = document.getElementById(this.NextID); 
                NextButton.data = {name:this.Name};
                NextButton.onclick = ScrollBoothNext;                    
                
                if(document.getElementById(this.BackID) != null)
                {
                    var BackButton = document.getElementById(this.BackID);
                    BackButton.data = {name:this.Name};
                    BackButton.onclick = ScrollBoothBack;
                                        
                    if(this.onscrollboothload != null)
                    {
                        this.onscrollboothload(this.Name);
                    }   
                }
                else
                {
                    this.addError({id:4,text:"The Back button ID is invalid or it has not loaded in the DOM"});
                    return;
                }                     
            }    
            else
            {
                this.addError({id:3,text:"The Next button ID is invalid or it has not loaded in the DOM"});
                return;
            }    
        }
        else
        {
            this.addError({id:2,text:"The content property is null"});
            return;        
        }        
    }
    else
    {     
        this.addError({id:1,text:"HolderID is invalid or the element is not loaded in the DOM"});
        return;
    }   
};

ScrollBooth.prototype.reset = function()
{
    document.getElementById(this.HolderID).innerHTML = "";
    document.getElementById(this.NavID).innerHTML = "";
    
    this.PageCount = 0;
    
    var NextButton = document.getElementById(this.NextID); 
    NextButton.data = null;
    NextButton.onclick = function(){};
    
    var BackButton = document.getElementById(this.BackID);
    BackButton.data = null;
    BackButton.onclick = function(){};    
};

function ScrollBoothScrollToPage(Name,PageNumber)
{  
    var Scroller = document.getElementById(Name+"Scroll");    
    var CurrentPage = Scroller.data.current;
    var Width = Scroller.data.width;
        
    if(document.getElementById(Name + "NavPage" + CurrentPage) != null)
    {
        var CurrentLink = document.getElementById(Name + "NavPage" + CurrentPage);
        CurrentLink.className = "ScrollBoothPageNumberButton";
        CurrentLink.onmouseover = function()
        {
            this.className = "ScrollBoothPageNumberButtonOver";
        };
        CurrentLink.onmouseout = function()
        {
            this.className = "ScrollBoothPageNumberButton";
        }; 
    }
    
    Scroller.data.current = PageNumber;         
    
    if(document.getElementById(Name + "NavPage" + PageNumber) != null)
    {
        var NewLink = document.getElementById(Name + "NavPage" + PageNumber);
        NewLink.className = "ScrollBoothPageNumberButtonCurrent";
        NewLink.onmouseover = function()
        {
            this.className = "ScrollBoothPageNumberButtonCurrentOver";
        };
        NewLink.onmouseout = function()
        {
            this.className = "ScrollBoothPageNumberButtonCurrent";
        };
    }    
    
    $("#"+Scroller.id).animate({left:-((PageNumber - 1) * Width) + "px"},"normal","swing",function()
    {        
        if(this.data.onscrolltopage != null)
        {
            this.data.onscrolltopage(this.data.current,new String(this.data.name+"Page"+this.data.current));
        }
    });       
}

function ScrollBoothPageNav()
{
    var Name = this.data.name;
    var PageNumber = this.data.page;    
    
    ScrollBoothScrollToPage(Name,PageNumber);    
}

function ScrollBoothNext()
{
    var Name = this.data.name;    
    var Total = document.getElementById(Name+"Scroll").data.total;    
    var CurrentPage = document.getElementById(Name+"Scroll").data.current;
    
    var PageNumber = CurrentPage + 1;
    
    if(Total > 0 && PageNumber <= Total)
    {
        ScrollBoothScrollToPage(Name,PageNumber);
    }
}

function ScrollBoothBack()
{
    var Name = this.data.name;       
    var CurrentPage = document.getElementById(Name+"Scroll").data.current;
    
    var PageNumber = CurrentPage - 1;
    
    if(PageNumber >= 1)
    {       
        ScrollBoothScrollToPage(Name,PageNumber);
    }
}
