﻿var Comments = {
    commentlock: 0,
    SendComment: function(id, cType, pCommentId) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        } else {
            if (!this.commentlock) {
                this.commentlock = 1;
                var comment = $('#txtComment').val();
                $.ajax({
                    type: 'POST',
                    url: RootUrl + "/ajax/Comments.ashx",
                    dataType: 'json',
                    data: 'act=addcomment&cId=' + id + '&cType=' + cType + '&pCommentId=' + pCommentId + '&text=' + comment,
                    success: function(RESULT) {
                        this.commentlock = 0;
                        if (RESULT.status == true) {
                            $('#writecomment').hide();
                            var htmlResult = "<div class='modalTitle'><h1>Yorum ekleme<h1></div><div class='okayMSGFormat'><p>Yorumunuz başarıyla kaydedildi.</p></div><div class='popupBtnContain fix'><a href='javascript:void(0);' class='modal-closer btnOne darkBlueBac floatRight'>Tamam<span></span></a></div>";                            
                            openDialogWithSetHTML(htmlResult);
                            //$('#commentresult').html("Yorumunuz başarıyla gönderildi.");
                           // $('#commentresult').fadeIn(500);
                            $('#txtComment').val('');
                        } else {
                            var message = (RESULT.message.length > 0) ? v.message : 'Yorum ekleme başarısız';
                            alert(message);
                        }
                    }
                });
            }
        }
    },
    SendFeedComment: function(id, cType, pCommentId, txtElement) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        } else {
            var comment = $('#' + txtElement).val();
            if (comment == '') {
                $('#imgWaiting' + id).hide();
                $('#spnStatus' + id).show();
                $('#spnStatusText' + id).html('Yorum kısmı boş olmamalı');
                setTimeout("$('#spnStatus" + id + "').hide()", 3000);
                return;
            }
            $('#spnStatus' + id).show();
            $.ajax({
                type: 'POST',
                url: RootUrl + "/ajax/Comments.ashx",
                dataType: 'json',
                data: 'act=addcomment&cId=' + id + '&cType=' + cType + '&pCommentId=' + pCommentId + '&text=' + comment,
                success: function(RESULT) {
                    if (RESULT.status == true) {
                        $('#spnStatusText' + id).html('Yorumunuz başarıyla gönderildi');
                        $('#' + txtElement).val('');
                        Comments.GetPreviousFeeds(id);
                    } else {
                        $('#spnStatus' + id).show();
                        $('#spnStatusText' + id).html('Yorumunuz gönderilemedi');
                        var message = (RESULT.message.length > 0) ? v.message : 'Yorum ekleme başarısız';
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $('#spnStatus' + id).show();
                    $('#spnStatusText' + id).html('Yorumunuz gönderilemedi');
                },
                complete: function() {
                    $('#imgWaiting' + id).hide();
                    setTimeout("$('#spnStatus" + id + "').hide()", 5000);
                }
            });
        }
    },
    GetPreviousFeeds: function(feedId) {
        $.ajax({
            type: 'POST',
            url: RootUrl + "/ajax/Comments.ashx",
            data: 'act=previousComments&fId=' + feedId,
            success: function(RESULT) {
                $('#previousComments' + feedId).html(RESULT);
            }
        });
    },
    DeleteComment: function(pCommentId) {

        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {

            var comment = $('#txtComment').val();
            $.getJSON('/Ajax/Comments.ashx?act=deletecomment&commentId=' + pCommentId,
                    function(data) {
                        if (data.status == true)
                        //alert('Yorumunuz silindi');
                            $('#comment' + pCommentId).fadeOut(500);

                        else {
                            var message = (data.message.length > 0) ? data.message : 'Yorum silme başarısız';
                            alert(message);
                        }
                    }
                )
        }
    },

    DiggComment: function(commentId) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        } else {
            // $('#commentButtons'+commentId).html("<img src='../images/l4.gif'/>");
            var comment = $('#txtComment').val();
            $.getJSON('/Ajax/Comments.ashx?act=digg&commentId=' + commentId,
                function(data) {
                    if (data.status == true) {
                        // alert(data.message);
                        $('#commentOy' + commentId).html(data.message);
                    } else {
                        var message = (data.message.length > 0) ? data.message : 'Yorum ekleme başarısız';
                        alert(message);
                    }
                }
            )
        }
    },

    BuryComment: function(commentId) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {
            var comment = $('#txtComment').val();
            $.getJSON('/Ajax/Comments.ashx?act=bury&commentId=' + commentId,
                function(data) {
                    if (data.status == true) {
                        //  alert('Yorumu yerin dibine geçirdiniz');
                        $('#commentOy' + commentId).html(data.message);
                    } else {
                        var message = (data.message.length > 0) ? data.message : 'Yorum ekleme başarısız';
                        alert(message);
                    }
                }
            )
        }
    },

    ReportComment: function(commentId, note, cat) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {
            //    var comment = $('#txtComment').val();

            $.getJSON('/Ajax/Comments.ashx?act=addcommentreport&commentId=' + commentId + '&note=' + note + '&cat=' + cat,
                function(data) {
                    if (data.status == true)
                        alert('İhbarınız bize ulaştı');
                    else {
                        var message = (data.message.length > 0) ? data.message : 'İhbar eklenemedi';
                        alert(message);
                    }
                })
        }
    }
}



