﻿$.cnblogs.LocationDialog = $.cnblogs.Dialog.extend({
    data: [{ name: "北京", cities: ["西城", "东城", "崇文", "宣武", "朝阳", "海淀", "丰台", "石景山", "门头沟", "房山", "通州", "顺义", "大兴", "昌平", "平谷", "怀柔", "密云", "延庆"] }, { name: "天津", cities: ["青羊", "河东", "河西", "南开", "河北", "红桥", "塘沽", "汉沽", "大港", "东丽", "西青", "北辰", "津南", "武清", "宝坻", "静海", "宁河", "蓟县", "开发区"] }, { name: "河北", cities: ["石家庄", "秦皇岛", "廊坊", "保定", "邯郸", "唐山", "邢台", "衡水", "张家口", "承德", "沧州", "衡水"] }, { name: "山西", cities: ["太原", "大同", "长治", "晋中", "阳泉", "朔州", "运城", "临汾"] }, { name: "内蒙古", cities: ["呼和浩特", "赤峰", "通辽", "锡林郭勒", "兴安"] }, { name: "辽宁", cities: ["大连", "沈阳", "鞍山", "抚顺", "营口", "锦州", "丹东", "朝阳", "辽阳", "阜新", "铁岭", "盘锦", "本溪", "葫芦岛"] }, { name: "吉林", cities: ["长春", "吉林", "四平", "辽源", "通化", "延吉", "白城", "辽源", "松原", "临江", "珲春"] }, { name: "黑龙江", cities: ["哈尔滨", "齐齐哈尔", "大庆", "牡丹江", "鹤岗", "佳木斯", "绥化"] }, { name: "上海", cities: ["浦东", "杨浦", "徐汇", "静安", "卢湾", "黄浦", "普陀", "闸北", "虹口", "长宁", "宝山", "闵行", "嘉定", "金山", "松江", "青浦", "崇明", "奉贤", "南汇"] }, { name: "江苏", cities: ["南京", "苏州", "无锡", "常州", "扬州", "徐州", "南通", "镇江", "泰州", "淮安", "连云港", "宿迁", "盐城", "淮阴", "沐阳", "张家港"] }, { name: "浙江", cities: ["杭州", "金华", "宁波", "温州", "嘉兴", "绍兴", "丽水", "湖州", "台州", "舟山", "衢州"] }, { name: "安徽", cities: ["合肥", "马鞍山", "蚌埠", "黄山", "芜湖", "淮南", "铜陵", "阜阳", "宣城", "安庆"] }, { name: "福建", cities: ["福州", "厦门", "泉州", "漳州", "南平", "龙岩", "莆田", "三明", "宁德"] }, { name: "江西", cities: ["南昌", "景德镇", "上饶", "萍乡", "九江", "吉安", "宜春", "鹰潭", "新余", "赣州"] }, { name: "山东", cities: ["青岛", "济南", "淄博", "烟台", "泰安", "临沂", "日照", "德州", "威海", "东营", "荷泽", "济宁", "潍坊", "枣庄", "聊城"] }, { name: "河南", cities: ["郑州", "洛阳", "开封", "平顶山", "濮阳", "安阳", "许昌", "南阳", "信阳", "周口", "新乡", "焦作", "三门峡", "商丘"] }, { name: "湖北", cities: ["武汉", "襄樊", "孝感", "十堰", "荆州", "黄石", "宜昌", "黄冈", "恩施", "鄂州", "江汉", "随枣", "荆沙", "咸宁"] }, { name: "湖南", cities: ["长沙", "湘潭", "岳阳", "株洲", "怀化", "永州", "益阳", "张家界", "常德", "衡阳", "湘西", "邵阳", "娄底", "郴州"] }, { name: "广东", cities: ["广州", "深圳", "东莞", "佛山", "珠海", "汕头", "韶关", "江门", "梅州", "揭阳", "中山", "河源", "惠州", "茂名", "湛江", "阳江", "潮州", "云浮", "汕尾", "潮阳", "肇庆", "顺德", "清远"] }, { name: "广西", cities: ["南宁", "桂林", "柳州", "梧州", "来宾", "贵港", "玉林", "贺州"] }, { name: "海南", cities: ["海口", "三亚"] }, { name: "重庆", cities: ["渝中", "大渡口", "江北", "沙坪坝", "九龙坡", "南岸", "北碚", "万盛", "双桥", "渝北", "巴南", "万州", "涪陵", "黔江", "长寿"] }, { name: "四川", cities: ["成都", "达州", "南充", "乐山", "绵阳", "德阳", "内江", "遂宁", "宜宾", "巴中", "自贡", "康定", "攀枝花"] }, { name: "贵州", cities: ["贵阳", "遵义", "安顺", "黔西南", "都匀"] }, { name: "云南", cities: ["昆明", "丽江", "昭通", "玉溪", "临沧", "文山", "红河", "楚雄", "大理"] }, { name: "西藏", cities: ["拉萨", "林芝", "日喀则", "昌都"] }, { name: "陕西", cities: ["西安", "咸阳", "延安", "汉中", "榆林", "商南", "略阳", "宜君", "麟游", "白河"] }, { name: "甘肃", cities: ["兰州", "金昌", "天水", "武威", "张掖", "平凉", "酒泉"] }, { name: "青海", cities: ["黄南", "海南", "西宁", "海东", "海西", "海北", "果洛", "玉树"] }, { name: "宁夏", cities: ["银川", "吴忠"] }, { name: "新疆", cities: ["乌鲁木齐", "哈密", "喀什", "巴音郭楞", "昌吉", "伊犁", "阿勒泰", "克拉玛依", "博尔塔拉"]}],

    areEqual: function(x, y) {
        return ((x.province == y.province) && (x.city == y.city));
    },

    createSpan: function(item) {
        var self = this; //保持this引用

        var span = $.create({
            tag: 'span',
            text: item.province == item.city ? item.city : item.province + '-' + item.city,
            children: {
                tag: 'img',
                attributes: { src: '/Image/btn_delete.png', alt: '删除', title: '删除该项' },
                style: { width: '13px', height: '13px', position: 'relative', top: '2px', cursor: 'pointer' },
                callback: function(element) {
                    element.click(function() {
                        var item = element.parent().data('item');
                        self.removeItem(item);
                    });
                }
            }
        });

        span.data('item', item);

        return span;
    },

    isDuplicated: function(item) {
        var self = this; //保持this引用

        return ($.grep(this.items, function(element) {
            return self.areEqual(element, item);
        }).length > 0);
    },

    addItem: function(item) {
        if (this.isDuplicated(item) == false) {
            if (this.max == this.items.length) {
                if (this.max == 1) {
                    this.selectedArea.children('span:gt(0)').remove();
                    this.items = new Array();
                }
                else {
                    alert('最多只能选择五个地区，请删除后再选择！');
                    return;
                }
            }
            var span = this.createSpan(item);
            item.associatedSpan = span;

            this.selectedArea.append(span);
            this.items.push(item);
        }
    },

    getItem: function(element) {
        var province = element.attr('title');
        var city = element.html();
        if (!province) {
            province = city;
        }
        var item = { province: province, city: city };
        return item;
    },

    removeItem: function(item) {
        var items = this.items;
        this.items = new Array();
        for (var i = 0; i < items.length; i++) {
            if (this.areEqual(item, items[i])) {
                //不加入新的数组
                //删除相应的span
                items[i].associatedSpan.remove();
            }
            else {
                //加入到新的数组
                this.items.push(items[i]);
            }
        }
    },

    showCities: function(province) {
        var self = this; //保持this引用

        var cities = $.grep(this.data, function(element) {
            return (element.name == province);
        })[0].cities;

        this.cityArea.empty();

        var addCityClickHandler = function(element) {
            element.click(function() {
                var item = self.getItem(element);
                self.addItem(item);
            });
        };

        var index = 0;
        var tr;
        for (var i = 0; i < cities.length; i++) {
            if (index++ == 0) {
                tr = $.create({ tag: 'tr' });
                this.cityArea.append(tr);
            }
            if (index == 3) {
                index = 0;
            }

            var city = cities[i];

            var td = $.create({
                tag: 'td', children: {
                    tag: 'a', attributes: { href: '###', title: province }, text: city, callback: addCityClickHandler
                }
            });

            tr.append(td);
        }
    },

    initialize: function(options) {
        var self = this; //保持this引用
        $.cnblogs.LocationDialog.baseClass.initialize.call(this, options);
        this.confirmed = options.confirmed || { scope: this, handler: function(items) { } };
        this.items = new Array();
        this.max = options.max || 5;
        if (!this.dialogOptions) {
            this.dialogOptions = {};
        }
        $.extend(this.dialogOptions, {
            width: 600,
            height: 495,
            title: '请选择地区',
            resizable: false,
            modal: true,
            overlay: {
                'background-color': '#EEEEEE',
                opacity: 0.6
            },
            buttons: {
                '确定': function() {
                    self.close();
                    self.confirmed.handler.call(self.confirmed.scope, self.items);
                }
            }
        });
    },

    getContentObj: function() {
        var self = this; //保持this引用

        var addCityClickHandler = function(element) {
            element.click(function() {
                self.cityArea.empty();
                self.selectedProvince.empty();
                var item = self.getItem(element);
                self.addItem(item);
            });
        };

        var addProvinceClickHandler = function(element) {
            element.click(function() {
                self.selectedProvince.html($(this).text());
                self.showCities($(this).text());
            });
        };

        var obj = $.create({
            tag: 'div', attributes: { id: 'dialog' }, children: 
                { tag: 'div', attributes: { id: 'dialog_wrapper' }, children: 
                    { tag: 'div', style: { margin: '10px 20px' }, children: [
                        { tag: 'div', children: [
                            { tag: 'div', attributes: { id: 'dialog_left' }, style: { 'float': 'left', width: '310px' }, children: [
                                { tag: 'h3', className: 'dialog_title_name', text: '省份列表' },
                                { tag: 'table', className: 'dialog_list dialog_province_block', children: [
                                    { tag: 'tr', children: [
                                        { tag: 'td',className:'th',text: '直辖市：' },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '北京', callback: addCityClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '上海', callback: addCityClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '天津', callback: addCityClickHandler }
                                        }
                                    ] }, //tr
                                    { tag: 'tr', children: [
                                        {
                                            tag:'td',className:'th',text:'&nbsp;'
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '重庆', callback: addCityClickHandler }
                                        },
                                        { tag: 'td', attributes: { colspan: '2'} }
                                    ] }, //tr
                                    { tag: 'tr', className: 'dialog_even', children: [
                                        { tag: 'td', className:'th',text: '华北-东北:' },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '河北', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '山西', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '内蒙古', callback: addProvinceClickHandler }
                                        }
                                    ] }, //tr
                                    { tag: 'tr', className: 'dialog_even', children: [
                                        {
                                            tag:'td',className:'th',text:'&nbsp;'
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '辽宁', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '吉林', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '黑龙江', callback: addProvinceClickHandler }
                                        }
                                    ] }, //tr
                                    { tag: 'tr', children: [
                                        { tag: 'td', className:'th',text: '华东地区：' },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '江苏', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '浙江', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '安徽', callback: addProvinceClickHandler }
                                        }
                                    ] }, //tr
                                    { tag: 'tr', children: [
                                        {
                                            tag:'td',className:'th',text:'&nbsp;'
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '福建', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '江西', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '山东', callback: addProvinceClickHandler }
                                        }
                                    ] }, //tr
                                    { tag: 'tr', className: 'dialog_even', children: [
                                        { tag: 'td', className:'th', text: '华南-华中：' },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '广东', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '广西', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '海南', callback: addProvinceClickHandler }
                                        }
                                    ] }, //tr
                                    { tag: 'tr', className: 'dialog_even', children: [
                                        {
                                            tag:'td',className:'th',text:'&nbsp;'
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '河南', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '湖北', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '湖南', callback: addProvinceClickHandler }
                                        }
                                    ] }, //tr
                                    { tag: 'tr', children: [
                                        { tag: 'td', className:'th', text: '西北-西南：' },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '陕西', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '甘肃', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '青海', callback: addProvinceClickHandler }
                                        }
                                    ] }, //tr
                                    { tag: 'tr', children: [
                                        {
                                            tag:'td',className:'th',text:'&nbsp;'
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '宁夏', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '新疆', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '四川', callback: addProvinceClickHandler }
                                        }
                                    ] }, //tr
                                    { tag: 'tr', children: [
                                        {
                                            tag:'td',className:'th',text:'&nbsp;'
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '贵州', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '云南', callback: addProvinceClickHandler }
                                        },
                                        { tag: 'td', children:
                                            { tag: 'a', attributes: { href: '###' }, text: '西藏', callback: addProvinceClickHandler }
                                        }
                                    ] } //tr
                                ] } //table
                            ] }, //div
                            { tag: 'div', attributes: { id: 'dialog_right' }, style: { 'float': 'right', width: '230px', 'border-left': '1px dotted #ccc', 'margin-top': '30px', height: '300px' }, children: [
                                { tag: 'div', attributes: { id: 'dialog_select_tip' }, text: '请选择工作城市--' },
                                { tag: 'h3', className: 'dialog_title_name', callback: function(element) { self.selectedProvince = element; } },
                                { tag: 'table', className: 'dialog_list dialog_province_block', style: { width: '210px', 'margin-left': '30px' }, callback: function(element) { self.cityArea = element; } }
                            ] }, //div
                            { tag: 'div', style: { clear: 'both'} } //div
                        ] },  //div
                        { tag: 'div', className: 'dialog_joboffer_block', children: [
                            { tag: 'span', className: 'dialog_selected_span', text: '已选地点：' }
                        ], callback: function(element) { self.selectedArea = element; } //div
                    }]
                } }
            });

            return obj;
        }
    });

