﻿
$(function() {
    $("#mumuTeach").jSuggest({
        url: "services/autocompleteservice.aspx",
        type: "GET",
        data: "searchQuery",
        autoChange: true,
        delay: 10,
        opacity: 0.8,
        minchar: 1
    });
    $("#mumuTeach").keydown(function(evt) {
        if (evt.keyCode == 13) {
            getMumuEdit($(this).val());
        }
    });
    $("div.items img").click(function() {
        selectImage($(this).attr("src"));
    });
    $("div.scrollable").scrollable({ size: 5 });
    $("input").tooltip({
        position: "center right",
        offset: [-2, 10],
        effect: "fade",
        opacity: 0.7,
        tip: '.tooltip'
    });
});
function selectImage(src) {
    $("#divImgSelected").html("<img id='imgSelected' src='" + src + "' />");
}
function createMumu() {
    var name = $("#mumuName").val();
    var password = $("#mumuPassword").val();
    var image = $("#imgSelected").attr("src");
    var hello = $("#mumuHello").val();
    var bye = $("#txtBye").val();
    var presentation = $("#mumuPresentation").val();

    if (presentation.length > 140 || hello.length > 140 || name.length > 20 || password.length > 20
    || hello.length > 140 || bye.length > 140) {
        alert("To much text");
        return;
    }

    var jsonText = JSON.stringify({ name: name, password: password, image: image, greeting: hello, bye:bye });
    $.ajax({
        type: "POST",
        url: "services/mumuservice.asmx/CreateMumu",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        dataType: "json",
        success: function(response) {
            var id = response.d;
            if (id === 0) {
                $("#divResponse").text("There's already a mumu with that name, try another name");
            }
            else if (id > 0) {
                addAnswer(id, 3, presentation);
                $("#divResponse").text("Mumu created");
            }
        }
    });
}
function addAnswer(mumuid, questionid, text)
{
    var jsonText = JSON.stringify({ mumuId:mumuid, questionId:questionid, aText:text });
    $.ajax({
        type: "POST",
        url: "services/qnaservice.asmx/AddAnswer",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        dataType: "json",
        success: function(response) {            
        }
    });
}

function getMumuEdit(name) {

    $("#divEditAnswers").html("");
    var jsonText = JSON.stringify({ name: name });
    $.ajax({
        type: "POST",
        url: "services/mumuservice.asmx/GetMumuEdit",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        dataType: "json",
        success: function(response) {
            $("#divEditResponse").html(response.d);            
        }
    });
}

function newQuestion(mumuid) {
    var qText = $("#newQuestion").val();
    if (qText.length > 140 || qText.length == 0) {
        alert("To long text or empty");
        return;
    }
    var jsonText = JSON.stringify({ qText: qText, mumuId: mumuid });
    $.ajax({
        type: "POST",
        url: "services/qnaservice.asmx/AddQuestion",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        dataType: "json",
        success: function(response) {
            var id = response.d;
            addAnswer(mumuid, id, $("#newAnswer").val());
            $("#divStatus").html("Question added");
            setTimeout("$('#divStatus').html('')", 2000);
        }
    });
    
}

function getMumuEditPassword() {
    var jsonText = JSON.stringify({ name: $("#mumuTeach").val(), password: $("#enterPassword").val() });
    $.ajax({
        type: "POST",
        url: "services/mumuservice.asmx/GetMumuEditPassword",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        dataType: "json",
        success: function(response) {
            $("#divEditResponse").html(response.d);
        }
    });
}

function getQnA(mumuid, page) {
    var jsonText = JSON.stringify({ mumuId:mumuid, page:page });
    $.ajax({
        type: "POST",
        url: "services/qnaservice.asmx/GetQnA",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        dataType: "json",
        success: function(response) {
            $("#divEditAnswers").html(response.d);
        }        
    });
}
var beforeText;
function startAnswerEdit(ta) {
    beforeText = $(ta).val();
}

function answerEditSave(ta, id) {
    var afterText = $(ta).val();
    if (beforeText == afterText) {
        return;
    }
    if (afterText.length > 140) {
        alert("To long text");
        return;
    }
    var jsonText = JSON.stringify({ answerId: id, aText: afterText });
    $.ajax({
        type: "POST",
        url: "services/qnaservice.asmx/AnswerEditSave",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        dataType: "json",
        success: function(response) {
            $(ta).css({ "background": "#A8AF98" });
        },
        error: function(msg) { $(ta).css({ "background": "red" }); }
    });
}
function enterPassword(e) {
    if (e.keyCode == 13) {
        getMumuEditPassword();
    }
}