var Rating = {
    Rate: function(rating, contentId, contentType) {
        //$('#yukleniyor').show();
        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {
            $.getJSON('/Ajax/Ratings.ashx?act=rate&cId=' + contentId + '&cType=' + contentType + '&r=' + rating,
                function(data) {
                    if (data.status == true) {
                        Rating.SetDynamicStars(data.rating);
                        //  $('#spanRateCount<%= ContentId %><%= ContentType %>').html(data.ratecount);

                        var htmlResult = "<div class='modalTitle'><h1>Oy verme<h1></div><div class='okayMSGFormat'><p>" + data.rating + " puanınız kaydedildi.<br />Video oylamasına katıldığınız için teşekkür ederiz.</p></div><div class='popupBtnContain fix'><a herf='#' class='modal-closer btnOne darkBlueBac floatRight'>Tamam<span></span></a></div>";
                        openDialogWithSetHTML(htmlResult);
                        // $('#resMessage').html(data.rating + " puanınız başarıyla gönderildi.");
                        // $('#resMessage').show();
                    }
                    else {
                        var message = (data.message.length > 0) ? data.message : 'Oy verme başarısız';
                        alert(message);


                    }
                }
            )
        }
    },
    SetDynamicStars: function(value) {
        $('#userrating').attr("style", "width:" + value * 20 + "%");
    }
}

var Favorites = {
    AddFavorite: function(contentType, contentId) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {
            $.getJSON('/Ajax/Favorites.ashx?act=addfav&cId=' + contentId + '&cType=' + contentType,
                function(data) {
                 
                    if (data.status == true) {
                        $('#resMessages').html(data.message);
                        openDialogWithSetHTML(data.message);
                        
                    }
                    else {
                        var message = (data.message.length > 0) ? data.message : 'Favori ekleme başarısız';
                        $('#resMessage').html(message);
                        openDialogWithSetHTML(message); 
                    }
                    
                }
            )
        }
    },
    RemoveFavorite: function(contentType, contentId) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {
            $.getJSON('/Ajax/Favorites.ashx?act=delfav&cId=' + contentId + '&cType=' + contentType,
                function(data) {
                    if (data.status == true) {
                        var htmlResult = "<div class='modalTitle'><h1>Favorilerinizden silindi<h1></div><div>Bu video favorilerinizden silindi.</div>";
                        openDialogWithSetHTML(htmlResult);
                        $('#liVideo' + contentId).hide();
                    }
                    else {
                        var htmlResult = "<div class='modalTitle'><h1>Favorilerinizden silinemedi<h1></div><div>Video favorilerinizden silinirken bir hata oldu <br />. Lütfen birazdan tekrar deneyiniz</div>";
                        openDialogWithSetHTML(htmlResult);
                    }
                }
            )
        }
    }
}