$.cnblogs.IndustryDialog = $.cnblogs.Dialog.extend({
    createSpan: function(name, chk) {
        var self = this; //保持this引用

        var span = $.create({
            tag: 'span',
            text: name,
            attributes: { title: name },
            children: {
                tag: 'img',
                attributes: { src: '/Image/btn_delete.png', alt: '删除', title: '删除该项' },
                style: { width: '13px', height: '13px', position: 'relative', top: '2px', cursor: 'pointer' },
                callback: function(element) {
                    element.click(function() {
                        var item = element.parent().data('item');
                        var chk = element.parent().data('checkbox');
                        self.removeItem(item, chk);						
                    });
                }
            }
        });
        span.data('item', name).data('checkbox', chk);
        return span;
    },

    addItem: function(name, chk) {
        if (this.max == this.items.length) {
            if (this.max == 1) {
                this.selectedArea.children('span:last').data('checkbox').attr('checked', false);
                this.selectedArea.children('span:gt(0)').remove();
                this.items = new Array();
            }
            else {
                alert('最多只能选择五个，请删除后重新选择！');
                chk.attr('checked', false);
                return;
            }
        }
        var span = this.createSpan(name, chk);
        this.selectedArea.append(span);
        this.items.push(name);
    },

    removeItem: function(name, chk) {

        var span = this.selectedArea.children('span[@title="' + name + '"]:eq(1)');

        var items = this.items;
        this.items = new Array();

        for (var i = 0; i < items.length; i++) {
            if (items[i] != span.attr('title')) {
                this.items.push(items[i]);
            }
        }

        span.remove();
        
        if (chk) {
            chk.attr('checked', false);
        }
    },

    initialize: function(options) {
        var self = this; //保持this引用

        $.cnblogs.IndustryDialog.baseClass.initialize.call(this, options);
        this.confirmed = options.confirmed || { scope: this, handler: function(items) { } };
        this.items = new Array();
        this.max = options.max || 5;

        if (!this.dialogOptions) {
            this.dialogOptions = {};
        }
        $.extend(this.dialogOptions, {
            width: 600,
            height: 300,
            title: '请选择行业类别',
            resizable: false,
            modal: true,
            overlay: {
                'background-color': '#EEEEEE',
                opacity: 0.6
            },
            buttons: {
                '确定': function() {
                    self.close();
                    self.confirmed.handler.call(self.confirmed.scope, self.items);
                }
            }
        });
    },

    getContentObj: function() {
        var self = this; //保持this引用

        var addChangeHandler = function(element) {
            element.change(function() {
                var chk = $(this);
                var name = chk.val();
                var checked = chk.attr('checked');
                if (checked) {
                    self.addItem(name, chk);
                }
                else {
                    self.removeItem(name);
                }
            });
            element.click(function() {
                document.body.focus();
            });
        };

        var obj = $.create({
            tag: 'div', attributes: { id: 'dialog' }, children: 
                { tag: 'div', attributes: { id: 'dialog_wrapper' }, children: 
                    { tag: 'div', style: { margin: '10px 20px' }, children: [
                        { tag: 'table', className: 'dialog_list', children: {
                            tag: 'tbody', children: [
                                { tag: 'tr', className: 'dialog_odd', children: [
                                    { tag: 'th', text: '计算机类：' },
                                    { tag: 'td', children: {
                                        tag: 'table', className: 'dialog_inner_table', children: {
                                            tag: 'tbody', children: [
                                                { tag: 'tr', children: [
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_internet', value: '互联网' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_internet' }, text: '互联网' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_soft', value: '计算机软件' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_soft' }, text: '计算机软件' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_hardware', value: '计算机硬件' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_hardware' }, text: '计算机硬件' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_service', value: '计算机服务' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_service' }, text: '计算机服务' }
                                                    ] }
                                                ] }, //tr
                                                { tag: 'tr', children: [
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_business', value: '电子商务' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_business' }, text: '电子商务' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_game', value: '游戏' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_game' }, text: '游戏' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_consult', value: '咨询' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_consult' }, text: '咨询' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_embed', value: '嵌入式' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_embed' }, text: '嵌入式' }
                                                    ] }
                                                ] }, //tr
                                                { tag: 'tr', children: [
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_network', value: '电信/通信/网络设备' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_network' }, text: '电信/通信/网络设备' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_computer_other', value: '计算机其他' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_computer_other' }, text: '计算机其他' }
                                                    ] }
                                                ] } //tr
                                            ] } //tbody
                                        } //table
                                    } //td
                                ] },  //tr
                                { tag: 'tr', className: 'dialog_even', children: [
                                    { tag: 'th', text: '非计算机类：' },
                                    { tag: 'td', children: [
                                        { tag: 'table', className: 'dialog_inner_table', children: [
                                            { tag: 'tbody', children: [
                                                { tag: 'tr', children: [
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_finance', value: '金融/银行' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_finance' }, text: '金融/银行' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_manu', value: '制造' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_manu' }, text: '制造' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_education', value: '教育/培训' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_education' }, text: '教育/培训' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_biology', value: '生物/医药' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_biology' }, text: '生物/医药' }
                                                    ] }
                                                ] }, //tr
                                                { tag: 'tr', children: [
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_life', value: '餐饮/酒店/旅游' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_life' }, text: '餐饮/酒店/旅游' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_traffic', value: '物流/交通/运输' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_traffic' }, text: '物流/交通/运输' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_build', value: '房地产/建筑/装潢' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_build' }, text: '房地产/建筑/装潢' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_gov', value: '政府' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_gov' }, text: '政府' }
                                                    ] }
                                                ] }, //tr
                                                { tag: 'tr', children: [
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'checkbox', attributes: { id: 'chk_other', value: '其他' }, callback: addChangeHandler },
                                                        { tag: 'label', attributes: { 'for': 'chk_other' }, text: '其他' }
                                                    ] }
                                                ] } //tr
                                            ] } //tbody
                                        ] } //table
                                    ] } //td
                                ] } //tr
                            ] } //tbody
                        }, //table
                    { tag: 'div', className: 'dialog_joboffer_block', callback: function(element) { self.selectedArea = element; }, children: [
                        { tag: 'span', className: 'dialog_selected_span', text: '已选行业：' },
                    ] } //div
                ] } //div
            } //div
        });

        return obj;
    }
});

