﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("BandSite");

BandSite.CommentList = function(element) {
    BandSite.CommentList.initializeBase(this, [element]);
    
    this._foreignID = null;
}

BandSite.CommentList.prototype = {
    initialize: function() {
        BandSite.CommentList.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
        var that = this;
        
        this._listView.add_constructingNewItem(
            function(sender, e) {                                
                e.item.DateTime = new Date();
                
                if ($find('SignInPanel')) {
                    var friend = $find('SignInPanel')._friend;
                    if (friend) {
                        e.item.FriendID = friend.ID;
                        e.item.FriendFirstName = friend.FirstName;
                        e.item.FriendLastName = friend.LastName;
                        e.item.FriendImageFileName = friend.ImageFileName;
                    } else {
                        programError("Should not be able to add a comment if not signed in");
                    }
                } else {
                    programError("SignInPanel not found");
                }            
                
                that._raiseEvent('constructingNewComment', {item: e.item});                
            }            
        );   
        
        this._listView.add_listLoaded(
            function(sender, e) {
                that._raiseEvent('listLoaded', e);
            }
        );     
        
        this._listView.add_itemSave(
            function(sender, e) {
                VisitorService.EmailAlert(
                    that._bandID,
                    "AltPro: Comment added",
                    (e.item.IsGuestbookComment ? "Guestbook Entry: " : "Comment: ") + e.item.Text + " by " + e.item.FriendFirstName + " " + e.item.FriendLastName,
                    function(result) {
                    },
                    webServiceError         
                );                
            }        
        );
        
        if ($find("SignInPanel")) {
            if ($find("SignInPanel")._friend) {
                this._addCommentPanelSet.showPanel("SignedInPanel");
            } else {
                this._addCommentPanelSet.showPanel("NotSignedInPanel");
            }
            
            $find("SignInPanel").add_signedIn(
                function(sender, e) {
                    that._addCommentPanelSet.showPanel("SignedInPanel");
                }
            );
            $find("SignInPanel").add_signedOut(
                function(sender, e) {
                    that._addCommentPanelSet.showPanel("NotSignedInPanel");
                }
            );            
        }
        
    },
    dispose: function() {        
        //Add custom dispose actions here
        BandSite.CommentList.callBaseMethod(this, 'dispose');
    },

    set_foreignID: function(foreignID) {
        this._foreignID = foreignID;
        this._listView.set_foreignID(foreignID);
    },    
    
    loadList:function() {
        this._listView.loadList();
    }
}
BandSite.CommentList.createProperty('bandID');
BandSite.CommentList.createProperty('listView');
BandSite.CommentList.createProperty('addCommentPanelSet');
BandSite.CommentList.createEvent('constructingNewComment');
BandSite.CommentList.createEvent('listLoaded');
BandSite.CommentList.registerClass('BandSite.CommentList', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
