var TourMapSaveBox = Class.create({
	
	container_id: null,
	
	div_id: 'tour-map-save-box',
	div: null,
	
	ajax_include_layer_url: '/tmap/include/l_box_save_route.jsp',
	
	ajax_save_url : '',
	
	html_save_box: '',
	
	req_cource_title: null,
	req_cource_descr: null,
	req_cource_route: null,
	
	// 플래시에 저장된 route id
	rid: null,
	
	initialize: function(container_id) {
		this.container_id = container_id;
		if(!$(this.div_id)) {
			$(this.container_id).insert(new Element('div', { id: this.div_id }));
		}
		this.div = $(this.div_id);
		
		this.div.setStyle({
			position: 'absolute',
			top: '100px',
			left: '50px'
		});
		
		this.div.hide();
		
		new Ajax.Request(this.ajax_include_layer_url, {
			asynchronouse: false,
			method: 'get',
			onComplete: function(response) {
				this.html_save_box = response.responseText;
			}.bindAsEventListener(this)
		});
	},

	show_save_box: function(rid) {
		this.rid = rid;
		this.req_cource_route = map.map_get_route(rid)
		div.show();
	},
	
	hide_save_box: function() {
		div.hide();
	},
	
	save_route: function() {
		new Ajax.Request(ajax_save_url, {
			asynchronous: true,
			method: 'post',
			parameters: {
				mode		: '',
				title		: this.req_cource_title,
				description	: this.req_cource_descr,
				route		: this.req_cource_route
			},
			onComplete: function(response) {
				alert("saved");
				this.hide_save_box();
			}.bindAsEventListener(this)
		});
	}
});

var TourMapInfoSpotBox = Class.create({
	
	flash_map: null,
	div_id: 'l_tour_info_spot',
	div: null,
	
	ajax_update_url: '/tmap/include/l_tour_info_spot.jsp',
	
	spot_id: null,
	spot_order: null,
	
	initialize: function(config) {
	
		this.div = $(this.div_id);
		
		if(config && config.flash_map) 	this.flash_map = config.flash_map;
		//if(this.flash_map) 				this.flash_map.map_on_tour_icon_click = this.on_tour_icon_click.bindAsEventListener(this);
		
	},
	
	update_spot_name: function() {
		$$('.tour-info-spot-name').each(function(o) {
			o.update(this.course_name + '의 ' + this.spot_order + '번 경유지 주변정보');
		}.bindAsEventListener(this));
	},
	
	on_tour_icon_click: function(id, order, course_name) {
//		if(console) console.log(arguments);
		this.spot_id = id;
		this.spot_order = order;
		this.course_name = course_name;
		
		this.div.update($(this.div_id + '_close').innerHTML);
		this.update_spot_name();
		
		Event.observe($('l_tour_info_spot_btn_plus'), 'click', this.on_click_open.bindAsEventListener(this));
		
		this.div.show();
	},
	
	on_click_open: function() {
		new Ajax.Updater(
			this.div_id,
			this.ajax_update_url,
			{
				onComplete: function() {
					this.update_spot_name();
					Event.observe($('l_tour_info_spot_btn_close'), 'click', this.on_click_close.bindAsEventListener(this));
				}.bindAsEventListener(this)
			}
		);
	},
	
	on_click_close: function() {
		this.on_tour_icon_click(this.spot_id, this.spot_order, this.course_name);
	}

});