$.cnblogs.TechniqueRequirementDialog = $.cnblogs.Dialog.extend({
    data: [ { text: 'ASP.NET', value: 'ASP.NET', items: [ { text: 'ASP.NET', value: 'ASP.NET' }, { text: 'C#', value: 'C#' } ] }, { text: 'C#', value: 'C#', items: [ { text: 'C#', value: 'C#' }, { text: 'Enterprise Library', value: 'Enterprise Library' } ] } ],
    
    areEqual: function(x, y) {
        return (x.technique == y.technique);
    },
    
    isDuplicated: function(item) {
        var self = this;
        
        return ($.grep(this.items, function(element) {
            return self.areEqual(element, item);
        }).length > 0);
    },
    
    createSpan: function(item) {
        var self = this; 
        
        var span = $.create({
            tag: 'span',
            text: item.technique + '-' + item.levelName,
            children: {
                tag: 'img',
                attributes: { src: '/Image/btn_delete.png', alt: '删除', title: '删除该项' },
                style: { width: '13px', height: '13px', position: 'relative', top: '2px', cursor: 'pointer' },
                callback: function(element) {
                    element.click(function() {
                        var item = element.parent().data('item');
                        self.removeItem(item);
                    });
                }
            }
        });
        
        span.data('item', item);
        
        return span;
    },
    
    getItem: function() {
        var item = {
            technique: this.technique.val(),
            experience: this.experience.val(),
            level: this.level.val(),
            levelName: this.level.children('option:selected').html(),
            comment: this.comment.val()
        };
        return item;
    },
    
    addItem: function(item) {
        if (this.isDuplicated(item) == false) {
            var span = this.createSpan(item);
            item.associatedSpan = span;
            this.selectedArea.append(span);
            this.items.push(item);
        }
        else {
            this.modifyItem(item);
        }
    },
    
    modifyItem: function(item) {
        var self = this;
        
        var items = $.grep(this.items, function(element) {
            return self.areEqual(item, element);
        });
        if (items.length > 0) {
            items[0].level = item.level;
            items[0].levelTip = item.levelTip;
            items[0].comment = item.comment;
            
            var span = self.createSpan(item);
            items[0].associatedSpan.remove();
            items[0].associatedSpan = span;
            self.selectedArea.append(span);
        }
    },
    
    removeItem: function(item) {
        var items = this.items;
        this.items = new Array();
    
        for (var i = 0; i < items.length; i++) {
            if (this.areEqual(items[i], item) == false) {
                this.items.push(items[i]);
            }
        }
        
        item.associatedSpan.remove();
    },
    
    ensureValid: function() {
        if (this.technique.val().length == 0) {
            this.techniqueTip.show();
            return false;
        }
        else {
            this.techniqueTip.hide();
        }
        if (parseInt(this.experience.val()) == NaN) {
            this.experienceTip.show();
            return false;
        }
        else {
            this.experienceTip.hide();
        }
        if (this.level.val().length == 0) {
            this.levelTip.show();
            return false;
        }
        else {
            this.levelTip.hide();
        }
        return true;
    },
    
    initialize: function(options) {
        var self = this; //保持this引用

        $.cnblogs.TechniqueRequirementDialog.baseClass.initialize.call(this, options);
        this.items = new Array();
        this.confirmed = options.confirmed || { scope: this, handler: function(items) { } };

        if (!this.dialogOptions) {
            this.dialogOptions = {};
        }
        $.extend(this.dialogOptions, {
            width: 420,
            height: 334,
            title: '请选择技能要求',
            resizable: false,
            modal: true,
            overlay: {
                'background-color': '#EEEEEE',
                opacity: 0.6
            },
            buttons: {
                '确定': function() {
                    self.close();
                    self.confirmed.handler.call(self.confirmed.scope, self.items);
                }
            }
        });

        if (options.items) {
            self.initItems = options.items;
        }
    },
    
    getContentObj: function() {
        var self = this; //保持this引用
    
        var obj = $.create(
            { tag: 'div', attributes: { id: 'dialog' }, children: [
                { tag: 'div', attributes: { id: 'dialog_wrapper_small'}, children: [
                    { tag: 'div', style: { margin: '10px 20px' }, children: [
                        { tag: 'table', className: 'dialog_list', style: { width: '390px' }, children: [
                            { tag: 'tbody', children: [
                                { tag: 'tr', className: 'dialog_odd', children: [
                                    { tag: 'th', attributes: { valign: 'top' }, style: { 'padding-top': '10px' }, text: '请选择：' },
                                    { tag: 'td', children: [
                                        { tag: 'table', style: { width: '294px' }, children: [
                                            { tag: 'tbody', children: [
                                                { tag: 'tr', children: [
                                                    { tag: 'td', text: '类别：' },
                                                    { tag: 'td', children: [
                                                        { tag: 'select', style: { width: '120px' }, callback: function(element) { self.type = element; } }
                                                    ] },
                                                    { tag: 'td' }
                                                ] },
                                                { tag: 'tr', children: [
                                                    { tag: 'td', text: '技能：' },
                                                    { tag: 'td', children: [
                                                        { tag: 'select', style: { width: '120px' }, callback: function(element) { self.technique = element; } }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'div', style: { display: 'none' }, className: 'validation-erorr', text: '请选择', callback: function(element) { self.techniqueTip = element; } }
                                                    ] } 
                                                ] }, 
                                                { tag: 'tr', children: [
                                                    { tag: 'td', text: '掌握程度：' },
                                                    { tag: 'td', children: [
                                                        { tag: 'select', style: { width: '120px' }, callback: function(element) { self.level = element; }, children: [
                                                            { tag: 'option', value: '', text: '请选择' },
                                                            { tag: 'option', value: '1', text: '入门' },
                                                            { tag: 'option', value: '2', text: '了解' },
                                                            { tag: 'option', value: '3', text: '一般' },
                                                            { tag: 'option', value: '4', text: '熟练' },
                                                            { tag: 'option', value: '5', text: '精通' }
                                                        ] }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'div', style: { display: 'none' }, className: 'validation-erorr', text: '请选择', callback: function(element) { self.levelTip = element; } }
                                                    ] }
                                                ] },
                                                { tag: 'tr', children: [
                                                    { tag: 'td', text: '使用时间：' },
                                                    { tag: 'td', children: [
                                                        { tag: 'input', type: 'text', className: 'input_default', style: { width: '60px' }, callback: function(element) { self.experience = element; } },
                                                        { tag: 'span', text: '月' }
                                                    ] },
                                                    { tag: 'td', children: [
                                                        { tag: 'div', style: { display: 'none' }, className: 'validation-erorr', text: '请输入', callback: function(element) { self.experienceTip = element; } }
                                                    ] }
                                                ] },                                                
                                                { tag: 'tr', children: [
                                                    { tag: 'td', attributes: { valign: 'top' }, style: { 'padding-top': '12px' }, text: '备注' },
                                                    { tag: 'td', attributes: { colspan: '2' }, children: [
                                                        { tag: 'textarea', className: 'memo', callback: function(element) { self.comment = element; } }
                                                    ] }
                                                ] },
                                                { tag: 'tr', children: [
                                                    { tag: 'td' },
                                                    { tag: 'td', attributes: { colspan: '2', align: 'left' }, children: [
                                                        { tag: 'input', type: 'button', className: 'btn_add', value: '', callback: function(element) {
                                                            element.click(function() {
                                                                if (self.ensureValid()) {
                                                                    var item = self.getItem();
                                                                    self.addItem(item);
                                                                }
                                                            });
                                                        } }
                                                    ] }
                                                ] }
                                            ] }
                                        ] }
                                    ] }
                                ] }
                            ] }
                        ] },
                        { tag: 'div', className: 'dialog_joboffer_block_2', callback: function(element) { self.selectedArea = element; }, children: [
                            { tag: 'span', className: 'dialog_selected_span', text: '技能要求：' }
                        ] }
                    ] }
                ] }
            ] }
        );

        var source = new $.cnblogs.DoubleListBindingSource({
            data: self.data
        });
        source.bindTo({
            parent: self.type,
            child: self.technique,
            defaultItem: { text: '请选择', value: '' }
        });
        
        if (self.initItems) {
            $.each(self.initItems, function() {
                self.addItem(this);
            });
        }
        
        return obj;
    }
});

