/* daum map api
 * bicyclemap.co.kr : 0b4c9240a450d9dea88fc3cc4b79482b98a18526
 * localhost:8000 : 85ef8a523ace3aecacd3db410b48e2f165683527
 * 172.30.100.227:8000 : 2badefef7a141f649d09b9746e00852495736e43
 * bmap:8000 : e3c766ac2f937490cd6e2db41bcdb548bcd80eb5
 */

var proc_querystring = function() {
    var params = bCore.parse_ext_querystring();
    
    if ( $('.spot_point_on_map').length > 0 ) {
        $('.spot_point_on_map').removeClass('selected_spot_point');
    }

    bMap.check_display_courses();
    
    if ( bMap.current_course_id && parseInt(bMap.current_course_id) > 0 ) {
        bMap.hide_course(bMap.current_course_type, bMap.current_course_id);
    }

    if ( params.length > 0 ) {
        if ( 'screen' in params ) {
            bMap.screen = params.screen;
        }
        
        if ( 'zoom_level' in params ) {
            bMap.zoom_level = params.zoom_level;
        }
        
        switch ( params.mode ) {
            case 'browsing':
                bMap.mode = 'browsing';
                bMap.edit_target = '';

                $('.nav_bar_body_container').hide();
                
                $('.map_nav_tabbody').hide();
                $('#map_nav_browsing').show();
                
                switch ( params.display ) {
                    case 'theme':
                        $('#sub_menu').each(function() {
//                            $(this).css('background-position', 'left -46px');
                        });
                        
                        $('#nav_bar_theme_body').show();
                        if ( params.theme_id && params.theme_id > 0 ) {
                            bMap.view_themes(params.theme_type || 'all', params.theme_id || '');
                        }
                    break;
                    
                    case 'shop':
                        $('#nav_bar_shop_body').show();
                        params.shop_id = parseInt(params.shop_id);
                        if ( params.shop_id && params.shop_id > 0 ) {
                            bMap.view_shop(params.shop_id);
                        }
                    break;
                    
                    case 'photo':
                        bMap.current_photo_id = ('photo_id' in params) ? parseInt(params.photo_id) : 0;

                        if ( typeof bMap.current_photo_id == 'number' && bMap.current_photo_id > 0 ) {
                            bMap.current_photo_id = String(bMap.current_photo_id);
                            bMap.view_photo(bMap.current_photo_id);
                        }
                    break;
                    
                    case 'competition':
                    break;
                    
                    case 'spot':
                        bMap.current_spot_id = ('spot_id' in params) ? parseInt(params.spot_id) : 0;
                        
                        if ( typeof bMap.current_spot_id == 'number' && bMap.current_spot_id > 0 ) {
                            bMap.display_spot(bMap.current_spot_id);
                            bMap.view_spot(bMap.current_spot_id);
                        }
                    break;
                    
                    case 'course':
                    default:
                        $('#nav_bar_map_body').show();
                        bMap.current_course_type = ('course_type' in params) ? params.course_type : 'public';
                        bMap.current_course_id = ('course_id' in params) ? parseInt(params.course_id) : 0;
                        
                        if ( bMap.current_course_type && bMap.current_course_id ) {
                            bMap.hide_course(bMap.current_course_type, bMap.current_course_id);
                            bMap.current_course_id = String(bMap.current_course_id);
                            bMap.view_course(bMap.current_course_type, bMap.current_course_id, -1);
                        }
                        else {
                            bMap.display_public_courses();
                        }
                    break;
                }
            break;
            
            case 'editting':
                $('.nav_bar_body_container').hide();
                $('#nav_bar_map_body').show();
                bMap.mode = 'editting';
                
                switch ( params.display ) {
                    case 'way':
                        bMap.current_course_id = ('course_id' in params) ? parseInt(params.course_id) : -1;
                        if ( bMap.current_course_id < 0 ) {
                            alert('선택된 코스가 없습니다. 코스를 먼저 선택하거나 생성해야 합니다.');
                            window.location.href = '#mode=browsing&display=course&course_id=0';
                        }
                        bMap.current_course_id = String(bMap.current_course_id);
                        bMap.current_course_type = ('course_type' in params) ? params.course_type : 'public';
                        
                        bMap.current_way_id = ('way_id' in params) ? params.way_id : 'new';
                        
                        if ( bMap.current_way_id === 'new' ) {
                            bMap.current_way_id = 0;
                            bMap.edit_target = 'way';
                            bMap.ui_new_way(bMap.current_course_type, bMap.current_course_id, bMap.current_way_id);
                        }
                        else if ( bMap.current_way_id === 'select' ) {
                            bMap.current_way_id = 0;
                            bMap.ui_open_course_form(bMap.current_course_type, bMap.current_course_id);
                            bMap.ui_select_way(bMap.current_course_type, bMap.current_course_id);
                        }
                        else {
                            bMap.current_way_id = parseInt(bMap.current_way_id);
                            bMap.edit_target = 'way';
                            bMap.ui_new_way(bMap.current_course_type, bMap.current_course_id, bMap.current_way_id);
                        }
                    break;
                    
                    case 'spot':
                        bMap.edit_target = 'spot';
                        bMap.current_spot_id = ('spot_id' in params) ? params.spot_id : 0;
                        bMap.ui_open_spot_form(bMap.current_spot_id);
                    break;

                    case 'photo':
                        bMap.edit_target = 'photo';
                        bMap.current_photo_id = ('photo_id' in params) ? params.photo_id : 0;
                        bMap.view_photo(bMap.current_photo_id);
                    break;
                    
                    case 'course':
                    default:
                        bMap.current_course_id = ('course_id' in params) ? params.course_id : -1;
                        bMap.current_course_type = ('course_type' in params) ? params.course_type : 'public';
                        bMap.ui_open_course_form(bMap.current_course_type, bMap.current_course_id);
                    break;
                }
            
            break;
            
            default:
                $('.map_nav_tabbody').hide();
                $('#map_nav_browsing').show();
                
                break;
        }
    }
    else {
        $('.map_nav_tabbody').hide();
        $('#map_nav_browsing').show();
    }
}
 

$(window.location).watch('href', function(name, o, n) {
    proc_querystring();
    bMap.fullscreen(bMap.screen);
});


var limit_loaded_courses_count = 20;

var bMap = new Base({
    a: false,
    u: '',
	obj: null,
	zoom_ctrl: null,
	coord: {
	    curr: {}
	},
	dom: {
	    container: null,
	    map: null,
	    fullscreen: null,
	    e_node_list: null
	},
	embed: false,
	
	displayed_public_course: {
	    '1': true, /* 자전거 전용 도로 */
	    '2': false, /* 자동차 겸용 */
	    '3': false /* 보행 겸용 */
	},
	
	spots: {},
	spot_objs: {},
	photos: {},
	photo_objs: {},
	
	courses: {
	    public: {},
	    mine: {},
	    theme: {}
	},
	ways_bound: {},
	courses_bound: {
	    public: {},
	    mine: {},
	    theme: {}
	},
	ways: {},
	ways_on_course: {
	    public: {},
	    mine: {},
	    theme: {}
	},
	node_marks_on_way: {},
	node_marks_on_course: {
	    public: {},
	    mine: {},
	    theme: {}
	},
	way_polylines: {},
	course_polylines: {
	    public: {},
	    mine: {},
	    theme: {}
	},
	way_id_pos: {
	    public: {},
	    mine: {},
	    theme: {}
	},
	editting_nodes: [],
	editting_node_coords: [],
	editting_way: null,
	
	ui: {
	    node_menu: null,
	    browsing_menu: null
	},
	flag_spots: {},
	shops: {},
	competitions: {},
	
	mode: 'browsing',
	edit_target: '',
	get_current_location: null,
	current_way_id: -1,
	current_course_id: -1,
	current_course_type: '',
	current_spot_id: 0,
	current_photo_id: 0,
	current_coord: {x:null, y:null},
	screen: 'default', /* default, full */
	zoom_level: 2,
	
	ways_url: '/ways/',
	user_ways_url: '/ways/mine/',
	course_url: '/course/',
	shop_url: '/shop/',
	coursetype_url: '/course/update/',
	course_image_url: '/course/edit/image/',
	spot_image_url: '/spot/edit/image/',
	map_photo_url: '/map/image/%d/',
	map_photos_url: '/map/images/',
	map_photo_edit_url: '/map/image/edit/',
	connect_way_course_url: '/course/add_way/',
	get_public_courses: '/course/public/',
	loaded_courses_count: 0,
	default_options: {},
	lock_editting_drag: false,
	scrap_src_url: '<iframe frameborder="%frameborder%" src="http://dev.bicyclemap.co.kr/map/embed/#mode=display&display=course&course_type=%course_type%&course_id=%course_id%" width="%width%" height="%height%"></iframe>',
	scrap_curr_course: {
	    course_type: '',
	    course_id: -1
	}
});