function limitChars(textid, limit, infodiv) {
    var text = $('#' + textid).val();
    var textlength = text.length;
    if (textlength > limit) {
        //$('#' + infodiv).html(limit+' karakter limitini doldurdunuz.');
        $('#' + textid).val(text.substr(0, limit));
        return false;
    }
    else {
        $('#' + infodiv).html((limit - textlength) + " karakter kaldı.");
        return true;
    }
}


SearchFromBig.init = function(event, elem) {

    var KEY = { ENTER: 13, ESC: 27 };
    var data = elem.value;
    switch (event.keyCode) {

        case KEY.ENTER:
            SearchFromBig();
            break;
    }

}


function Search() {
    var key = encodeURIComponent($('#txtSearch').val());
    var cat = encodeURIComponent($('#selCategory').val());
    if (key != 'Aramak için tıklayınız.') {
        window.location = RootUrl + "/search.aspx?kw=" + key + "&cat=" + cat;
        return false;
    }
}

function SearchFromBig() {
    var key = encodeURIComponent($('#txtBigSearch').val());
    var cat = encodeURIComponent($('#selCategory').val());
    window.location = RootUrl + "/search.aspx?kw=" + key + "&cat=" + cat;
    return false;
}




jQuery(document).ready(function ()
{
    $('#txtSearch').keyup(function (event)
    {
        if (event.keyCode == 13 || event.keyCode == '13')
        {
            event.cancel;
            var key = encodeURIComponent($('#txtSearch').val());
            var cat = encodeURIComponent($('#selCategory').val());
            window.location = RootUrl + "/search.aspx?kw=" + key + "&cat=" + cat;
            // parent.window.location = url;
        }
    });

    $('#txtBigSearch').keyup(function (event)
    {
        if (event.keyCode == 13 || event.keyCode == '13')
        {
            event.cancel;
            var key = encodeURIComponent($('#txtSearch').val());
            var cat = encodeURIComponent($('#selCategory').val());
            window.location = RootUrl + "/search.aspx?kw=" + key + "&cat=" + cat;
            // parent.window.location = url;
        }
    });

    $('#txtSearch').one("click", function (event)
    {
        $('#txtSearch').val('');
    });
});

function getParameterByName(name)
{
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}



var MessProcess = {
    OpenMessageDialog: function(userid, username, useravatar) {
        modalWindow.windowId = "messageDialog";
        modalWindow.content = '<div id="messageWindow" style="width:400px;height:200px">    <img id="messAvatar"/>Kime :<span id="messToUser"></span>Başlık :<input type="text" id="messTitle">Açıklama :<input type="text" id="messText"><input type="hidden" id="messUserId" /><a onclick="return MessProcess.Send()">Gönder </a><a onclick="return modalWindow.close()">İptal</a></div><span  id="messLoading" style="display:none;" ><img src="images/l3.gif"/></span>';
        modalWindow.open();
        $("#messAvatar").attr("src", useravatar);
        $("#messToUser").html(username);
        $("#messUserId").val(userid);
    },
    Send: function() {
        var touserid = $("#messUserId").val();
        var messtitle = $("#messTitle").val();
        var messtext = $("#messText").val();
        $("#messLoading").show();
        $.ajax({
            type: 'POST',
            url: "ajax/AjaxPrivate.ashx",
            data: "action=createThread&touserids=" + touserid + "&title=" + messtitle + "&text=" + messtext,
            success: function(RESULT) {
                $("#messLoading").hide();
                if (RESULT != "-1") {
                    MessProcess.ShowMessage("Mesajınız başarıyla gönderildi.");
                }
                else {
                    MessProcess.ShowMessage("Mesaj gönderimi sırasında hata oluştu. Lütfen tekrar deneyiniz.");
                }
            }
        }
            );
    },
    ShowMessage: function(messageText) {
        modalWindow.SetContent('messageDialog', "<div>" + messageText + "</div>");
        modalWindow.waitAndClose(2000);
    }
}