$.cnblogs.LanguageRequirementDialog = $.cnblogs.Dialog.extend({
    areEqual: function(x, y) {
        return (x.language == y.language);
    },
    
    isDuplicated: function(item) {
        var self = this;
        
        return ($.grep(this.items, function(element) {
            return self.areEqual(item, element);
        }).length > 0);
    },

    createSpan: function(item) {
        var self = this;

        var span = $.create({
            tag: 'span',
            text: item.language + '-' + item.levelName,
            children: {
                tag: 'img',
                attributes: { src: '/Image/btn_delete.png', alt: '删除', title: '删除该项' },
                style: { width: '13px', height: '13px', position: 'relative', top: '2px', cursor: 'pointer' },
                callback: function(element) {
                    element.click(function() {
                        var item = element.parent().data('item');
                        self.removeItem(item);
                    });
                }
            }
        });

        span.data('item', item);

        return span;
    },
    
    ensureValid: function() {
        if (this.language.val().length == 0) {
            this.languageTip.show();
            return false;
        }
        else {
            this.languageTip.hide();
        }
        if (this.level.val().length == 0) {
            this.levelTip.show();
            return false;
        }
        else {
            this.levelTip.hide();
        }
        return true;
    },

    getItem: function() {
        var item = {
            language: this.language.val(),
            level: this.level.val(),
            levelName: this.level.children('option:selected').html(),
            comment: this.comment.val()
        };
        return item;
    },

    addItem: function(item) {
        if (this.isDuplicated(item) == false) {
            var span = this.createSpan(item);
            item.associatedSpan = span;
            this.selectedArea.append(span);
            this.items.push(item);
        }
        else {
            this.modifyItem(item);
        }
    },
    
    modifyItem: function(item) {
        var self = this;
        
        var items = $.grep(this.items, function(element) {
            return self.areEqual(item, element);
        });
        if (items.length > 0) {
            items[0].level = item.level;
            items[0].levelTip = item.levelTip;
            items[0].comment = item.comment;

            var span = self.createSpan(item);
            items[0].associatedSpan.remove();
            items[0].associatedSpan = span;
            self.selectedArea.append(span);
        }
    },

    removeItem: function(item) {
        var items = this.items;
        this.items = new Array();

        for (var i = 0; i < items.length; i++) {
            if (this.areEqual(items[i], item) == false) {
                this.items.push(items[i]);
            }
        }

        item.associatedSpan.remove();
    },

    initialize: function(options) {
        var self = this; //保持this引用

        $.cnblogs.LanguageRequirementDialog.baseClass.initialize.call(this, options);
        this.confirmed = options.confirmed || { scope: this, handler: function(items) { } };
        this.items = new Array();
        if (!this.dialogOptions) {
            this.dialogOptions = {};
        }
        $.extend(this.dialogOptions, {
            width: 420,
            height: 288,
            title: '请选择外语要求',
            resizable: false,
            modal: true,
            overlay: {
                'background-color': '#EEEEEE',
                opacity: 0.6
            },
            buttons: {
                '确定': function() {
                    self.close();
                    self.confirmed.handler.call(self.confirmed.scope, self.items);
                }
            }
        });

        if (options.items) {
            self.initItems = options.items;
        }
    },

    getContentObj: function() {
        var self = this;
        
        var obj = $.create(
            { tag: 'div', attributes: { id: 'dialog' }, children: [
                { tag: 'div', attributes: { id: 'dialog_wrapper_small' }, children: [
                    { tag: 'div', style: { margin: '10px 20px' }, children: [
                        { tag: 'table', className: 'dialog_list', style: { width: '390px' }, children: [
                            { tag: 'tbody', children: [
                                { tag: 'tr', className: 'dialog_odd', children: [
                                    { tag: 'th', attributes: { valign: 'top' }, style: { 'padding-top': '10px' }, text: '请选择：' },
                                    { tag: 'td', children: [
                                        { tag: 'table', style: { width: '294px' }, children: [
                                            { tag: 'tbody', children: [
                                                { tag: 'tr', children: [
                                                    { tag: 'td', text: '语种：' }, //td
                                                    { tag: 'td', children: [
                                                        { tag: 'select', style: { width: '120px' }, callback: function(element) { self.language = element; }, children: [
                                                            { tag: 'option', value: '', text: '请选择' },
                                                            { tag: 'option', value: '英语', text: '英语' },
                                                            { tag: 'option', value: '日语', text: '日语' },
                                                            { tag: 'option', value: '法语', text: '法语' }
                                                        ] }
                                                    ] }, //td
                                                    { tag: 'td', children: [
                                                        { tag: 'div', style: { display: 'none' }, text: '请选择', callback: function(element) { self.languageTip = element; } }
                                                    ] } //td
                                                ] }, //td
                                                { tag: 'tr', children: [
                                                    { tag: 'td', text: '程度：' }, //td
                                                    { tag: 'td', children: [
                                                        { tag: 'select', style: { width: '120px' }, callback: function(element) { self.level = element; }, children: [
                                                            { tag: 'option', value: '', text: '请选择' },
                                                            { tag: 'option', value: '1', text: '入门' },
                                                            { tag: 'option', value: '2', text: '了解' },
                                                            { tag: 'option', value: '3', text: '一般' },
                                                            { tag: 'option', value: '4', text: '良好' },
                                                            { tag: 'option', value: '5', text: '精通' }
                                                        ] } //select
                                                    ] }, //td
                                                    { tag: 'td', style: { display: 'none' }, text: '请选择', callback: function(element) { self.levelTip = element; } } //td
                                                ] }, //tr
                                                { tag: 'tr', children: [
                                                    { tag: 'td', attributes: { valign: 'top' }, style: { 'padding-top': '12px' }, text: '备注：' }, //td
                                                    { tag: 'td', attributes: { colspan: '2' }, children: [
                                                        { tag: 'textarea', className: 'memo', callback: function(element) { self.comment = element; } } //textarea
                                                    ] } //td
                                                ] }, //tr
                                                { tag: 'tr', children: [
                                                    { tag: 'td' }, //td
                                                    { tag: 'td', attributes: { colspan: '2', align: 'left' }, children: [
                                                        { tag: 'input', type: 'button', className: 'btn_add', value: '', callback: function(element) {
                                                            element.click(function() {
                                                                if (self.ensureValid()) {
                                                                    var item = self.getItem();
                                                                    self.addItem(item);
                                                                }
                                                            });
                                                        } } //input
                                                    ] }
                                                ] } //tr
                                            ] }//tbody
                                        ] } //table
                                    ] } //td
                                ] } //th
                            ] } //tr
                        ] } //tbody
                    ] }, //table
                    { tag: 'div', className: 'dialog_joboffer_block', callback: function(element) { self.selectedArea = element; }, children: [
                        { tag: 'span', className: 'dialog_selected_span', text: '外语要求：' }
                    ] }
                ] } //div
            ] } //div
        ); //div

        if (self.initItems) {
            $.each(self.initItems, function() {
                self.addItem(this);
            });
        }
        
        return obj;
    }
});

