﻿// <![CDATA[
//var urlChatService = "http://localhost:52280/";
var urlChatService = "http://chatservice2010.naddate.com/";
var member_id;
var profile_status;
var member_level;

$(document).ready(function() {
    $.ajaxSetup({
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        type: "POST"
    });
    member_id = $("input[id$=hdViewProfileActionMemberId]").val();
    profile_status = $("input[id$=hdViewProfileActionProfileStatus]").val();
    member_level = $("input[id$=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; } });

    // Initial Countries Dialog
    $("#message-alert").dialog({
        autoOpen: false,
        modal: true,
        width: 400,
        title: "Message Box",
        bgiframe: true,
        buttons: {
            "ตกลง": function() {
                $("#message-alert").dialog("close");
            }
        }
    });

    // 1 Add Favor
    $("a[id$=hplFavorAction]").click(function() {
        if (!($(this).is("[disabled]"))) {
            $(this).attr("disabled", "disabled");
            //$(this).children("img").attr("src", "./images/loading/cyrcle-24-org.gif");
            if (member_id == 0) {
                messageAlert("กรุณาเข้าระบบก่อนทำการเก็บรายชื่อคนพิเศษ");
            } else {
                var favorite_id = $("span[id$=lblMemberId]").html();
                addFavor(favorite_id, $(this));
            }
        }
        return false;
    });

    // 2 Send Inter
    $("a[id$=hplInterAction]").click(function() {
        if (!($(this).is("[disabled]"))) {
            $(this).attr("disabled", "disabled");
            $(this).html("<span class='s48_png_gray s48_inter'></span>");
            //$(this).children("img").attr("src", "./images/loading/cyrcle-24-org.gif");
            if (member_id == 0) {
                messageAlert("กรุณาเข้าระบบก่อนทำการแสดงความสนใจ");
            } else {
                var interest_id = $("span[id$=lblMemberId]").html();
                sendInter(interest_id, $(this));
            }
        }
        return false;
    });

    // 3 Send Msg
    $("a[id$=hplMsgAction]").click(function() {
        if (!($(this).is("[disabled]"))) {
            if (member_id == 0) {
                messageAlert("กรุณาเข้าระบบก่อนส่งข้อความให้สมาชิกท่านนี้");
            } else {
                window.location.href = "viewprofilemessage.aspx?id=" + $("span[id$=lblMemberId]").html();
            }
        }
        return false;
    });

    // 4 Send SMS
    $("a[id$=hplSmsAction]").click(function() {
        if (!($(this).is("[disabled]"))) {
            if (member_id == 0) {
                messageAlert("กรุณาเข้าระบบก่อนส่งข้อความให้สมาชิกท่านนี้");
            } else {
                window.location.href = "viewprofilesms.aspx?id=" + $("span[id$=lblMemberId]").html();
            }
        }
        return false;
    });

    // 5 Send Free Msg
    $("a[id$=hplFmsgAction]").click(function() {
        if (!($(this).is("[disabled]"))) {
            if (member_id == 0) {
                messageAlert("กรุณาเข้าระบบก่อนส่งฟรีข้อความจากนัดเดทให้สมาชิกท่านนี้");
            } else {
                window.location.href = "viewprofilefreemessage.aspx?id=" + $("span[id$=lblMemberId]").html();
            }
        }
        return false;
    });

    // 6 Chat
    $("a[id$=hplChatAction]").click(function() {
        //alert(profile_status);
        if (!($(this).is("[disabled]"))) {
            if (member_id == 0) {
                messageAlert("กรุณาเข้าระบบ ก่อนทำการสนทนานะคะ");
            } else if (profile_status == "N") {
                messageAlert("ข้อมูลของท่านไม่ผ่านการตรวจสอบ ไม่สามารถทำการสนทนาได้ค่ะ");
            } else if (member_level == "F") {
                messageAlert("กรุณาสมัครสมาชิก Premium ก่อนทำการสนทนานะคะ");
            } else {
                startChat($("span[id$=lblMemberId]").html());
                //chatbodyopen($("span[id$=lblMemberId]").html(), $("span[id$=lblUserName]").html());
            }
        }
        return false;
    });

    // 7 Send Block
    $("a[id$=hplBlockAction]").click(function() {
        if (!($(this).is("[disabled]"))) {
            $(this).attr("disabled", "disabled");
            //$(this).children("img").attr("src", "./images/loading/cyrcle-24-org.gif");
            if (member_id == 0) {
                messageAlert("กรุณาเข้าระบบก่อนทำการรป้องกันการติดต่อจากสมาชิกท่านนี้");
            } else {
                var block_id = $("span[id$=lblMemberId]").html();
                sendBlock(block_id, $(this));
            }
        }
        return false;
    });

    // 8 Report Click
    $("a[id$=hplReportAction]").click(function() {
        $("div#report-admin").show();
        return false;
    });

    $("input[id$=btnReport]").click(function() {
        if ($("textarea[id$=txtReason]").val() != "") {
            var report_id = $("span[id$=lblMemberId]").html();
            sendReport(report_id, $("textarea[id$=txtReason]").val(), $(this));
            $("div#report-admin").hide();
        } else {
            messageAlert("กรุณากรอกเหตุผลก่อนแจ้งลบประวัติ");
        }
        return false;
    });

    $("input[id$=btnCancel]").click(function() {
        $("div#report-admin").hide();
        return false;
    });

    //New Icon
    $("#profile-action a[id$=Action]").mouseover(function() {
        var txt = $(this).attr("title");
        var icon = $(this).html();
        //alert(icon);
        $("#profile-action #show-text").html(txt);
        $("#show-icon").html(icon.replace("s32", "s48").replace("s32", "s48"));
    }).mouseout(function() {
        $("#profile-action #show-text").html("สานสัมพันธ์ที่นี่");
        $("#show-icon").html("");
    });

    //Member Gifts
    $("#ddlGiftCategory").change(function() {
        if ($(this).val() == "0") {
            $("#dlGift > span").show();
        } else {
            $("#dlGift > span").hide();
            $("#dlGift > span[cat=" + $(this).val() + "]").show();
        }

    });

});

// Message Alert
function messageAlert(msg) {
    $("#message-detial").html(msg);
    $("#message-alert").dialog("open");
    setTimeout(function() {
        $("#message-alert").dialog("close");
    }, 5000);
}

// 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(ret.d[0]);
            obj.removeAttr("disabled");
            //obj.children("img").attr("src", "./images/icon/icon-favor.gif");
            if (ret.d[1] == "Y") {
                obj.html("<span class='s32_png_gray s32_favor'></span>");
                obj.attr("disabled", "disabled");
            } else {
                obj.html("<span class='s32_png s32_favor'></span>");
            }
        },
        error: function(ev) {
            var x = $.parseJSON(ev.responseText);
            messageAlert(x.Message);
            obj.removeAttr("disabled");
            //obj.children("img").attr("src", "./images/icon/icon-favor.gif");
        }
    });
}

