﻿var GD = { Version: '1.0.0.0' };
GD.Page = Class.create({
    initialize: function() {
        this.type = null; //大类
        this.Quality = null; //小类（品质）
        this.Infomation = null; //关键字
        this.otherInfo = null;
        this.ListDate = null;
        this.FCurrentPage = 1;
        this.FPageLine = 10;
        this.divPaginationID = null;
        this.Op = null;
        this.DataValue = "";
        this.DataID = "";
        this.DataIndex = -1;
    },
    GetEleByClientID: function(divPagination, word, type, smallType, other) {
        this.divPaginationID = divPagination;
        this.Infomation = word == "" ? null : unescape(word);
        this.type = type == "" ? null : unescape(type);
        this.Quality = smallType == "" ? null : smallType;
        this.otherInfo = other == "" ? null : other;
        //加载查询条件
        if (this.Infomation != null) {
            jQuery('#txtInformation').val(this.Infomation);
        }
        if (this.type != null) {
            jQuery('#type>li>a').each(function() {
                if (jQuery(this).text() == page.type) {
                    jQuery('#type>li').removeClass();
                    jQuery('#type>li').addClass("navgane_1");
                    jQuery(this).parent().removeClass();
                    jQuery(this).parent().addClass("navgane");
                    ShowQuality(page.type, page.Quality);
                }
            });
        }
        if (this.Infomation != null || this.type != null || this.Quality != null || this.otherInfo != null) {
            this.LoadData();
        }
    },
    //查询
    Search: function() {
        if (this.CheckRight()) {
            this.FCurrentPage = 1;
            this.LoadData();
            this.ShowSearch();
        }
    },
    //显示查询条件
    ShowSearch: function() {
    },
    LoadData: function() {
        this.ListDate = RXSG.GameData.DataBase.GetDataBaseList(this.Infomation, this.Quality, this.type, this.otherInfo, this.FCurrentPage, this.FPageLine).value;
        this.Formate();
    },
    Formate: function() {
        mouseOut();
        CloseDiv();
        if (this.ListDate == null || this.ListDate.IsSuccess == false) {
            var html = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"list5\"><tr><td class=\"Gameli\" colspan=\"3\">系统忙，请稍候再试！</td></tr></table>";
            var pagingation = new Pagination("Pagination1", 0, 1, 0, 0, "page.PageChange", this.divPaginationID, 1);
            this.ShowResult(html);
        }
        else {
            var DataList = this.ListDate.DataList;
            if (DataList == null || DataList.length == 0) {
                var html = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"list5\"><tr><td class=\"Gameli\" colspan=\"3\">无相关资料！</td></tr></table>";
                var pagingation = new Pagination("Pagination1", 0, 1, 0, 0, "page.PageChange", this.divPaginationID, 1);
                this.ShowResult(html);
            }
            else {
                var i = 0;
                var row = DataList[i];
                var html = "";
                if (this.type != "全部") {
                    html += "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"list5\">";
                    switch (this.type) {
                        case "名将": html += "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_2\">说明</td></tr>"; break;
                        case "名城": html += "<tr><td class=\"topas\">名称</td><td class=\"topas_3\">类型</td><td class=\"topas_3\">坐标</td></tr>"; break;
                        case "装备":
                            html += "<tr><td class=\"topas_3\">名称</td><td class=\"topas_4\">类型</td><td class=\"topas_4\">品质</td><td class=\"topas_5\">等级</td><td class=\"topas_6\">说明</td><td class=\"topas_4\">套装</td></tr>";
                            break;
                        case "兵种": html += "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_3\">克制兵种</td><td class=\"topas_3\">被克兵种</td></tr>"; break;
                        case "道具": html += "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_7\">说明</td><td class=\"topas_2\">价格</td></tr>"; break;
                        case "计谋": html += "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_2\">说明</td></tr>"; break;
                        case "城防": html += "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_3\">克制兵种</td><td class=\"topas_3\">被克兵种</td></tr>"; break;
                        case "科技": html += "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_1\">等级</td><td class=\"topas_7\">说明</td></tr>"; break;
                        case "任务": html += "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_7\">说明</td><td class=\"topas_2\">奖励</td></tr>"; break;
                        case "建筑": html += "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_1\">等级</td><td class=\"topas_7\">说明</td></tr>"; break;
                        case "爵位": html += "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_7\">说明</td><td class=\"topas_2\">升级奖励</td></tr>"; break;
                        case "官职": html += "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_7\">说明</td><td class=\"topas_2\">升级奖励</td></tr>"; break;
                        default: html += ""; break;
                    }
                    while (row != null) {
                        html += "<tr><td class=\"Gameli\">";
                        var datav = "";
                        if (i == this.DataIndex) {
                            datav = this.DataValue;
                        }
                        if (row[2] != null && row[2] != "")
                            html += "<img src=\"" + row[2] + "\" width=\"64\" height=\"64\" onmouseover=\"mouseMove(event,'" + row[4] + "','" + row[0] + "','" + datav + "')\" onmouseout=\"mouseOut()\" onclick=\"mouseClick(event,'" + row[4] + "','" + row[0] + "','" + datav + "')\"/>";
                        html += "<a href=\"javascript:void(0);\" onclick=\"mouseClick(event,'" + row[4] + "','" + row[0] + "','" + datav + "')\" onmouseover=\"mouseMove(event,'" + row[4] + "','" + row[0] + "','" + datav + "')\" onmouseout=\"mouseOut()\">" + row[1] + "</a></td>";
                        var result = row[3].split("|");
                        for (n = 0; n < result.length; n++) {
                            var a = result[n];
                            a = subString(result[n], 20, true);
                            if (result[n].length == 0) a = "资料暂缺";
                            var b = result[n].length == 0 ? "资料暂缺" : result[n];
                            html += "<td class=\"Gameli_1\"><a title=\"" + b + "\">" + a + "</a></td>";
                        }
                        html += "</tr>";
                        if (parseInt(this.Op) == 1) {
                            html += this.GetStrengData(i, result[1]);

                        }
                        i++;
                        row = DataList[i];
                    }
                    html += "</table>";
                } //查询全部时显示的信息
                else {
                    var typeindex = null; //上一条信息类型
                    while (row != null) {
                        var datav = "";
                        if (i == this.DataIndex) {
                            datav = this.DataValue;
                        }
                        if (typeindex == null || row[4] != typeindex) {
                            html += "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"list5\">";
                            html += this.GetTypeTitleById(row[4]);
                            typeindex = row[4];
                        }
                        html += "<tr><td class=\"Gameli\">";
                        if (row[2] != null && row[2] != "")
                            html += "<img src=\"" + row[2] + "\" width=\"64\" height=\"64\" onmouseover=\"mouseMove(event,'" + row[4] + "','" + row[0] + "','" + datav + "')\" onmouseout=\"mouseOut()\"/>";
                        html += "<a href=\"javascript:void(0);\" onclick=\"mouseClick(event,'" + row[4] + "','" + row[0] + "','" + datav + "')\" onmouseover=\"mouseMove(event,'" + row[4] + "','" + row[0] + "','" + datav + "')\" onmouseout=\"mouseOut()\">" + row[1] + "</a></td>";
                        var result = row[3].split("|");
                        for (n = 0; n < result.length; n++) {
                            var a = result[n];
                            a = subString(result[n], 20, true);
                            if (result[n].length == 0) a = "资料暂缺";
                            html += "<td class=\"Gameli_1\">" + a + "</td>";
                        }
                        html += "</tr>";
                        if (typeindex == 12) {
                            html += this.GetStrengData(i, result[1]);
                        }
                        i++;
                        row = DataList[i];
                        if (row == null) html += "</table>";
                        else if (row[4] != typeindex) html += "</table>";
                    }
                }
                this.ShowResult(html);
            }
            var FSumCount = this.ListDate.Tag;
            var FCurrentPageLine = i;
            var FPageCount = Math.ceil(FSumCount / this.FPageLine);
            var pagingation = new Pagination("Pagination1", FPageCount, this.FCurrentPage, FSumCount, FCurrentPageLine, "page.PageChange", this.divPaginationID, 1);
        }
        //显示具体信息
    },
    //获取装备强化数据
    GetStrengData: function(index, sgdc_EquipQualityName) {
        var htm = "";
        htm += "<tr>";
        htm += "<td class=\"Gameli_3\" colspan=\"6\" id=\"" + index + "\">";
        var DataValue = RXSG.GameData.DataBase.GetStrengData_Xml(sgdc_EquipQualityName).value;
        for (var i = 0; i < DataValue.length; i++) {
            htm += " <a href=\"javascript:void(0);\" id=\"" + index + "text" + i + "\" onclick=\"page.DataClick('" + index + "text" + i + "','" + DataValue[i][0] + "','" + index + "')\" >" + DataValue[i][1] + "</a>";
            //htm += "<a href=\"javascript:void(0);\"  id=\"" + index + "text" + i + "\"  onclick=\"page.DataClick(\"" + index + "text" + i + "\" , \"" + DataValue[i][0] + "\")>" + DataValue[i][1] + "</a>";
        }
        htm += "</td>";
        return htm;
    },
    DataClick: function(id, value, tdid) {
        this.DataIndex = tdid;
        this.DataValue = value;
        this.DataID = this.DataID + id + "|";
        this.LoadData();
    },
    //分页调整
    PageChange: function(pNum) {
        this.FCurrentPage = parseInt(pNum);
        this.LoadData();
    },
    //根据ID获得相应类别信息栏
    GetTypeTitleById: function(id) {
        var html = "";
        switch (parseInt(id)) {
            case 1: html = "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_3\">克制兵种</td><td class=\"topas_3\">被克兵种</td></tr>"; break; //城防
            case 2: html = "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_7\">说明</td><td class=\"topas_2\">奖励</td></tr>"; break; //任务
            case 3: html = "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_2\">说明</td></tr>"; break; //名将
            case 4: html = "<tr><td class=\"topas\">名称</td><td class=\"topas_3\">类型</td><td class=\"topas_3\">坐标</td></tr>"; break; //名城
            case 5: html = "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_7\">说明</td><td class=\"topas_2\">升级奖励</td></tr>"; break; //爵位
            case 6: html = "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_7\">说明</td><td class=\"topas_2\">升级奖励</td></tr>"; break; //官职
            case 7: html = "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_1\">等级</td><td class=\"topas_7\">说明</td></tr>"; break; //建筑
            case 8: html = "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_1\">等级</td><td class=\"topas_7\">说明</td></tr>"; break; //科技
            case 9: html = "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_3\">克制兵种</td><td class=\"topas_3\">被克兵种</td></tr>"; break; //兵种
            case 10: html = "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_2\">说明</td></tr>"; break; //计谋
            case 11: html = "<tr><td class=\"topas\">名称</td><td class=\"topas_1\">类型</td><td class=\"topas_7\">说明</td><td class=\"topas_2\">价格</td></tr>"; break; //道具
            case 12: html = "<tr><td class=\"topas_3\">名称</td><td class=\"topas_4\">类型</td><td class=\"topas_4\">品质</td><td class=\"topas_5\">等级</td><td class=\"topas_6\">说明</td><td class=\"topas_4\">套装</td></tr>"; break; //装备          
            default: html = ""; break;
        }
        return html;
    },
    //判断查询条件是否符合要求
    CheckRight: function() {
        this.Infomation = $F('txtInformation').trim() == "" ? null : $F('txtInformation').trim();

        this.otherInfo = null;
        this.Quality = jQuery('#QualityLi>input:checked').html() != null ? (jQuery('#QualityLi>input:checked').val() == "" ? null : jQuery('#QualityLi>input:checked').val()) : null;
        this.type = jQuery('#type>li.navgane>a').text();

        if (this.type == "全部" && this.Infomation == null) {
            var html = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"list5\"><tr><td class=\"Gameli\" colspan=\"3\">请输入关键词</td></tr></table>";
            var pagingation = new Pagination("Pagination1", 0, 1, 0, 0, "page.PageChange", this.divPaginationID, 1);
            this.ShowResult(html);
            return false;
        }
        return true;
    },
    //表格中填充数据
    ShowResult: function(htm) {
        jQuery("#resultList").html(htm);
    }
})

