/* test test last */
if(!window.daum) window.daum = {};
if(!window.daum.Life) daum.Life = {};
window.Life = daum.Life;

var pre_id="l_2";//켜진거

Life.topMouseover = function(id){

	var backgroundNone = id.charAt(id.length-1);
	var overNumber = parseInt(backgroundNone);
	overNumber--;
	
	
	for(var i=1; i<5; i++){
		
		if(overNumber!=0&& i==overNumber){
			var preOverId = "s_"+overNumber;
			daum.$(preOverId).style.background = "none";
		}
		else{
			daum.$("s_"+i).style.background="url('http://i1.daumcdn.net/localimg/life/images/top/ln_t_sero1.gif') no-repeat right bottom";
		}
	}
	document.getElementById(pre_id).style.display="none";
	var small_id=pre_id.replace("l","s");
	document.getElementById(small_id).style.display="inline";

	var select = document.getElementById(id);
	select.style.display="none";
	var replace_id = id.replace("s","l");
	document.getElementById(replace_id).style.display="inline";
		
	pre_id=replace_id;
			
}


Life.ToggleAction = function( btnToggleId, options ){
	this.btnToggle = daum.$(btnToggleId);
	this.options = {
		isShow: false,
		showCallbackFunc: null,
		hideCallbackFunc: null
	};
	daum.extend( this.options, options );
	this._initialize();
};
Life.ToggleAction.prototype = {
	_initialize: function(){
		if( this.btnToggle ){
			this._bindEvent();
			this.flag = this.options.isShow;

			if( this.flag ){
				this.show();
			} else {
				this.hide();
			}
		}
	},
	_bindEvent: function(){
		this.bind( this.btnToggle );
	},
	bind: function( id ){
		daum.addEvent( daum.$(id), "click", function(e){
			this.toggle();
			daum.stopEvent(e);
			daum.preventDefault(e);
			return false;
		}.bind(this));
	},
	show: function(){
		if( this.options.showCallbackFunc && typeof this.options.showCallbackFunc == "function" ){
			this.options.showCallbackFunc(this.btnToggle);
		}
		this.flag = true;
	},
	hide: function(){
		if( this.options.hideCallbackFunc && typeof this.options.hideCallbackFunc == "function" ){
			this.options.hideCallbackFunc(this.btnToggle);
		}
		this.flag = false;
	},
	toggle: function(){
		if( this.flag ){
			this.hide();
		} else {
			this.show();
		}
	}
};
/*
 * RollOver Class
**/
Life.RollOver = function(options) {
	daum.extend(this,options);
	this.init();
}
Life.RollOver.prototype = {
	init : function() {
		for(var i=0,cnt=this.target.length;i<cnt;++i){
			daum.addEvent(this.target[i],"mouseover",this.over.bind(this,i));
			daum.addEvent(this.target[i],"mouseout",this.out.bind(this,i));
		}
	},
	over : function(idx) {
		daum.addClassName(this.target[idx],this.overCss);
	},
	out : function(idx){
		daum.removeClassName(this.target[idx],this.overCss);
	}
};
/*
 * Radio Class
**/
Life.Radio = function(panel, options){
	this.panel = daum.$(panel);
	
	this.options = {
		basicClassName : "radio",
		activeClassName : "radioOn",
		onchange : function(){}
	};
	daum.extend(this.options, options || {});
	
	this.draw();
};
Life.Radio.prototype = {
	draw : function(){
		this.radios = this.getRadioElements();
		
		this.createImageRadio();		
		
		var checkedRadio = [];
		for(var i=0,loop=this.radios.length; i<loop; i++){			
			this.radios[i].label = daum.Element.getNext(this.radios[i].dom);
			this.radios[i].custom = this.element.cloneNode(false);
			this.radios[i].custom.radioIndex = i;
			
			this.radios[i].label.parentNode.insertBefore(this.radios[i].custom, this.radios[i].label);
			
			if(this.radios[i].dom.checked){
				checkedRadio.push(this.radios[i]); 
			}
			
			this.bindEvent(this.radios[i]);
		}	
		
		if(checkedRadio.length > 0){
			checkedRadio.each(function(r){
				r.custom.className = this.options.activeClassName;
				this.options.onchange.call(this, r);
			}.bind(this));			
		}
	},
	
	bindEvent : function(radio){
		radio.label.onclick = radio.custom.onclick = this.onCustomRadioClick.bindAsEventListener(this, radio.custom.radioIndex);
	},
	
	onCustomRadioClick : function(e, index){
		var CustomRadio = this.radios[index];
		
		var radioName = CustomRadio.dom.name;
		CustomRadio.dom.checked = true;
		
		for(var i=0,loop=this.radios.length; i<loop; i++){
			if(this.radios[i].dom.name === radioName){
				if(this.radios[i].dom !== CustomRadio.dom){
					this.radios[i].custom.className = this.options.basicClassName;
				}else{
					this.radios[i].custom.className = this.options.activeClassName;
				}
			}
		}
		
		this.options.onchange.call(this, CustomRadio);
	},
	
	createImageRadio : function(){
		this.element = document.createElement("span");
		this.element.className = this.options.basicClassName;		
	},
	
	getRadioElements : function(){
		var input = this.panel.getElementsByTagName("input");
		
		var radios = [];
		for(var i=0,loop=input.length; i<loop; i++){
			if(input[i].type.toLowerCase() == "radio"){
				radios.push({
					dom : input[i]
				});
			}
		}
		return radios;
	},
	
	clear : function(){
		for(var i=0,loop=this.radios.length; i<loop; i++){			
			this.radios[i].dom.checked = false;
			this.radios[i].custom.className = this.options.basicClassName;			
			this.radios[i].label.className = this.radios[i].label.className.replace("On","");
		}
	}
};
/*
 * Pagination Class  
**/
Life.Pagination = function(options) {
	daum.extend(this, options);
	this.init();
};
Life.Pagination.prototype = {
	init : function() {
		//this.list = daum.$T((this.listTag) ? this.listTag : 'li',daum.$(this.listId));
		this.list = daum.getChildElements(daum.$(this.listId));
		this.totalCount = this.list.length;
		this.totalPage = Math.ceil(this.totalCount/this.maxCount);
		this.needPaging = (this.totalCount > this.maxCount);
		
		this.prevBt = daum.$(this.prevBtId);
		this.nextBt = daum.$(this.nextBtId);
		if(this.prevBt) daum.addEvent(this.prevBt,"click",this.prev.bind(this));
		if(this.nextBt) daum.addEvent(this.nextBt,"click",this.next.bind(this));
		
		if(!this.needPaging) {
			if(this.prevBt && this.prevBtDisabled) daum.addClassName(this.prevBt, this.prevBtDisabled);
			if(this.nextBt && this.nextBtDisabled) daum.addClassName(this.nextBt, this.nextBtDisabled);
		}
		else {
			if(this.prevBt && this.prevBtDisabled) daum.removeClassName(this.prevBt, this.prevBtDisabled);
			if(this.nextBt && this.nextBtDisabled) daum.removeClassName(this.nextBt, this.nextBtDisabled);
		}
		this.curPage = 1;
		this.exec(1);
	},
	prev : function() {
		this.exec(--this.curPage);
	},
	next : function() {
		this.exec(++this.curPage);
	},
	exec : function(n) {
		if(n<=0)  this.curPage = this.totalPage;
		else if(n>this.totalPage)  this.curPage = 1;

		var min = this.maxCount*(this.curPage-1), max = this.maxCount*this.curPage;
		for(var i=0;i<this.totalCount;++i) {
			if(min <= i && i < max) this.list[i].style.display = "block";
			else this.list[i].style.display = "none";
		}
	}
};
/*
 * IconPaging Class  
**/
Life.IconPaging = function(options, tagname) {
	daum.extend(this,options);
	this.init(tagname);
}
Life.IconPaging.prototype = {
	init : function() {
		for(var i=0,cnt=this.target.length;i<cnt;++i){
			daum.addEvent(this.target[i],"click",this.goPage.bind(this,i))
		}
		this.on(0);
	},
	
	goPage : function(idx){
		this.on(idx);
	},
	
	on : function(idx) {
		for(var i=0,cnt=this.target.length;i<cnt;++i){
			if(i==idx) daum.addClassName(this.target[i],this.onCss);
			else daum.removeClassName(this.target[i],this.onCss);
		}
		if(!this.list) return false;
		for(var i=0,loop=this.list.length; i<loop; i++){
			if(i == idx) this.list[i].style.display = "block";
			else this.list[i].style.display = "none";
		}
	},
	
	setPage : function(page){
		this.on(page-1);		
	},
	
	setPageSize : function(size){
	}
}
/*
 * IconNumPaging Class  
**/
Life.IconNumPaging = function(option, tagname){
	this.$super(option || {}, tagname);
}.inherit(Life.IconPaging).members({
	init : function(tagname) {		
		this.list = daum.$T(tagname || "li",daum.$(this.listId));
		this.$super();
	},
    on : function(idx){
		for(var i=0,cnt=this.target.length;i<cnt;++i){
			var css = this.target[i].className;
			if(i==idx) this.target[i].className = css.replace("normal_","current_");
			else this.target[i].className = css.replace("current_","normal_");
		}			
		for(var i=0,cnt=this.list.length;i<cnt;++i) {
			if(i==idx) this.list[i].style.display = "";
			else this.list[i].style.display = "none";
		}
    }
});
/*
 * ListBarPaging Class  
**/   
Life.ListBarPaging = function(options) {
	daum.extend(this,options);	
}
Life.ListBarPaging.prototype = {
	template : '<button type="button" class="imgbutton"><span>#{i}페이지 보기</span></button>',
	
	init : function(){
		this.panel.innerHTML = "";
		for(var i=0,loop=this.pagesize; i<loop; i++){		
			var div = document.createElement("div");
			div.innerHTML = this.template.replace("#{i}",i+1);
			var btn = daum.Element.getFirstChild(div);
			this.panel.appendChild(btn);
		}
		  
		this.target = daum.$T("button",this.panel);
		
		for(var i=0,cnt=this.target.length;i<cnt;++i){
			daum.addEvent(this.target[i],"click", this.goPage.bind(this, i));
		}
		this.on(0);
	},
	
	goPage : function(idx){
		this.on(idx);
	},
	
	on : function(idx) {		
		for(var i=0,cnt=this.target.length;i<cnt;++i){
			if(i==idx) daum.addClassName(this.target[i],this.onCss);
			else daum.removeClassName(this.target[i],this.onCss);
		}
	},
	
	setPage : function(page){
		this.on(page-1);		
	},
	
	setPageSize : function(size){
		this.pagesize = parseInt(size, 10);
		this.init();
		
		if(this.onInitialize) this.onInitialize();
	}
}
/*
 * CheckBytes Library
**/
Life.CheckTextBytes = function(){
	var loginMessage = "먼저 로그인 하셔야 합니다.\n로그인 페이지로 이동 하시겠습니까??";
    return function( field, bytes, totalBytes, isLogin ){
    	if( isLogin ){
	    	daum.addEvent( field, "keyup", function(ev){
				method.cutStr();
	    		method.syncByte();
	    	});
    	} else {
    		daum.addEvent( field, "focus", function(ev){
    			var loginURL = "http://login.daum.net/accounts/loginform.do?url=";
    			if(confirm( loginMessage )){
    	    		top.location = loginURL + escape (parent.document.location.href);
    	   		}
    			field.blur();
	    	});
    	}
    	
    	var method = {
    		cutStr: function(){
				var nowBytes = field.value.byteLength();
				if( nowBytes > totalBytes ){ 
    				var cutText = daum.String.cutString(field.value,  totalBytes - 1);
					alert( "댓글을 "+totalBytes+"byte 미만으로 입력해주시기 바랍니다." );
    				field.value = cutText;
				}
    		},
    		syncByte: function(){
    			var nowBytes = field.value.byteLength();
    			bytes.innerHTML = nowBytes;
    		}
    	};
		
		method.syncByte();
    	
    	return method;
    };
}();

