﻿// <![CDATA[
//var urlChatService = "http://localhost:52280/";
var urlChatService = "http://chatservice2010.naddate.com/";
var member_id;
var profile_status;
var member_level;

$(document).ready(function() {
    // 0 Initial Ajax
    $.ajaxSetup({
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        type: "POST"
    });
    member_id = $("#hdViewProfileActionMemberId").val();
    profile_status = $("#hdViewProfileActionProfileStatus").val();
    member_level = $("#hdViewProfileActionMemberLevel").val();
    //    $.ajax({ url: "data/getMemberId.aspx", success: function(ret) { member_id = ret; } });
    //    $.ajax({ url: "data/getMemberProfileStatus.aspx", success: function(ret) { profile_status = ret; } });
    //    $.ajax({ url: "data/getMemberLevel.aspx", success: function(ret) { member_level = ret; } });
    $("img.addToolTip").attr("style", "cursor:pointer;")

    // 1 Add Favor
    $("a[id$=FavorAction]").click(function() {
        if ($(this).attr('disabled') != "disabled") {
            if (member_id == 0) {
                messageAlert($(this).parentsUntil("#table-result").siblings(".message"), "กรุณาเข้าระบบก่อนทำการเก็บรายชื่อคนพิเศษ");
            } else {
                var favorite_id = $(this).parent(".img-button").attr("member_id");
                $(this).parentsUntil("#table-result").siblings(".message").html("<img src='./images/loading/ajax-loader-black.gif' style='margin-top:65px;' />").show();
                addFavor(favorite_id, $(this));
            }
        }
    });

    // PR Profile
    $("table[prprofile=1]").addClass("table-pr");
    $("table[prprofile=1] .td-image, table[prprofile=1] .td-info").addClass("td-pr");

    // 2 Send Inter
    $("a[id$=InterAction]").click(function() {
        if (!($(this).is("[disabled]"))) {
            if (member_id == 0) {
                messageAlert($(this).parentsUntil("#table-result").siblings(".message"), "กรุณาเข้าระบบก่อนทำการแสดงความสนใจ");
            } else {
                var interest_id = $(this).parent(".img-button").attr("member_id");
                //$(this).attr("src", "./images/loading/cyrcle-24-org.gif");
                $(this).parentsUntil("#table-result").siblings(".message").html("<img src='./images/loading/ajax-loader-black.gif' style='margin-top:65px;' />").show();
                sendInter(interest_id, $(this));
            }
        }
    });

    // 3 Send Msg
    $("a[id$=MsgAction]").click(function() {
        if (!($(this).is("[disabled]"))) {
            if (member_id == 0) {
                messageAlert($(this).parentsUntil("#table-result").siblings(".message"), "กรุณาเข้าระบบก่อนส่งข้อความให้สมาชิกท่านนี้");
            } else {
                window.location.href = "viewprofilemessage.aspx?id=" + $(this).parent(".img-button").attr("member_id");
            }
        }
    });

    // 4 Send SMS
    $("a[id$=SmsAction]").click(function() {
        if (!($(this).is("[disabled]"))) {
            if (member_id == 0) {
                messageAlert($(this).parentsUntil("#table-result").siblings(".message"), "กรุณาเข้าระบบก่อนส่งข้อความให้สมาชิกท่านนี้");
            } else {
                window.location.href = "viewprofilesms.aspx?id=" + $(this).parent(".img-button").attr("member_id");
            }
        }
    });

    // 5 Send Free Msg
    $("a[id$=FmsgAction]").click(function() {
        if (!($(this).is("[disabled]"))) {
            if (member_id == 0) {
                messageAlert($(this).parentsUntil("#table-result").siblings(".message"), "กรุณาเข้าระบบก่อนส่งฟรีข้อความจากนัดเดทให้สมาชิกท่านนี้");
            } else {
                window.location.href = "viewprofilefreemessage.aspx?id=" + $(this).parent(".img-button").attr("member_id");
            }
        }
    });

    // 6 Chat
    $("a[id$=ChatAction]").click(function() {
        if (!($(this).is("[disabled]"))) {
            if (member_id == 0) {
                messageAlert($(this).parentsUntil("#table-result").siblings(".message"), "กรุณาเข้าระบบ ก่อนทำการสนทนานะคะ");
            } else if (profile_status == "N") {
                messageAlert($(this).parentsUntil("#table-result").siblings(".message"), "ข้อมูลของท่านไม่ผ่านการตรวจสอบ ไม่สามารถทำการสนทนาได้ค่ะ");
            } else if (member_level == "F") {
                messageAlert($(this).parentsUntil("#table-result").siblings(".message"), "กรุณาสมัครสมาชิก Premium ก่อนทำการสนทนานะคะ");
            } else {
                startChat($(this).parent(".img-button").attr("member_id"));
                //chatbodyopen($(this).parent(".img-button").attr("member_id"), $(this).parent(".img-button").attr("user_name"));

            }
        }
    });

    //New Icon
    $(".img-button a[id$=Action]").mouseover(function() {
        var txt = $(this).attr("title");
        $(this).parent().children("#show-text").html(txt);
    }).mouseout(function() {
        $(this).parent().children("#show-text").html("");
    });

});

