/**
 * @author Tim
 */
FLN.Topic = function(initData){
    this.id = initData.id; //id for this topic. must be unique
    this.numberOfPagesInDisplay = initData.numberOfPagesInDisplay; //the total number of pages in the topic that should appear in the section that displays your current page.
    this.displayPageNumbering = initData.displayPageNumbering; //should the page numbering be displayed for this topic?
    this.firstPageId = initData.firstPageId;
    this.subTopicIds = initData.subTopicIds;	
    
    this.numberOfPages = initData.pages.length;
    
    /*
     * Do two things:
     * 1. create an object that hashes page Ids against page objects for easy look up of pages.
     * 2. create an array of the page objects
     */
    this.pagesHash = {};
    this.pages = [];
	//alert(initData.pages.length);
    for (var i = 0; i < initData.pages.length; i++) {
        var p = new FLN.Page(initData.pages[i],this.id);
        this.pagesHash[p.id] = p;
        this.pages.push(p);
    }
};
/**
 * returns the first html page in the scoData object
 * @return {Object} a page object.
 */
FLN.Topic.prototype.getFirstPage = function(){
	return this.pagesHash[this.firstPageId];	
};

/**
 * returns an array of pages that have the specified status
 * @param {String} s
 * @return {Array} result an array of string ids
 */
FLN.Topic.prototype.getPagesByStatus = function(s){
    var result = FLN.Utils.filterObjectArray(this.pages,{status:s});
    return result;
}

FLN.Topic.prototype.getPageById = function(id){
	return this.pagesHash[id];
}

FLN.Topic.prototype.getCompletedPagesThatContributeToCompletionArray = function(){
	var result = FLN.Utils.filterObjectArray(this.pages,{status:"completed",contributeToCompletion:true});
	return result;	
}

FLN.Topic.prototype.getCompletedPagesArray = function(){
	var result = FLN.Utils.filterObjectArray(this.pages,{status: "completed"});
	return result;		
}
/**
 * gets if this topic is complete. a topic is considered complete if all its contributeToCompletion pages are "completed"
 * @return {Boolean} topicCompleted is this topic complete?
 */
FLN.Topic.prototype.isComplete = function(){
	var pages = this.pages;
	var topicCompleted = true;
    for (var i = 0; i < pages.length; i++) {
        if ((pages[i].status != "completed") && (pages[i].contributeToCompletion == true)) {
            topicCompleted = false;
        }
    }
    return topicCompleted;
};

/*
 * Test topic stuff
 */
/*
var topic = new FLN.Topic({
    id: "menu", //id for this topic. must be unique
    numberOfPagesInDisplay: "Menu", //the total number of pages in the topic that should appear in the section that displays your current page.
    displayPageNumbering: false, //should the page numbering be displayed for this topic?
    pages: [{
        id: "main_menu.html", //id for this page. must be unique
        prevId: "", //id of the page previous to this one
        nextId: "", //id of the page after this one
        pageNumber: "", //the number that should appear in the section that displays your current page in the whole module.
        topicPageNumber: "", //the number that should appear in the section that displays your current page in the current topic.
        enableMenuButtonPolicy: "always", //forces the menu button to be visible ("always") or invisible ("never"), or rely on the FLN.scoDatadisplayMenuPrereq array ("default")
        status: "not attempted",
        contributeToCompletion: false, //does this page count toward completion? it may not matter to overall completion of the module if a particular page is not completed
        bookmarkable: true, //does this page make bookmarks? Some pages might not
        enableNextPolicy: "never", //when is the next button enabled? three values: "never": never display, "always": always display, "completed": only display if the page is completed
        enableBackPolicy: "never" //when is the back button enabled? two values: "never": never display, "always": always display
    },{
        id: "main_menu1.html", //id for this page. must be unique
        prevId: "", //id of the page previous to this one
        nextId: "", //id of the page after this one
        pageNumber: "", //the number that should appear in the section that displays your current page in the whole module.
        topicPageNumber: "", //the number that should appear in the section that displays your current page in the current topic.
        enableMenuButtonPolicy: "always", //forces the menu button to be visible ("always") or invisible ("never"), or rely on the FLN.scoDatadisplayMenuPrereq array ("default")
        status: "not attempted",
        contributeToCompletion: false, //does this page count toward completion? it may not matter to overall completion of the module if a particular page is not completed
        bookmarkable: true, //does this page make bookmarks? Some pages might not
        enableNextPolicy: "never", //when is the next button enabled? three values: "never": never display, "always": always display, "completed": only display if the page is completed
        enableBackPolicy: "never" //when is the back button enabled? two values: "never": never display, "always": always display
    },{
        id: "main_menu2.html", //id for this page. must be unique
        prevId: "", //id of the page previous to this one
        nextId: "", //id of the page after this one
        pageNumber: "", //the number that should appear in the section that displays your current page in the whole module.
        topicPageNumber: "", //the number that should appear in the section that displays your current page in the current topic.
        enableMenuButtonPolicy: "always", //forces the menu button to be visible ("always") or invisible ("never"), or rely on the FLN.scoDatadisplayMenuPrereq array ("default")
        status: "not attempted",
        contributeToCompletion: true, //does this page count toward completion? it may not matter to overall completion of the module if a particular page is not completed
        bookmarkable: true, //does this page make bookmarks? Some pages might not
        enableNextPolicy: "never", //when is the next button enabled? three values: "never": never display, "always": always display, "completed": only display if the page is completed
        enableBackPolicy: "never" //when is the back button enabled? two values: "never": never display, "always": always display
    }]
});

//alert(topic.getPagesByStatus("not attempted").length);
//topic.getPageById("main_menu2.html").setStatus("asad");
//alert(topic.getPagesByStatus("not attempted").length);
//alert(topic.getCompletedPagesThatContributeToCompletionArray().length);
//alert(YAHOO.lang.dump(topic,3));
*/