$.cnblogs.JobPositionGrid = $.cnblogs.Control.extend({
    initialize: function(options) {
        $.cnblogs.JobPositionGrid.baseClass.initialize.call(this, options);
        //data形式参考代码
        this.data = options.data;
        if (!this.data) {
            this.data = [
                { name: '程序开发：', items: ['.NET工程师', 'JAVA工程师', 'PHP工程师', 'C/C++工程师', '移动开发', '其他'] },
                { name: '数据库开发：', items: ['SQL Server开发', 'Oracle开发', 'DB2开发', 'MySql开发', '其他'] },
                { name: '网页设计：', items: ['页面制作', '平面/3D设计', 'Flex工程师', 'Flash设计师', 'Silvelight工程师', '其他'] },
                { name: '管理/架构：', items: ['项目经理', '技术总监', '首席技术官(CTO)', '产品经理', '系统分析师', '系统架构师', '质量管理与测试', '其他'] },
                { name: '其他职位：', items: ['用户体验设计师', '技术支持', '网站编辑', '技术讲师', '产品运营', '产品销售', '产品策划', '其他'] }
            ];
        }

        //每行放几个元素，默认4个
        this.columns = options.columns || 4;

        this.selected = options.selected || { scope: this, handler: function(item) { } };
    },

    getContentObj: function() {
        var self = this; //保持this引用

        //生成<table><tbody></tbody></table>
        var obj = $('<table class="dialog_list" style="margin-top: 10px;"></table>');
        var body = $('<tbody></tbody>');
        obj.append(body);

        //对大类循环
        for (var i = 0; i < this.data.length; i++) {
            //生成<tr></tr>
            var row = $('<tr></tr>');
            if (i % 2 != 0)
                row = $('<tr class="dialog_even"></tr>');
            //生成<th>大类名</th>
            var header = $('<th></th>').html(this.data[i].name);
            //生成<td><table><tbody></tbody></table></td>
            var columnTd = $('<td></td>');
            var columnTable = $('<table class="dialog_inner_table"></table>');
            var columnBody = $('<tbody></tbody>');
            columnTd.append(columnTable);
            columnTable.append(columnBody);

            row.append(header).append(columnTd);
            body.append(row);

            //对职位名循环
            var column = 0;
            var columnRow = $('<tr></tr>');
            columnBody.append(columnRow);
            for (var j = 0; j < this.data[i].items.length; j++) {
                var item = this.data[i].items[j];
                //根据columns的值来判断是否要换行
                if (column++ == this.columns) {
                    //换行，生成新的<tr></tr>
                    columnRow = $('<tr></tr>');
                    columnBody.append(columnRow);
                }
                //生成<td><a href="###" title="职位名">职位名</a></td>
                var positionName = (item == '其他' ? this.data[i].name.substring(0, this.data[i].name.length - 1) : item);
                var positionLink = $('<a></a>')
                    .attr('href', '###')
                    .attr('title', positionName)
                    .html(item)
                    .click(function() {
                        self.selected.handler.call(self.selected.scope, $(this).attr('title'))
                    });
                columnRow.append($('<td></td>').append(positionLink));
            }
        }

        return obj;
    }
});