// Message Alert
function messageAlert(obj, msg) {
    obj.html(msg);
    obj.show();
    setTimeout(function() {
        obj.fadeOut("normal");
    }, 2500);
}

// f1 Add Favor
function addFavor(favorite_id, obj) {
    var json = { "member_id": member_id, "favorite_id": favorite_id };
    var jsons = JSON.stringify(json);

    //alert(jsons);

    $.ajax({
        url: "data/wsMember.asmx/addFavorite",
        data: jsons,
        success: function(ret) {
            messageAlert(obj.parentsUntil("#table-result").siblings(".message"), ret.d[0]);
            if (ret.d[1] == "Y") {
                obj.html("<span class='s24_png_gray s24_favor'></span>");
                obj.attr("disabled", "disabled");
            } else {
                obj.html("<span class='s24_png s24_favor'></span>");
            }

        },
        error: function(ev) {
            var x = $.parseJSON(ev.responseText);
            alert("insertion failed.\n" + x.Message);
        }
    });
}

// f2 Send Interest
function sendInter(interest_id, obj) {
    var json = { "member_id": member_id, "interest_id": interest_id };
    var jsons = JSON.stringify(json);

    //alert(jsons);

    $.ajax({
        url: "data/wsMember.asmx/sendInterest",
        data: jsons,
        success: function(ret) {
            messageAlert(obj.parentsUntil("#table-result").siblings(".message"), ret.d[0]);
            if (ret.d[1] == "Y") {
                obj.html("<span class='s24_png_gray s24_inter'></span>");
                obj.attr("disabled", "disabled");
            } else {
                obj.html("<span class='s24_png s24_inter'></span>");
            }

        },
        error: function(ev) {
            var x = $.parseJSON(ev.responseText);
            alert("insertion failed.\n" + x.Message);
        }
    });
}

// f3 Chat
function startChat(chatter_id) {
    //Add Chatter
    var json = { member_id: member_id, chatter_id: chatter_id };
    //var jsons = JSON.stringify(json);

    //alert(jsons);

    $.ajax({
        url: urlChatService + "ChatService.asmx/AddNewChatter",
        data: json,
        dataType: "jsonp",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        success: function(ret) {
            var cItms = ret.d;
            var cItem = eval("(" + cItms + ")");
            //var cItem = ret.d;
            $("#hdReceiverId").val(cItem.MemberId);
            $("#td-message").show();
            $("#chat-win").css("width", "610px");

            $("#imgReceiverImage").attr("src", "thumbMember.aspx?filename=" + cItem.MemberId + "/" + cItem.ImageMember + "&size=50");
            $("#aReceiverURL").attr("href", "viewprofile.aspx?id=" + cItem.MemberId);
            $("#spReceiverName").html(cItem.UserName);

            if ((cItem.OnlineStatus == 0) || (cItem.ChatStatus == 0)) {
                $("#txtMessage").attr("disabled", "disabled");
            } else {
                $("#txtMessage").removeAttr("disabled");
            }
        },
        complete: function(ret) {
            if ($("#chat-max").is(":hidden")) {
                $("#chat-max").show();
                getChatterList();
                $.cookie("chat-" + member_id, null, { path: '/', expires: -1 });
                $("#chat-notification").children("span").html("").removeClass("new-msg");
                $("#chat-notification").hide();

                getChattersInterval = setInterval("getChatterList()", 90000);
            } else {
                getChatterList();
                $.cookie("chat-" + member_id, null, { path: '/', expires: -1 });
                $("#chat-notification").children("span").html("").removeClass("new-msg");
                $("#chat-notification").hide();

                //getChattersInterval = setInterval("getChatterList()", 90000);
            }
        },
        error: function(ev) {
            var x = $.parseJSON(ev.responseText);
            alert("insertion failed.\n" + x.Message);
        }
    });
}

// ]]>
