function addOption(val, label){

	var addOpt = document.createElement('option');

	addOpt.value = val;

	addOpt.appendChild(document.createTextNode(label));

	return addOpt;

}



function showPagingNavi(xml,id ) {

	

	var result = '';

	

	var currentBlock  = "";

	var totalBlocks   = "";

	var beginPage     = "";

	var endPage       = "";

	var pagesPerBlock = "";

	var pageNo        = "";

	var totalPages    = "";

	

	$(xml).find("pageInfo").each(function () {

		

		currentBlock  = Number($(this).find("currentBlock").text());

		totalBlocks   = Number($(this).find("totalBlocks").text());

		beginPage     = Number($(this).find("beginPage").text());

		endPage       = Number($(this).find("endPage").text());

		pagesPerBlock = Number($(this).find("pagesPerBlock").text());

		pageNo        = Number($(this).find("pageNo").text());

		totalPages    = Number($(this).find("totalPages").text());

	});

	

	var prev_block    = "<img src='"+ _WEB_CONTEXT_NAME +"/images/btn_first.gif' alt='맨처음 가기'/>";

	var next_block    = "<img src='"+ _WEB_CONTEXT_NAME +"/images/btn_last.gif' alt='마지막 가기'/>";

	var prev          = "<img src='"+ _WEB_CONTEXT_NAME +"/images/btn_prev.gif' alt='이전 페이지'/>";

	var next          = "<img src='"+ _WEB_CONTEXT_NAME +"/images/btn_next.gif' alt='다음 페이지'/>";

	

	//이전 페이지 블록으로 이동

	if(currentBlock == 1) {

		result    += "<a href='javascript:'>"+prev_block+"</a>　";

	} else {

		result    += "<a href='javascript:goPage(\""+((currentBlock - 2) * pagesPerBlock + 1)+"\");'>"+prev_block+"</a>　";

	}

	

	//이전 페이지로 이동

	if(pageNo == 1) {

		result    += "<a href='javascript:'>"+prev+"</a>　";

	} else {

		result    += "<a href='javascript:goPage(\""+(pageNo - 1)+"\");'>"+prev+"</a>　";

	}

	

	for(var i = beginPage ; i <= endPage ; i++) {

		if(pageNo == i){

			result += '<a href="javascript:goPage(\''+i+'\');" class="onpage">' +i+ '</a>';

		}else{

			result += '<a href="javascript:goPage(\''+i+'\');">' +i+ '</a>';

		}

	}

	

	//다음 페이지로 이동

	if(pageNo == totalPages) {

		result += "<a href='javascript:'>"+next+"</a>　";

	} else {

		result += "<a href='javascript:goPage(\""+(pageNo + 1)+"\");'>"+next+"</a>　";

	}

	

	//다음 블록으로 이동

	if(currentBlock == totalBlocks) {

		result += "<a href='javascript:'>"+next_block+"</a>　";

	} else {

		result += "<a href='javascript:goPage(\""+(currentBlock * pagesPerBlock + 1)+"\");'>"+next_block+"</a>　";

	}

       

	$("#"+id).html(result);

}



/*크로스도메인 문제로 JSONP로 변경*/

function getAddressFromWgs(x, y){

		var url = "http://61.108.209.20/geo/reversegeocoding?";

		var param = "&format=javascript";

		param += "&lon="+x;

		param += "&lat="+y;

		param += "&version=1";

		param += "&coordType=WGS84GEO";

		param += "&reqCoordType=WGS84GEO";

		param += "&addressType=A01&callback=callback";



		 jQuery.ajax({

			 async : false,

			 url : url,

			 data : param,

			 dataType : "jsonp",

			 jsonp : "callback",

			 success : function(data){

				$('#address_jibun').text(data.addressInfo.fullAddress);

			 },

			 error:function(request,status,error){

				 alert("code:"+request.status+"n"+"message:"+request.responseText+"n"+"error:"+error);

			

			 }

	 });

}



/*크로스도메인 문제로 JSONP로 변경*/

function getNewAddressFromWgs(x, y){

		var url = "http://61.108.209.20/geo/reversegeocoding?";

		var param = "&format=javascript";

		param += "&lon="+x;

		param += "&lat="+y;

		param += "&version=1";

		param += "&coordType=WGS84GEO";

		param += "&reqCoordType=WGS84GEO";

		param += "&addressType=A04&callback=callback";



		jQuery.ajax({

			 async : false,

			 url : url,

			 data : param,

			 dataType : "jsonp",

			 jsonp : "callback",

			 success : function(data){

				 $('#address_new').text(data.addressInfo.fullAddress);

			 },

			 error:function(request,status,error){

				 alert("code:"+request.status+"n"+"message:"+request.responseText+"n"+"error:"+error);

			

			 }

	 });

}



