﻿SADE =
{
    __namespace: true
}

SADE.RenkliDergi =
{
    __namespace: true,

    AjaxFailed: function(e)
    {
        //alert(e.get_statusCode());
        alert(e.get_message());
    },
    
    IsKeyPressed: function(e, code)
    {
        var e = (e ? e : event);
        
        if (!e) return false;
        
        var charCode = (e.charCode ? e.charCode : (e.keyCode ? e.keyCode : (e.which ? e.which : 0)));

        return (charCode == code);
    },
    
    Tab: function(tab, group, section)
    {
        jQuery("div.tablist a").removeClass("active");
        
        jQuery(tab).addClass("active");
        
        jQuery("div.tabGroup" + group).hide();
        
        jQuery("#tab_" + group + "_" + section).show();
    },

    StatementCommentAdd: function()
    {
        var commentText = $("commentText");

        var commentSubmit = $("commentSubmit");

        if (commentText.value == "")
        {
            commentText.focus();

            jQuery(commentText).effect("highlight", {}, 2500);

            return;
        }

        window.setTimeout
        (
            function() { commentSubmit.disabled = true; },
            0
        );

        SADE.RenkliDergi.Service.RenkliDergi.StatementCommentAdd
            (
                SADE.RenkliDergi.WallId,
                commentText.value,
                SADE.RenkliDergi.StatementCommentAddSuccess,
                SADE.RenkliDergi.AjaxFailed
            );
    },

    StatementCommentAddSuccess: function(returnValue, requestContext)
    {
        window.location.href = window.location.pathname;
    },

    StatementCommentDelete: function(id)
    {
        var commentSubmit = $("commentSubmit");

        window.setTimeout
        (
            function() { commentSubmit.disabled = true; },
            0
        );

        SADE.RenkliDergi.Service.RenkliDergi.StatementCommentDelete
            (
                id,
                SADE.RenkliDergi.StatementCommentDeleteSuccess,
                SADE.RenkliDergi.AjaxFailed
            );
    },

    StatementCommentDeleteSuccess: function(returnValue, requestContext)
    {
        window.location.href = window.location.pathname;
    },
    
    CommentAdd: function()
    {
        var commentText = $("commentText");

        var commentSubmit = $("commentSubmit");

        if (commentText.value == "")
        {
            commentText.focus();
            
            jQuery(commentText).effect("highlight", {}, 2500);
        
            return;
        }

        window.setTimeout
        (
            function() { commentSubmit.disabled = true; },
            0
        );

        SADE.RenkliDergi.Service.RenkliDergi.CommentAdd
            (
                SADE.RenkliDergi.TopicId,
                null,
                commentText.value,
                SADE.RenkliDergi.CommentAddSuccess,
                SADE.RenkliDergi.AjaxFailed
            );
    },

    CommentAddSuccess: function(returnValue, requestContext)
    {
        window.location.href = window.location.pathname;
    },
    
    CommentDelete: function(id)
    {
        var commentSubmit = $("commentSubmit");

        window.setTimeout
        (
            function() { commentSubmit.disabled = true; },
            0
        );

        SADE.RenkliDergi.Service.RenkliDergi.CommentDelete
            (
                id,
                SADE.RenkliDergi.CommentDeleteSuccess,
                SADE.RenkliDergi.AjaxFailed
            );
    },

    CommentDeleteSuccess: function(returnValue, requestContext)
    {
        window.location.href = window.location.pathname;
    },

    TopicWrite: function()
    {
        var topicTitle = $("topicTitle");

        var WYSIWYG = $(SADE.RenkliDergi.WYSIWYG);
        
        var topicSubmit = $("topicSubmit");

        if (topicTitle.value == "")
        {
            topicTitle.focus();
            
            jQuery(topicTitle).effect("highlight", {}, 2500);
        
            return;
        }
        
        if (WYSIWYG.getHTML() == "")
        {     
            alert("Lütfen yazı alanını boş bırakmayınız.");
            return;
        }

        window.setTimeout
        (
            function() { topicSubmit.disabled = true; },
            0
        );

        SADE.RenkliDergi.Service.RenkliDergi.TopicSubmit
            (
                topicTitle.value,
                WYSIWYG.getHTML(),
                SADE.RenkliDergi.TopicWriteSuccess,
                SADE.RenkliDergi.AjaxFailed
            );
    },
    
    TopicWriteSuccess: function(returnValue, requestContext)
    {
        alert("Tebrikler Kalemşör, yazınız editör onayına alındı.");

        window.location.href = "/";
    },
    
    Digg: function()
    {
        SADE.RenkliDergi.Service.RenkliDergi.Digg
            (
                SADE.RenkliDergi.TopicId,
                SADE.RenkliDergi.DiggSuccess,
                SADE.RenkliDergi.AjaxFailed
            );
    },
    
    DiggSuccess: function(returnValue, requestContext)
    {
        if (returnValue.Status)
            jQuery("atacla").fadeOut("slow");
        else
            jQuery("atacla").fadeIn("slow");
    },
    
    VideoInfo: function()
    {
        var videoUrl = jQuery("#videoUrl");
       
        var videoSubmit = jQuery("#videoSubmit");

        if (videoUrl.val() == "")
        {
            return;
        }
        
        window.setTimeout
        (
            function() { videoSubmit.attr("disabled", "true") },
            0
        );
    
        SADE.RenkliDergi.Service.RenkliDergi.VideoInfo
            (
                videoUrl.val(),
                SADE.RenkliDergi.VideoInfoSuccess,
                SADE.RenkliDergi.AjaxFailed
            );
    },
    
    VideoInfoSuccess: function(returnValue, requestContext)
    {
        var videoUrl = jQuery("#videoUrl");
       
        var videoSubmit = jQuery("#videoSubmit");
    
        switch (returnValue.Status)
        {
            case 1:
            case 2:
                videoUrl.select();
                videoUrl.focus();
                
                videoSubmit.removeAttr("disabled");
                
                alert(returnValue.Message);
                break;
            default:
                jQuery("#videoTitle").val(returnValue.Video.Title);
                jQuery("#videoDescription").val(returnValue.Video.Description);
            
                alert(returnValue.Video);
                break;
        }
    }
}