Mys.Profile = function(config)
{
	Ext.apply(this,config);
	this.initialize();
};

Mys.Profile.prototype = {
	defaultGroupCount : 4,
	defaultFriendCount : 12,
	listTemplate : new Ext.Template('<div class="item">' +
										'<div class="iconBox_1 userIcon">' +
											'<a {logUrl} onclick="return false">' +
												'<img src="{imgSrc}" class="userImage" alt="이미지" width="40"/>' +
												"<span class='group'>{groupFlag}</span>"+
												"<span class='domain'>{mydomain}</span>" +
												"<span class='userId'>{userId}</span>" +
											'</a>' +
										'</div>' +
										'<p><a {logUrl}>{name}</a><br/><br/>{date}</p>' +
									'</div>'),

	initialize : function(){

		this.friendListEl = Ext.get("userFriendList");
		this.groupListEl = Ext.get("userGroupList");

		Ext.get("detailInfo").select(".item").on("click", this.onClickChannel, this);
		this.insertList(this.defaultGroupCount, "group");
		this.insertList(this.defaultFriendCount, "friend");
	},

  onClickChannel : function(e, el)
  {
    if(el.className != "item") return;
    var str = el.innerHTML;
    window.str = str;
    str = str.indexOf("(")>0 ? str.substring(0,str.indexOf("(")).trim() : str.trim();
    Global.modelGroup.createChannel(str, this.cbCreateChannel, this);
  },

  cbCreateChannel : function(data)
  {
    window.open("http://"+data.response.domain+".tinidiary.com");
//    window.open("http://"+data.response.domain+"."+DOMAIN);
  },

	insertList : function(cnt, type){
		var listEl, listData,start, end;
		switch(type){
			case "group" :
				listEl = this.groupListEl;
				listData = this.groupListData;
				break;

			case "friend" :
				listEl = this.friendListEl;
				listData = this.friendListData;
		}
		if(!listData || !listData.total){ listEl.remove(); return; }

		start = 0;
		end = listData.total < cnt?listData.total:cnt;

		listEl.child(".count").update("("+listData.total+")");


		listEl.child(".showPrev a").on("click", this.showPrevList, this, {type:type});
		listEl.child(".showNext a").on("click", this.showNextList, this, {type:type});

		this.showList(type, start, end);
	},

	showPrevList : function(el, evt, opt){
		var start, end, cnt;
		switch(opt.type){
			case "group" :
				cnt = this.defaultGroupCount;
				start = this.currentGroupStart;
				break;
			case "friend" :
				cnt = this.defaultFriendCount;
				start = this.currentFriendStart;
				break;
		}

		if(start == 0)
			return;

		end = start;
		start -= cnt;
		this.showList(opt.type, start, end);
	},

	showNextList : function(el, evt, opt){
		var start, end, cnt, total, leftCnt;
		switch(opt.type){
			case "group" :
				cnt = this.defaultGroupCount;
				start = this.currentGroupStart;
				total = this.groupListData.total;
				break;
			case "friend" :
				cnt = this.defaultFriendCount;
				start = this.currentFriendStart;
				total = this.friendListData.total;
				break;
		}

		leftCnt = total - (start+cnt);

		if(leftCnt <= 0)
			return;

		start += cnt;

		end = (leftCnt <= cnt)?start+leftCnt : start+cnt;

		this.showList(opt.type, start, end);
	},

	showList : function(type, start, end){
		var listData, i, dd, tplData, rowNum, total, imgSrc, date, query = "";

		switch(type){
			case "group" :
					listData = this.groupListData.group;
					listEl = this.groupListEl;
					total = this.groupListData.total;
					rowNum = 2;
					this.currentGroupStart = start;
					break;
			case "friend" :
					if(this.cafe)
						listData = this.friendListData.member_profile; //카페와 일반나눔
					else
						listData = this.friendListData.friend;
					total = this.friendListData.total;
					listEl = this.friendListEl;
					rowNum = 3;
					this.currentFriendStart = start;
					break;
		}

		listEl.child(".list").update("");
		for(i = start; i < end; ++i){
			imgSrc = listData[i].mylogo?listData[i].mylogo:"../images/icon/defaultImage.gif";

			if(this.cafe){
				name = listData[i].realname;
				date = listData[i].name;
			}
			else{
				name = listData[i].name;
				if(listData[i].last_add_date){
					dd = Date.parseDate(listData[i].last_add_date, "Y-m-d G:i:s");
					date = dd.dateFormat("Y-m-d");
				}
				else{
					date = "";
				}
			}


			//그룹에도 링크 걸리도록, 이름에도 링크걸리도록
			//tplData = {imgSrc:imgSrc, name: name, date: date, logUrl : type=="friend"?"href=http://"+listData[i].mydomain+"."+DOMAIN:"onclick='return false'"};
			tplData = {mydomain:encode64(listData[i].mydomain), userId : encode64(listData[i].user_id), imgSrc:imgSrc, name: name, date: date, groupFlag : (type == "group")?1:0, logUrl : "href=http://"+listData[i].mydomain+"."+DOMAIN};

			query += this.listTemplate.apply(tplData);
		}

		query += "<div class='clear'></div>";
		var list = listEl.child(".list");

		var line = Math.ceil((end - start) / rowNum);

		list.insertHtml("beforeEnd", query);
		list.setStyle("height", line*71);


		var prevEl = listEl.child(".showPrev");
		var nextEl = listEl.child(".showNext");

		(start == 0)?prevEl.setStyle("visibility","hidden") : prevEl.show();

		(end == total)?nextEl.setStyle("visibility", "hidden") : nextEl.show();

		Mys.LayerNavi.set();
	}

};