var TourMapInfoBox = Class.create({
	
	ajax_base_url: '/tmap/ajax',
	ajax_url: '/TourAroundDetail.jsp',
		
	curr_spot_list: '0',
	curr_rnd_cd: 'TRRC0001',
	prev_btn_img: null,
	layer_id: null,
	layer: null,
	title_class: null,
	data_id: null,
	list_table_id: null,
	page_id: null,
	init_btn_id: null,
	
	initialize: function(config) {
		this.layer_id = config.layer_id;
		this.layer = $(this.layer_id);
		this.title_class = config.title_class;
		this.data_id = config.data_id;
		this.list_table_id = config.list_table_id;
		this.page_id = config.page_id;
		this.init_btn_id = config.init_btn_id;
	},
	
	tour_info_toggle: function() {
		if(this.layer.step == 'close') {
			this.prev_btn_img = $(this.init_btn_id);
			this.layer.step = 'open';
			this.layer.update($(this.layer_id+'_open').innerHTML);
			this.tour_info_tab_change($(this.init_btn_id), this.curr_rnd_cd);
			this.tour_info_data_update(this.curr_rnd_cd, this.curr_spot_list, 1);		
		}
		else {
			this.layer.step = 'close';
			this.layer.update($('l_tour_info'+'_close').innerHTML);
			$$(this.title_class).each(function(o) { o.update($(this.layer_id).course_name) }.bindAsEventListener(this));
		}
	},
	
	tour_info_tab_change: function(img, rnd_cd) {
		if(this.prev_btn_img) {
			this.prev_btn_img.src = this.prev_btn_img.src.replace('on.gif', 'off.gif');
		}
	
		this.prev_btn_img = img;

		img.src = img.src.replace('off.gif', 'on.gif');
	
		this.curr_rnd_cd = rnd_cd;
		this.tour_info_data_update(rnd_cd, this.curr_spot_list, 1);
	},
	
	tour_info_data_update: function(rnd_cd, spot_list, p) {
		var page = 1;
		if(p) {
			page = p;
		}

		$(this.data_id).update($(this.list_table_id).innerHTML);
		new Ajax.Request(this.ajax_base_url + this.ajax_url, {
			asynchronous: false,
			method: 'post',
			parameters: {
				tr_rnd_cd: rnd_cd,
				tr_base_id: spot_list,
				pageNo: page
			},
			onComplete: function(response) {
				var rInfo = response.responseJSON;
				var html = '';
				for(var i=0; i<rInfo.result.length; ++i) {
					var tt = t_tour_info;
					for(var d in rInfo.result[i]) {
						var re = new RegExp('__' + d + '__', 'g');
						tt = tt.replace(re, rInfo.result[i][d]);
					}
					html += tt;
				}
				var tbody = $(this.data_id).firstDescendant();
				Element.insert(tbody, html);
	
				$(this.page_id).update(rInfo.paging);
			}.bindAsEventListener(this)
		});
	},
	
	tour_info_page: function(p) {
		this.tour_info_data_update(this.curr_rnd_cd, this.curr_spot_list, p);
	}
});


var cTourMapSaveBox;
var cTourMapInfoBox;
var cTourMapInfoSpotBox;

function on_map_course_clk(cntX, cntY) {
//	cntX = 425336.87;
//	cntY = 619332.92;
	map.move_center(cntX, cntY);
	map.map_set_tour_here(cntX, cntY);
	map.set_resolution(512);
	map.update();
}


function on_map_course_pos(orig, cntX, cntY) {
	
	if (orig == 'depart' && cntX && cntY) {

		cTourMap.dep_x = cntX, cTourMap.dep_y = cntY;
		new Ajax.Request('/tmap/ajax/GeoCode.jsp', {
			asynchronous: false,
			method: 'post',
			parameters: {
				x: cntX,
				y: cntY
			},
			onComplete: function(response) {
				var rInfo = response.responseJSON;
				$('tour_input_depart').value = 
					[rInfo['RI_NM'], rInfo['SGG_NM'], rInfo['SIDO_NM'], rInfo['UMD_NM']].join(',');
			}
		});

		map.map_insert_poi(orig, cntX, cntY);

	}
	else if (orig == 'arrive' && cntX && cntY) {

		cTourMap.arv_x = cntX, cTourMap.arv_y = cntY;
		new Ajax.Request('/tmap/ajax/GeoCode.jsp', {
			asynchronous: false,
			method: 'post',
			parameters: {
				x: cntX,
				y: cntY
			},
			onComplete: function(response) {
				var rInfo = response.responseJSON;
				$('tour_input_arrive').value = 
					[rInfo['RI_NM'], rInfo['SGG_NM'], rInfo['SIDO_NM'], rInfo['UMD_NM']].join(',');
			}
		});

		map.map_insert_poi(orig, cntX, cntY);
		
	}
}

