//----------------------------------------------------------------------//
var AJAX_URL_PARAM = null;

var AJAX_BUS_OPEN_API = '/MapServer/BusService'; // 대중교통 연동
// var AJAX_MAP_OPEN_API='/ajax/test.jsp'; // MAP서버 연동 주소
var AJAX_MAP_OPEN_API = '/MapServer/Service'; // MAP서버 연동 주소
var AJAX_MAP_SAVE_OPEN_API = '/MapServer/SaveService'; // MAP저장 연동 주소
// ----------------------------------------------------------------------//
var recallFunc = '';
var recallFuncBusLane = '';
function URLencode(sStr) {
	return escape(sStr).replace(/\+/g, '%2C').replace(/\"/g, '%22').replace(
			/\'/g, '%27');
}

function executeSearchAjax(async) {
	if (async == null)
		async = true;
	$.ajax( {
		type : "POST",
		url : AJAX_MAP_OPEN_API,
		data : "svcCode=uTourOnion&" + this.AJAX_URL_PARAM,
		async : async,
		timeout: 30000,
		dataType : "json",
		success : recallFunc
	});
}

function executeAjaxArgument(async, parameter, _cbkFunc, targetThis, paramObj, _cbErrFunc) {
	var arrObj = new Array();

	if (async == null)
		async = true;

	$.ajax( {
		type : "POST",
		url : AJAX_MAP_OPEN_API,
		data : "svcCode=uTourOnion&" + parameter,
		async : async,
		dataType : "json",
		timeout: 30000,
		success : function(data) {
			if (!targetThis) {
				_cbkFunc(data);
			} else {
				if (!paramObj) {
					_cbkFunc.call(targetThis, data);
				} else {
					arrObj.push(data);
					for ( var i = 0; i < paramObj.length; i++) {
						arrObj.push(paramObj[i]);
					}

					_cbkFunc.apply(targetThis, arrObj);
				}
			}
		},
		error:function(xhr, ajaxOptions, thrownError){
			debugger;
			//alert('error');
			if(!_cbErrFunc)
				alert('error!');
			else{
				if (!targetThis) {
					_cbErrFunc();
				} else {
					_cbErrFunc.call(targetThis);
				}
			}
		}
	});
}

// 버스 노선번호
function executeBusNoSearchAjax(async) {
	if (async == null)
		async = true;
	$.ajax( {
		type : "POST",
		url : AJAX_BUS_OPEN_API,
		data : "svcCode=uTourOnion&" + this.AJAX_URL_PARAM,
		async : async,
		timeout: 30000,
		dataType : "json",
		success : recallFunc
	});
}

/**
 * 버스 관련 검색 기능 수행 (Argument 인자 처리용)
 *
 * @param url
 * @param async
 * @param _cbkFunc
 * @param targetThis
 * @param arrayObj
 * @return
 */
function executeBusSearchAjaxArgument(async, _cbkFunc, targetThis, paramObj) {
	var arrObj = new Array();

	if (async == null)
		async = true;

	$.ajax( {
		type : "POST",
		url : AJAX_BUS_OPEN_API,
		data : "svcCode=uTourOnion&" + this.AJAX_URL_PARAM,
		async : async,
		timeout: 30000,
		dataType : "json",

		success : function(data) {
			if (!targetThis) {
				_cbkFunc(data);
			} else {
				if (!paramObj) {
					_cbkFunc.call(targetThis, data);
				} else {
					arrObj.push(data);
					for ( var i = 0; i < paramObj.length; i++) {
						arrObj.push(paramObj[i]);
					}

					_cbkFunc.apply(targetThis, arrObj);
				}
			}
		}
	});
}

// 버스노선결과
function executeBusLaneSearchAjax(async) {

	if (async == null)
		async = true;
	$.ajax( {
		type : "POST",
		url : AJAX_BUS_OPEN_API,
		data : "svcCode=uTourOnion&" + this.AJAX_URL_PARAM,
		async : async,
		timeout: 300000,
		dataType : "json",
		success : recallFuncBusLane
	});
}

function executeAjaxDetail(url, async, _recallFunc) {
	if (async == null)
		async = true;
	$.ajax( {
		type : "GET",
		url : url,
		async : async,
		timeout: 300000,
		dataType : "text",
		success : _recallFunc
	});
}

function executeBusSearchAjax() {
	var urlInfo = getBusOpenApiUrl();
	viewOpenApiResult(urlInfo);
	$.post(this.AJAX_BUS_OPEN_API, 'svcCode=uTourOnion&' + this.AJAX_URL_PARAM,
			recallFunc, "json");
}

function resultoutput(data) {
	$("#SearchOpenAPIResult").val(data);
}

function viewOpenApiResult(data) {
	$("#SearchOpenAPIUrl").val(data);
}

function getMapOpenApiUrl() {
	return this.AJAX_MAP_OPEN_API + '&' + this.AJAX_URL_PARAM;
}

function getBusOpenApiUrl() {
	return this.AJAX_BUS_OPEN_API + '&' + this.AJAX_URL_PARAM;
}

function BusPathDialogOpen(sztitle, x, y) {
	if (!sztitle)
		sztitle = '데이타 결과 정보';
	if (!x)
		x = 500;
	if (!y)
		y = 350;

	if (!$(BusPathResult).dialog('isOpen'))
		if ($(BusPathResult).dialog('isOpen') == false)
			$(BusPathResult).dialog('open');
		else
			$(BusPathResult).dialog( {
				position : [ 'left', 'bottom' ],
				width : x,
				height : y,
				resizable : true,
				closeOnEscape : true,
				title : sztitle,
				modal : false,
				buttons : {
					"종료" : function() {
						$(this).dialog("close");
					}
				}
			});
	else
		$(BusPathResult).dialog('open');
}

function COMMIFY(n) {
	var reg = /(^[+-]?\d+)(\d{3})/;   // 정규식
	n += '';                          // 숫자를 문자열로 변환

	while (reg.test(n))
		n = n.replace(reg, '$1' + ',' + '$2');

	return n;
}

function COMMIFYKM(param) {
	var n = param/1000;
//	var dotN = param%1000;
//
//	if (n>1){
//		var reg = /(^[+-]?\d+)(\d{3})/;   // 정규식
//		n += '';                          // 숫자를 문자열로 변환
//
//		while (reg.test(n))
//			n = n.replace(reg, '$1' + ',' + '$2');
//	}

	//return n + "." + dotN;
	return n;
}