bMap.extend({
    init: function(target, options) {
		if ( !document.getElementById(target) ) { throw 'the target element does not exist'; }
		if ( typeof jQuery == 'undefined' ) { throw 'jQuery is required'; }
		if ( typeof DMap == 'undefined' ) { throw 'Daum MAP API is required'; }
		
		if ( !('zoom_ctrl' in options) ) {
    		options['zoom_ctrl'] = true;
		}
		
		if ( (('embed' in options) && options.embed === true) || bMap.embed === true ) {
		    options.width = $(window).width();
		    options.height = $(window).height();
		    $('#btn_fullscreen').hide();
		}
		
		bMap.default_options = options;
        
        bMap.dom.container = $('#'+target);
        bMap.dom.container.show();
        
        bMap.dom.map = $('#'+options.map);
        bMap.dom.map.show();
        
		bMap._init_map(target, options);

        bMap._init_ui({
            zindex: 300,
			width: options.width || $('#'+target).width(),
            height: options.height || $('#'+target).height()
        });
		        
        bMap.set_current_location();
        
        bMap._init_event();
        
        if ( ('after_run' in options) && (typeof options.after_run == 'function') ) {
            options.after_run();
        }
        
        bMap._load_default_data_ajax();

        if ( !('on_public_course' in options) ) { options.on_public_course = ['3']; }        
        for ( var i = 0; i < options.on_public_course.length; ++i ) {
            bMap.turn_display_public_course(String(options.on_public_course[i]));
        }
        
        if ( 'on_spot_category' in options && options.on_spot_category.length > 0 ) {
            var i = 0;
            for ( ; i < options.on_spot_category.length; ++i ) {
                bMap.flag_spots[options.on_spot_category[i]] = false;
                bMap.turn_spot_category(options.on_spot_category[i]);
            }
        }

        bMap.display_spots();
        bMap.display_photos_icon(bMap.photos);
    },
    
    turn_display_public_course: function(way_type) {
        var el = $('#btn_display_way_by_'+way_type);
        var image_url = el.attr('src');
        
    	if ( bMap.displayed_public_course[way_type] === true ) {
    	    el.attr('src', image_url.replace('_on.gif', '_off.gif'));
    	}
    	else {
    	    el.attr('src', image_url.replace('_off.gif', '_on.gif'));        	    
    	}
    	bMap.displayed_public_course[way_type] = !bMap.displayed_public_course[way_type];
    	
    	bMap.hide_public_courses();
        bMap.display_public_courses();
    },
    
    _load_default_data_ajax: function() {
        bMap.load_user_ways();
    },
    
    _init_map: function(target, options) {
		bMap.obj = new DMap(options.map, {
			point: new DLatLng(options.coordinates.lat, options.coordinates.lon),
			level: options.level || 2,
			width: (options.width || bMap.dom.container.width())-$('#nav_bar_body').width(),
			height: options.height || bMap.dom.container.height()
		});
		
		bMap.dom.map.css('position', 'absolute');
        
        if ( options.zoom_ctrl == true ) {
    		bMap.zoom_ctrl = new DZoomControl();
    		bMap.obj.addControl(bMap.zoom_ctrl);
        }


//		bMap.map_type = new DMapTypeControl();
//		bMap.obj.addControl(bMap.map_type);
		
		bMap.coord.curr = options.coordinates;
    
    },
    
    _init_event: function() {
        bMap.ob_document_ready();
        DEvent.addListener(bMap.obj, 'click', bMap.event_map_click);
        DEvent.addListener(bMap.obj, 'zoom', bMap.event_map_zoom);
        
        $(bMap.obj.main).watch('MapMode', function(name, o, n) {
            if ( n === 4 ) {
                $('#distance_mode_status').text('O');
            }
            else {
                $('#distance_mode_status').text('X');
            }
        });
        
        $('#course_info_scrap_btn').click(bMap.ui_scrap_course);
        
        $(window).resize(function() {
            bMap._resize_all_ui();
        });
    },
    
    set_current_location: function() {
        if ( typeof navigator != 'undefined' && typeof navigator.geolocation != 'undefined' && typeof navigator.geolocation.getCurrentPosition != 'undefined' ) {
            var get_location_options = {
        		maximumAge: 1,
        		timeout: 3000,
        		enableHighAccuracy: true
        	};
            
            navigator.geolocation.getCurrentPosition(function(pos) {
                bMap.get_current_location = pos;
                
                if ( bMap.current_way_id > 0 || bMap.current_course_id > 0 ) {
                    return false;
                }
                else {
                    bMap.obj.setCenter(new DLatLng(pos.coords.latitude, pos.coords.longitude));
                }
            }, function(err) {}, get_location_options);
        }
    },
    
    toggle_fullscreen: function() {
        bMap.fullscreen( ( bMap.screen == 'full' ) ? 'default' : 'full' );
    },
    
    fullscreen: function(change_mode) {
        if ( change_mode && change_mode === 'full') {
            bMap.screen = 'full';
            bMap.dom.fullscreen.append(bMap.dom.container);
            
            $(document.body).css({
                overflow: 'hidden'
            });
            bMap.dom.container.css({
                margin: 0
            });
        }
        else {
            bMap.screen = change_mode || 'default';
            
            $('#default_size_container').append(bMap.dom.container);
            
            $(document.body).css({
                overflow: 'auto'
            });
            bMap.dom.container.css({
                width: bMap.default_options.width,
                height: bMap.default_options.height,
                margin: 0
            });
        }
	    
	    bMap._resize_all_ui();
        
        bMap._daum_map_set_size();
    },
    
    _daum_map_set_size: function() {
        if ( !bMap.obj ) {
            return false;
        }
        
//        bMap.obj.main.options.width = bMap.dom.map.width();
//        bMap.obj.main.options.height = bMap.dom.map.height();

        bMap.obj.main.reSetPanelSize(bMap.dom.map.width(), bMap.dom.map.height());
    },
    
    _resize_all_ui: function() {
        if ( bMap.screen === 'full' ) {
            bMap.dom.container.width( $(window).width() );
            bMap.dom.container.height( $(window).height() );
        }
	    
	    var nav_bar_body_width = ( $('#nav_bar_body').css('display') == 'none' ) ? 0 : $('#nav_bar_body').width();
		
		$('#nav_spot_icons')
		    .width( bMap.dom.container.width() - nav_bar_body_width)
		    .css({
		        top: bMap.dom.container.height() - $('#nav_spot_icons').height(),
		        left: nav_bar_body_width
		    });
		
		$('#display_way_by_type')
		    .css({
		       left: nav_bar_body_width + 10
		    });
		
		$('#nav_bar, #nav_bar_body, #nav_bar_toggle, .nav_bar_body_container').height(bMap.dom.container.height());
		
		$('#btn_nav_bar_toggle').css('margin-top', ($('#nav_bar_toggle').height() - $('#btn_nav_bar_toggle').height())/2 );

        
        bMap._resize_nav_bar_form('.except_course_scroll_box1', '.course_scroll_box1');
        bMap._resize_nav_bar_form('.except_course_scroll_box2', '.course_scroll_box2');
        bMap._resize_nav_bar_form('.except_course_scroll_box3', '.course_scroll_box3');
        bMap._resize_nav_bar_form('.except_course_scroll_box4', '.course_scroll_box4');
        bMap._resize_nav_bar_form('.except_course_scroll_box5', '.course_scroll_box5');
        bMap._resize_nav_bar_form('.except_course_scroll_box6', '.course_scroll_box6');
        bMap._resize_nav_bar_form('.except_course_scroll_box7', '.course_scroll_box7');
		
		bMap.dom.map
		    .css({
		        width: bMap.dom.container.width() - nav_bar_body_width,
		        height: bMap.dom.container.height() - $('#nav_spot_icons').height(),
		        top: 0,
		        left: nav_bar_body_width
		    });
		    
		$('#map_photo_loading')
		    .width(nav_bar_body_width)
		    .height($('#nav_bar_toggle').height());
		
    },
    
    _resize_nav_bar_form: function(excepts, target) {
  		var course_scroll_box_height = 0;
  		$(excepts).each(function() {
  		    course_scroll_box_height += $(this).outerHeight(true);
  		});
  		$(target).each(function() {
  		    $(this).height( $('#nav_bar_body').height() - course_scroll_box_height );
  		});
    },
    
    turn_spot_category: function(target) {
        var el = $('#btn_spot_'+target);
        
		if ( target in bMap.flag_spots ) {
		    if ( bMap.flag_spots[target] === true ) {
    		    var flag = false;
    		    var img = el.attr('src').replace('/ico_'+target+'.gif', '/ico_'+target+'_n.gif');
		    }
		    else {
    		    var flag = true;
    		    var img = el.attr('src').replace('/ico_'+target+'_n.gif', '/ico_'+target+'.gif');
		    }
		}
		else {
		    var flag = false;
		    var img = el.attr('src').replace('/ico_'+target+'.gif', '/ico_'+target+'_n.gif');
		}
	    bMap.flag_spots[target] = flag;
	    el.attr('src', img);
    },
    
    _init_ui: function(options) {
        $('#map_uploading_image, #map_loading_image').hide();
        bMap.dom.fullscreen = $('<div></div>')
                .attr('id', 'map_fullscreen')
                .css({
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    zIndex: 10
                }).show();
        $(document.body).prepend(bMap.dom.fullscreen);

        $('.spot_point_on_map').removeClass('selected_spot_point');
        
        bMap.fullscreen(bMap.screen);
        
        $('#btn_fullscreen').attr('href', '#'+bCore.param_string+$('#btn_fullscreen').attr('href'));
		
		$('#nav_bar, #nav_spot_icons')
			.css({
				position: 'absolute',
				zIndex: options.zindex + 1 || 301
			});

		
		$('.btn_map_spot').click(function(e) {
    		e.preventDefault();
    		e.stopPropagation();
    		
    		if ( $(this).attr('id').indexOf('btn_spot_') === -1 ) {
    		    return false;
    		}
    		var target = $(this).attr('id').replace('btn_spot_', '');

    		bMap.turn_spot_category(target);
		    
	        bMap.display_spots();
		    
		    return false;
		});

		
		$('#nav_bar_toggle').click(function(e) {
    		e.preventDefault();
    		e.stopPropagation();
    		
    		if ( $('#nav_bar_body').css('display') == 'none' ) {
    		    $('#nav_bar_body').show();
    		    var nav_bar_body_width = $('#nav_bar_body').width();
    		}
    		else {
    		    $('#nav_bar_body').hide();
    		    var nav_bar_body_width = 0;
    		}
    		
    		$('#nav_spot_icons')
    		    .width( bMap.dom.container.width() - nav_bar_body_width)
    		    .css('left', nav_bar_body_width);

    		$('#display_way_by_type')
    		    .css({
    		       left: nav_bar_body_width + 10
    		    });
    		
    		$('#map_uploading_image, #map_loading_image').css({
    		    top: (bMap.dom.container.height() - $('#map_uploading_image').height())/2,
    		    left: (bMap.dom.container.width() - $('#map_uploading_image').width() + nav_bar_body_width)/2
		    });
    		
    		bMap.dom.map
    		    .css({
    		        width: bMap.dom.container.width() - nav_bar_body_width,
    		        left: nav_bar_body_width
    		    });
    		
    		$('#nav_bar').width(nav_bar_body_width + $(this).width());
    		
    		bMap._daum_map_set_size();
            
            return false;
		});
		
		if ( $('#nav_bar_body').css('display') == 'none' ) {
		    nav_bar_body_width = 0;
		}
		else {
		    nav_bar_body_width = $('#nav_bar_body').width();
		}
		
		$('#map_uploading_image, #map_loading_image').css({
		    top: (bMap.dom.container.height() - $('#map_uploading_image').height())/2,
		    left: (bMap.dom.container.width() - $('#map_uploading_image').width()+nav_bar_body_width)/2,
		    position: 'absolute',
		    zIndex: 999
		}).hide();
        
        bMap.dom.e_node_list = $('#editting_node_list');
        
		bMap._resize_all_ui();
    },
    
    remove_way_from_course: function(course_type, course_id, way_id) {
        bMap.add_way_to_course(course_type, course_id, way_id, true);
    },
    
    add_way_to_course: function(course_type, course_id, way_id, do_remove) {
        $.post(bMap.connect_way_course_url, {
            course_id: course_id,
            way_id: way_id,
            do_remove: ( typeof do_remove == 'undefined') ? false : do_remove,
            course_type: course_type
        }, function(data) {
            alert(data.msg);
            
            if ( data.result ) {
                bMap.remove_course_from_client(course_type, course_id);
                
/*                bMap.load_course(course_type, course_id, false); */
                
                window.location.href = '#mode=browsing&display=course&course_type='+course_type+'&course_id='+course_id;
                window.location.reload(true);
            }
        }, 'json');
    },
    
    ui_open_spot_form: function(spot_id) {
        spot_id = String(spot_id);
        
        $('.map_nav_tabbody').hide();
        $('#nav_bar_map_body, #map_spot_editting').show();
                
        if ( parseInt(spot_id) > 0 && (spot_id in bMap.spots) ) {
            $('#id_spot_name').val(bMap.spots[spot_id].name);
            $('#id_spot_address').val(bMap.spots[spot_id].address);
            $('#id_spot_description').val(bMap.spots[spot_id].description_nb);
            $('#id_spot_category').val(bMap.spots[spot_id].category);
            $('#id_spot_latitude').val(bMap.spots[spot_id].latitude);
            $('#id_spot_longitude').val(bMap.spots[spot_id].longitude);
            $('#btn_back_spot_viewing').attr('href', '#mode=browsing&display=spot&spot_id='+spot_id);
            $(bMap.spot_objs[spot_id].panel).addClass('selected_spot_point');
        }
        else {
            $('.spot_point_on_map').removeClass('selected_spot_point');
            $('#form_edit_spot').get(0).reset();
            $('#btn_back_spot_viewing').attr('href', '#mode=browsing&display=course');
        }

        bMap.coord2addr(
            $('#id_spot_latitude').val(),
            $('#id_spot_longitude').val(),
            'bMap.fill_addr_into_spot'
        );
        
        bMap._resize_nav_bar_form('.except_course_scroll_box5', '.course_scroll_box5');
    },
        fill_addr_into_spot: function(data) {
            $('#id_spot_address').val(data.fullName);
        },
    
    ui_open_course_form: function(type, course_id) {
        $('.map_nav_tabbody, #editting_way_list_actions').hide();
        $('#map_course_editting, #editting_way_list_actions').show();
        
        $('#id_course_course_id').val(course_id);
        $('#id_course_course_type').val(type);
        if ( parseInt(course_id) > 0 ) {
            if ( typeof bMap.courses[type][course_id] == 'undefined' ) {
                bMap.load_course(type, course_id, false);
            }
            $('#id_way_actions, #course_image_form').show();
            $('#id_way_actions_disabled').hide();
            
            $('#id_course_name').val(bMap.courses[type][course_id].name);
            $('#id_course_description').val(bMap.courses[type][course_id].description_nb);
        }
        else {
            $('#id_way_actions, #course_image_form').hide();
            $('#id_way_actions_disabled').show();
        }
        $('.nav_bar_body_container').hide();
        $('#nav_bar_map_body').show();
        
        bMap._resize_nav_bar_form('.except_course_scroll_box2', '.course_scroll_box2');
    },
    
    get_boundary_of_way: function(type, course_id, way_id) {
        var i = 0, boundary = {n:null, e:null, s:null, w:null};
        
        if ( type != '' ) {
            if ( !(way_id in bMap.ways_on_course[type]) ) {
                return false;
            }
            if ( course_id in bMap.courses_bound[type] ) {
                return false;
            }
            var nodes = bMap.ways_on_course[type][way_id].nodes;
        }
        else {
            if ( way_id in bMap.ways_bound ) {
                return false;
            }

            var nodes = bMap.ways[way_id].nodes;
        }

        for ( ; i < nodes.length; ++i ) {
            boundary = bMap.compare_side_coord(boundary, nodes[i].coord.lon, nodes[i].coord.lat);            
        }
        
        if ( type != '' ) {
            bMap.courses_bound[type][course_id] = boundary;
        }
        else {
            bMap.ways_bound[way_id] = boundary;
        }
    },
    
    ui_new_way: function(type, course_id, way_id) {        
        var i = 0;
        
        bMap.remove_only_way_viewed();
        bMap.cancel_node_editting();

        if ( way_id > 0 ) {
            if ( !(way_id in bMap.ways) ) {
                bMap.load_ways(way_id, false);
            }

            var way = bMap.ways[way_id];
            $('#id_way_name').val(way.name);
            $('#id_way_waytype').val(way.waytype);

            for ( i = 0; i < way.nodes.length; ++i ) {
                bMap._add_node_to_map(new DPoint(way.nodes[i].coord.lon, way.nodes[i].coord.lat), way.nodes[i].id);
            }

            bMap.get_boundary_of_way('', course_id, way_id);
            var boundary = bMap.ways_bound[way_id];
            bMap.obj.setBound(boundary.e, boundary.s, boundary.w, boundary.n);        
        }
        else {
            $('#id_way_name').val('');
            $('#id_way_waytype').val('0');
        }

        $('.map_nav_tabbody, .nav_bar_body_container, #exists_way_list, #exists_way_list_actions').hide();
        $('.nav_bar_body_container, #map_way_editting, #editting_way_list_actions').show();
        
        bMap._resize_nav_bar_form('.except_course_scroll_box3', '.course_scroll_box3');
    },
    
    load_user_ways: function() {
        $.post(bMap.user_ways_url, {}, function(data) {
            if ( data.result ) {
                bMap.ways_on_course.mine = data.ways;
            }
        }, 'json');
    },
    
    ui_select_way: function(course_type, course_id) {
        $('.map_nav_tabbody, #editting_way_list_actions, .btn_action_remove_way').hide();
        $('#map_way_selecting, #exists_way_list, #exists_way_list_actions, .btn_action_add_way').show();
        
        var i = 0; e = bMap.courses[course_type][course_id].ways.length, hide_els = [], show_els = [];
        
        $('.user_way_box').show();
        for ( ; i < e; ++ i ) {
            hide_els.push('#user_way_'+bMap.courses[course_type][course_id].ways[i].id+' .btn_action_add_way');
            show_els.push('#user_way_'+bMap.courses[course_type][course_id].ways[i].id+' .btn_action_remove_way');
        }

        $(hide_els.join(', ')).hide();
        $(show_els.join(', ')).show();
        
        bMap._resize_nav_bar_form('.except_course_scroll_box4', '.course_scroll_box4');
    },
    
    update_coursetype: function() {
        if ( bMap.current_course_id < 0 || !bMap.current_course_type ) {
            alert("required a course for updating");
            return false;
        }
        
        $.post(bMap.coursetype_url, {
            action: 'course_type',
            course_type: bMap.current_course_type,
            update_to_type: $('#id_update_coursetype').val(),
            course_id: bMap.current_course_id
        }, function(data) {
        
        }, 'json');
        
        return false;
    },
    
    distance_mode: function(code) {
        if ( bMap.obj.getMode() === 4 ) {
            bMap.obj.setMode(1);
            $('#distance_mode_status').text('X');
        }
        else {
            bMap.obj.setMode(4);
            $('#distance_mode_status').text('O');
        }
    },
    
    cancel_node_editting: function(e) {
        if ( e ) {
    		e.preventDefault();
    		e.stopPropagation();
        }
		
		bMap.remove_editting_node(0);
		bMap.dom.e_node_list.empty();
        
        return false;
    },
    remove_editting_node: function(idx) {
        var i = 0, e = bMap.editting_nodes.length;
        
        delete bMap['editting_node_coords'];
        bMap['editting_node_coords'] = [];
        
        for ( ; i < e; ++i ) {
            if ( i < idx ) {
                bMap.editting_node_coords.push(bMap.editting_nodes[i].coord);
            }
            else {
        	    bMap.obj.removeOverlay(bMap.editting_nodes[i].obj);
        	    bMap.editting_nodes[i].dom.remove();
            }
        }
        if ( idx < 1 ) {
    		delete bMap['editting_nodes'];
    		
    		bMap['editting_nodes'] = [];
        }
        else {
    	    bMap.editting_nodes = bMap.editting_nodes.slice(0, idx);
        }
        
        bMap._draw_editting_way();
    },
    
    event_map_zoom: function(new_level, old_level) {
        if ( bMap.mode == 'editting' && bMap.edit_target == 'way' ) {
            // todo
        }
        else if ( new_level != old_level ) {
            if ( bMap.current_course_type && bMap.current_course_id && bMap.current_course_type != 'public' ) {
                bMap.remove_course_from_client(bMap.current_course_type, bMap.current_course_id, true);
                bMap.load_course(bMap.current_course_type, bMap.current_course_id, false, false);                
            }

            bMap.load_public_courses();
        }
        else {
            
        }
    },
    load_public_courses: function() {
        $.getJSON(bMap.get_public_courses, {
            zoom: bMap.obj.getLevel()
        }, function(data) {
            bMap.remove_public_courses_from_client();
            bMap.courses.public = data.ways;
            bMap.display_public_courses();
        });
    },
    
    hide_public_courses: function() {
        var i;
        for ( i in bMap.courses.public ) {
            bMap.hide_course('public', i);
        }        
    },
    
    remove_public_courses_from_client: function() {
        var i;
        for ( i in bMap.courses.public ) {
            bMap.remove_course_from_client('public', i, true);
        }        
    },

    display_public_courses: function() {
        var i, w_p;
        for ( i in bMap.courses.public ) {
            w_p = bMap.display_ways('public', i, bMap.courses.public[i].ways, false, true)
            bMap.course_polylines.public[i] = w_p;
        }
    },

    event_map_click: function(e) {
        var coord = new DPoint(e.x, e.y);
        bMap.current_coord = coord;
        
        switch ( bMap.mode ) {
            case 'editting':
                if ( !bMap.u ) {
                    return false;
                }
                if ( bMap.obj.getMode() !== 4 ) {
                    if ( bMap.edit_target == 'way' ) {
                        bMap._add_node_to_map(coord);
                    }
                    else if ( bMap.edit_target == 'spot' ) {
                        bMap._click_for_spot_menu(coord);
                    }
                    else {
                        
                    }
                }
            break;
            
            case 'browsing':
            default:
                if ( bMap.obj.getMode() !== 4 ) {
                    if ( !bMap.u ) {
                        return false;
                    }
                    bMap._click_for_spot_menu(coord);
                }
            break;
        }
    },
        _click_for_spot_menu: function(coord) {
            if ( bMap.ui.browsing_menu === null || !('panel' in bMap.ui.browsing_menu) ) {
                bMap.ui.browsing_menu = new DInfoWindow($('#ui_browsing_menu').html(), {
        			removable: true,
        			coordinate: 'wgs84',
        			zindex: 160
                });
                
                $('#ui_browsing_menu').empty();
                
                bMap.obj.addOverlay(bMap.ui.browsing_menu);
                $(bMap.ui.browsing_menu.panel).css({
                    cursor: 'default'
                })
                .find('li')
                    .mouseover(function() {
                        $(this).css('background-color', '#ddd');
                    })
                    .mouseout(function() {
                        $(this).css('background-color', '#fff');
                    });

            }

            $('#id_spot_latitude').val(coord.y);
            $('#id_spot_longitude').val(coord.x);
            $('#id_spot_spot_id').val(bMap.current_spot_id);
            
            bMap.ui.browsing_menu.setPoint(coord);
            bMap.ui.browsing_menu.show();
        },
    
    coord2addr: function(lat, lon, funcname) {
        var i, param_arr = []; params = {
            apikey: bMap.daum_apikey,
            latitude: lat,
            longitude: lon,
            inputCoordSystem: 'WGS84',
            output: 'json',
            callback: funcname
        };
        
        for ( i in params ) {
            param_arr.push(i+'='+params[i]);
        }

        $('<script></script>')
            .attr('id', 'el_coord2addr')
            .attr('type', 'text/javascript')
            .attr('src', 'http://apis.daum.net/maps/coord2addr?'+param_arr.join('&'))
            .appendTo('head');
    },
        callback_coord2addr: function(data) {
            console.log(data.fullName)
        },
        
        _show_spot_menu: function(coord) {
            $('#ui_spot_menu').css({
                top: 200,
                left: 300
            }).show();
        },
        
        _add_node_to_map: function(coord, node_id) {
            if ( typeof node_id == 'undefined') {
                var node_id = 0;
            }
            var obj = new DMark(coord, {
            	mark: '<div id="editting_node_on_map_'+bMap.editting_nodes.length+'_'+node_id+'" class="editting_node_mark">'+((bMap.editting_nodes.length > 0) ? bMap.editting_nodes.length : 'S' )+'</div>',
                draggable: true,
                clickable: false,
                zindex: 300
            });
            if ( !( 'size' in obj.panel) ) {
                obj.panel['size'] = {
                    width: $(obj.panel).width(),
                    height: $(obj.panel).height()
                };
            }
            
            $(obj.panel)
            .mousedown(function(e) {
        		bMap.lock_editting_drag = true;
            })
            .mouseup(function(e) {
                if ( bMap.lock_editting_drag == true ) {
                    var obj = $(this).find("div[id^='editting_node_on_map_']");
                    var p = bMap.obj.getCoordByPixPoint(
                        new DPoint(e.currentTarget.offsetLeft + obj.width()/2, e.currentTarget.offsetTop + obj.height()/2),
                    'wgs84');
                    
                    var t_idx = obj.attr('id').replace('editting_node_on_map_','').split('_');
                    var idx = parseInt(t_idx[0]);
                    var node_id = parseInt(t_idx[1]);
                    
                    bMap.editting_nodes[idx].coord = p;
                    bMap.editting_nodes[idx].node_id = node_id;
                    bMap.editting_node_coords[idx] = p;
                    
                    $('#editting_node_lat_'+idx+'_'+node_id).text(String(p.y).substring(0,8)+' N');
                    $('#editting_node_lon_'+idx+'_'+node_id).text(String(p.x).substring(0,8)+' E');
                    
                    bMap._draw_editting_way();
                    bMap.lock_editting_drag = false;
                }
            });

    		
    		var s = [], d = null, pl = null;
            
            s.push('<div id="editting_node_'+bMap.editting_nodes.length+'_'+node_id+'" class="editting_node_mark_box"><div class="inner_editting_node_mark_box">');
            s.push('<span style="float: left; margin-left: 10px; font-weight: bold;" class="editting_node_mark">');
            s.push(((bMap.editting_nodes.length > 0) ? bMap.editting_nodes.length : 'S' ));
            s.push('</span>')
            s.push('<span id="editting_node_lat_'+bMap.editting_nodes.length+'_'+node_id+'" style="float: left; margin: 0 20px 0 10px;">'+String(coord.y).substring(0,8)+' N</span>');
            s.push('<span id="editting_node_lon_'+bMap.editting_nodes.length+'_'+node_id+'" style="float: left;">'+String(coord.x).substring(0,8)+' E</span>');
            s.push('<span style="float: right; margin-right: 10px;">');
            s.push('<img src="/media/images/map/btn_course_node_delete.gif" alt="" style="cursor: pointer;" onclick="bMap.remove_editting_node('+bMap.editting_nodes.length+'); return false;" />');
            s.push('</span>');
            s.push('</div></div>')
            
            d = $(s.join(''));
            
            bMap.dom.e_node_list.append(d);
            bMap.editting_nodes.push({
                coord: coord,
                obj: obj,
                dom: d,
                node_id: node_id
            });
            bMap.editting_node_coords.push(coord);
            bMap._draw_editting_way();
            
            bMap.obj.addOverlay(obj);
            
            return obj;
        },
        _draw_editting_way: function() {
            if ( bMap.editting_way !== null ) {
                bMap.obj.removeOverlay(bMap.editting_way);
            }
            
            if ( bMap.editting_node_coords.length > 1 ) {
                bMap.editting_way = new DPolyline(bMap.editting_node_coords);
                bMap.obj.addOverlay(bMap.editting_way);
            }            
        },
    
    create_node_menu: function(contents, options) {
        if ( !options ) {
            var options = {};
        }
		
		bMap.ui.node_menu = new DInfoWindow(contents || '', {
			removable: options.removable || false,
			width: options.width || 200,
			height: options.height || 150,
			zindex: options.zindex || 2,
			coordinate: 'wgs84'
		});
		
		$(bMap.ui.node_menu.getObject())
			.css({
				cursor: 'default'
			});
		
		bMap.obj.addOverlay(bMap.ui.node_menu);
    },
    
    hide_node_menu: function() {
        if ( bMap.mode == 'editting' ) {
            bMap.mode = 'browsing';
        }
        
        bMap.ui.node_menu.hide();
    },
    show_node_menu: function(el) {
        var sp = el.attr('id').split('_');
        var type = sp[0], course_id = sp[2], node_id = sp[3], coords = sp[4].split('---');
        
        var coordPoint = new DPoint(coords[0], coords[1]);
        
        if ( bMap.ui.node_menu === null ) {
            bMap.create_node_menu();
        }
        
        if ( bMap.ui.node_menu.isShow() ) {
            bMap.ui.node_menu.hide();
            return false;
        }
        
        bMap.ui.node_menu.setContents(
            bCore.replace_html_template_tags($('#ui_show_node_menu').html(), {
                course_name: bMap.courses[type][course_id].name,
                course_id: course_id
            })
        );
        bMap.ui.node_menu.setPoint(coordPoint);
        bMap.ui.node_menu.show();
    },
    
    ob_click_mark: function(e) {
		e.preventDefault();
		e.stopPropagation();
        
        switch ( bMap.mode ) {
            case 'browsing':
            default:
                bMap.show_node_menu($(this));
            break;
        }
        
        return false;
    },
    ob_mouseover_mark: function(e) {
        return false;
    },
    ob_mouseout_mark: function(e) {
        return false;
    },
    
    edit_way_form: function(type, course_id, way_id) {
        window.location.href = '#mode=editting&display=way&course_type='+bMap.current_course_type+'&course_id='+bMap.current_course_id+'&way_id='+way_id;
        return false;
    },
    
    load_ways: function(way_id, async) {
        if ( typeof way_id == 'undefined' ) {
            return false;
        }
        if ( typeof async == 'undefined' ) {
            var async = true;
        }
        
        $.ajaxSetup({
            async: async
        });
        
        if ( typeof way_id == 'number' || typeof way_id == 'string' ) {
            way_id = [way_id];
        }
        
        var id_list = [];
        for ( var i = 0; i < way_id.length; ++i ) {
            if ( !(String(way_id[i]) in bMap.ways) ) {
                id_list.push(String(way_id[i]));
            }
        }

        $('#map_loading_image').show();
        $.post(bMap.ways_url, {
            way_id: id_list.join('/'),
            zoom: bMap.obj.getLevel()
        },
        function(data) {
            if ( typeof data != typeof [] || !data ) {
                $('#map_loading_image').hide();
                return false;
            }
            
            for ( var i = 0; i < data.length; ++i ) {
                bMap.ways[data[i].id] = data[i];
            }
            
            $('#map_loading_image').hide();

        }, 'json');
        
        $.ajaxSetup({
            async: !async
        });
        
        return true;
    },
    
    remove_only_way_viewed: function() {
        for ( i in bMap.way_polylines ) {
            bMap.obj.removeOverlay(bMap.way_polylines[i].item);
            delete bMap.way_polylines[i];
        }        
        
        for ( i in bMap.node_marks_on_way ) {
            for ( j = 0; j < bMap.node_marks_on_way[i].length; ++j ) {
                bMap.obj.removeOverlay(bMap.node_marks_on_way[i][j]);
                delete bMap.node_marks_on_way[i][j];
            }
            delete bMap.node_marks_on_way[i];
        }        
    },
    
    display_mark: function(dpoint, options) {
        if ( !options || typeof options != 'object' ) {
            return false;
        }
        else {
            if ( !('icon' in options) || !('id' in options) ) {
                return false;
            }
            
            if ( !('size' in options) ) {
                options['size'] = [32, 32];
            }
        }
		
		var ret = new DMark(dpoint, {
			mark: new DIcon(options.icon, new DSize(options.size[1], options.size[0])),
		    draggable: options.draggable || false,
		    clickable: options.clickable || true,
		    zindex: options.zindex || 100
		});
		
		if ( 'classname' in options ) {
		    $(ret.panel).addClass(options['classname']);
		}
		if ( 'id' in options ) {
		    $(ret.panel).attr('id', options['id']);
		}
		
		$(ret.panel).click(bMap.ob_click_mark).mouseover(bMap.ob_mouseover_mark).mouseout(bMap.ob_mouseout_mark);
		
		bMap.obj.addOverlay(ret);
		
		return ret;
    },
    
    compare_side_coord: function(boundary, x, y) {
        if ( !boundary.w || (boundary.w && boundary.w > x) ) {
            boundary.w = x;
        }
        if ( !boundary.e || (boundary.e && boundary.e < x) ) {
            boundary.e = x;
        }
        if ( !boundary.s || (boundary.s && boundary.s > y) ) {
            boundary.s = y;
        }
        if ( !boundary.n || (boundary.n && boundary.n < y) ) {
            boundary.n = y;
        }
        
        return boundary;
    },
    
    display_ways: function(type, course_id, ways, draw_node_mark, on_waytype) {
        if ( typeof draw_node_mark == 'undefined') {
            var draw_node_mark = false;
        }
        var mark_options = {
			icon: '/media/images/map/icon/node.gif',
			size: [24, 24],
			draggable: false,
			clickable: true,
			zindex: 10,
			classname: 'node_icon',
            id: ''
        };
                
        var i = 0, j = 0, way_arr = [], dp_node, w_array = [], stroke_color = '', node_mark;
        var boundary = w_boundary = {n:null, e:null, s:null, w:null};

        if ( type != '' && !(course_id in bMap.node_marks_on_course[type]) ) {
            bMap.node_marks_on_course[type][course_id] = [];
        }
        
        for ( ; i < ways.length; ++i ) {
            if (
                !on_waytype ||
                (on_waytype === true && bMap.displayed_public_course[ways[i].waytype] === true) ||
                (bMap.current_course_type == 'public' && bMap.current_course_id == course_id)
            ) {
                if ( type != '' && !(ways[i].id in bMap.ways_on_course[type]) ) {
                    bMap.ways_on_course[type][ways[i].id] = ways[i];
                }
            
                if ( type == '' && !(ways[i].id in bMap.node_marks_on_way) ) {
                    bMap.node_marks_on_way[ways[i].id] = [];
                }

                way_arr = [];
            
                for ( j = 0; j < ways[i].nodes.length; ++j ) {
                    boundary = bMap.compare_side_coord(boundary, ways[i].nodes[j].coord.lon, ways[i].nodes[j].coord.lat);
                    w_boundary = boundary;
                
                    dp_node = new DPoint(ways[i].nodes[j].coord.lon, ways[i].nodes[j].coord.lat);
                                
                    way_arr.push(dp_node);
                
                    if ( draw_node_mark ) {
                        mark_options.id = type + '_' + bMap.mode + '_' + String(course_id) + '_' + String(ways[i].nodes[j].id) + '_' + String(ways[i].nodes[j].coord.lon) + '---' + String(ways[i].nodes[j].coord.lat);
                    
                        node_mark = bMap.display_mark(
                            new DPoint(ways[i].nodes[j].coord.lon, ways[i].nodes[j].coord.lat),
                            mark_options
                        )

                        if ( type != '' ) {
                            bMap.node_marks_on_course[type][course_id].push(node_mark);
                        }
                        else {
                            bMap.node_marks_on_way[ways[i].id].push(node_mark);
                        }
                    }
                }

                w_array.push({
                    way_id: ways[i].id,
                    item: bMap._draw_way(ways[i].waytype, way_arr, (bMap.current_course_type == 'public' && bMap.current_course_id == course_id)),
                    s_node: {lat:ways[i].nodes[0].coord.lat, lon:ways[i].nodes[0].coord.lon},
                    e_node: {lat:ways[i].nodes[ways[i].nodes.length-1].coord.lat, lon:ways[i].nodes[ways[i].nodes.length-1].coord.lon}
                });

                bMap.ways_bound[ways[i].id] = w_boundary;
                w_boundary = {n:null, e:null, s:null, w:null};            

                bMap.obj.addOverlay(w_array[w_array.length-1].item, 'waytype_'+ways[i].waytype);
            }
        }
        --i;
        --j;
        
        
        if ( type != '' ) {
            bMap.courses_bound[type][course_id] = boundary;            
        }

        return w_array;
    },
        _draw_way: function(way_type, way_arr, current_course) {
            switch ( way_type ) {
                case '1':
                    var stroke_color = '#ff0000';
                    break;
                case '2':
                    var stroke_color = '#00ff00';
                    break;
                case '3':
                    var stroke_color = '#0000ff';
                    break;
                case '0':
                default:
                    var stroke_color = '#666666';
                    break;
            }

            if ( current_course === true ) {
                stroke_color = '#0ff';
            }
            
            switch ( bMap.obj.getLevel() ) {
                case 0:
                case 1:
                case 2:
                    var line_weight = 12;
                break;

                case 3:
                    var line_weight = 8;
                break;

                case 4:
                    var line_weight = 6;
                break;

                case 5:
                    var line_weight = 3.5;
                break;

                case 6:
                    var line_weight = 2.8;
                break;
                case 7:
                    var line_weight = 2.2;
                break;
                case 8:
                    var line_weight = 1.8;
                break;

                case 9:
                    var line_weight = 1.5;
                break;
                case 10:
                case 11:
                    var line_weight = 1.2;
                break;
                case 12:
                    var line_weight = 0.8;
                break;
            }
                        
            return new DPolyline(way_arr, {
                    strokeWeight: line_weight,
                    strokeColor: stroke_color
            });
        },
    
    convert_coord_to_point: function(arr) {
        var i = 0, e = arr.length, way_arr = [];
        
        for ( ; i < e; ++i ) {
            way_arr.push(new DPoint(arr[i].longitude, arr[i].latitude));
        }
        return way_arr;
    },
    
    view_competition: function(competition_id) {
        if ( !competition_id ) {
            alert('지정한 대회 정보를 찾을 수 없습니다.');
            return false;
        }
    
    },
    
    view_competition_coord: function(competition_id, coord) {
        var p = new DPoint(coord.lon, coord.lat);
        bMap.add_competition_on_map(competition_id, p);
        bMap.set_center_map(p);
    },
    
    view_shop: function(shop_id) {
        if ( !shop_id ) {
            alert('지정한 상점을 찾을 수 없습니다.');
            return false;
        }
        
        $('#nav_bar_body').show();
        
        $.getJSON(bMap.shop_url+String(shop_id)+'/', function(data) {
            $('#nav_store_list_box').show().html(data.html);
            
            if ( data.coord.lat !== -1 || data.coord.lon !== -1 ) {
                var p = new DPoint(data.coord.lon, data.coord.lat);
                bMap.add_shop_on_map(data.shop_id, p);
                bMap.set_center_map(p);
            }
            else {
                alert('해당 상점엔 위치 정보가 없습니다.');
            }
        });
    
    },
    
    set_center_map: function(coord) {
        var point = bMap.obj.getPixByCoordPoint(coord);
        point.add(
            ( $('#nav_bar_body').css('display') == 'none' ) ? 0 : $('#nav_bar_body').width(),
            ( bMap.screen == 'full' ) ? 0 : bMap.dom.container.height() / 2.5
        );
        
        bMap.obj.setCenter(bMap.obj.getCoordByPixPoint(point, 'wgs84'));
    },
    add_shop_on_map: function(shop_id, coord) {
        if ( !(shop_id in bMap.shops) ) {
    		var ret = new DMark(coord, {
    			mark: new DIcon('/media/images/map/ico_spot_shop.png', new DSize(40, 24), new DPoint(20, 24)),
    		    draggable: false,
    		    clickable: true,
    		    zindex: 101
    		});
    		
    		bMap.shops[shop_id] = ret;
    		bMap.obj.addOverlay(ret);
    		
    		return true;
        }
    },
    add_competition_on_map: function(competition_id, coord) {
        if ( !(competition_id in bMap.competitions) ) {
    		var ret = new DMark(coord, {
    			mark: new DIcon('/media/images/map/ico_spot_event.png', new DSize(40, 24), new DPoint(20, 24)),
    		    draggable: false,
    		    clickable: true,
    		    zindex: 101
    		});
    		
    		bMap.competitions[competition_id] = ret;
    		bMap.obj.addOverlay(ret);
    		
    		return true;
        }
    },
    
    view_themes: function(style, theme_id) {
        if ( !theme_id ) { var theme_id = 0; }
        
        $('#nav_bar_body').show();
        
        bMap.load_course('theme', theme_id);
    },
    
    load_photos: function(func) {
        $.getJSON(bMap.map_photos_url, {}, function(data) {
            bMap.photos = data;
            
            if ( typeof func == 'function' ) {
                func(bMap.photos);
            }
        });
    },
    display_photos_icon: function(data, photo_id) {
        var i, obj;
        
        var data2 = ( typeof just_one == 'undefined' ) ? data : {photo_id: data};
        
        
        for ( i in data2 ) {
            bMap.photo_objs[i] = true;
            obj = new DMark(
                new DPoint(data[i].longitude, data[i].latitude),
                {
                    mark: new DIcon('/media/images/map/icon/icon_photo.gif', new DSize(18, 18)),
                    draggable: false,
                    clickable: true,
                    zindex: 120,
                    offset: new DPoint(-9, -9)
                });

            bMap.obj.addOverlay(obj, 'mapphoto_icon_'+((data[i].is_admin) ? 'bmap' : 'user') );
            $(obj.panel)
                .attr('id', 'photo_'+i)
                .addClass('photo_point_on_map')
                .click(function(e) {
                	e.preventDefault();
                	e.stopPropagation();

                	var el = $(this);
                	var items = el.attr('id').split('_');
                	var photo_id = String(items[items.length-1]);

                	window.location.href = '#mode=browsing&display=photo&photo_id='+photo_id;

                    return false;
            });

        }
    },
    
    load_photo: function(photo_id, func) {
        photo_id = String(photo_id);
        $.getJSON(bMap.map_photo_url.replace('%d', photo_id), {}, function(data) {
            bMap.photos[photo_id] = data;
            
            if ( typeof func == 'function' ) {
                func(photo_id);
            }
        });
    },
    
    view_photo: function(photo_id) {
        if ( typeof photo_id == 'undefined' ) {
            var photo_id = String(bMap.current_photo_id);
        }
        photo_id = String(photo_id);

        $('.map_nav_tabbody, #map_photo_owner, #map_photo_browsing .map_nav_title').hide();
        $('#nav_bar_map_body, #map_photo_browsing').show();

        if ( photo_id === '0' ) {
            $('#ui_upload_photo').show();
            $('#ui_map_image_upload').parent().show();
            $('#view_map_photo, #map_photo_loading, #map_photo_loading_image').hide();
        }
        else {
            $('#map_photo_loading, #map_photo_loading_image').show();

            if ( bMap.edit_target ) {
                $('#ui_edit_photo').show();
                $('#ui_view_photo').hide();
            }
            else {
                $('#ui_view_photo').show();
                $('#ui_edit_photo').hide();
            }

            if ( !(photo_id in bMap.photos) || !bMap.photos[photo_id].image_url ) {
                bMap.load_photo(photo_id, bMap.display_map_photo);
            }            
            else {
                bMap.display_map_photo(photo_id);
                $('#map_photo_loading, #map_photo_loading_image').hide();
            }
            $('#view_map_photo, #map_photo_owner').show();
        }

        bMap._resize_nav_bar_form('.except_course_scroll_box7', '.course_scroll_box7');        
    },
    
    view_spot: function(spot_id) {
        spot_id = String(spot_id);
        
        if ( !(spot_id in bMap.spots) ) {
            return false;
        }
        
        if ( !(spot_id in bMap.spot_objs) || !bMap.spot_objs[spot_id] ) {
            bMap.display_spot(spot_id);
        }
        
        $('.map_nav_tabbody').hide();
        $('#nav_bar_map_body, #map_spot_viewing').show();
        
        bMap.obj.setCenter(
            new DPoint(bMap.spots[spot_id].longitude, bMap.spots[spot_id].latitude),
            bMap.obj.getLevel()
        );
        
        $(bMap.spot_objs[spot_id].panel).removeClass('selected_spot_point').addClass('selected_spot_point');
        
        $('#view_spot_name').text(bMap.spots[spot_id].name);
        $('#view_spot_category_name').text(bMap.spots[spot_id].category_name);
        $('#view_spot_description').text(bMap.spots[spot_id].description_nb);
        $('#view_spot_images').empty();
        
        $('#btn_spot_editting').attr('href', '#mode=editting&display=spot&spot_id='+spot_id);
        
        bMap.display_spot_images(spot_id);
        
        bMap._resize_nav_bar_form('.except_course_scroll_box6', '.course_scroll_box6');
    },
    
    display_map_photo: function(photo_id) {
        var data;
        photo_id = String(photo_id);
        
        if ( !(photo_id in bMap.photos) ) {
            alert('해당 사진이 없습니다.');
            return false;
        }
        
        $('#view_map_photo')
            .find('a')
                .attr('href', bMap.photos[photo_id].image_url)            
            .end().find('img')
                .attr('src', bMap.photos[photo_id].image_url)
            .end().show();
        
        $('#map_photo_owner > span').text(bMap.photos[photo_id].user);
        $('#map_photo_loading, #map_photo_loading_image').hide();
    },
    
    display_spot_images: function(spot_id, new_data) {
        var data, i = 0, obj;
        spot_id = String(spot_id);
        
        if ( spot_id ) {
            data = bMap.spots[spot_id].images;
            $('#view_spot_images').empty();
        }
        else {
            data = [new_data];
            Array2.insertAt(bMap.spots[new_data.spot_id].images, 0, new_data);
        }
        
        for ( ; i < data.length; ++i ) {
            obj = $('<a></a>')
                .attr('href', data[i].image_url)
                .attr('rel', 'image_colorbox')
                .append(
                    $('<img />')
                        .attr('src', data[i].image_url)
                        .attr('id', 'spot_'+data[i].spot_id+'_'+data[i].image_id)
                        .addClass('spot_image_box')
                );
            
            if ( spot_id ) {
                obj.appendTo($('#view_spot_images'));
            }
            else {
                obj.prependTo($('#view_spot_images'))
            }
        }
        $("a[rel='image_colorbox']").colorbox();
        
    },
    
    display_spots: function() {
        var i, j;

        for ( i in bMap.spots ) {
            if ( !(i in bMap.spot_objs) ) {
                bMap.display_spot(i);
            }
            bMap.spot_objs[i].hide();

            for ( j in bMap.flag_spots ) {
                if (
                    ((j in bMap.flag_spots) && bMap.flag_spots[j] === true && j === bMap.spots[i].category) ||
                    bMap.current_spot_id == i
                ) {
                    bMap.spot_objs[i].show();
                }
            }
        }
        
    },
    display_spot: function(spot_id) {
        bMap.spot_objs[spot_id] = new DMark(
            new DPoint(bMap.spots[spot_id].longitude, bMap.spots[spot_id].latitude),
            {
                mark: new DIcon('/media/images/map/ico_'+bMap.spots[spot_id].category+'.gif', new DSize(22, 22)),
                draggable: false,
                clickable: true,
                zindex: 150,
                offset: new DPoint(-11, -11)
            });
        
        $(bMap.spot_objs[spot_id].panel)
            .attr('id', 'spot_'+bMap.spots[spot_id].category+'_'+spot_id)
            .addClass('spot_point_on_map')
            .click(function(e) {
            	e.preventDefault();
            	e.stopPropagation();
            	
            	var el = $(this);
            	var items = el.attr('id').split('_');
            	var spot_id = String(items[items.length-1]);
            	
            	window.location.href = '#mode=browsing&display=spot&spot_id='+spot_id;

                return false;
        });
        
        bMap.obj.addOverlay(bMap.spot_objs[spot_id], 'mark_spot_'+bMap.spots[spot_id].category);
    },
    
    view_course: function(type, course_id, node_pos) {
        $('#course_info_content').show();
        
        if ( typeof type == 'undefined' && typeof course_id == 'undefined' ) {
            window.location.href = '#mode=browsing&display=course&course_type='+bMap.current_course_type+'&course_id='+bMap.current_course_id;
        }
        bMap.current_course_id = String(course_id);
        bMap.current_course_type = type;
        
        if ( type == 'public' ) {
            bMap.display_course(type, course_id, true);

            if ( bMap.mode == 'browsing' ) {
                bMap.show_course_info(type, course_id);
            }
        }
        else {
            bMap.load_course(bMap.current_course_type, bMap.current_course_id);
        }
    },
    
    load_course: function(type, course_id, async, do_set_bound) {
        if ( !type || !course_id || parseInt(course_id) < 1 ) {
            return false;
        }

        if ( typeof do_set_bound == 'undefined' ) {
            var do_set_bound = true;
        }

        if ( typeof async == 'undefined' ) {
            var async = true;
        }
        $.ajaxSetup({
            async: async
        });
        
        if ( !(course_id in bMap.courses[type]) && type != 'public' ) {
            $('#map_loading_image').show();
            
            $.getJSON(bMap.course_url, {
                type: type,
                course_id: course_id,
                zoom_level: ( bMap.obj ) ? bMap.obj.getLevel() : bMap.zoom_level
            },
            function(data) {
                bMap.courses[type][course_id] = data;
                var old_zoom = bMap.obj.getLevel();

                bMap.display_course(type, course_id, do_set_bound);

                var new_zoom = bMap.obj.getLevel();

                if ( bMap.courses[type][course_id].default_zoomlevel === 0 ) {
                    $.post(bMap.coursetype_url, {
                        action: 'default_zoom',
                        course_id: course_id,
                        course_type: type,
                        course_zoom: new_zoom
                    }, function(data) {});
                }
                
                if ( old_zoom !== new_zoom ) {
                    return false;
                }
                
                if ( bMap.mode == 'browsing' ) {
                    bMap.show_course_info(type, course_id);
                }
                
                $('#map_loading_image').hide();
                $.ajaxSetup({
                    async: !async
                });

            });
        }
        else {
            $('#map_loading_image').hide();

            if ( type == 'public' || type == 'theme' || type == 'suggest' ) {
                if ( typeof bMap.courses[type][course_id].default_zoomlevel == 'number' && bMap.courses[type][course_id].default_zoomlevel > 0 ) {
                    bMap.obj.main.zoomTo(bMap.obj.main.getScale(bMap.courses[type][course_id].default_zoomlevel));
                }
            }
            
            $('#map_loading_image').hide();
            bMap.display_course(type, course_id, do_set_bound);

            if ( bMap.mode == 'browsing' ) {
                bMap.show_course_info(type, course_id);
            }
            
        }
    },
    
    show_course_info: function(type, course_id) {
        var data = bMap.courses[type][course_id];
        
        $('#course_info_name').text(data.name);
        $('#course_info_distance').text(data.distance);
        $('#course_info_time').text(data.how_long_time);
        $('#course_info_title_image').attr('src', data.full_image).show();
        $('#course_info_description').empty().html(data.description);
        
        if ( data.full_image ) {
            $('#course_info_description')
                .prepend(
                    $('<img/>').attr('src',data.full_image)
                );
        }
        
        $('#course_info_scrap_btn').attr('href', '#mode=display&display=course&course_type='+type+'&course_id='+String(course_id)+'&scrap=embed');
        
        $('.nav_bar_body_container').hide();
        $('#nav_bar_course_info').show();
        
        if ( data.ways.length < 1 ) {
            $('.course_info_meta').show().first().hide();
            $('#course_info_scrap_btn').hide();
        }
        else {
            $('.course_info_meta').show();
            $('.not_exists_ways').hide();
            $('.course_info_manage_ways, #course_info_scrap_btn').show();
        }
        
        if ( !bMap.a && bMap.u != data.username ) {
            $('.users_commands').hide();
        }
        else {
            $('.users_commands').show();
        }
    },
    
    ui_scrap_course: function(e) {
    	e.preventDefault();
    	e.stopPropagation();
    	
    	var el = $(this);
    	
    	var params = bCore.parse_ext_querystring(el.attr('href'));
    	
    	bMap.scrap_curr_course.course_type = params.course_type;
    	bMap.scrap_curr_course.course_id = params.course_id;
    	
    	if ( !('scrap' in params && 'course_id' in params) || !params.course_id ) {
    	    alert('담아갈 대상을 찾지 못했습니다.');
    	    return false;
    	}
    	
    	if ( $('#ui_scrap_window').css('display') != 'none' ) {
    	    $('#ui_scrap_window').hide();
    	    return false;
    	}
    	
    	var ret = '';
    	switch ( params.scrap ) {
    	    case 'embed':
    	        bMap.update_scrap_ui();
    	    default:
    	    break;
    	}
    	
    	$('#ui_scrap_window').css({
    	    position: 'absolute',
    	    top: $('#ui_scrap_window').height() / 2,
    	    left: (bMap.dom.container.width() - $('#ui_scrap_window').width()) / 2,
    	    zIndex: 500
    	})
    	.find('h3').text(bMap.courses[params.course_type][params.course_id].name)
    	.end().find('')
    	.end().show();
    	
    	$('#ui_scrap_url').val(window.location.href);
        
        return false;
    },
        update_scrap_ui: function() {
	        $('#ui_scrap_html_code').text(
	            bCore.replace_html_template_tags(bMap.scrap_src_url, {
	                course_type: bMap.scrap_curr_course.course_type,
	                course_id: bMap.scrap_curr_course.course_id,
	                frameborder: $('#ui_scrap_html_code_border').val() || 0,
	                width: ($('#ui_scrap_html_code_width').val() < 100) ? 100 : $('#ui_scrap_html_code_width').val(),
	                height: ($('#ui_scrap_html_code_height').val() < 100) ? 100 : $('#ui_scrap_html_code_height').val()
	            })
	        );
		},
    
    remove_course_from_client: function(course_type, course_id, force) {
        if ( typeof force == 'undefined' ) {
            var force = false;
        }
        
        if ( force === false && (course_type == 'public' || course_type == 'theme' || course_type == 'suggest') ) {
            return false;
        }
        
        delete bMap.courses[course_type][course_id];
        
        bMap.hide_course(course_type, course_id);
    },
    
    hide_course: function(course_type, course_id) {
        if ( course_id in bMap.course_polylines[course_type] ) {
            for ( var i = 0; i < bMap.course_polylines[course_type][course_id].length; ++i ) {
                bMap.obj.removeOverlay(bMap.course_polylines[course_type][course_id][i].item);
                delete bMap.course_polylines[course_type][course_id][i];
            }
            delete bMap.course_polylines[course_type][course_id];
        }
        
        if ( course_id in bMap.node_marks_on_course[course_type] ) {
            for ( i = 0; i < bMap.node_marks_on_course[course_type][course_id].length; ++i ) {
                $(bMap.node_marks_on_course[course_type][course_id][i].panel).unbind();
                bMap.obj.removeOverlay(bMap.node_marks_on_course[course_type][course_id][i]);
                delete bMap.node_marks_on_course[course_type][course_id][i];
            }
            delete bMap.node_marks_on_course[course_type][course_id];
        }
    },
    
    toggle_show_course: function(el) {
        if ( $(el).css('display') == 'none' ) {
            $(el).show();
        }
        else {
            $(el).hide();
        }
    },
    
    check_display_courses: function() {
        var i, j;
        
        for ( i in bMap.courses ) {
            for ( j in bMap.courses[i] ) {
                if ( !(j in bMap.course_polylines[i]) ) {
                    bMap.display_course(i, j, false);
                }
            }
        }        
    },
    
    display_course: function(type, course_id, do_set_bound) {
        if ( bMap.courses[type][course_id].ways.length < 1 ) {
            return false;
        }
        if ( typeof do_set_bound == 'undefined' ) {
            var do_set_bound = true;
        }
        
        var coord = new DPoint(
            bMap.courses[type][course_id].ways[0].nodes[0].coord.lon,
            bMap.courses[type][course_id].ways[0].nodes[0].coord.lat
        );
        
        if ( course_id in bMap.course_polylines[type] ) {
            if ( do_set_bound ) {
                bMap.set_bound_course(type, course_id, coord);
            }
        }
        else {
            var w_p = bMap.display_ways(type, course_id, bMap.courses[type][course_id].ways, false, false);
            bMap.course_polylines[type][course_id] = w_p;

            if ( do_set_bound ) {
                bMap.set_bound_course(type, course_id, coord);
            }
        }
    },
    
    set_bound_course: function(type, course_id, coord) {
        if ( bMap.courses_bound[type][course_id] ) {
            bMap.obj.setBound(
                bMap.courses_bound[type][course_id].w,
                bMap.courses_bound[type][course_id].s,
                bMap.courses_bound[type][course_id].e,
                bMap.courses_bound[type][course_id].n
            );
        }
        else {
            bMap.obj.setCenter(coord);
        }
    }
});

