
var isCtrl = false;
var isShift = false;
var ls = -1;
function MOutBgColor(obj) {
    if (obj.style.backgroundColor != "#e2e2e2") {
        obj.style.backgroundColor = "white";
    }
}
function MOverBgColor(obj) {

    if (obj.style.backgroundColor != "#e2e2e2") {
        obj.style.backgroundColor = "#f7ecea";
    }
}
function Open(obj) {
    ItemSingleSelect(obj);
    window.external.SeeMoreForPhrase();
}
function MDown(obj) {
    //trace right button of mouse
    if (event.button == 2) {
        var bgColor = obj.style.backgroundColor;
        if (bgColor != "#e2e2e2") {
            ItemSingleSelect(obj);
        }
        window.external.ShowRightClickContextMenuStrip(event.clientX,event.clientY);
    }
}

function selectAllPhrasesClick(obj) {
    if (obj.checked == true) {
        var ids = "";
        $("#phraseListDiv table").each(function () {
            $(this).css("backgroundColor", "#e2e2e2");
            ids = ids + $(this).attr("id") + ",";
        });
        window.external.GetSuccessiveSlt(ids);
    }
    else {
        window.external.GetSuccessiveSlt("");
        ClearSelected();
    }
}

function ItemSingleSelect(obj) {
    var selectAllCheckBox = $("#selectAllButton");
    if (selectAllCheckBox.attr("checked") == true) {
        selectAllCheckBox.attr("checked", false);
    }

    ls = $(obj).find("td").eq(0).text();
    $("table").each(function () {
        if ($(this).attr("id") != obj.id) {
            $(this).css("backgroundColor", "white");
        }
    });
    var bgColor = obj.style.backgroundColor;
    //unselected
    if (bgColor != "#e2e2e2") {
        obj.style.backgroundColor = "#e2e2e2";
    }
    window.external.GetPhraseSingleSelected(obj.id);
}
function ItemSelect(obj) {
    var selectAllCheckBox = $("#selectAllButton");
    if (selectAllCheckBox.attr("checked") == true) {
        selectAllCheckBox.attr("checked", false);
    }

    var i = 0;

    if (!isShift) {
        ls = $(obj).find("td").eq(0).text();
    }

    if (!isShift) {

        if (!isCtrl) {
            $("table").each(function () {
                if ($(this).css("backgroundColor") == "#e2e2e2") {
                    i++;
                }
                if ($(this).attr("id") != obj.id) {
                    $(this).css("backgroundColor", "white");
                }
            });
        }
        var bgColor = obj.style.backgroundColor;
        //unselected
        if (bgColor == "#e2e2e2") {
            if (i <= 1) {
                obj.style.backgroundColor = "#f7ecea";
            }
        }
        else {
            obj.style.backgroundColor = "#e2e2e2";
        }

        window.external.GetPhraseSelected(obj.id, isCtrl);
    }
    else {
        if (ls == -1) {
            ls = $(obj).find("td").eq(0).text();
            var bgColor = obj.style.backgroundColor;
            obj.style.backgroundColor = "#e2e2e2";
            window.external.GetPhraseSelected(obj.id, isCtrl);
            
        }
        else {
            var tm = $(obj).find("td").eq(0).text();
            var ils = parseInt(ls);
            var itm = parseInt(tm);
            if (ils > itm) {
                ils = ils + itm;
                itm = ils - itm;
                ils = ils - itm;
            }
            var ids = "";
            
            $("table").each(function () {

                var t = $(this).find("td").eq(0).text();
                var it = parseInt(t);
                if (ils <= it && it <= itm) {
                    $(this).css("backgroundColor", "#e2e2e2");
                    ids = ids + $(this).attr("id") + ",";
                }
                else {
                    $(this).css("backgroundColor", "white");
                }
            });

            window.external.GetSuccessiveSlt(ids);
        }
    }
    i = 0;
}
function initializelLs() {
    ls = -1;
}
function ClearSelected() {
    $("table").each(function () {
        $(this).css("backgroundColor","white")
    });
}
function PhraseKeyUp() {
    if (event.keyCode == 13) {
        if ($.trim($('#txtDefi').text()) != "") {
            window.external.AddClick();
        }
        else {
            if ($.trim($('#txtPhrase').val()) != "") {
                $('#txtDefi').focus();
            }
        }
    }
    else {
        window.external.txtPhraseKeyUp();
    }
}
function InitinalSort(ca, ud) {
    $('#thPh').click(function () {
        u = window.external.GetSortedDirection();
        if (u) {
            window.external.SortPhrase('false', 'false');
        }
        else {
            window.external.SortPhrase('false', 'true');
        }
    });
    $('#thTime').click(function () {
        u = window.external.GetSortedDirection();
        if (u) {
            window.external.SortPhrase('true', 'false');
        }
        else {
            window.external.SortPhrase('true', 'true');
        }
    });


    $('#thPh').mouseover(function () {
        if (ud) {
            $('#aalup').show();
        }
        else {
            $('#aaldown').show();
        }
    });
    $('#thPh').mouseout(function () {
        ca = window.external.GetSortedColumn();
        if (ca) {
            $('#aalup').hide();
            $('#aaldown').hide();
        }
    });
    $('#thTime').mouseover(function () {
        if (ud) {
            $('#achup').show();
        }
        else {
            $('#achdown').show();
        }
    });
    $('#thTime').mouseout(function () {
        ca = window.external.GetSortedColumn();
        if (!ca) {
            $('#achup').hide();
            $('#achdown').hide();
        }
    });

    $('#aalup').css({ display: "none" });
    $('#aaldown').css({ display: "none" });
    $('#achup').css({ display: "none" });
    $('#achdown').css({ display: "none" });
    if (ca) {
        if (ud) {
            $('#achup').show();
        }
        else {
            $('#achdown').show();
        }
    }
    else {
        if (ud) {
            $('#aalup').show();
        }
        else {
            $('#aaldown').show();
        }
    }
    $("div").css({ "cursor": "default" });
}
function SearchInBing(obj) {
    window.open('http://cn.bing.com/search?mkt=zh-CN&setLang=zh&form=BDCN21&q=' + encodeURIComponent(String(obj)).substring(0, 1800));
}

