function TCommentFormActions (params) {
    var _this = this;
    var postId;
    var formObject;
    var commentBtn;
    var divContainer;
    var captchaImg;
    
    function onCommentBtnClick() {
        if ($(divContainer).filter(':hidden').length) {
            _this.expendDiv();
        } else {
            _this.shrinkDiv();
        }
        return false;
    }
    
    function onFormSubmit(_this) {
        var form = _this.target;  
        var url  = _this.target.action;
        var data = {
            'data[comment][name]'    : (form['data[comment][name]'] != undefined)?form['data[comment][name]'].value:"",
            'data[comment][email]'   : (form['data[comment][email]'] != undefined)?form['data[comment][email]'].value:"",
            'data[comment][text]'    : (form['data[comment][text]'] != undefined)?form['data[comment][text]'].value:"",
            'data[comment][captcha]' : (form['data[comment][captcha]'] != undefined)?form['data[comment][captcha]'].value:""
        };
        var response = $.ajax({
            type : 'POST',
            url  : url, 
            data : data, 
            async : false,
        }).responseText;
        if ($.trim(response).length) {
            $("#comments").append(response);
            var newComment = $('#comments').children().filter(':last')[0];
            var commentHeight = $(newComment).height();
            var commentMargin = $(newComment).css('margin-bottom');
            $(newComment).height(0);
            $(newComment).css({'margin-bottom' : '0px'});
            FB.XFBML.Host.parseDomElement(newComment); 
            setTimeout( function () {
            	$(newComment).animate({'height' : commentHeight}, 1000, "linear", function(){
            		$(newComment).animate({'marginBottom' : commentMargin}, 400);
            	});
            }, 200);
        } 
        AuthManager.updateCommentBlock();
        return false;
    }
    
    this.init = function (params) {
        try {
            postId = params.postId;
            formObject   = document.getElementsByName('comment_form_' + postId)[0];
            formObject.onsubmit = onFormSubmit; 
            divContainer = document.getElementById('comment_block_' + postId);
            if (params.commentBtnId != null) {
                commentBtn   = document.getElementById(params.commentBtnId);
                commentBtn.controlObject = _this;
                commentBtn.onclick = onCommentBtnClick;
            }
            captchaImg = document.getElementById('captcha_' + postId );
        } catch(err) {
                
        }
    }
    this.expendDiv =  function () {
        var dat = new Date()
        captchaImg.src = "/captcha?cache=" + dat.getTime();
        $('.commentBlock').hide();
        $(divContainer).show();
    }

    this.shrinkDiv =  function () {
        $(divContainer).hide();
    }

    this.init(params);
    
}

TAuthManager = function(auth_type, post_id) {
    var _this = this;
    var _authType = auth_type;
    var _postId = post_id;
    this.Twitter  = new TTwitter(_this);
    this.Facebook = new TFacebook(_this); 

    this.init = function() {
       //_this.setAuthType(_authType);
           
    }
    
    this.checkConnectStatus = function() {
        FB.Connect.get_status().waitUntilReady( function( status ) {
           switch ( status ) { 
               case FB.ConnectState.connected : 
                   $(".authSimple").hide()
                   $(".authFacebook").show(); 
                   break; 
               case FB.ConnectState.appNotAuthorized: 
               case FB.ConnectState.userNotLoggedIn: 
                   $(".authFacebook").hide(); 
                   $(".authSimple").show()
           } 
       });         
    }
    
    this.setAuthType = function(auth_type) {
        switch(auth_type) {
            case 'auth_facebook' : {
                _authType = 'auth_facebook';
                $('.authBlock').hide();
                $('#facebookAuthBlock').show();
                break;
            }
            case 'auth_twitter' : {
                _authType = 'auth_twitter';
                $('.authBlock').hide();
                $('#twitterAuthBlock').show();
                break;
            }
            default : {
                _authType = 'auth_simple';
                $('.authBlock').hide();
                $('#simpleAuthBlock').show();
            }
        }
    }
    
    this.updateCommentBlock = function() {
        var commentBlock = $.ajax({
            type  : 'GET',
            url   : '/blog/commentBlock/' + _postId,
            async : false
        }).responseText;
        $("#commentFormContainer").html(commentBlock);
        
    }
    $(document).ready(_this.init);
}

/**
 *   Object manages twitter action 
 */
TTwitter = function(authManager) {
    var _this = this;
    var _authManager = authManager;
    var twitterWindow;
    var twitterInterval;
    
    /**
     *  Actions to be done when twitter 
     *  popup window closed
     */
    function checkTwitterWindow() {
        if (twitterWindow.closed) {
            clearInterval(twitterInterval);
            _authManager.updateCommentBlock();
        }
    } /*checkTwitterWindow function*/
    
    /**
     *  Show twitter login form  
     */
    this.startConnect = function() {
        var popupOptions = "location=0,status=0,width=800,heigth=500";
        twitterWindow = window.open('/blog/TWConnect','twitterWindow', popupOptions);
        twitterInterval = setInterval(checkTwitterWindow, 1000);
    } /*startConnect functon*/
    
    this.logout = function() {
        try {
            $.ajax({
                type  : 'GET',
                url   : '/blog/TWLogout/',
                async : false
            });
            _authManager.updateCommentBlock();
            return false;
        } catch (err) {
            return true;
        }
    }
}

TFacebook = function (authManager) {
    var _this = this;    
    var _authManager = authManager;
    
    this.logout = function() {
        try {
            $.ajax({
                type  : 'GET',
                url   : '/blog/FBLogout/',
                async : false
            });
            _authManager.updateCommentBlock();
            return false;
        } catch (err) {
            return true;
        }
    }
    
    this.startConnect = function() {
        _authManager.updateCommentBlock();
    }

}
                           