//截取字符串 包含中文处理
//(串,长度,增加...)
function subString(str, len, hasDot) {
    var newLength = 0;
    var newStr = "";
    var chineseRegex = /[^\x00-\xff]/g;
    var singleChar = "";
    var strLength = str.replace(chineseRegex, "**").length;
    for (var i = 0; i < strLength; i++) {
        singleChar = str.charAt(i).toString();
        if (singleChar.match(chineseRegex) != null) {
            newLength += 2;
        }
        else {
            newLength++;
        }
        if (newLength > len) {
            break;
        }
        newStr += singleChar;
    }
    if (hasDot && strLength > len) {
        newStr += "...";
    }
    return newStr;
}

//构造一个函数
function MergFunctionName() {
    var result = arguments[0] + "(", a = arguments;
    if (a.length > 1) {
        for (i = 1; i < a.length; i++) {
            result += "\'" + arguments[i] + "\'";
            if (i != (a.length - 1))
                result += ",";
        }
    }
    result += ")";
    return result;
}
//类别选择
jQuery(function() {
    jQuery('#type>li>a')
            .click(function(event) {
                if (this == event.target) {
                    jQuery('#type>li').removeClass();
                    jQuery('#type>li').addClass("navgane_1");
                    jQuery(this).parent().removeClass();
                    jQuery(this).parent().addClass("navgane");
                    page.type = jQuery(this).text();
                    ShowQuality(jQuery(this).text());
                }
            })
});

