$(function(){
    var $win = $(window),
        mapW = 0,
        winH = $win.height(),
        winW = $win.width(),
        $bodyContainer = $('#bodyContainer'),
        $extendMap = $('#extendMap');

    // 导航功能
    var navObj = {
        wrap: $('#navWrap'),
        init: function() {
            this.events();
        },
        events: function() {
            var that = this;
            that.wrap.on('mouseover', 'li', function() {
                var $this = $(this);
                $this.addClass('active');
            }).on('mouseout','li', function () {
                var $this = $(this);
                $this.removeClass('active');
            });
        }
    };
    // 地图页面功能
    var mapObj = {
        extendMapWrap: $('#extendMap .baidu-map'),
        sideW: $('#extendMap .map-list').width(),
        init: function() {
            this.events();
            this.resize();
        },
        resize: function() {
            var that = this;
            $win.on('resize onload', function() {
                winH = $win.height();
                winW = $win.width();
                mapW = winW > 600 ? (winW - that.sideW) : (600 - that.sideW);
                that.extendMapWrap.width(mapW - 3).height(winH);
            });
        },
        events: function() {
            var that = this;
            $('#extendBtn').on('click', function(e) {
                e.preventDefault();
                winH = $win.height();
                winW = $win.width();
                $bodyContainer.hide();

                // 重设百度地图的高度
                mapW = winW > 600 ? (winW - that.sideW) : (600 - that.sideW);
                that.extendMapWrap.width(mapW - 3).height(winH);
                $extendMap.show();
            });
            $('#shrinkBtn').on('click', function(e) {
                e.preventDefault();
                $bodyContainer.show();
                $extendMap.hide();
            });

        }
    };

    var selectObj = {
        selectOption: $('.select-option', $extendMap),
        init: function() {
            this.events();
        },
        showSelect: function(elem) {
            this.selectOption.hide();
            elem.show();
        },
        selectedArea: function(elem, index, curOption) {
            var $simulateSelect = elem.parents('.simulate-select'),
                $headerInput = $('.header-input', $simulateSelect);

            curOption.hide();
            $('li', curOption).removeClass('active').eq(index).addClass('active');
            $headerInput.val(elem.text());
        },
        events: function() {
            var that = this;
            $extendMap.on('click', '.select-header', function() {
                var $this = $(this),
                    $option = $this.parents('.simulate-select').find('.select-option');
                that.showSelect($option);
            });
            that.selectOption.on('click', 'li', function() {
                var $this = $(this),
                    index =  $this.index(),
                    $curSelOption = $this.parents('.select-option');
                that.selectedArea($this, index, $curSelOption);
            });
        }
    };
    navObj.init();
    mapObj.init();
    selectObj.init();
    //处理容器里模块为空的情况
    $.each($("[id][runat=server][default=true]"),
		function() {
    	    dealDefaultPanelBackground($(this));
        }
    );
});