function on_map_tour_load_done() {
	
	var route_list = map.map_get_route_list().split(',');
	// console.log( map.map_get_route_list() );
	var cListElm = $('course_list').firstDescendant();
	var routes = [];
	var color = ['#8800FF', '#000088', '#FFFF00'];
	
	for (var i = 0; i < route_list.length; i += 2) {
		var rday = route_list[i];
		var rid = route_list[i + 1];

		var route_tr = '<tr id="tr_' + rid + '"><td style="background-color: ' + color[i/2] + '"><img id="img_'+ rid +'" onclick="javascript:map_on_off_click(\'' + rid + '\');" src="/design/images/place/tourmap/icon_see_on.gif" /></td><td>' + rday + '</td><td width="4"></td><td align="right" width="88" class="text03"><a href="javascript:select_tour_map(\'' + rid + '\', \'' + rday + '\');">선택</a>| <a href="javascript:cTourMapSaveBox.show_save_box(\'' + rid + '\');">저장</a>| <a href="javascript:map_remove_tr(\'' + rid + '\', false);">삭제</a></td></tr>';

		routes.push(route_tr);
		
		}

	cListElm.update(routes.join(''));
}

function map_save_route(rid) {
	if(!$('tour-map-save_box')) {
		$('tour-map-container').insert(new Element("div", { id: 'tour-map-save-box' }));
	}
	$('tour-map-save-box').setStyle({
		position: 'absolute',
		top: '100px',
		left: '50px'
	});
	new Ajax.Updater('tour-map-save-box', '/tmap/include/l_box_save_route.jsp');	
}

function map_save_route2(rid) {
	// console.log(map.map_get_route(rid));

	var ajax_url = '/tmap/ajax/SaveTourRoute.jsp?';
	var get_params = map.map_get_route(rid) + '&mode=temp';
	// console.log(ajax_url + get_params);
		new Ajax.Request(ajax_url + get_params, {
			asynchronous: false,
			method: 'get',
			onComplete: function(response) {
				var msg = response.responseJSON;
				if (msg['result'] > 0) {
					alert('해당 경로가 저장 되었습니다');
				} else {
					alert('다시 한번 시도해 주십시요');
				}
			}
		});
}

var route_flags = {};
function map_on_off_click(rid) {
	if (route_flags[rid]) {
		map.map_on_off_tour_map(rid, true);
		route_flags[rid] = false;
		$('img_' + rid).src = "/design/images/place/tourmap/icon_see_on.gif";
	} else {
		map.map_on_off_tour_map(rid, false);
		route_flags[rid] = true;
		$('img_' + rid).src = "/design/images/place/tourmap/icon_see_off.gif";
	}
}

function map_remove_tr(rid) {
	var tr_del = $('tr_' + rid);
	if (tr_del) {
		map.map_on_off_tour_map(rid);
		tr_del.remove();
	}
}

var t_tour_info = 
  '<tr>'
+ '<td width="69" height="30" align="center"><a onfocus="blur();" href="#" onclick="tour_info_detail(\'__TR_RND_CD__\', \'__TR_RND_REF_ID__\')">__TR_RND_NM__</a></td>'
+ '<td width="7" align="left"> </td>'
+ '<td width="194" align="center"><a onfocus="blur();" href="#" onclick="tour_info_detail(\'__TR_RND_CD__\', \'__TR_RND_REF_ID__\')">__JUSO_NM__</a></td>'
+ '<td width="67" align="center"><img alt="지도" src="/design/images/place/tourmap/btn_map.gif" onclick="map_move_to(\'__CENTER_X__\', \'__CENTER_Y__\');" style="cursor: pointer;" /></td>'
+ '</tr>'
+ '<tr align="center">'
+ '<td height="1" bgcolor="#e1e1e1" colspan="8"/>'
+ '</tr>';

function map_move_to(x, y) {
	map.move_center(x, y);
	map.set_resolution(64);
	map.update();
}

