﻿Type.registerNamespace('THBComment');

THBComment._Interface = function() {
}

THBComment._Interface.prototype = {
    pageLoad: function() {
    },
    dispose: function() {
    },
    setFocus: function() {
    },
    showReplyPanel: function(tbTitleClientID, tbCommentClientID, threadGuid, threadItemIndex, title) {
        if (Sys.Services.AuthenticationService.get_isLoggedIn() == true) {

            $get(btSendClientID).disabled = false;

            $addHandler( document.body, 'keydown', this.eventKeyDown );

            $get(commentCommandTypeClientID).value = "reply";

            var modalWrite = $find('ModalWrite');
            modalWrite.add_shown(function() { $get(tbCommentClientID).focus(); });
    
            $get(tbTitleClientID).value = $get(title).innerHTML.trim();
           
            $get(btSendClientID).value = "Répondre";
            $get(tbCommentClientID).value = "";
            $get(threadGuidClientID).value = threadGuid;
            $get(threadItemIndexClientID).value = threadItemIndex;
        
            AjaxControlToolkit.ModalPopupBehavior.invokeViaServer('ModalWrite', true);
        }
        else {
            THBLogin.Interface.setCallBack(Function.createDelegate(this , function(){this.showReplyPanel(tbTitleClientID, tbCommentClientID, threadGuid, threadItemIndex, title)}));
            THBLogin.Interface.showLogin();
        }
    },
    eventKeyDown: function(e) {
    
      if (e.keyCode ===  Sys.UI.Key.esc)
      {
        e.preventDefault();

        this.hideWritePanel();                
      }
    },
    hideReplyPanel: function() {
        $get('thb-panel-login').style.display = 'none';
    },
    showWritePanel: function(tbTitleClientID, tbCommentClientID) {
        if (Sys.Services.AuthenticationService.get_isLoggedIn() == true) {
        
            $get(btSendClientID).disabled = false;

            $addHandler( document.body, 'keydown', this.eventKeyDown );

            $get(commentCommandTypeClientID).value = "create";

            var modalWrite = $find('ModalWrite');
            modalWrite.add_shown(function() { $get(tbTitleClientID).focus(); });

            var tbTitle = $get(tbTitleClientID);            
            tbTitle.value = "";
            
            $get(btSendClientID).value = "Poster";
            $get(tbCommentClientID).value = "";
            $get(threadGuidClientID).value = "";
            AjaxControlToolkit.ModalPopupBehavior.invokeViaServer('ModalWrite', true);
        }
        else {
            THBLogin.Interface.setCallBack(Function.createDelegate(this , function(){this.showWritePanel(tbTitleClientID, tbCommentClientID)}));
            THBLogin.Interface.showLogin();
        }
    },
    hideWritePanel: function() {
        AjaxControlToolkit.ModalPopupBehavior.invokeViaServer('ModalWrite', false);
    }
};
THBComment._Interface.registerClass('THBComment._Interface', null, Sys.IDisposable);

THBComment.Interface = new THBComment._Interface();

if (Sys && Sys.Application) {
    Sys.Application.notifyScriptLoaded();
}