//region scrollbar ops
var offset=0;
function ResetScrollBarPos() {
    $('#phraseListDiv').scrollTop(offset);
}
function InitScrollBar() {
    $('#phraseListDiv').scroll(
        function () {
            offset = $('#phraseListDiv').scrollTop();
        });
}

var lastBodyHeight = 0;
var lastBodyWidth=0;
function AdjustWordListSize() {
    var lastDivHeight = parseInt($("#phraseListDiv").css("height"));
    var curClientHeight = document.body.clientHeight;
    $("#phraseListDiv").css("height", lastDivHeight + curClientHeight - lastBodyHeight);
    lastBodyHeight = curClientHeight;

    var lastVocaWidth = $(".divVoca").width();
    var curBodyWidth = $("body").width();
    $(".divVoca").width(lastVocaWidth + curBodyWidth - lastBodyWidth);
    lastBodyWidth = $('body').width();
}
function RecordBodySize() {
    lastBodyHeight = document.body.clientHeight;
    lastBodyWidth = $('body').width();
    var divHomeMargineTop = $("#divHome").outerHeight(true) - $("#divHome").outerHeight();
    var otherHeight = $(".actBTN").outerHeight(true) + $("#phraseListPageTail").outerHeight(true) + $("#phraseListHead").outerHeight(true) + divHomeMargineTop;
    $("#phraseListDiv").height(lastBodyHeight - otherHeight);

    var vocaArea = $(".divVoca");
    vocaArea.width(lastBodyWidth - parseInt(vocaArea.css("margin-left")) - parseInt(vocaArea.css("margin-right")));
}
//endregion