﻿// <![CDATA[
//var urlChatService = "http://localhost:52280/";
var urlChatService = "http://chat.premiummate.com/";
var member_id;
var profile_status;
var member_level;

$(document).ready(function() {
    $.ajaxSetup({
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        type: "POST"
    });
    $.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: false,
        title: "Message Box",
        bgiframe: false,
        buttons: {
            "OK": function() {
                $("#message-alert").dialog("close");
            }
        }
    });

    // 1 Add Favor
    $("a[id$=lbnAddFavor]").click(function() {
        if ($(this).attr("disabled") != "disabled") {
            $(this).attr("disabled", "disabled");
            $(this).children("img").attr("src", "./images/loading/cyrcle-24-org.gif");
            if (member_id == 0) {
                messageAlert("Please login before adding a new favorite to your list.");
            } else {
                var favorite_id = $("span[id$=lblMemberId]").html();
                addFavor(favorite_id, $(this));
            }
        }
        return false;
    });

    // 2 Send Inter
    $("a[id$=lbnSendInter]").click(function() {
        if ($(this).attr("disabled") != "disabled") {
            $(this).attr("disabled", "disabled");
            $(this).children("img").attr("src", "./images/loading/cyrcle-24-org.gif");
            if (member_id == 0) {
                messageAlert("Please login before sending an 'interested' notice to another member.");
            } else {
                var interest_id = $("span[id$=lblMemberId]").html();
                sendInter(interest_id, $(this));
            }
        }
        return false;
    });

    // 3 Send Msg
    $("a[id$=lbnSendMsg]").click(function() {
        if ($(this).attr("disabled") != "disabled") {
            if (member_id == 0) {
                messageAlert("Please login before sending a message to this member.");
            } else {
                window.location.href = "viewprofilemessage.aspx?id=" + $("span[id$=lblMemberId]").html();
            }
        }
        return false;
    });

//    // 4 Send SMS
//    $("a[id$=lbnSendSms]").click(function() {
//        if ($(this).attr("disabled") != "disabled") {
//            if (member_id == 0) {
//                messageAlert("Please login before sending a message to this member.");
//            } else {
//                window.location.href = "viewprofilesms.aspx?id=" + $("span[id$=lblMemberId]").html();
//            }
//        }
//        return false;
//    });

    // 5 Send Free Msg
    $("a[id$=lbnSendFree]").click(function() {
        if ($(this).attr("disabled") != "disabled") {
            if (member_id == 0) {
                messageAlert("Please login before sending a message to this member.");
            } else {
                window.location.href = "viewprofilefreemessage.aspx?id=" + $("span[id$=lblMemberId]").html();
            }
        }
        return false;
    });

    // 6 Chat
    $("a[id$=lbnStartChat]").click(function() {
        if ($(this).attr("disabled") != "disabled") {
            if (member_id == 0) {
                messageAlert("Please login before chat to this member.");
            } else if (profile_status == "N") {
                messageAlert("Your profile has not been approved. You cannot chat to this member.");
            } else if (member_level == "F") {
                messageAlert("Please subscribe before chat to this member.");
            } else {
                startChat($("span[id$=lblMemberId]").html());
                //chatbodyopen($("span[id$=lblMemberId]").html(), $("span[id$=lblUserName]").html());
            }
        }
        return false;
    });

    // 7 Send Block
    $("a[id$=lbnBlock]").click(function() {
        if ($(this).attr("disabled") != "disabled") {
            $(this).attr("disabled", "disabled");
            $(this).children("img").attr("src", "./images/loading/cyrcle-24-org.gif");
            if (member_id == 0) {
                messageAlert("Please login before blocking correspondence from this member.");
            } else if (member_level == "F") {
                messageAlert("This feature is only available to subscribers.");
            } else {
                var block_id = $("span[id$=lblMemberId]").html();
                sendBlock(block_id, $(this));
            }
        }
        return false;
    });

    // 8 Report Click
    $("a[id$=lbnReport]").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("Please input your reasons.");
        }
        return false;
    });

    $("input[id$=btnCancel]").click(function() {
        $("div#report-admin").hide();
        return false;
    });

});

// 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");
        },
        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");
        },
        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");
        },
        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);
        }
    });
}
// ]]>