//显示品质选择项
function ShowQuality(type, smallType) {
    if (type == "装备") {
        page.Op = 1;
    }
    if (type == "全部") {
        $('QualityLi').hide();
    }
    else {
        var Quality = RXSG.GameData.DataBase.GetQualityByType(type).value;
        if (Quality == null || Quality.IsSuccess == false) {
            $('QualityLi').innerHTML = "";
            $('QualityLi').hide();
        }
        else {
            var DataList = Quality.DataList;
            if (DataList == null || DataList.length == 0) {
                $('QualityLi').innerHTML = "";
                $('QualityLi').hide();
            }
            else {
                var i = 0;
                var row = DataList[i];
                var html = page.type + "品质："
                while (row != null) {
                    if (smallType != null && row[1] == smallType) {
                        html += "<input name=\"Quality\" type=\"radio\" value=\"" + row[1] + "\" id=\"Quality_" + row[1] + "\" checked=\"checked\"/>";
                        html += "<label for=\"Quality_" + row[1] + "\">" + row[0] + "</label>";
                    }
                    else if (smallType == null && i == 0) {
                        html += "<input name=\"Quality\" type=\"radio\" value=\"" + row[1] + "\" id=\"Quality_" + row[1] + "\" checked=\"checked\"/>";
                        html += "<label for=\"Quality_" + row[1] + "\">" + row[0] + "</label>";
                    }
                    else {
                        html += "<input name=\"Quality\" type=\"radio\" value=\"" + row[1] + "\" id=\"Quality_" + row[1] + "\"/>";
                        html += "<label for=\"Quality_" + row[1] + "\">" + row[0] + "</label>";
                    }
                    i++;
                    row = DataList[i];
                }
                $('QualityLi').innerHTML = html;
                $('QualityLi').show();
            }
        }
    }
}