function select_tour_map(rid, course_name) {
	$('l_tour_info').step = 'close';
	$('l_tour_info').rid = rid;
	$('l_tour_info').course_name = course_name;
	$('l_tour_info').update($('l_tour_info'+'_close').innerHTML);
	$$('.tour-info-course-name').each(function(o) { o.update(course_name) });
	
	$('l_tour_info').show();
	map.map_select_tour_map(rid);
	//_curr_spot_list = map.map_get_tour_spot_list(rid);
	cTourMapInfoBox.curr_spot_list = map.map_get_tour_spot_list(rid);
}

function tour_info_detail(tr_rnd_cd, tr_rnd_ref_id, async) {

//	볼거리   TRRC0001
//	즐길거리 TRRC0002
//	먹거리	 TRRC0003
//	숙박시설 TRRC0004
//	체험마을 TRRC0005

	/*
	var url = "";

	if(tr_rnd_cd = 'TRRC0001'){
		url = "http://www.welchon.com/default.portal?_pt_%24service=render/s_%24_u_ett_%24_u_ett_03_%24_u_ett_03_02_01_%24_ff8080811e350a60011e3519605b00c0/sview/snormal&viewId="+tr_rnd_ref_id;
	}else if(tr_rnd_cd = 'TRRC0002'){
		url = "http://www.welchon.com/default.portal?_pt_%24service=render/s_%24_u_ett_%24_u_ett_03_%24_u_ett_03_01_01_%24_ff8080811e350a60011e351a1981012e/sview/snormal&viewId="+tr_rnd_ref_id;
	}else if(tr_rnd_cd = 'TRRC0003'){
		url = "http://www.welchon.com/default.portal?_pt_%24service=render/s_%24_u_ett_%24_u_ett_03_%24_u_ett_03_03_01_%24_ff8080811e350a60011e351a4e44019c/sview/snormal&viewId="+tr_rnd_ref_id;
	}else if(tr_rnd_cd = 'TRRC0004'){
		url = "#";
	}else if(tr_rnd_cd = 'TRRC0005'){
		url = "#";
	}

//	top.location.href = url;
	window.open(url,'_blank');
	*/

	var url = base_url + "/include/tour_around_detail.jsp";

	if(async == undefined) {
		async = true;
	}

	new Ajax.Request(url,
		{
			asynchronous: false,
			method: 'post',
			parameters: {
				tr_rnd_cd : tr_rnd_cd
			,	tr_rnd_ref_id : tr_rnd_ref_id
			},
			onComplete: tour_info_view
		}
	);

//	alert(['js/l_left_02.js', 'tour_info_detail', tr_rnd_cd, tr_rnd_ref_id]);
}

function tour_info_view(transport){
	$('div_tour_information').show();
	$('div_tour_information').update(transport.responseText);
}

function tour_info_close(transport){
	$('div_tour_information').hide();
}
// ---------------------------------------------------------------------------------
// Init : l_left_02.js
// ---------------------------------------------------------------------------------
function init_l_left_02() {
	cTourMapSaveBox = new TourMapSaveBox('tour-map-container');
	
	cTourMapInfoBox = new TourMapInfoBox({
		layer_id: 'l_tour_info',
		title_class: '.tour-info-course-name',
		data_id: 'tour_info_data',
		list_table_id: 'tour_list_table',
		page_id: 'tour_info_page',
		init_btn_id: 'btn_rnd_01'
	});
	
	cSpotInfoBox = new TourMapInfoBox({
		layer_id: 'l_tour_info_spot',
		title_class: '.tour-info-spot-name',
		data_id: 'tour_info_spot_data',
		list_table_id: 'tour_list_table',
		page_id: 'tour_info_spot_page',
		init_btn_id: 'btn_rnd_01_spot'
	});
	
	new Draggable($('l_tour_info'));
	new Draggable($('l_tour_info_spot'));
	cTourMapInfoSpotBox = new TourMapInfoSpotBox({
		flash_map: map
	});
}
/*
(function() {
	
})();
*/