/* 커뮤니티전용 */
var commu = {};

commu.yScrollingBtn=function(cid, millisec1,millisec2, speed, height, btnprevid, btnnextid)
{
	this.cid= cid;
	this.millisec1 = millisec1;	//처음시작 텀
	this.millisec2 = millisec2; //실행 텀
	this.speed = speed;			//한번에 이동할 px
	this.height = height;

	this.h =0;
	this.div = document.getElementById(this.cid);

	var btnup = document.getElementById(btnprevid);
	var btndown = document.getElementById(btnnextid);

	this.htmltxt = this.div.innerHTML;
	this.div.innerHTML = this.htmltxt+this.htmltxt;
	this.div.isover=false;
	this.div.onmouseover=function(){ this.isover=true; }
	this.div.onmouseout=function(){	this.isover=false; }
	this.div.scrollTop=0;

	var self = this;
 
	if(btnup) {
		btnup['onmouseover'] =function(){ self.div.isover=true; }
		btnup['onmouseout'] =function(){ self.div.isover=false; }
		btnup['onclick'] = this.yUp.bind(this);
	}

	if(btndown) {
		btndown['onmouseover'] =function(){ self.div.isover=true; }
		btndown['onmouseout'] =function(){ self.div.isover=false; }
		btndown['onclick'] = this.yDown.bind(this);
	}
 
	window.setTimeout(this.play.bind(this), this.millisec1);
}

commu.yScrollingBtn.prototype={
	play : function(){
		var self =this;
 
		if(!this.div.isover)
		{
			if(this.div.scrollTop >= this.div.scrollHeight/2)
			{
				this.div.scrollTop=0;
			}
			else(this.height)
			{
				
				this.h += this.speed;
				this.div.scrollTop += this.speed;
 
				if(this.h>=this.height) {
					if(this.h>this.height) {
						this.div.scrollTop -= this.h % this.height;
					}
					this.h=0;
					window.setTimeout(this.play.bind(this), this.millisec1);
					return;
				}
			}
		}

		window.setTimeout(this.play.bind(this), this.millisec2);
	},
	yUp : function() {
		if(this.div.scrollTop >= this.div.scrollHeight/2) {
			this.div.scrollTop=0;
			this.h=0;
		} else if (this.height) {
			if (this.h == this.height) {
				this.h = 0;
			}

			this.div.scrollTop += this.speed;
			this.h += this.speed;

			if(this.h >= this.height) {
				if(this.h>this.height) {
					this.div.scrollTop -= this.h % this.height;
				}
				this.h=0;
				return;
			}
		}
		window.setTimeout(this.yUp.bind(this), this.millisec2);
	},
	yDown : function() {
		if (this.div.scrollTop <= 0 ) {
			this.div.scrollTop = this.div.scrollHeight/2;
			this.h = this.height;
		} else if (this.height) {
			if(this.h == 0 ) {
				this.h = this.height;
			}
			this.div.scrollTop -= this.speed;
			this.h -= this.speed;
			if (this.h <= 0) {
				if(this.h<0) {
					if(this.div.scrollTop != 0) {
						this.div.scrollTop -= this.h;
					}
				}
				this.h = this.height;
				return;
			}
		}
		window.setTimeout(this.yDown.bind(this), this.millisec2);
	}
};

var callTypeId = "QDC";

commu.getPopularQnaClick = function(type, ele){
	callTypeId = type;
	// 이미 불러 온 데이타가 있는 경우 다시 call 하지 않음
	if(daum.$(ele).innerHTML != "" ) return; 
	
	var callUrl = "/community/qna/popular.json?maxLength=36&category_id=" + type + "&element=" + ele;
	finance.callSrc("qnaList", callUrl);
}

commu.getTopQnaList = function(_data){
	var cnList = "themeList";
	if( _data.qnatype == "popular"){
	 	cnList = "counselList";
	}		
	var ulTemp = '<ul class="topList ' + cnList + '">$li</ul>'; 
	var moreTemp = '<a href="http://finance.daum.net/community/consult.daum?category_id=' + callTypeId + '" class="btnSmallMore">더보기</a>';
	var liTemp = '<li><a href="$url" title="$title">$cutTitle</a>&nbsp;<span class="reply">답변<strong>$answerCount</strong></span><span class="writer">$nickname</span></li>';
	var result = "";
	var element = _data.element ; 
 	var list = _data.list; 
	for( var i=0; i< list.length; i++){
		var temp = liTemp ;
 		temp = temp.replace(/\$url/gi, list[i].url);
		temp = temp.replace(/\$title/gi, list[i].title);
/*
		if(i==0) {
			temp = temp.replace(/\$cutTitle/gi, "<b>" + list[i].cutTitle + "</b>");
		} else {
*/
			temp = temp.replace(/\$cutTitle/gi, list[i].cutTitle);
//		}
		temp = temp.replace(/\$nickname/gi, list[i].nickname);
		temp = temp.replace(/\$answerCount/gi, list[i].answerCount);
		result += temp; 
	}
	
	daum.$(element).innerHTML = ulTemp.replace(/\$li/gi, result) + moreTemp; 

/*
	var cnList = "themeList";
	if( _data.qnatype == "popular"){
	 	cnList = "counselList";
	}		

	var result = "";
	var element = _data.element ; 
 	var list = _data.list; 
	result = '<ul class="topList ' + cnList + '">' ; 
	for( var i=0; i< list.length; i++){
		var temp = '<li><a href="'+list[i].url+'" title="'+list[i].title+'">'+list[i].cutTitle+'</a>&nbsp;<span class="reply">답변<strong>'+list[i].answerCount+'</strong></span><span class="writer">'+list[i].nickname+'</span></li>'
		result += temp; 
	}
	daum.$(element).innerHTML = result + '</ul>'
	*/

}
 

commu.getThemeQnaClick = function(type, ele){
	// 이미 불러 온 데이타가 있는 경우 다시 call 하지 않음
	if(daum.$(ele).innerHTML != "" ) return; 

	var callUrl = "/community/qna/recent.json?service=rich&type=invest&item_id=" + type + "&element=" + ele;
	finance.callSrc("themeQnaList", callUrl);
}
