﻿$(document).ready(function() {
jQuery.extend($.xs.profile, {
    weblog: {
        setup: function(intWeblogPostId, blnProfileMy) {
            if (blnProfileMy) {
                $('#WeblogNewPost').click(function() {
                    if ($.xs.isDisabled($(this))) return false;
                    $(this).attr('disabled',true);
                    
                    $('#WeblogForm').validate({
                        submitHandler: function(form) {
                            if ($.xs.isDisabled($('#Save'))) return false;
                            
                            $('#Save').attr('disabled',true);
                            var strPostData = $("#WeblogForm").serialize();
                            
                            $.ajax({
                                type: 'POST',
                                url: '/xseno/_site/pages/profile/_post.aspx?id=29',
                                data: strPostData,
                                success: function(data) {
                                    if (!$.xs.error.checker(parseFloat(data))) { $('#Save').attr('disabled',false); return false; }
                                    $.xs.redirect(window.location.href.replace('post/'+intWeblogPostId+'/',''));
                                }
                            });
                        },
                        focusInvalid: true,
                        rules: {
                            Subject: { required:true, minlength:2, maxlength:40 },
                            Message: { required:true, minlength:2, maxlength:8000 }
                           
                        },
                        messages: {
                            Subject: '<div class="b_misc_error">Een titel is verplicht.</div>',
                            Message: '<div class="b_misc_error">Een post is verplicht.</div>'
                            
                        }
                    });
                    
                    $('#Message').focus(function() { $.xs.emoticons('#Message'); });
                    $('#xs_mPh_WeblogNewPostPh').show();
                    $('#Subject').focus();
                    
                    if ($.browser.msie && $.browser.version < 7) {
                        $('.m_page').css('height','auto'); 
                    }
                });
                
                $('a[rel=DeletePost]').click(function() {
                    if ($.xs.isDisabled($(this))) return false;
                    if (confirm('Weet je zeker dat je deze comment wilt verwijderen?')) {
                        $(this).attr('disabled',true);
                        var strPostId = $(this).attr('id').replace('DeletePost','');
                        
                        $.ajax({
                            type: 'POST',
                            url: '/xseno/_site/pages/profile/_post.aspx?id=40',
                            data: 'PostId='+strPostId,
                            success: function(data) {
                                if (!$.xs.error.checker(parseFloat(data))) { $('#DeletePost'+strPostId).attr('disabled',false); return false; }
                                $('#Post'+strPostId).hide();
                            }
                        });
                    }
                });
            }
            
            else {
                $("input[name='Rating']").click(function() { 
                    if ($.xs.isDisabled($('input[name="Rating"]'))) return false;
                    $('input[name="Rating"]').attr('disabled',true);
                    
                    $.getJSON('/xseno/_site/pages/profile/_post.aspx?id=39&WeblogPostId='+intWeblogPostId+'&Rating='+$(this).val(), function(data) {
                        $('#xs_mPh_RatingPh').empty();
                        $('#xs_mPh_RatingPh').append('<strong>'+data.resultset.count+'x gestemd, gemiddelde:</strong> ');
                        
                        var i;
                        for (i=1;i<=data.resultset.total;i++) {
                            $('#xs_mPh_RatingPh').append('<img src="/xseno/_site/img/icons/star_red.gif" align="absbottom" />');
                        }
                    });
                });
            }
            
            $('#WeblogPostPostSave').click(function() {
                if ($.xs.isDisabled($(this))) return false;
                
                if ($('#WeblogPostPost').val().length == 0) {
                    alert('Type een reactie.');
                    return false;
                }
                
                $('#WeblogPostPostSave').attr('disabled',true);
                
                $.ajax({
                    type: 'POST',
                    url: '/xseno/_site/pages/profile/_post.aspx?id=38',
                    data: 'WeblogPostId='+intWeblogPostId+'&Message='+$.xs.urlEncode($('#WeblogPostPost').val()),
                    success: function(data) {
                        if (!$.xs.error.checker(parseFloat(data))) { $('#WeblogPostPostSave').attr('disabled',false); return false; }
                        window.location.reload();
                    }
                });
            });
            
            $('#WeblogPostPost').focus(function() { $.xs.emoticons('#WeblogPostPost'); });
        },
        edit: {
            setup: function(intPostId) {
                $('#DeletePost'+intPostId).click(function() {
                    if ($.xs.isDisabled($(this))) return false;
                    
                    if (confirm('Weet je zeker dat je deze weblog post wilt verwijderen?')) {
                        $(this).attr('disabled',true);
                         
                        $.ajax({
                            type: 'POST',
                            url: '/xseno/_site/pages/profile/_post.aspx?id=31',
                            data: 'PostId='+intPostId,
                            success: function(data) {
                                if (!$.xs.error.checker(parseFloat(data))) { $(this).attr('disabled',false); return false; }
                                $.xs.redirect(window.location.href.replace('post/'+intPostId+'/',''));
                            }
                        });
                    }
                });
                
                $('#EditPost'+intPostId).click(function() {
                    if ($.xs.isDisabled($(this))) return false;
                    
                    $(this).attr('disabled',true);
                    $('#PostPh'+intPostId).hide(); $('#PostForm'+intPostId).show();
                    
                    $('#PostForm'+intPostId).validate({
                        submitHandler: function(form) {
                            if ($.xs.isDisabled($('#SavePost'+intPostId))) return false;
                            
                            $('#SavePost'+intPostId).attr('disabled',true);
                            var strPostData = $('#PostForm'+intPostId).serialize();
                            
                            $.ajax({
                                type: 'POST',
                                url: '/xseno/_site/pages/profile/_post.aspx?id=30',
                                data: strPostData+'&PostId='+intPostId,
                                success: function(data) {
                                    if (!$.xs.error.checker(parseFloat(data))) { $(this).attr('disabled',false); return false; }
                                    window.location.reload();
                                }
                            });
                        },
                        focusInvalid: true,
                        rules: {
                            Subject: { required:true, minlength:2, maxlength:40 },
                            Message: { required:true, minlength:2, maxlength:8000 }
                           
                        },
                        messages: {
                            Subject: '<div class="b_misc_error">Een titel is verplicht.</div>',
                            Message: '<div class="b_misc_error">Een post is verplicht.</div>'
                        }
                    });
                    
                    $('#PostForm'+intPostId+' textarea[name=Message]').focus(function() { $.xs.emoticons('#'+$(this).attr('id')); });
                });
            }
        }
    }
});
});