$.cnblogs.JobPositionSelectDialog = $.cnblogs.Dialog.extend({
    initialize: function(options) {
        var self = this; //保持this引用

        $.cnblogs.JobPositionSelectDialog.baseClass.initialize.call(this, options);
        
        this.confirmed = options.confirmed || { scope: this, handler: function(item) { } };

        if (!this.dialogOptions) {
            this.dialogOptions = {};
        }
        $.extend(this.dialogOptions, {
            width: 600,
            height: 410,
            title: '请选择职位名称(只能选择一个)',
            resizable: false,
            buttons: {
                '确定': function() {
                    self.close();
                }
            },
            close: function() {
                self.confirmed.handler.call(self.confirmed.scope, self.selectedSpan.html());
                self.close(true);
            },
            modal: true,
            overlay: {
                'background-color': '#EEEEEE',
                opacity: 0.6
            }
        });

        this.selected = {
            scope: this,
            handler: function(item) {
                this.selectedSpan.html(item);
            }
        };
    },

    getContentObj: function() {
        var obj = $('<div id="dialog"></div>');
        var outWrapper = $('<div style="margin:10px 20px;"></div>');
        obj.append(outWrapper);
        var wrapper = $('<div id="dialog_wrapper"></div>');
        outWrapper.append(wrapper);

        var gridDiv = $('<div></div>');
        wrapper.append(gridDiv);
        var grid = new $.cnblogs.JobPositionGrid({
            container: gridDiv,
            selected: this.selected
        });
        grid.render();

        var selectedDiv = $('<div class="dialog_joboffer_block"></div>').append('<span class="dialog_selected_span">已选职位：</span>');
        wrapper.append(selectedDiv);
        this.selectedSpan = $('<span id="selected"></span>');
        selectedDiv.append(this.selectedSpan);

        return obj;
    }
});