//鼠标当前位置
function mousePosition(ev) {
    if (ev.pageX || ev.pageY) {
        return { x: ev.pageX, y: ev.pageY };
    }
    //    var top = 0;left = 0;
    //    if (document.body && document.body.scrollTop && document.body.scrollLeft) {
    //        top = document.body.scrollTop;
    //        left = document.body.scrollleft;
    //    }
    //    if (document.documentElement && document.documentElement.scrollTop && document.documentElement.scrollLeft) {
    //        top = document.documentElement.scrollTop;
    //        left = document.documentElement.scrollLeft;
    //    }
    return {
        x: ev.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft,
        y: ev.clientY + document.documentElement.scrollTop - document.documentElement.clientTop
    };
}
//鼠标移到标题上触发事件 action：类型 typeid：信息ID
function mouseMove(e, action, typeid, strengdata) {
    var Action = "strengdata=" + strengdata + "&action=" + action + "&typeid=" + typeid + "&showtype=0";
    var options = {
        method: 'get',
        asynchronous: true,
        parameters: Action,
        onComplete: LoadDeatial
    };
    new Ajax.Request('AjaxDiv.aspx?no-cache=' + Math.random(), options);
    var ev = e || window.event;
    var mousePos = mousePosition(ev);
    var obj = document.getElementById('Detail_Div');
    document.getElementById('Detail_Div').style.left = mousePos.x + 15 + "px"; //指定层显示时其左上角的横向坐标（事件触发时的坐标偏移10）
    var WinHeight = document.documentElement.clientHeight + document.documentElement.scrollTop;
    while (mousePos.y + obj.clientHeight > WinHeight) {
        mousePos.y -= obj.clientHeight;
    }
    document.getElementById('Detail_Div').style.top = mousePos.y + 15 + "px"; //指定层显示时其左上角的纵向坐标（事件触发时的坐标偏移10）
    //obj.style.visibility = 'visible';
}
//鼠标单击标题触发事件 action：类型 typeid：信息ID
function mouseClick(e, action, typeid, strengdata) {
    $('Detail_Div').hide();
    $('Click_Div').hide();
    $('Mask_DIV').className = "overlay";
    var ev = e || window.event;
    var mousePos = mousePosition(ev);
    var obj = document.getElementById('Click_Div');
    obj.style.left = mousePos.x + 15 + "px"; //指定层显示时其左上角的横向坐标（事件触发时的坐标偏移10）
    obj.style.top = mousePos.y + 15 + "px"; //指定层显示时其左上角的纵向坐标（事件触发时的坐标偏移10）
    obj.style.visibility = 'visible';
    var Action = "strengdata=" + strengdata + "&action=" + action + "&typeid=" + typeid + "&showtype=1";
    var options = {
        method: 'get',
        asynchronous: true,
        parameters: Action,
        onComplete: ClickDivSuccess
    };
    new Ajax.Request('AjaxDiv.aspx?no-cache=' + Math.random(), options);
}
//AJAX引用成功
function LoadDeatial(xmlhttprequest) {
    var html = xmlhttprequest.responseText;
    $('Detail_Div').show();
    if (html != "error") {
        $('Detail_Div').innerHTML = html;
    }
    else {
        $('Detail_Div').innerHTML = "<ul class=\"listProp_1\"><li><b>连接超时！</b></li></ul>";
    }

}

