/*
   Class: PlatterState
   Contains objects to save, load, and manipulate the state of the Platter in the BST.
   The basic use of the PlatterState is to instantiate an object of class PlatterState.
   The PlatterState object will automatically contain a PanelCollection and panels will automatically
   contain a HistoryCollection.  The objects are addressed as state.panel[#].history[#].property
   and subsets of that.

   class PlatterState()
        property CurrentPanel number
        property Translation string
        property panel ("PanelCollection")
        method Save()
        method Load(stateinfo)
        method Display()

    class PanelCollection()
        method Add(obj)

    class Panel(translation)
        property TranslationId string
        property Keyword string
        property Reference string
        property BookGroup string
        property BookName string
        property BookCode string
        property ChapterId number
        property VerseId number
        property Sequence number
        property SortBy string
        property LoadTranslation true/false
        property AudioLink string
        property FontSize integer
        property history ("HistoryCollection")

    class HistoryCollection(index)
        property CurrentHistory
        method Add(obj)
        method Previous()
        method Next()

    class History(displaytype,requeststring)
        property DisplayType string
        property RequestString string
*/

/*
    Variable: BSTState
    Holds the panel data.
*/
var BSTState;


/*
    Function: CreateStateCollection
    Create the collection object.

    Parameters:
        ClassName - the class-name.

    Returns:
        an object.

    See Also:
        <Finder.js>

*/
function CreateStateCollection(ClassName) {
    var obj=new Array();
    eval("var t=new "+ClassName+"()");
    for(_item in t) {
            eval("obj."+_item+"=t."+_item);
    }
    return obj;
}


/*
    Class: PlatterState
    Contains a collection of Panels.

*/
function PlatterState() {
    this.CurrentPanel = 0;
    this.Translation = "";

    this.panel = new CreateStateCollection("PanelCollection");

/*
    Function: Save
    Serialize the objects to a string of JavaScript for later execution.

    Parameters:
        none

    Returns:
        an string

*/
    this.Save=function() {
        var str="this.CurrentPanel="+this.CurrentPanel+"; ";
        str+="this.Translation='"+this.Translation+"'; ";
        str+=this.panel.privateSave();
        return str;
    }
/*
    Function: Load
    Users the stateinfo parameter as JavaScript code to load the objects.

    Parameters:
        stateinfo - serializes objects as a string of JavaScript

    Returns:
        nothing

*/
    this.Load=function(stateinfo) {
        //delete the previous panel collection and create a new one
        this.panel = "";
        this.panel = new CreateStateCollection("PanelCollection");

        //execute the stateinfo a javascript
        eval(stateinfo);
    }
/*
    Function: DeletePanel
    Removes an entire panel's history from the object.

    Parameters:
        panel - 0 or 1.

    Returns:
        nothing

*/
    this.DeletePanel=function(panel) {
        if(panel==0) {
            //delete panel 0 and panel 1 becomes 0
            this.panel.shift();
        } else {
            //delete panel 1 only
            this.panel.pop();
        }
        this.CurrentPanel = 0;
    }
/*
    Function: Display
    Primarily for debugging, it lists the contents of the PlatterState as html.

    Parameters:
        none

    Returns:
        html text

*/
    this.Display=function() {
        var str=this.Save();
        for(var i=0;i<50;i++)
            str = str.replace(";","<br/>");
        return str;
    }
}


/*
   Class: PanelCollection
   A collection of panels.  Usually 1 or 2 panels.

*/
function PanelCollection() {
    this.Add=function(obj) {
        this.push(obj);
    }
    this.privateSave=function() {
        var str="";
        for(var i=0;i<this.length;i++)
            str+=this[i].privateSave(i);
        return str;
    }
}


/*
   Class: Panel
   The history of a panel whenever it is saved in MyBST.js

*/
function Panel() {
    this.CategoryName="bibles";
    this.TranslationId = "";
    this.Keyword = "";
    this.Reference = "";
    this.BookGroup = "";
    this.BookName = "";
    this.BookCode = "";
    this.Sequence = 0;
    this.HasAudio = false;
    this.ChapterId = 1;
    this.VerseId = 1;
    this.SortBy = "bible";
    this.LoadTranslation = true;
    this.SaveState = true;
    this.AudioLink = "";
    this.FontSize = "11";
    this.ChapterCount = false;

    this.history = new CreateStateCollection("HistoryCollection");

    //set the current history in the history collection to the index
    this.history.CurrentHistory=0;

    this.privateSave=function(panel) {
        var str="this.panel.Add(new Panel()); ";
        str+="this.panel["+panel+"].CategoryName='"+this.CategoryName+"';";
        str+="this.panel["+panel+"].TranslationId='"+this.TranslationId+"';";
        str+="this.panel["+panel+"].Keyword='"+this.Keyword+"';";
        str+="this.panel["+panel+"].Reference='"+this.Reference+"';";
        str+="this.panel["+panel+"].BookGroup='"+this.BookGroup+"';";
        str+="this.panel["+panel+"].BookName='"+this.BookName+"';";
        str+="this.panel["+panel+"].BookCode='"+this.BookCode+"';";
        str+="this.panel["+panel+"].Sequence='"+this.Sequence+"';";
        str+="this.panel["+panel+"].ChapterId="+this.ChapterId+";";
        str+="this.panel["+panel+"].VerseId="+this.VerseId+";";
        str+="this.panel["+panel+"].SortBy='"+this.SortBy+"';";
        str+="this.panel["+panel+"].LoadTranslation="+this.LoadTranslation+";";
        str+="this.panel["+panel+"].AudioLink='"+this.AudioLink+"';";
        str+="this.panel["+panel+"].FontSize="+this.FontSize+";";
        str+="this.panel["+panel+"].ChapterCount='"+this.ChapterCount+"';";
        str+=this.history.privateSave(panel);
        return str;
    }
}


/*
   Class: HistoryCollection
   A collection of panel history objects.

*/
function HistoryCollection(index) {
    this.CurrentHistory = index;

/*
    Function: Add
    Adds a panel history object to the collection.

    Parameters:
        obj - a new history object.

    Returns:
        nothing

*/
    this.Add=function(obj) {
        if(this.CurrentHistory<this.length-1) {
            var loopct = this.length-this.CurrentHistory-1;
            for(var i=0;i<loopct;i++)
                this.pop();
        }
        this.push(obj);
        this.CurrentHistory=this.length-1;
    }
/*
    Function: Previous
    Returns the previous panel history in the list.

    Parameters:
        none

    Returns:
        a history object.

*/
    this.Previous=function() {
        this.CurrentHistory = Math.max(0,this.CurrentHistory-1);
        return this.CurrentHistory;
    }
/*
    Function: Next
    Returns the next panel history in the list.

    Parameters:
        none

    Returns:
        a history object.

*/
    this.Next=function() {
        this.CurrentHistory = Math.min(this.length-1,this.CurrentHistory+1);
        return this.CurrentHistory;
    }
    this.privateSave=function(panel) {
        var str="";
        str+="this.panel["+panel+"].CurrentHistory="+this.CurrentHistory+";";
        for(var i=0;i<this.length;i++)
            str+=this[i].privateSave(panel,i);
        return str;
    }
}


/*
   Class: History
   A single history of a panel.

*/
function History(displaytype,requeststring) {
    this.DisplayType = displaytype;
    this.RequestString = requeststring;

    this.privateSave=function(panel,index) {
        var str="this.panel["+panel+"].history.Add(new History('"+this.DisplayType+"','"+this.RequestString+"')); ";
        return str;
    }
}