// 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(ret.d[0]);
            obj.removeAttr("disabled");
            //obj.children("img").attr("src", "./images/icon/icon-interest.gif");
            if (ret.d[1] == "Y") {
                obj.html("<span class='s48_png_gray s48_inter'></span>");
                obj.attr("disabled", "disabled");
            } else {
                obj.html("<span class='s48_png s48_inter'></span>");
            }
        },
        error: function(ev) {
            var x = $.parseJSON(ev.responseText);
            messageAlert(x.Message);
            obj.removeAttr("disabled");
            //obj.children("img").attr("src", "./images/icon/icon-interest.gif");
        }
    });
}

// f3 Send Block
function sendBlock(block_id, obj) {
    var json = { "member_id": member_id, "block_id": block_id };
    var jsons = JSON.stringify(json);

    //alert(jsons);

    $.ajax({
        url: "data/wsMember.asmx/sendBlock",
        data: jsons,
        success: function(ret) {
            messageAlert(ret.d[0]);
            obj.removeAttr("disabled");
            //obj.children("img").attr("src", "./images/icon/icon-block.gif");
            if (ret.d[1] == "Y") {
                obj.html("<span class='s32_png_gray s32_block'></span>");
                obj.attr("disabled", "disabled");
            } else {
                obj.html("<span class='s32_png s32_block'></span>");
            }
        },
        error: function(ev) {
            var x = $.parseJSON(ev.responseText);
            messageAlert(x.Message);
            obj.removeAttr("disabled");
            //obj.children("img").attr("src", "./images/icon/icon-block.gif");
        }
    });
}

// f4 Send Report
function sendReport(report_id, reason, obj) {
    var json = { "inform_id": member_id, "member_id": report_id, "reason": reason };
    var jsons = JSON.stringify(json);

    //alert(jsons);

    $.ajax({
        url: "data/wsMember.asmx/sendReport",
        data: jsons,
        success: function(ret) {
            messageAlert(ret.d);
            obj.removeAttr("disabled");
            //obj.children("img").attr("src", "./images/icon/icon-report.gif");
        },
        error: function(ev) {
            var x = $.parseJSON(ev.responseText);
            messageAlert(x.Message);
            obj.removeAttr("disabled");
            //obj.children("img").attr("src", "./images/icon/icon-report.gif");
        }
    });
}

// f5 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);
        }
    });
}
// ]]>