var Follow = {
    AddToFollowings: function(followid) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {
            $("#followProcess" + followid).show();
            $.ajax({
                type: 'POST',
                url: RootUrl + "/ajax/AjaxPrivate.ashx",
                data: "action=addtofollowings&followingid=" + followid,
                success: function(RESULT) {
                    $("#followProcess" + followid).hide();
                    if (RESULT == "1") {
                        Follow.ShowMessage("Takip listenize eklendi.", followid);
                        $("#fallowStatus" + followid).show();
                        Follow.ToggleButtons(false, followid);
                    }
                    else {
                        Follow.ShowMessage("Takip listenize eklerken hata oluştu. Lütfen tekrar deneyiniz.", followid);
                    }
                }
            });
        }
    },
    RemoveFromFollowings: function(followid) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {
            $("#followProcess" + followid).show();
            $("#messageAlert" + followid).hide();

            $.ajax({
                type: 'POST',
                url: RootUrl + "/ajax/AjaxPrivate.ashx",
                data: "action=removefromfollowings&followingid=" + followid,
                success: function(RESULT) {
                    $("#followProcess" + followid).hide();
                    if (RESULT == "1") {
                        Follow.ShowMessage("Takip listenizden silindi.", followid);
                         $("#fallowStatus" + followid).hide();
                         $("#listItemFallow" + followid).animate({ opacity: "hide" }, "slow");
                        Follow.ToggleButtons(true, followid);
                    }
                    else {
                        Follow.ShowMessage("Takip listenizden silerken hata oluştu. Lütfen tekrar deneyiniz.", followid);
                    }
                }
            });
        }
    },
    BlockFollower: function(followid) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {
            $("#followProcess" + followid).show();
            $("#messageAlert" + followid).hide();

            $.ajax({
                type: 'POST',
                url: RootUrl + "/ajax/AjaxPrivate.ashx",
                data: "action=blockfollower&followingid=" + followid,
                success: function(RESULT) {
                    $("#followProcess" + followid).hide();
                    if (RESULT == "1") {
                        $("#blockFollow" + followid).hide();
                        $("#unBlockFollow" + followid).show();
                        Follow.ShowMessage("Kullanıcı Engellendi.", followid);
                        $("#fallowStatus" + followid).hide();
                        $("#removeFromFollowings" + followid).hide();
                        $("#sendMessage" + followid).hide();
                        $("#blockStatus" + followid).show();
                    }
                    else {
                        Follow.ShowMessage("Takip listenizden silerken hata oluştu. Lütfen tekrar deneyiniz.", followid);
                    }
                }
            });
        }
    },
    RemoveBlockFromFollower: function(followid) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {
            $("#followProcess" + followid).show();
            $("#messageAlert" + followid).hide();

            $.ajax({
                type: 'POST',
                url: RootUrl + "/ajax/AjaxPrivate.ashx",
                data: "action=removeblockfromfollower&followingid=" + followid,
                success: function(RESULT) {
                    $("#followProcess" + followid).hide();
                    if (RESULT == "1") {
                        $("#blockFollow" + followid).show();
                        $("#unBlockFollow" + followid).hide();
                        Follow.ShowMessage("Engellmeme Kaldırıldı.", followid);
                        $("#fallowStatus" + followid).show();
                        $("#removeFromFollowings" + followid).show();
                        $("#sendMessage" + followid).show();
                        $("#blockStatus" + followid).hide();
                    }
                    else {
                        Follow.ShowMessage("Takip listenizden silerken hata oluştu. Lütfen tekrar deneyiniz.", followid);
                    }
                }
            });
        }
    },
    ShowMessage: function(messageText, userid) {
        $("#messageAlert" + userid).show();
        $("#messageAlert" + userid).fadeOut(2000);
        $("#messageAlert" + userid).html(messageText);
    },
    ToggleButtons: function(ShowAdd, userid) {
        if (ShowAdd) {
            $("#addToFollowings" + userid).show();
            $("#removeFromFollowings" + userid).hide();
        } else {
            $("#addToFollowings" + userid).hide();
            $("#removeFromFollowings" + userid).show();
        }
    }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////

