/**
 * 공통 javascripts
 * 함수들에 대한 설명은 아래 페이지를 참고하세요.
 * http://play.daumcorp.com/x/wDfoAQ
 */
(function(){
	daum.addEvent(window, 'load', function(){
		if(window.pageYOffset <= 0){
			setTimeout(scrollTo, 0, 0, 1);
		}
	});

	function checkKeyValue(query,clear) {
		daum.Element[daum.$(query).value.isEmpty() ? 'hide' : 'show'](clear);
	}

	daum.addEvent('query','keyup',function(){checkKeyValue('query','btnTotalSearchClear')});
	daum.addEvent('query','focus',function(){checkKeyValue('query','btnTotalSearchClear')});
	daum.addEvent('btnTotalSearchClear','click',function(){
		daum.Element.hide('btnTotalSearchClear');
		daum.$('query').value="";
		daum.$('query').focus();
	});
	daum.addEvent('btnTotalSearch','click',function(){
		window.location.href = "http://m.search.daum.net/mobile/search?q="+daum.$('query').value;
	});

	daum.Event.addEvent('btnTotalSearchLayer','click',function(){
		daum.Element.show('daumTotalSearchBack');
		daum.Element.show('daumTotalSearchLayer');
		setLayerBGsize(daum.$('daumTotalSearchBack'));
	});

	daum.Event.addEvent('btnTotalSearchClose','click',function(){
		daum.Element.hide('daumTotalSearchBack');
		daum.Element.hide('daumTotalSearchLayer');
	});

	function getBGsize(){
		var offset = 1;
		if(daum.Browser.opera){
			offset = 2;
			if(daum.Browser.xperiax1){
				offset = 1.75;
			}
		}
		return Math.max((document.documentElement.clientHeight * offset), (document.documentElement.scrollHeight * offset));
	}
	function setLayerBGsize(obj){
		obj.style.height = getBGsize(obj)+"px";
	}

})();


// suggest init
daum.suggest.Service.init();
var daumTotalSearchSuggest = daum.suggest.Service.add(
	"query",
	"daumTotalSuggestLayer",
	"http://sug.tocteen.daum.net/",
	"/second_mbsuggest",
	"daumTotalSearchSuggest.dataModel.forceLoadComplete",
	true ).
setEncodeKeyword( "utf_in_out" ).
setBeforeSubmitFunc( function(form){window.location.href = "http://m.search.daum.net/mobile/search?q="+form.query.value+"&nil_profile=section"} ).
setLimit( [5,0] ).
setForm( document.daumTotalSearchLayer );

/**
 * Daum Apps(iphone)에서 이벤트 발생시 호출되는 메소드
 *
 * 전체화면 전환 버튼 클릭시 발생하는 이벤트
 * @param params true/false
 */
function daumAppsFullScreen(param){
	try {
		// do something
	} catch (e) {}
}

/**
 * Geolocation function
 * @param sCallback 성공시 실행될 callback
 * pos.coords.longitude , pos.coords.latitude
 * @param eCallback 에러 또는 위치정보 사용거부후 실행되는 callback
 * error.code , error.message
 */
function getGeoLocation(sCallback,eCallback){
	if(typeof navigator.geolocation == "object"){
		navigator.geolocation.getCurrentPosition(
			function(POS){
				if(typeof sCallback == "function"){
					sCallback(POS);
				}
			}
		,eCallback);
	}else{
		alert("해당 모바일기기 또는 브라우져에서 geolocation기능을 지원하지 않습니다.");
	}
}



/**
 * Viewport 의 content 의 값을 Json Object 로 반환한다.
 * @return retValue json
 */
function getViewportInfo(){
	var metas = document.getElementsByTagName("META");
	var retValue = {};
	for(var i = 0 ; i < metas.length ; i++){
		if(metas[i].name == "viewport"){
			var tmp =  metas[i].getAttribute("content").split(",");
			for(var j = 0 ; j < tmp.length ; j++){
				retValue[tmp[j].split("=")[0].replace(/^\s\s*/, '').replace(/\s\s*$/, '')] = tmp[j].split("=")[1];
			}
			retValue["length"] = tmp.length;
		}
	}
	return retValue;
}


// Orientation change event
var supportsOrientationChange = "onorientationchange" in window
var orientationEvent = supportsOrientationChange ? "orientationchange" : (("resize" in window) ? "resize" : null);
if(orientationEvent != null ){ // orientchange event 및 resize  를 사용가능한 디바이스
	// jigu 의 addEvent
	daum.Event.addEvent(window, orientationEvent, updateOrientation);
}else{ // polaris
	window.setInterval(updateOrientation, 100);
}

/**
 * Orientation 변경시 수행되는 작업.
 * 가로, 세로 모드 별개의 UI를 위해 class 에 orientation 변경 (polaris 에서 [attr] selector 동작안하기 때문 class 사용)
 * body.portrait .btn_close{...}
 * body.landscape .btn_close{...}
 * @return
 */
function updateOrientation(){
	var orient = getOrinetation();
	// 가변 레이아웃을위한 body class 세팅
	setOrinetation("class",orient);

	if(orient == "portrait"){
		// portrait
	}else{
		// landscape
	}

}

/**
 * 해당 모바일 기기의 현재 Orientation 을 반환한다.
 * window.orientation가 있으면 orient체크(safari,android)
 * opera 는 client 사이즈가 고정이므로 screen 사이즈로 비교, 나머지는 client 사이즈로 비교
 * @return orient portrait/landscape
 */
function getOrinetation() {
	var sWidth = window.screen.width;
	var sHeight = window.screen.height;
	var cWidth = document.documentElement.clientWidth;
	var cHeight = document.documentElement.clientHeight;
	var orient = "landscape";

	if(window.orientation){
		switch(window.orientation){
			case 0:
			case 180:
				orient = "portrait";
				break;
		}
	}else if(daum.Browser.opera){
		if(sWidth < sHeight){
			orient = "portrait";
		}
	}else{
		if(cWidth < cHeight){
			orient = "portrait";
		}
	}

	return orient;
}

/**
 * document 의 body 에 속성 name 에 orient 값을 넣는다.
 * @param orient
 */
function setOrinetation(name,orient){
	var body = document.body ? document.body : document.documentElement;
	body.setAttribute(name, orient);
}

