$(function() {

    var buttons = $('img[name=add_btn], #close_btn, #submit_btn');
    buttons.css('cursor','pointer');
    buttons.css('cursor','hand');
    
    var totalMin = 20;
    var totalMax = 300; 
    
    var count_content = function() {
        var content = $('#content_rply').val();

        if (content.length < totalMin)
        {
	        var count = (totalMin - parseInt(content.length, 10));
        	var output = '您的內容尚不足 <span>' + count + '</span> 字';
        } 
        else if (content.length < totalMax)
        {
	        var count = (totalMax - parseInt(content.length, 10));
        	var output = '您還可輸入 <span>' + count + '</span> 字';
        } 
        else 
        {
        	var output = '您還可輸入 <span>0</span> 字';
        }

        $('#content_rply_count').empty();
        $('#content_rply_count').append(output);
    };

    $('img[name=add_btn]').click(function(){
        var isLogin = checkLogin();

        if (isLogin)
        {
            $.blockUI({
                message: $('#guru_reply_block'),
                css: {
                    border: 'none',
                    backgroundColor: '#000',
                    top: ($(window).height() - 384) /2 + 'px', 
                    left: ($(window).width() - 511) /2 + 'px'
                }
            });
        }
    });

    $('#close_btn').click(function() {
        $.unblockUI();
        return false;
    });


    $('#content_rply').val('');
    $('#content_rply').keyup(function(){
        count_content();
    });
    
    $('#submit_btn').click(function() {
        var content = $('#content_rply').val();
        
        if (content.length < totalMin)
        {
        	var count = (totalMin - parseInt(content.length, 10));
            alert('您的內容尚不足 ' + count + ' 字');
        }
        else if (content.length > totalMax)
        {
        	var count = (parseInt(content.length, 10) - totalMax);
            alert('您的內容超過 ' + count + ' 字');
        }
         else
        {
            $('#result_message').find('h2').text('資料傳送中，請稍後..');
            $.blockUI({ 
                message: $('#result_message'), 
                css: { 
                    width: '390px', height: '100px', top: ($(window).height() - 115) /2 + 'px', left: ($(window).width() - 390) /2 + 'px',
                    border: 'none', padding: '5px', backgroundColor: '#ffe8ab', '-webkit-border-radius': '10px', '-moz-border-radius': '10px',                    
                    opacity: '.8', color: '#fff'
                } 
            });    

            $.ajax({
                async: false, type: "POST", url: DOC_ROOT + '/inc/discussion/async_add_reply.php', 
                data: {rand: Math.random(), id: $('#articleID').val(), title: $('#title_rply').val(), content: $('#content_rply').val(), type: 'redirect'},
                success: function(data){
                    result = JSON.parse(data);
                    if(result.status == 'false')
                    {
                        $('#result_message').find('h2').text(result.message);
                        setTimeout($.unblockUI, 1000);
                    }
                    else
                    {
                        $(this).unbind('click');
                        $('#result_message').find('h2').text(result.message);
                        location.href = result.url;
                        setTimeout('location.href = result.url', 1000);
                    }              
                }
            });        
        }
    });
});
