Tmap.Control.SelectTourmap = Class.create({

	oDepart		:	null,
	subMnDep    :   false,
	subMnArv    :   false,
	oArrive		:	null,
	oTheme		:	null,
	oMake		:	null,
	oReset		:	null,
	lThemeSelect:	null,
	arrTheme	:   [],
	blk_prty    :   Prototype.Browser['IE'] ? 'block' : 'table-row',
	dep_jido_flag:	false,
	arv_jido_flag:	false,
	dep_x		:	null,
	dep_y		:	null,
	arv_x		:	null,
	arv_y		:	null,
	dep_jido_notice:null,
	arv_jido_notice:null,
	tour_input_depart: null,
	tour_input_arrive: null,
	dep_name: null,
	dep_region_cd: null,
	arv_name: null,
	arv_region_cd: null,
	type		:	null,

	initialize: function(options) {

		
	
		Object.extend(this, options);
		
		this.tour_input_depart = $('tour_input_depart');
		this.tour_input_arrive = $('tour_input_arrive');
		Event.observe(this.oDepart, 'click', this.on_click_btn_depart.bindAsEventListener(this));
		Event.observe(this.oArrive, 'click', this.on_click_btn_arrive.bindAsEventListener(this));
		Event.observe(this.oTheme, 'click', this.on_click_area_theme.bindAsEventListener(this));
		Event.observe(this.oMake, 'click', this.on_click_make.bindAsEventListener(this));
		Event.observe(this.oReset, 'click', this.on_click_reset.bindAsEventListener(this));
		Event.observe(this.dep_srch, 'click', this.on_click_dep_srch.bindAsEventListener(this));
		Event.observe(this.arv_srch, 'click', this.on_click_arv_srch.bindAsEventListener(this));
		Event.observe(this.dep_jido, 'click', this.on_click_dep_jido.bindAsEventListener(this));
		Event.observe(this.arv_jido, 'click', this.on_click_arv_jido.bindAsEventListener(this));
	},
	
	on_click_btn_depart: function() {
		if (!this.subMnDep) {
			this.open_sub_menu('tr_dep');
			this.subMnDep = true;
		} else if (this.subMnDep) {
			this.close_sub_menu('tr_dep');
		}
	},

	close_sub_menu: function(subMnName) {
		for (var i = 1; i <= 3; i++) {
			var tr_tmp = subMnName + i;
			if ($(tr_tmp).style.display == this.blk_prty)
				$(tr_tmp).style.display = "none";
		}

		if (subMnName == 'tr_dep')
			this.subMnDep = false;
		else if (subMnName == 'tr_arv')
			this.subMnArv = false;
	},

	open_sub_menu: function(subMnName) {
		for (var i = 1; i <= 3; i++) {
			var tr_tmp = subMnName + i;
			if ($(tr_tmp).style.display == 'none')
				$(tr_tmp).style.display = this.blk_prty;
		}

		if (subMnName == 'tr_dep')
			this.subMnDep = true;
		else if (subMnName == 'tr_arv')
			this.subMnArv = true;
	},
	
	on_click_btn_arrive: function() {
		if (!this.subMnArv) {
			this.open_sub_menu('tr_arv');
		} else if (this.subMnArv) {
			this.close_sub_menu('tr_arv');
		}
	},

	on_click_dep_jido: function() {
		this.dep_jido_flag = true;
		this.dep_jido_notice.update('&nbsp;지도를 클릭하세요.');
		if(map) map.map_tour_map_start();
	},
	
	on_click_arv_jido: function() {
		this.arv_jido_flag = true;
		this.arv_jido_notice.update('&nbsp;지도를 클릭하세요.');
		if(map) map.map_tour_map_arrive();
	},

	on_click_area_theme: function() {
		this.oTheme.value = '';
		this.lThemeSelect.style.display = "block";
		this.lThemeSelect.style.top = "150px";
		this.lThemeSelect.style.left = "300px";
		this.lThemeSelect.style.position = "absolute";
		this.lThemeSelect.style.zindex = "2";
		var themeAdm = new Tmap.Control.SelectTheme('SearchField2');
	},
	
	on_click_make: function() {
		var theme = this.arrTheme.join(',');
		map.map_delete_tour_map();
		map.map_draw_tour_map(this.dep_x, this.dep_y, this.arv_x, this.arv_y, theme);
//		if(console) {
//			console.log([this.dep_x, this.dep_y, this.arv_x, this.arv_y, theme]);
//		}
	},
	
	on_click_reset: function() {
		map.map_delete_tour_map();
		this.tour_input_depart.value = '';
		this.tour_input_arrive.value = '';
	},

	on_click_dep_srch: function() {
		var dep_name, dep_region_cd;

		$$('#adm_sgg_l02_1 option').each(function(elem){
                if (elem.selected) {
					dep_region_cd = elem.value;
				}
        });

		
		dep_name = this.dep_txt.value;

		// for later spot_search_paging
		this.dep_name = dep_name, this.dep_region_cd = dep_region_cd;

		this.type = "depart";
		
		if (dep_region_cd) {
			this.depart_search_page(1);
		} else {
			alert('Please Choose Region Code');
		}
	},
	
	depart_search_page: function(p) {
		var page = 1;
		if(p) {
			page = p;
		}

		var ajax_url = "/tmap/ajax/SearchSpot.jsp";

		new Ajax.Request(ajax_url, {
			asynchronous: false,
			method: 'post',
			parameters: {
				name: this.dep_name,
				region_cd: this.dep_region_cd,
				type : this.type,
				pageNo: p
			},
			onComplete: function(response) {
				var cInfo = response.responseJSON;
				var cListElm = $('course_list').firstDescendant();
				var cItems = [];


				cInfo['result'].each(function(course) {
					var item = '<tr><td width="142"><span class="ts11">'
							   + course.TR_BASE_NM
							   +'</span></td><td align="right" colspan="2"><img onclick=\"on_map_course_pos(\'depart\', '
							   + course.CENTER_X + ', ' + course.CENTER_Y 
							   + ')\" alt="선택" src="/design/images/place/tourmap/btn_ok.gif"/><img alt="지도" onclick=\"on_map_course_clk('
							   + course.CENTER_X + ', ' + course.CENTER_Y + ')\" src="/design/images/place/tourmap/btn_map.gif"/></td></tr>';
					cItems.push(item);
				});

				cListElm.update(cItems.join(''));

				$('course_page').update(cInfo['paging']);
			}
		});
	},
	on_click_arv_srch: function() {

		var ajax_url = "/tmap/ajax/SearchSpot.jsp";

		var arv_name, arv_region_cd;

		$$('#adm_sgg_l02_2 option').each(function(elem){
			if (elem.selected) {
				arv_region_cd = elem.value;
			}
        });

		arv_name = this.arv_txt.value;
		// for later spot_search_paging
		this.arv_name = arv_name, this.arv_region_cd = arv_region_cd;

		this.type = "arrive";

		if (arv_region_cd) {
			this.arrive_search_page(1);
		} else {
			alert('Please Choose Region Code');
		}
	},

	arrive_search_page: function(p) {
		var page = 1;
		if(p) {
			page = p;
		}

		var ajax_url = "/tmap/ajax/SearchSpot.jsp";

		new Ajax.Request(ajax_url, {
			asynchronous: false,
			method: 'post',
			parameters: {
				name: this.arv_name,
				region_cd: this.arv_region_cd,
				type : this.type,
				pageNo: p
			},
			onComplete: function(response) {
				var cInfo = response.responseJSON;
				var cListElm = $('course_list').firstDescendant();
				var cItems = [];

				cInfo['result'].each(function(course) {
					var item = '<tr><td width="142"><span class="ts11">'
							   + course.TR_BASE_NM
							   +'</span></td><td align="right" colspan="2"><img onclick=\"on_map_course_pos(\'arrive\', '
							   + course.CENTER_X + ', ' + course.CENTER_Y 
							   + ')\" alt="선택" src="/design/images/place/tourmap/btn_ok.gif"/><img alt="지도" onclick=\"on_map_course_clk('
							   + course.CENTER_X + ', ' + course.CENTER_Y + ')\" src="/design/images/place/tourmap/btn_map.gif"/></td></tr>';
					cItems.push(item);
				});

				cListElm.update(cItems.join(''));

				$('course_page').update(cInfo['paging']);
			}
		});
	},
	
	on_map_click: function(x, y) {
		if (this.dep_jido_flag) {
			if (x && y) {
				this.dep_x = x;

				this.dep_y = y;
				new Ajax.Request('/tmap/ajax/GeoCode.jsp', {
					asynchronous: false,
					method: 'post',
					parameters: {
						x: x,
						y: y
					},
					onComplete: function(response) {
						var rInfo = response.responseJSON;
						// alert(response.responseText);
						this.tour_input_depart.value = 
							[rInfo['SIDO_NM'], rInfo['SGG_NM'], rInfo['UMD_NM'], rInfo['RI_NM']].join(',');
					}.bindAsEventListener(this)
				});
				
				this.dep_jido_flag = false;
				this.dep_jido_notice.update('');
				
				if (this.subMnDep) {
					this.close_sub_menu('tr_dep');
					this.subMnDep = false;
				}
			}
		} else if (this.arv_jido_flag) {
			if (x && y) {
				this.arv_x = x, this.arv_y = y;
				new Ajax.Request('/tmap/ajax/GeoCode.jsp', {
					asynchronous: false,
					method: 'post',
					parameters: {
						x: x,
						y: y
					},
					onComplete: function(response) {
						var rInfo = response.responseJSON;
						
						this.tour_input_arrive.value = 
							[rInfo['SIDO_NM'], rInfo['SGG_NM'], rInfo['UMD_NM'], rInfo['RI_NM']].join(',');
					}.bindAsEventListener(this)
				});
				this.arv_jido_flag = false;
				this.arv_jido_notice.update('');
				if (this.subMnArv) {
					this.close_sub_menu('tr_arv');
					this.subMnArv = false;
				}
			}
		}
	}
});