bMap.extend({
    ob_document_ready: function(e) {
        proc_querystring();
        
        $('.btn_display_way_by_type').click(function(e) {
            if ( typeof e != 'undefined') {
            	e.preventDefault();
            	e.stopPropagation();
            }
        	
        	var el = $(this);
        	var id_name = el.attr('id'), image_url = el.attr('src');
        	var way_type = id_name.replace('btn_display_way_by_', '');
        	
        	bMap.turn_display_public_course(way_type);

            return false;
        });
        
        $('.toggle_map_nav_title').click(function(e) {
        	e.preventDefault();
        	e.stopPropagation();
        	
        	var el = (e) ? $(e.target) : $(this);
        	
        	$('.course_scroll_box1').hide();
        	
        	if ( el.next().css('display') == 'none' ) {
        	    el.next().show();
        	}
        	else {
        	    el.next().hide();
        	}
            
            return false;
        });
        $('.toggle_map_nav_title').first().trigger('click');
        
        $('#btn_editting_cancel').click(bMap.cancel_node_editting);
        $('#btn_editting_course_cancel').click(function(e) {
        	e.preventDefault();
        	e.stopPropagation();
        	
        	var res = confirm('취소하면 작업 중인 데이터는 저장되지 않습니다!');
        	
        	if ( res ) {
    	        $('#form_edit_course').get(0).reset();
        	    if ( bMap.current_course_id > 0 ) {
        	        window.location.href = '#mode=browsing&display=course&course_type='+bMap.current_course_type+'&course_id=' + bMap.current_course_id;
        	    }
        	    else {
        	        window.location.href = '#mode=browsing&display=course';
        	    }
        	}
        	else {
        	    return false;
        	}
            
            return false;
        });
        
        $('#course_info_modify_btn').click(function(e) {
            window.location.href = '#mode=editting&course_type='+bMap.current_course_type+'&display=course&course_id=' + bMap.current_course_id;
        });
        $('.course_info_new_way').click(function(e) {
            window.location.href = '#mode=editting&display=way&course_type='+bMap.current_course_type+'&course_id='+bMap.current_course_id+'&way_id=new';
        });
        $('.course_info_select_ways').click(function(e) {
            window.location.href = '#mode=editting&display=way&course_type='+bMap.current_course_type+'&course_id='+bMap.current_course_id+'&way_id=select';
        });
        
        $("a[rel='mapphoto_colorbox']").colorbox();
        
        $('#form_edit_way').submit(function(e) {
        	e.preventDefault();
        	e.stopPropagation();
        	
        	var form_el = $(this);
            
            var lat = [], lon = [], node_id = [], course_id = 0;
            
            for ( var i = 0; i < bMap.editting_nodes.length; ++i ) {
                lat.push(bMap.editting_nodes[i].coord.y);
                lon.push(bMap.editting_nodes[i].coord.x);
                node_id.push(bMap.editting_nodes[i].node_id);
            }
            
            var data = form_el.serialize() + '&' + $.param({
                lat: lat,
                lon: lon,
                node_id: node_id,
                course_id: bMap.current_course_id,
                way_id: bMap.current_way_id
            });
            
            $.post(form_el.attr('action'), data, function(data) {
                if ( data.result ) {
                    bMap.current_way_id = data.way_id;
                    
                    if ( (bMap.current_course_type in bMap.courses) && (bMap.current_course_id in bMap.courses[bMap.current_course_type]) ) {
                        var way_name = '['+bMap.courses[bMap.current_course_type][bMap.current_course_id].name+']';
                    }
                    else {
                        var way_name = '';
                    }
                    var mv = confirm('도로를 입력했습니다. '+way_name+' 코스에 연결하시겠습니까?');
                    
                    if ( mv ) {
                        bMap.add_way_to_course(bMap.current_course_type, bMap.current_course_id, data.way_id);
                    }
                    else {
                    
                    }
                    
                    window.location.href = '#mode=browsing&display=course&course_type='+bMap.current_course_type+'&course_id='+bMap.current_course_id;
                    window.location.reload(true);
                }
                else {
                    alert(data.msg);
                }
                return false;
            }, 'json');
        	
        	return false;
        });
        
        $('.btn_action_way_n_course').click(function(e) {
        	e.preventDefault();
        	e.stopPropagation();
        	
        	var el = $(this), mv = false;
        	var el_id = el.attr('id'), way_id = 0;
        	
        	if ( bMap.current_course_type && bMap.current_course_id ) {
            	if ( el_id.indexOf('add_my_way_to_course_') === 0 ) {
            	    var msg = '코스에 연결하시겠습니까?';
            	    var func = bMap.add_way_to_course;
            	    var type = bMap.current_course_type;
            	    var course_id = bMap.current_course_id;
            	}
            	else if ( el_id.indexOf('remove_my_way_from_course_') === 0 ) {
            	    var msg = '코스에서 연결을 끊겠습니까?';
            	    var func = bMap.remove_way_from_course;
            	    var type = bMap.current_course_type;
            	    var course_id = bMap.current_course_id;
            	}
            	else if ( el_id.indexOf('see_my_way_') === 0 ) {
            	    var msg = false;
            	    var func = bMap.display_way;
            	    var type = '';
            	    var course_id = '';

            	    mv = true;
            	}
            	else if ( el_id.indexOf('edit_my_way_') === 0 ) {
            	    var msg = false;
            	    var func = bMap.edit_way_form;
            	    var type = '';
            	    var course_id = '';

            	    mv = true;
            	}
            	else {
            	    return false;
            	}
            	
            	var items = el_id.split('_');
            	way_id = items[items.length-1];
            	
            	if ( msg ) {
                    var mv = confirm(msg);
            	}
                
                if ( mv ) {
                    func(type, course_id, parseInt(way_id));
                    
                    window.location.href = '#mode=browsing&display=course&course_type='+bMap.current_course_type+'&course_id='+bMap.current_course_id;
                    window.location.reload(true);
                }
        	}
        	else {
        	    alert('선택된 코스가 없습니다.');
        	}
            
            return false;
        });
        
        $('#btn_editting_back_to_course').click(function(e) {
        	e.preventDefault();
        	e.stopPropagation();
        	
        	var mv = confirm('작업 중인 데이터는 저장되지 않습니다. 정말로 되돌아가시겠습니까?');
        	
        	if ( mv ) {
        	    bMap.cancel_node_editting();
                window.location.href = '#mode=browsing&display=course&course_type='+bMap.current_course_type+'&course_id='+bMap.current_course_id;
        	}
        
        });
        
        set_photo_uploader(
            '#id_course_image',
            bMap.course_image_url,
            $('#form_edit_course'),
            {
            do_submit: function() {
                if ( !$('#id_course_course_id').val() ) {
                    return false;
                }
            },
            do_complete: function(data) {
                alert(data.msg);
                if ( data.result ) {
                    $('#'+data.course_type+'_course_thumbnail_image_'+String(data.course_id))
                        .attr('src', data.image_url);
                }
            }
        });

        set_photo_uploader(
            '#ui_map_image_upload',
            bMap.map_photo_edit_url,
            function() {
                if ( !bMap.current_coord.x || !bMap.current_coord.y ) {
                    return null;
                }
                return [
                    {
                        name: 'photo_id',
                        value: bMap.current_photo_id
                    },
                    {
                        name: 'lat',
                        value: bMap.current_coord.y
                    },
                    {
                        name: 'lon',
                        value: bMap.current_coord.x
                    }
                ];
            },
            {
            do_submit: function() {
                if ( !bMap.current_coord ) {
                    return false;
                }
            },
            do_complete: function(data) {
                alert(data.msg);
                if ( data.result ) {
                    bMap.photos[data.photo_id] = data;
                    bMap.display_photos_icon(data, data.photo_id);
                    bMap.display_map_photo(data.photo_id);
                    window.location.href = '#mode=browsing&display=photo&photo_id='+data.photo_id;
                }
            }
        });            

        set_photo_uploader(
            '#id_spot_upload_image',
            bMap.spot_image_url,
            [{
                name: 'spot_id',
                value: bMap.current_spot_id
            }],
            {
            do_submit: function() {
                if ( !bMap.current_spot_id ) {
                    return false;
                }
            },
            do_complete: function(data) {
                alert(data.msg);
                if ( data.result ) {
                    bMap.display_spot_images(0, data);
                }
            }
        });

        
        $('#form_edit_spot').submit(function(e) {
        	e.preventDefault();
        	e.stopPropagation();

        	var form_el = $(this);
        	
        	$('#form_edit_spot').find('input, select, textarea').removeClass('.field_with_error');

            $.post(form_el.attr('action'), form_el.serialize(), function(data) {
                if ( data.result ) {
                    var spot_id = String(data.spot_id);
                    bMap.spots[spot_id] = data[spot_id];
                    form_el.get(0).reset();
                    
                    window.location.href = '#mode=browsing&display=spot&spot_id'+spot_id;
                    window.location.reload(true);

                    bMap.ui.browsing_menu.hide();
                }
                else {
                    if ( data.errors ) {
                        var i;
                        for ( i in data.errors ) {
                            $('#id_spot_'+i).addClass('field_with_error');
                        }
                    }

                    alert(data.msg);
                }
                
                return false;
            }, 'json');
            
        	return false;
        });
        
        $('#form_edit_course').submit(function(e) {
        	e.preventDefault();
        	e.stopPropagation();
        	
        	var form_el = $(this);
            
            $('#id_course_course_id').val(bMap.current_course_id);
            
            $.post(form_el.attr('action'), form_el.serialize(), function(data) {
                if ( ('result' in data) && data.result === true ) {
            		
            		bMap.remove_editting_node(0);
                	bMap['editting_way'] = null;
            		
            		bMap.dom.e_node_list.empty();
                    
                    bMap.current_course_id = data.course_id;
                    
                    $('#course_list_container').append(data.ret_html);
                    alert(data.msg);
                    form_el[0].reset();
                    window.location.href = data.redirect_to;
                    window.location.reload(true);
                }
                else {
                    alert(data.msg);
                }
                
                return false;
            }, 'json')
            
            return false;
        });
    }
});
