﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("BandSite");

BandSite.Forum = function(element) {
    BandSite.Forum.initializeBase(this, [element]);
    
    this._forumCategoryID = null;
    this._forumTopicID = null;
}

BandSite.Forum.prototype = {
    initialize: function() {
        BandSite.Forum.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        var that = this;
        
        this._forumCategoryListView.add_itemClicked(
            function(sender, e) {
                that.loadCategory(e.id);   
            }
        );
        
        this._forumTopicListView.add_constructingNewItem(
            function(sender, e) {
                e.item.ForumCategoryID = that._forumCategoryID;
                e.item.DateTime = new Date();
                if ($find('SignInPanel')) {
                    e.item.StarterFriendID = $find("SignInPanel")._friend.ID;
                }
            }
        );
        
        this._forumTopicListView.add_itemClicked(
            function(sender, e) {
                that.loadTopic(e.id);
            }
        );
        
        this._forumTopicCommentList.add_constructingNewComment(
            function(sender, e) {
                if (! that._forumTopicID) {
                    programError("ForumTopicID must be set to construct new photo comment");
                }
                e.item.ForumTopicID = that._forumTopicID;
            }
        );
        
        this._backToCategoriesListButton.add_buttonClicked(
            function(sender, e) {
                that._panelSet.showPanel("ForumCategoriesPanel");
            }
        );

        this._backToTopicsListButton.add_buttonClicked(
            function(sender, e) {
                that._panelSet.showPanel("ForumTopicsPanel");
            }
        );
        
        this._categoryDetails.add_itemLoaded(
            function(sender, e) {
                that._categoryDetailsLoaded = true;
                if (that._categoryTopicsLoaded) {
                    setTimeout(function() {
                        that._panelSet.showPanel("ForumTopicsPanel");
                    },400);
                }                
            }
        );
        this._forumTopicListView.add_listLoaded(
            function(sender, e) {
                that._categoryTopicsLoaded = true;
                if (that._categoryDetailsLoaded) {
                    setTimeout(function() {
                        that._panelSet.showPanel("ForumTopicsPanel");
                    },400);
                }                
            }
        );
        
        this._topicDetails.add_itemLoaded(
            function(sender, e) {
                that._topicDetailsLoaded = true;
                if (that._topicCommentsLoaded) {
                    setTimeout(function() {
                        that._panelSet.showPanel("ForumTopicCommentsPanel");
                    },400);
                }                                
            }
        );
        this._forumTopicCommentList.add_listLoaded(
            function(sender, e) {
                that._topicCommentsLoaded = true;
                if (that._topicDetailsLoaded) {
                    setTimeout(function() {
                        that._panelSet.showPanel("ForumTopicCommentsPanel");
                    },400);
                }                                                
            }
        );
        
        if ($find("SignInPanel")) {
            if ($find("SignInPanel")._friend) {
                this._addTopicPanelSet.showPanel("SignedInPanel");
            } else {
                this._addTopicPanelSet.showPanel("NotSignedInPanel");
            }
            
            $find("SignInPanel").add_signedIn(
                function(sender, e) {
                    that._addTopicPanelSet.showPanel("SignedInPanel");
                }
            );
            $find("SignInPanel").add_signedOut(
                function(sender, e) {
                    that._addTopicPanelSet.showPanel("NotSignedInPanel");
                }
            );
        }
        
    },
    dispose: function() {        
        //Add custom dispose actions here
        BandSite.Forum.callBaseMethod(this, 'dispose');
    },
    
    loadCategory: function(categoryID) {
        this._panelSet.showPanel("ForumTopicsLoadingPanel");
        
        this._categoryDetailsLoaded = false;
        this._categoryTopicsLoaded = false;
        this._forumCategoryID = categoryID;
        this._categoryDetails.set_itemID(categoryID);
        this._categoryDetails.loadItem();
        this._forumTopicListView.set_foreignID(categoryID);
        this._forumTopicListView.loadList();
        
        // this._panelSet.showPanel("ForumTopicsPanel"); 
    },
    
    loadTopic: function(topicID) {
        this._panelSet.showPanel("ForumTopicCommentsLoadingPanel");

        this._topicDetailsLoaded = false;
        this._topicCommentsLoaded = false;
        this._topicDetails.set_itemID(topicID);
        this._topicDetails.loadItem();
        this._forumTopicID = topicID;
        this._forumTopicCommentList.set_foreignID(topicID);
        this._forumTopicCommentList.loadList();
        
        // this._panelSet.showPanel("ForumTopicCommentsPanel");        
    },
    
    resetSection: function() {
        this._panelSet.showPanel("ForumCategoriesPanel");    
    }
    
}
BandSite.Forum.createProperty("bandID");
BandSite.Forum.createProperty("panelSet");
BandSite.Forum.createProperty("forumCategoryListView");
BandSite.Forum.createProperty("categoryDetails");
BandSite.Forum.createProperty("forumTopicListView");
BandSite.Forum.createProperty("topicDetails");
BandSite.Forum.createProperty("forumTopicCommentList");
BandSite.Forum.createProperty("backToCategoriesListDiv");
BandSite.Forum.createProperty("backToCategoriesListButton");
BandSite.Forum.createProperty("backToTopicsListDiv");
BandSite.Forum.createProperty("backToTopicsListButton");
BandSite.Forum.createProperty("addTopicPanelSet");

BandSite.Forum.registerClass('BandSite.Forum', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
