var currentTab = 1;
var review;
var complete;
var readed;
var isPractice = false;

var review_caption_on = "見直しマークを外す";
var review_caption_off = "あとで見直す";
var complete_caption_on = "解答済みマークを外す";
var complete_caption_off = "解答済みにする";

$(function () {

    var tabs;
    tabs = $("#tabs");

    var tabscount;
    tabscount = tabs.children("li").length;

    review = new Array(tabscount);
    complete = new Array(tabscount);
    readed = new Array(tabscount);
    for (i = 1; i < tabscount; i++) {
        review[i] = false;
        complete[i] = false;
        readed[i] = false;
    }

    // コピーを有効にする
    $("copy").on("click", function () {
        if (window.clipboardData) {
            window.clipboardData.setData("Text", $(this).text());
        }
    });

    //コマンドボタンを非表示にする
    $("#commands").addClass("commands_no_display");

    // クリックイベントを伝搬
    document.oncontextmenu = function () {
        window.external.DocumentContextmenu();
    }

    // タスク1を有効にする
    //changeTab(1);

});

function changeTab(tabnumber) {
    var isSameTabClicked = (currentTab == tabnumber);
    currentTab = tabnumber;

    var tabscount = $("#tabs").children("li").length;
    var activetabId = '#tab' + tabnumber;

    for (i = 1; i <= tabscount; i++) {
        var tabId = '#tab' + i;
        var boxId = '#box' + i;

        if (activetabId == tabId) {
            $(boxId).removeClass("instructions_no_display");
            $(tabId).addClass("active_tab");
            readed[i] = true;
        }
        else {
            $(boxId).addClass("instructions_no_display");
            $(tabId).removeClass("active_tab");
        }

        change_readed_inner(i, readed[i]); // 既読フラグを設定
    }

    if (tabnumber == 0) {
        $("#commands").addClass("commands_no_display");
    } else if (!isPractice) {
        $("#commands").removeClass("commands_no_display");
    }

    if (tabnumber == 1) {
        $("#previous_button").addClass("button_no_display");
        $("#previous_dummy").removeClass("button_no_display");
    } else {
        $("#previous_button").removeClass("button_no_display");
        $("#previous_dummy").addClass("button_no_display");
    }

    if (tabnumber == tabscount) {
        $("#next_button").addClass("button_no_display");
        $("#next_dummy").removeClass("button_no_display");
    } else {
        $("#next_button").removeClass("button_no_display");
        $("#next_dummy").addClass("button_no_display");
    }

    if (review[tabnumber]) {
        $("#review_icon_button").removeClass("button_no_display");
        $("#review_button").children("span").html(review_caption_on);
    } else {
        $("#review_icon_button").addClass("button_no_display");
        $("#review_button").children("span").html(review_caption_off);
    }

    if (complete[tabnumber]) {
        $("#complete_icon_button").removeClass("button_no_display");
        $("#complete_button").children("span").html(complete_caption_on);
    } else {
        $("#complete_icon_button").addClass("button_no_display");
        $("#complete_button").children("span").html(complete_caption_off);
    }

    if (readed[tabnumber]) {
        $(activetabId).addClass("readed_tab");
    } else {
        $(activetabId).removeClass("readed_tab");
    }

}

function change_review() {
    change_review_inner(currentTab, !review[currentTab]);
}

function change_review_inner(num, value) {
    var tab = $("#tab" + num);

    review[num] = value;
    if (review[num]) {
        $("#review_icon_button").removeClass("button_no_display");
        $("#review_icon" + num).removeClass("icon_no_display");
        $("#review_button").children("span").html(review_caption_on);
        tab.find(".task_isreserve").val(1);
    } else {
        $("#review_icon_button").addClass("button_no_display");
        $("#review_icon" + num).addClass("icon_no_display");
        $("#review_button").children("span").html(review_caption_off);
        tab.find(".task_isreserve").val(0);
    }
}

function change_complete() {
    change_complete_inner(currentTab, !complete[currentTab]);
}

function change_complete_inner(num, value) {
    var tab = $("#tab" + num);

    complete[num] = value;
    if (complete[num]) {
        $("#complete_icon_button").removeClass("button_no_display");
        $("#complete_icon" + num).removeClass("icon_no_display");
        $("#complete_button").children("span").html(complete_caption_on);
        tab.find(".task_iscomplete").val(1);
    } else {
        $("#complete_icon_button").addClass("button_no_display");
        $("#complete_icon" + num).addClass("icon_no_display");
        $("#complete_button").children("span").html(complete_caption_off);
        tab.find(".task_iscomplete").val(0);
    }
}
function change_readed(value) {
    change_readed_inner(currentTab, value);
}

function change_readed_inner(num, value) {
    var tab = $("#tab" + num);

    readed[num] = value;
    if (readed[num]) {
        tab.addClass("readed_tab");
        tab.find(".task_isreaded").val(1);
    } else {
        tab.removeClass("readed_tab");
        tab.find(".task_isreaded").val(0);
    }
}

function previous_task() {
    changeTab(currentTab - 1);
}

function next_task() {
    changeTab(currentTab + 1);
}

function practice() {
    isPractice = true;
    changeTab(1);
    $("#commands").addClass("commands_no_display");
}

/**
 * 各タスクの状態を反映します。
 */
function applyStatus(values) {
    var data = JSON.parse(values); 
    for (i in data) {
        var index = data[i].Index;
        review[index] = data[i].IsReserve;
        complete[index] = data[i].IsComplete;
        readed[index] = data[i].IsReaded;

        change_review_inner(index, review[index]);
        change_complete_inner(index, complete[index]);
        change_readed_inner(index, readed[index]);
    }
}