function getWgsFromAddress(city_do, gu_gun, dong, bunji, addressFlag){
	
	var url = "http://61.108.209.20/geo/geocoding?";

	var param = "&format=javascript";

	param += "&city_do="+city_do;

	param += "&gu_gun="+gu_gun;

	param += "&dong="+dong;

	param += "&bunji="+bunji;

	param += "&version=1";

	param += "&coordType=WGS84GEO";

	param += "&reqCoordType=WGS84GEO";

	param += "&addressFlag="+addressFlag+"&callback=callback";



	jQuery.ajax({

			 async : false,

			 url : url,

			 data : param,

			 dataType : "jsonp",

			 jsonp : "callback",

			 success : function(data){

			var lon 	= data.coordinateInfo.lon;

			var lat	  	= data.coordinateInfo.lat;

			var newLon 	= data.coordinateInfo.newLon;

			var newLat	= data.coordinateInfo.newLat;

			lon = lon==''?newLon:lon;

			lat = lat==''?newLat:lat;

			wgsXY = lon+","+lat;

			

			// 좌표 변환 WGS -> Mando

			var lonlatMando = Mando.wgs84ToMandoMap(lon, lat);

			xMando = lonlatMando.lon;

			yMando = lonlatMando.lat;

			var wgsLonLat = new UTISMap.LonLat(xMando, yMando);

			

			_MOVE_MARKER 	= new UTISMap.Layer.Markers("MoveMarkers");

			map.addLayer(_MOVE_MARKER); 

			

			var size = new UTISMap.Size(31, 39); // 마커 아이콘 크기

			var offset = new UTISMap.Pixel(-(size.w / 2), -size.h);

			var icon = new UTISMap.Icon("/images/map/present.png", size, offset);

			

			// 마커 생성

			_MOVE_MARKER.addMarker(new UTISMap.Marker(lonlatMando, icon));

			// 지도 이동

			map.setCenter(wgsLonLat, map.getZoom());

			

			setTimeout("locClearMarker()",3000);

			

			 },

			 error:function(request,status,error){

				 alert("code:"+request.status+"n"+"message:"+request.responseText+"n"+"error:"+error);

			 }

	 });

}



function locClearMarker(marker){

	_MOVE_MARKER.clearMarkers();

}



var linkIdInfo = new Object();



function getLinkIdFromMando(xy){

    var shapeLevel = map.getZoom()+1; 

    if(shapeLevel==12){

    	shapeLevel = 11;

    }  

	

	var url = "common/searchLinkInfo.do";

	var param = 	'&BBOX='					+	map.getExtent().toBBOX();

	   	param += 	'&WIDTH='					+	map.size.w;

	   	param += 	'&HEIGHT='					+	map.size.h;

	   	param += 	'&SHAPE_LAYER=UTIS:P_LV'	+	shapeLevel;

	   	param += 	'&X='						+	parseInt(xy.x);

	   	param += 	'&Y='						+	parseInt(xy.y);

	requestService(url, param, getLinkIdFromMandoCallback, false);

	return linkIdInfo;

}



function getLinkIdFromMandoCallback(xml){

	

	$(xml).find('list').each(function () {

		var id 			= $(this).find("id").text();

		var linklevel	= $(this).find("linklevel").text();

		var tonode		= $(this).find("tonode").text();

		var fromnode	= $(this).find("fromnode").text();

		var speed		= $(this).find("speed").text();

		var grade		= $(this).find("grade").text();

		var roadname	= $(this).find("roadname").text();

		var len			= $(this).find("len").text();

		var ttime		= $(this).find("ttime").text();

		

		linkIdInfo.id 		 	= id;

		linkIdInfo.linklevel 	= linklevel;

		linkIdInfo.tonode 		= tonode;

		linkIdInfo.fromnode 	= fromnode;

		linkIdInfo.speed 		= speed;

		linkIdInfo.grade 		= grade;

		linkIdInfo.roadname 	= roadname;

		linkIdInfo.len 			= len;

		linkIdInfo.ttime 		= ttime;

	});

}





/**

 * point 좌표(1차원배열)를 WKT POINT 포맷을 변경

 *  - 사용법

 *      var point = [];

		point[0] = [127.32654, 37.5844];

		var wkt = getWktPoint(point)

 * 

 * @param point

 * @returns {String}

 */

function getWktPoint(point){

	return 'POINT('+point[0][0]+' '+point[0][1]+')';

}







/**

 * polygon 좌표(2차원배열)를 WKT POLYGON 포맷을 변경

 *  - 사용법

 *  var polygon = [];

		polygon[0] = [127.1, 37.1];

		polygon[1] = [127.2, 37.2];

		polygon[2] = [127.3, 37.3];

		polygon[3] = [127.4, 37.4];

	var wkt = getWktPolygon(polygon);

		

 * @param polygon

 * @returns {String}

 */

function getWktPolygon(polygon){
	
	var str = '';

	for(var idx=0;idx<polygon.length;idx++){

		var point = polygon[idx];

//		str += point[0]+' '+point[1]+', ';

		str += point[0]+' '+point[1];

		if(idx!=polygon.length-1){
			
			str += ',';
		}


	}
	
	str += ',' + polygon[0][0] + ' ' + polygon[0][1];


//	// 시작 좌표를 한번더 표출

//	var point = polygon[0];

//	str += point[0]+' '+point[1];

	return 'POLYGON(('+str+'))';

}







/**

 * line 좌표(2차원배열)를 WKT LINE 포맷을 변경

 *  - 사용법

 *  var line = [];

		line[0] = [127.1, 37.1];

		line[1] = [127.2, 37.2];

		line[2] = [127.3, 37.3];

	var wkt = getWktLine(line);

		

 * @param polygon

 * @returns {String}

 */

function getWktLine(line){



	var str = '';

	for(var idx=0;idx<line.length;idx++){

		var point = line[idx];

		str += point[0]+' '+point[1];

		if(idx!=line.length-1)

			str += ',';

	}



	return 'LINE(('+str+'))';

}







/**

 * 레이아웃 자동 맞추기 함수

 */

function resizeDiv(){

	

	// 현재 페이지의 높이와 넓이를 구해서

	var docHeight = $(window).height();

	var docWidth = $(window).width();

	

	// 지도 표출 레이어의 사이즈를 다시 조정한다.

	$('#aMap').css('height',(docHeight-79)+'px');

	$('#aMap').css('width', (docWidth-320)+'px');

	

}