var Feeds = {
    lock: 0,
    currentFocus: '',
    UpdateStatus: function() {
        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {
            $("#updateProcess").show();
            var statusText = $("#statusText").val();
            $.ajax({
                type: 'POST',
                url: RootUrl + "/ajax/AjaxPrivate.ashx",
                data: "action=sendFeed&feedtype=STATUSUPDATE&statusText=" + statusText + "&returnAll=1",
                success: function(RESULT) {
                    $("#updateProcess").hide();
                    if (RESULT != "-1") {
                        window.parent.$("#userFeeds").html(RESULT);
                    }
                    else {
                        Follow.ShowMessage("Takip listenize eklerken hata oluştu. Lütfen tekrar deneyiniz.", followid);
                    }
                }
            });
        }
    },
    DeleteFeed: function(fId) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {
            $.ajax({
                type: 'POST',
                url: RootUrl + "/ajax/AjaxPrivate.ashx",
                data: "action=deleteFeed&fId=" + fId,
                success: function(RESULT) {
                    if (RESULT != "-1") {
                        openDialogWithSetHTML("<div class='modalTitle'><h1>İçerik Silme</h1></div><div class='okayMSGFormat'>Paylaşım Silindi</div><div class='popupBtnContain fix'><a href='javascript:void(0);' class='modal-closer btnOne darkBlueBac floatRight'>Tamam<span></span></a></div>");
                        window.parent.$("#userFeeds").html(RESULT);
                    }
                    else {
                        openDialogWithSetHTML("<div class='modalTitle'><h1>İçerik Silme</h1></div><div class='okayMSGFormat'>Paylaşım silinirken bir hata oluştu.Lütfen tekrar deneyin.</div><div class='popupBtnContain fix'><a href='javascript:void(0);' class='modal-closer btnOne darkBlueBac floatRight'>Tamam<span></span></a></div>");
                    }
                    //parent.modalWindow.waitAndClose(1500);
                }
            });
        }
    },
    SendVideoFeed: function(vid, title) {
        if (!IsAuthenticated) {
            LogOrReg('login');
        }
        else {

            $.ajax({
                type: 'POST',

                url: RootUrl + "/ajax/AjaxPrivate.ashx",
                data: "action=sendFeed&feedtype=VIDEOFEED&vid=" + vid + "&title=" + encodeURIComponent(title),
                success: function(RESULT) {
                    $('#divShare').hide();
                    parent.SetWindowSize("okmsgsize");
                    $('#divShareResult').show();
                    if (RESULT != "-1") {
                        $('#txtShareResult').html('<div class="modalTitle"><h1>Profilinde paylaş</h1></div><div class="okayMsgTwo">Paylaşımınız profilinize başarıyla eklendi</div><div class="popupBtnContain fix"><a href="#" class="btnOne darkBlueBac floatRight modal-closer">Tamam<span></span></a></div>');
                        window.parent.modalWindow.setCloseButton();
                    }
                    else {
                        $('#txtShareResult').html('<div class="modalTitle"><h1>Profilinde paylaş</h1></div><div class="hataMsgTwo">Profilinizde paylaştırırken hata oldu.</div><div class="popupBtnContain fix"><a href="#" class="btnOne darkBlueBac floatRight modal-closer">Tamam<span></span></a></div>');
                        window.parent.modalWindow.setCloseButton();
                    }

                }
            });
        }
    },
    ShowWriteBox: function(fId) {
        if (Feeds.currentFocus != '') {
            Feeds.HideWriteBox(Feeds.currentFocus);
        }
        $('#divFeedComments' + fId).toggle(0);
        $('#divFeedFake' + fId).toggle(0);
        $('#txtComment' + fId).focus();
        Feeds.currentFocus = fId;
    },
    HideWriteBox: function(fId) {
        $('#divFeedFake' + fId).toggle(0);
        $('#divFeedComments' + fId).toggle(0);
    }
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}
