﻿var Epomap = {};

String.prototype.subString = function(length, append) {
    if (this.length <= length)
        return this;
    else {
        return this.substr(0, length) + append;
    }
};

Epomap.mapquery = {
    loadingImg: "",
    resultCache: [],

    recordCount: 0,
    pageCount: 0,
    pageSize: 6,

    linkUrl: 'http://www.lymap.net:8719/newmappublic/index.phtml?',

    queryUrl: "http://www.lymap.net:8719/NewMapServer/WebServices/FeatureServer.php?layer=dmdz&version=1&request=query",
    query: function() {
        this.block();
        //debugger;
        $.get('themeclass.xml', function(res) {
            //debugger
        })
        NDomainRequest(this.queryUrl + "&page=1&maxfeatures=1", "Epomap.mapquery.prepareData");
    },

    block: function() {
        $.blockUI.defaults.css.border = "0px";
        $.blockUI.defaults.overlayCSS.backgroundColor = "gray";
        $.blockUI.defaults.css.width = "100px";
        $('#bz_con').block({ message: "<img src='../../content/images/progressbar_long.gif' alt='正在加载' />" });
    },
    unblock: function() {
        $('#bz_con').unblock();
    },

    prepareData: function(res) {

        if (!res || res != null) {
            //Epomap.mapquery.count = res.header.pages;
            var count = res.header.pages;
            Epomap.mapquery.count = count;

            var pageSize = Epomap.mapquery.pageSize;

            var pageCount;
            if (count % pageSize != 0) {
                pageCount = Math.floor(count / pageSize) + 1;
            } else {
                pageCount = Math.floor(count / pageSize);
            }

            Epomap.mapquery.pageCount = pageCount;

            NDomainRequest(this.queryUrl + "&maxfeatures=6&page=" + pageCount, "Epomap.mapquery.getLastPageSuccess");

        } else {
            $('#bz_con').html("获取最新标注失败，服务器忙");
        }
    },

    getLastPageSuccess: function(res) {

        if (!res || res != null) {

            for (var i = res.content.features.length - 1; i >= 0; i--) {
                Epomap.mapquery.resultCache.push(res.content.features[i]);
            }
            if (res.content.length == 6) {
                Epomap.mapquery.querySuccess(Epomap.mapquery.resultCache);
            } else {
                var pageCount = Epomap.mapquery.pageCount;
                NDomainRequest(this.queryUrl + "&maxfeatures=6&page=" + (pageCount - 1), "Epomap.mapquery.getLastSecondSuccess");
            }
        }
    },

    getLastSecondSuccess: function(res) {
        var cache = Epomap.mapquery.resultCache;
        var size = Epomap.mapquery.pageSize;
        for (var i = res.content.features.length - 1; i >= 0; i--) {
            if (cache.length == size) {
                Epomap.mapquery.querySuccess(Epomap.mapquery.resultCache);
                break;
            }
            else {
                cache.push(res.content.features[i]);
            }
        }
    },

    querySuccess: function(features) {

        var html1 = '<ul>';
        var properties;
        for (var i = 0; i < features.length; i++) {
            properties = features[i].properties;

            if (i % 2 == 0) {
                html1 += '<li><a href="' + Epomap.mapquery.linkUrl + 'lon=' + features[i].geometry.coordinates[0] + '&lat=' + features[i].geometry.coordinates[1] + '" title="' + properties["名称"] + '">' + properties["名称"].subString(12, '…') + '</a></li>';
            } else {
                html1 += '<li class="bg"><a href="' + Epomap.mapquery.linkUrl + 'lon=' + features[i].geometry.coordinates[0] + '&lat=' + features[i].geometry.coordinates[1] + '" title="' + properties["名称"] + '"> ' + properties["名称"].subString(12, '…') + '</a></li>'
            }
        }

        html1 += '</ul>';

        var html2 = '<ul>'

        for (var i = 0; i < features.length; i++) {
            properties = features[i].properties;

            if (properties["门牌号"] === '') {
                properties["门牌号"] = '&nbsp;'
            }
            if (properties["联系电话" === '']) {
                properties["联系电话"] = '&nbsp;';
            }

            if (i % 2 == 0) {
                html2 += '<li><a href="#" title="' + properties["门牌号"] + '">'
                + properties["门牌号"].subString(7, '… ') + properties["联系电话"] + '</a></li>';
            } else {
                html2 += '<li class="bg"><a href="#" title="'
                + properties["门牌号"] + '"> '
                + properties["门牌号"].subString(7, '… ')
                + properties["联系电话"] + '</a></li>';
            }
        }

        html2 += "</ul>";

        $('#bz_list_left').html(html1);
        $('#bz_list_right').html(html2);

        this.unblock();
    }
};