/*
 * ImageModalViewer Class
**/

Life.ImageModalViewer = function(url) {
	var e, d = document.documentElement, s,
		h = Math.max(d.scrollHeight, d.clientHeight); // innerHeight
	
	// We need a *masked* <iframe> to kick <select>s out in IE6
	e = document.createElement('iframe');
	s = e.style;
	s.position = 'absolute';
	s.zIndex = '99999';
	s.top = s.left = '0';
	s.width = '100%';
	s.height = h + 'px';
	s.border = '0';
	s.filter = 'mask()';
	document.body.appendChild(e);	

	e = document.createElement('div');
	e.onclick = function() {
		var p = this.parentNode;
		p.removeChild(this.previousSibling);
		p.removeChild(this.nextSibling);
		p.removeChild(this); // must be last
	};
	s = e.style;
	s.position = 'absolute';
	s.zIndex = '99999'; // or more?
	s.top = s.left = '0';
	s.width = '100%';
	s.height = h + 'px';
	s.overflow = 'hidden';
	s.backgroundColor = '#000';
	s.opacity = '0.7';
	s.filter = 'alpha(opacity=70)';
	document.body.appendChild(e);
	
	e = document.createElement('img');
	e.onload = function() {
		// have it centered
		this.style.marginLeft = -(this.width >> 1) - 10 + 'px';
		this.style.marginTop = -(this.height >> 1) - 10 + 'px';
	};
	e.onclick = function() {
		var p = this.parentNode;
		p.removeChild(this.previousSibling); // intended
		p.removeChild(this.previousSibling);
		p.removeChild(this); // must be last
	};
	e.src = url;
	s = e.style;
	s.position = 'absolute';
	s.zIndex = '99999';
	s.top = d.scrollTop + (d.clientHeight >> 1) + 'px';
	s.left = '50%';
	s.border = '10px solid #fff';
	document.body.appendChild(e);

	e = d = s = h = undefined;
};
/*
 * Life Login
**/
Life.Comment = {
	loginConfirm : function(){
	  	if(confirm("먼저 로그인 하셔야 합니다.\n로그인 페이지로 이동 하시겠습니까?")){
    		location.href = "http://login.daum.net/accounts/loginform.do?url=" + encodeURIComponent(document.location);
    	}
	},
	
	focusComment : function(target, islogin){	
    	if(islogin){    
    		target.style.backgroundImage = "none";
    	}else{
	    	Life.Comment.loginConfirm();
	    	target.blur();
    	}    
	}	
};
/*
 * Life Place CategoryList  
**/
Life.PlaceNearCategory = {
	"isCalling" : false,
	"food" : {
		url : "/place/api/PopularityAPI.do?dvsn=02",		
		data : []
	},
	"good" : {
		url : "/place/api/PopularityAPI.do?dvsn=01",				
		data : []
	},
	"public" : {
		url : "/place/api/PopularityAPI.do?dvsn=04",		
		data : []
	},
	"food_with_line" : {
		url : "/place/api/LinePopularityAPI.do?&dvsn=01",		
		data : []
	},
	"good_with_line" : {
		url : "/place/api/LinePopularityAPI.do?&dvsn=03",				
		data : []
	},
	"accommo_with_line" : {
		url : "/place/api/LinePopularityAPI.do?&dvsn=02",
		data : []
	},
	
	init : function(confirmid){
		this.confirmid = confirmid;		
		this.makeUrl();		
	},
	
	makeUrl : function(){
		var category = ["food", "good", "public", "food_with_line", "good_with_line", "accommo_with_line"];
		for(var i=0,loop=category.length; i<loop; i++){
			this[category[i]].url = Life.ContextPath + this[category[i]].url + "&confirmid="+ this.confirmid;
		};
	},
	
	request : function(category, callback){
		var callback = callback || function(){};
		this.ajax = this.ajax || {};
		if(!this.ajax[category]){
			this.ajax[category] = new daum.Ajax({
				url : this[category].url,
				method : "POST",
				timeout : 2000,
				onsuccess : this.getData.bind(this, category, callback),
				onfailure : callback
			});
		}
		if(!this[category].loaded){
			this.ajax[category].request(this[category].url, {
				onsuccess : this.getData.bind(this, category, callback),
				onfailure : callback
			});
		}else{
			this.getData(category, callback);
		}
	},

	getData : function(category, callback, xhr){
		if(xhr){
			var data = eval("("+ xhr.responseText +")").placelist;
			
			for(var i=0,loop=data.length; i<loop; i++){
				data[i].CONTEXT_PATH = Life.ContextPath;
				data[i].cplacename = data[i].cplacename || ""; 
				data[i].placename_full = data[i].placename + data[i].cplacename;
				data[i].placename_short = daum.String.cutString(data[i].placename_full, 34, "..");
				data[i].placeurl = Life.ContextPath +"/place/Top.do?confirmid="+ data[i].confirmid; 
				data[i].reviewurl = Life.ContextPath +"/place/ReviewAllList.do?confirmid="+ data[i].confirmid;
				data[i].photourl = Life.ContextPath +"/place/PhotoList.do?confirmid="+ data[i].confirmid;
				if(daum.maps) data[i].point = new daum.maps.Point(data[i].pointx, data[i].pointy);
				data[i].enc_placename = encodeURIComponent(data[i].placename);					
			};
			
			this[category].data = data;
			this[category].data.placeType = "PLACE_"+ category.toUpperCase();
			this[category].loaded = true;
		}
		callback(this[category].data);
	}	
};
/*
 * Life Place NearList
**/
Life.PlaceNearList = {
	list_template : new daum.Template('<li><a href="#{CONTEXT_PATH}/place/Top.do?confirmid=#{confirmid}" title="#{placename_full}">#{placename_short}</a></li>'),
	
	init : function(){
		this.category = ["food", "good", "public", "food_with_line", "good_with_line", "accommo_with_line"];
		
		this.model = Life.PlaceNearCategory;
		this.tabs = daum.$("nearPlace").getElementsByTagName("h5");
		this.lists = daum.$("nearPlace").getElementsByTagName("ul");
		
		/*
		 * 페이지 로딩 시 초기 right near 호출.
		 * 기존에는 0번 food를 호출하면 되지만, line정보가 들어있는 경우 3번 food_with_line을 호출해야됨. 
		 */
		this.startIndex = (this.tabs[0].style.display === 'none') ? 3 : 0;
		this.show(this.startIndex);

//		this.bindEvent();
					
	},

	bindEvent : function(){
		for(var i=0,loop=this.tabs.length; i<loop; i++){
			this.tabs[i].onmouseover = this.show.bind(this, i);
		}
	},

	show : function(idx){
		for(var i=0,loop=this.lists.length; i<loop; i++){
			if(idx == i){
				daum.Element.addClassName(this.lists[i], "on");
				daum.Element.addClassName(this.tabs[i], "on");
			}else{
				daum.Element.removeClassName(this.lists[i], "on");
				daum.Element.removeClassName(this.tabs[i], "on");
			}
		}
				
		this.request(this.category[idx]);
	},
	
	request : function(category){
		if(!this.model[category].loaded){
			this.model.request(category, this.draw.bind(this, category));
		}else{
			this.draw(category, this.model[category].data);
		}
	},

	draw : function(category, data){
		var panel = this.lists[this.startIndex], header = this.tabs[this.startIndex];
		
		for(var i=this.startIndex,loop=this.lists.length; i<loop; i++){
			if(daum.Element.hasClassName(this.lists[i], category)){					
				panel = this.lists[i], header = this.tabs[i];
				break;
			}
		};
		if(panel.getElementsByTagName("li").length == 0){
			if(data.length > 0){
				var listStr = [];
				for(var i=0,loop=data.length; i<loop; i++){					
					listStr.push(this.list_template.evaluate(data[i]));
					if(i > 8) break;
					/*리스트는 10개만 노출.*/
				}
				panel.innerHTML = listStr.join("");
			}else{
				panel.innerHTML = '<li class="noDesc">'+ this.getNoDataMessage(category) +'<\/li>';								
			}
		}
		if(!Life.PlaceNearCategory.isCalling){
			this.bindEvent();
			Life.PlaceNearCategory.isCalling = true;
		}
	},

	getNoDataMessage : function(category){
		switch(category){
			case "food" :
				return "주변에 인기 맛집이 없습니다."; break;
			case "good" :
				return "주변에 유명한 장소가 없습니다."; break;
			case "public" :
				return "주변에 유명한 장소가 없습니다."; break;
			case "food_with_line" :
				return "주변에 인기 맛집이 없습니다."; break;
			case "good_with_line" :
				return "주변에 유명한 장소가 없습니다."; break;
			case "accommo_with_line" :
				return "주변에 잘데가 없습니다."; break;
		};
	}
};