function ClickDivSuccess(xmlhttprequest) {
    var html = xmlhttprequest.responseText;
    $('Click_Div').show();
    if (html != "error") {
        $('Click_Div').innerHTML = html;
    }
    else {
        $('Click_Div').innerHTML = "<ul class=\"listProp_1\"><li><b>连接超时！</b></li></ul>";
        $('Click_Div').innerHTML += "<p class=\"quit\"><a href=\"javascript:void(0);\" onclick=\"CloseDiv();\"><img src=\"Images/quit.jpg\" border=\"0\" /></a></p>";
    }
}

//鼠标移开触发事件(关闭层)
function mouseOut(index) {
    $('Detail_Div').hide();
    $('Detail_Div').innerHTML = "";
}
//关闭按钮
function CloseDiv() {
    $('Mask_DIV').className = "";
    $('Click_Div').hide();
}

//拖动
var tHeight, lWidth;
function Down(e) {
    var event = window.event || e;
    tHeight = event.clientY - parseInt(document.getElementById('Click_Div').style.top.replace(/px/, ""));
    lWidth = event.clientX - parseInt(document.getElementById('Click_Div').style.left.replace(/px/, ""));
    document.onmousemove = Move;
    document.onmouseup = Up;
}
function Move(e) {
    var event = window.event || e;
    var top = event.clientY - tHeight;
    var left = event.clientX - lWidth;
    //判断 top 和 left 是否超出边界
    top = top < 0 ? 0 : top;
    top = top > document.body.offsetHeight - 150 ? document.body.offsetHeight - 150 : top;
    left = left < 0 ? 0 : left;
    left = left > document.body.offsetWidth - 300 ? document.body.offsetWidth - 300 : left;
    document.getElementById('Click_Div').style.top = top + "px";
    document.getElementById('Click_Div').style.left = left + "px";
}
function Up() {
    document.onmousemove = null;
}

//回车提交
function submitenter(event) {
    if (event.keyCode == 13) {
        event.returnValue = false;
        $('btnsearch').click();
    }
}