Life.EventView = {
	init : function(){
		if(!$('dongIcon')){
			return;
		}
		if(daum.getCookie('dong'))
			this.hide_view();
		else{
			this.display_view();
		}
	},
	
	display_view : function(){
		if($('dongIcon')){
			$('dongIcon').style.display = "block";
		}
		
	},
	hide_view : function(){
		$("dongIcon").style.display="none";
	},
	
	del_view : function(){
		if(confirm("24시간동안 안보이길 원하세요?")){
			daum.Browser.setCookie("dong", "N", 1);   
			this.hide_view();
		}else{
			this.hide_view();
			return;
		}
		this.hide_view();
	}
};

/**
 * iframe element를 받아서 그 안의 document.body 사이즈를 바탕으로 iframe의 height를 변경시킨다.
 * @iframeElm iframe에 해당하는 element
 */
Life.resizeIframe = function(iframeElm){
    if( iframeElm.tagName != 'IFRAME' && iframeElm.tagName != 'iframe' || iframeElm.src.length == 0) return;
	iframeElm.style.height = (iframeElm.contentWindow.document.body.offsetHeight) + "px";
}

/* 업데이트된 jigu와 map2와 충돌로 예상되는 문제를 해결하기 위해 cutstring 재정의*/
daum.String.cutString = function(s, limit, suff){
	var suffix = suff || "",
		_limit = limit - suffix.length,
		_byte = 0,
		_str = '',
		val, i;
	for(i=0;i<s.length;i+=1){			
		if(_limit>0){_str+=s.charAt(i);}			
		val = escape(s.charAt(i)).length;
		if(val>3){ _byte++; _limit--; }
		_byte++; _limit--;
	}
	_str+=suffix;
	return (limit >= _byte) ? s : _str; 
};

String.prototype.cutString = function(limit, stuff){
	return daum.String.cutString(this,limit,stuff);
}
