﻿/*
ID: feedlist.js
History
	0.1(2010-12-19) 파일생성 - maninblu

Description
	피드 리스팅 컨트롤
	
	interface
		$( {피드 리스트 영역} ).feed( options );
	
	options
		
Dependency
	jquery.js(1.4.3)
	jquery.ui.js(1.8.5)


Comment
	public enum FeedTypeCode : byte
	{
		All = 0,
		Chat = 1,
		Photo = 2,
		Link = 3,		
		Movie = 4,
		Blog = 5,
		Comment = 11,
		ProfileName = 31,
		Achievement = 101,
		GameFeed = 102,

	}
*/
(function($) {

    $.widget("ui.feed", {
        options: {
            FeedGroup: 0 // 0:일반피드, 1스타피드, 2즐겨찾기, 3인기챗, 4태그챗, 5게임기록, 6사진/동영상
		, TargetType: 0
		, StarTargetType: 0 // 스타피드의 경우 0:전체, 1:영자, 2:스타
		, FeedType: 0
		, MinFeedSN: 0
		, MinDate: "0001-01-01"
		, HitType: 0
		, OpenLevel: 0
		, PageType: 1 // 1:home, 2:profile	3:gamecenter, 4:chatview 5:profile_main
		, PageSize: 10 // chat page size
		, ViewFeedSN: 0 // view 페이지인 경우
		, ViewCommentSN: 0 // view 페이지인 경우
		, GetListOnCreate: true // home 튜토리얼 때문
		, OwnerNexonSN: "" // home 튜토리얼 때문
		, TagString: ""// 테그검색으로 페이지로 넘어온 경우
	    , GameCode: 65536
	    , IsStatic: false
        }

	, tagData: {}
	, IsFilteringAutoFeedOn: false
	, IsBestPingSelected: false
	, feedTypeCode: 1
	, jsonData: ""
	, stopListing: false

	, minDate0: "0001-01-01 00:00:00.000"
	, minDate1: "0001-01-01 00:00:00.000"

	, $currentMenu: null

	, $textarea: null
	, $byteSec: null
	, $writerSec: null
	, $chatFilter: null
	, $autoFeedOnOff: null
	, $bestPingsPagerSec: null
	, $tagSec: null

	, $noneChatDivs: null
	, $noneChat1: null
	, $noneChat2: null
	, $noneChat3: null
	, $noneChat4: null
	, $noneChat5: null
	, $noneChat6: null
	, $noneChat7: null
	, $noneChat8: null
	, $noneChat9: null

	, $bestPingCount: 0
	, Tag: null

	, _create: function() {
	    var self = this, opts = self.options, $target = self.element;
	    self.tagData = $target.tagdata();
	    self.IsFilteringAutoFeedOn = self.tagData.IsFilteringAutoFeedOn;

	    self.$writerSec = $("#d_chatWrite");
	    self.$noneChatDivs = $("#d_noChat1, #d_noChat2, #d_noChat3, #d_noChat4, #d_noChat5, #d_noChat6, #d_noChat7, #d_noChat8, #d_noChat9");
	    self.$chatFilter = $("#d_chatFilter");
	    self.$autoFeedOnOff = $("#d_autoFeedOnOff");
	    self.$bestPingsPagerSec = $("#d_pingGroupListPager");
	    self.$tagSec = $("#d_TagAdd");

	    self.$noneChat1 = self.$noneChatDivs.eq(0);
	    self.$noneChat2 = self.$noneChatDivs.eq(1);
	    self.$noneChat3 = self.$noneChatDivs.eq(2);
	    self.$noneChat4 = self.$noneChatDivs.eq(3);
	    self.$noneChat5 = self.$noneChatDivs.eq(4);
	    self.$noneChat6 = self.$noneChatDivs.eq(5);
	    self.$noneChat7 = self.$noneChatDivs.eq(6);
	    self.$noneChat8 = self.$noneChatDivs.eq(7);
	    self.$noneChat9 = self.$noneChatDivs.eq(8);

	    opts.OwnerNexonSN = $.ProfileUserInfo.NexonSN;

	    self.$bestPingsPagerSec.pager({
	        blockSize: 1
			, pageSize: 10
			, firstBtn: ""
			, lastBtn: ""
			, contents: 140
			, callback: function(pageNo) {
			    self.movePageBestPings(pageNo);
			}
	    });

	    // Init Options when it's default is not set;
	    if (opts.PageSize == 0) opts.PageSize = 10;

	    if (self.$writerSec.length) {
	        self.$writerSec.feedwriter(
	        {	
				GameCode : opts.GameCode
				,onCreationSucceeded: function(results, context) {				  
					if (opts.IsStatic) {
						$("div.d_Left0", $target).empty();
						self.getList(true);
					} else {
						$("#d_permalink").hide();
						self.$noneChatDivs.hide();
						var $newFeed = null;

						if (!$(".d_day0").is(":visible"))
							$(".d_day0").show();

						$("#d_noChat8").hide();
						if ($.browser.msie && $.browser.version < 8.0)
							$newFeed = $("div.d_Left0").prepend(results).find("div.feed").eq(0);
						else
							$newFeed = $("div.d_Left0").prepend(results).find("div.feed").eq(0).hide().slideDown();

						$newFeed.feedview();
					}
				}				
			}
	        );
	        self.$writerSec.feedwriter("setInitText", $("#contents").tagdata().ChatMsg);
	    }

	    $target.find("h2>a").each(function(index) {
	        $(this).click(function(event) {
	            self.toggleDay(index);
	            return false;
	        });
	    });

	    $("#d_btMoreList").click(function(event) {
	        if (opts.FeedGroup == 6) return false;
	        try {
	            if (opts.PageType == 1) {
	                $.sendWiselog("http://www.nexon.com/wl.aspx?home=btn_more");
	            } else if (opts.PageType == 2) {
	                $.sendWiselog("http://www.nexon.com/wl.aspx?profile=btn_more");
	            }
	        } catch (e) { }
	        if (opts.GameCode == 0) return false;
	        if (opts.GameCode == 65536) {
	            self.getList(false);
	        }
	        else {
	            self.getListByGameCode(false, opts.GameCode, self.Tag);
	        }
	    });

	    $("#d_btMoreMediaList").click(function(event) {
	        try {
	            /**
	            if (opts.PageType == 1) {
	            $.sendWiselog("http://www.nexon.com/wl.aspx?home=btn_more");
	            } else if (opts.PageType == 2) {
	            $.sendWiselog("http://www.nexon.com/wl.aspx?profile=btn_more");
	            }
	            **/
	        } catch (e) { }
	        if (opts.FeedGroup == 6)
	            self.getList(false);
	        else
	            return false;
	    });

	    opts.TagString = $("#contents").tagdata().TagString;
	    if (opts.TagString != undefined && opts.TagString.length > 0) {
	        opts.FeedGroup = 4;
	        /**
	        self.$chatFilter.find("ul.chatTab>li")
	        .removeClass("chatTab1Current")
	        .removeClass("chatTab2Current")
	        .removeClass("chatTab3Current");
	        self.$chatFilter.find(".chatTabSub, .pingGroupList").hide();
	        self.$chatFilter.find("ul.chatTab>li:eq(2)").addClass("chatTab3Current");
	        **/
	        self.Tag = opts.TagString;
	        //$("#d_tagSearchBtn").click();
	    } else {
	        if (opts.PageType == 4) {
	            self.getFeedInfo(opts.ViewFeedSN);
	        } else if (opts.PageType == 5) {
	            if (opts.GetListOnCreate)
	                self.getList(true);
	        } else {
	            if (opts.PageType == 1 && $.Cookie.get("IsBestPingDefault_" + $.LoginUserInfo.NexonSN) == "true") {
	                self.$chatFilter.find("ul.chatTabSub>li").removeClass("current");
	                self.$chatFilter.find("ul.chatTabSub>li").eq(1).addClass("current");
	                self.$chatFilter.find(".pingGroupList").hide();
	                self.initMembers();
	                self.$chatFilter.find(".pingGroupList").show();
	                opts.TargetType = 3;
	                self.getBestPingsList();
	                self.getList(true);
	                try {
	                    $.sendWiselog("http://www.nexon.com/wl.aspx?home=chatbestping_all");
	                } catch (e) { }
	            } else {
	                if (opts.GetListOnCreate)
	                    self.getList(true);
	            }
	        }
	    }

	    //------------------------Home Chat Filter section -----------------------------
	    if (opts.PageType == 1) { //Home
	        try {
	            $.sendWiselog("http://www.nexon.com/wl.aspx?home=chatall");
	        } catch (e) { }

	        self.$autoFeedOnOff.click(function() {
	            self.IsFilteringAutoFeedOn = !self.IsFilteringAutoFeedOn;
	            $target.ajaxNet(
					"/xsm/method/chatmethod.aspx?_vb=SetAutoFeedFilteringOption"
					, {
					    "IsFilteringAutoFeedOn": self.IsFilteringAutoFeedOn
					}
					, function(results, context) {
					    self.getList(true);
					    if ($("#d_pingGroupList").is(":visible")) {
					        self.getBestPingsList();
					    }
					}
				);

	            self.$autoFeedOnOff.removeClass();

	            if (self.IsFilteringAutoFeedOn) {
	                self.$autoFeedOnOff.addClass("feedOff");
	                $.alert("게임플레이 시에 자동으로 발급되는<br/>레벨업, 업적달성 등의 게임기록챗이 보이지 않도록 설정됐어요.");
	            } else {
	                self.$autoFeedOnOff.addClass("feedOn");
	                $.alert("게임플레이 시에 자동으로 발급되는<br/>레벨업, 업적달성 등의 게임기록챗이 노출되도록 설정됐어요.");
	            }
	            return false;
	        });

	        self.$chatFilter.find("ul.chatTab>li a").each(function(idx) {
	            var $a = $(this);
	            $a.click(function(e) {
	                self.$chatFilter.find("ul.chatTab>li")
					  .removeClass("chatTab1Current")
					  .removeClass("chatTab2Current")
					  .removeClass("chatTab3Current");
	                self.$chatFilter.find("ul.chatTab>li").eq(idx).addClass("chatTab" + (idx + 1) + "Current");
	                self.$chatFilter.find(".chatTabSub, .pingGroupList").hide();

	                self.initMembers();
	                $("#d_tagTxt").val("");

	                self.$autoFeedOnOff.hide();
	                if (idx == 0) { // 나+핑
	                    self.$autoFeedOnOff.show();
	                    self.$chatFilter.find(".chatTabSub").show();

	                    if ($.Cookie.get("IsBestPingDefault_" + $.LoginUserInfo.NexonSN) == "true") {
	                        self.$chatFilter.find(".pingGroupList").show();
	                        opts.TargetType = 3;
	                        self.getBestPingsList();
	                        self.getList(true);
	                        try {
	                            $.sendWiselog("http://www.nexon.com/wl.aspx?home=chatbestping_all");
	                        } catch (e) { }
	                    }
	                    else {
	                        self.getList(true);
	                    }

	                    try {
	                        $.sendWiselog("http://www.nexon.com/wl.aspx?home=chatall");
	                    } catch (e) { }
	                } else if (idx == 1) { // GM
	                    opts.FeedGroup = 1;
	                    opts.StarTargetType = 1;
	                    self.getList(true);
	                    try {
	                        $.sendWiselog("http://www.nexon.com/wl.aspx?home=chatgm");
	                    } catch (e) { }
	                } else if (idx == 2) { // 인기챗
	                    opts.FeedGroup = 3;
	                    self.getList(true);
	                    try {
	                        $.sendWiselog("http://www.nexon.com/wl.aspx?home=chathot");
	                    } catch (e) { }
	                }
	                return false;
	            });
	        });

	        // 친핑 챗
	        self.$chatFilter.find("ul.chatTabSub>li a").each(function(idx) {
	            var $a = $(this);
	            $a.click(function(e) {
	                self.$chatFilter.find("ul.chatTabSub>li").removeClass("current");
	                self.$chatFilter.find("ul.chatTabSub>li").eq(idx).addClass("current");
	                self.$chatFilter.find(".pingGroupList").hide();
	                self.initMembers();

	                $("#chatContent").watermark("changeText", "클릭하여 퐁 친구들에게 게임 이야기를 들려주세요.");
	                $("#chatContent").attr("title", "클릭하여 퐁 친구들에게 게임 이야기를 들려주세요.");

	                self.$autoFeedOnOff.show();
	                if (idx == 0) { // 나+핑						
	                    //$.Cookie.set("IsBestPingDefault_" + $.LoginUserInfo.NexonSN, "false", 1);
	                    opts.FeedGroup = 0;
	                    self.IsBestPingSelected = false;
	                    self.getList(true);
	                    try {
	                        $.sendWiselog("http://www.nexon.com/wl.aspx?home=chatall");
	                    } catch (e) { }
	                } else if (idx == 1) { // 친핑 챗
	                    //$.Cookie.set("IsBestPingDefault_" + $.LoginUserInfo.NexonSN, "true", 1);
	                    self.$chatFilter.find(".pingGroupList").show();
	                    opts.TargetType = 3;
	                    self.getBestPingsList();

	                    self.getList(true);
	                    try {
	                        $.sendWiselog("http://www.nexon.com/wl.aspx?home=chatbestping_all");
	                    } catch (e) { }
	                }
	                return false;
	            });
	        });

	        $("#btPingGroupSet").click(function() {
	            $.confirm("친핑을 설정하려면 설정메뉴로 이동해야 해요.<br/>설정 메뉴로 이동하시겠어요?", function() {
	                $.movePage( "/config/favoriteping.aspx" );
	            });
	            return false;
	        });

	        // 뜨는 챗
	        $("#d_popularChat").click(function() {
	            self.initMembers();
	            $("#d_tagTxt").val("");
	            opts.FeedGroup = 3;
	            self.getList(true);

	            try {
	                $.sendWiselog("http://www.nexon.com/wl.aspx?home=chathot_btn");
	            } catch (e) { }

	            return false;
	        });
	        /**
	        $("#d_tagSearchBtn").click(function() {
	        var tag = $("#d_tagTxt").val().trim();
	        tag = tag.replace("[", "").replace("]", "");
	        if (tag.length == 0) {
	        $.alert("검색 할 태그를 입력해주세요.");
	        return false;
	        }
	        self.$chatFilter.find(".pingGroupList").hide();
	        self.$autoFeedOnOff.parent('p').hide();
	        $("#divGuestbook").hide();
	        $("#d_notification").hide();
	        $("#d_navChat>ul li").removeClass();
	        $("#chatContent").watermark("changeText", "클릭하여 [" + tag + "] 챗에 참여하세요.");
	        $("#chatContent").attr("title", "클릭하여 [" + tag + "] 챗에 참여하세요.");
	        self.initMembers();
	        opts.FeedGroup = 4;
	        self.getList(true);

	            try {
	        $.sendWiselog("http://www.nexon.com/wl.aspx?home=chattag");
	        } catch (e) { }

	            $("#d_chatMain").show();

	            return false;
	        });

	        $("#d_tagTxt").keyup(function(e) {
	        if (e.keyCode == 13) {
	        $("#d_tagSearchBtn").click();
	        $(this).focus();
	        return false;
	        }
	        })
	        .watermark({
	        containerCss: {
	        opacity: ""
	        }
	        });
	        **/
	    }

	    //------------------------Home Chat Filter section -----------------------------

	    $("#d_pingGroupListNone2 button:eq(0)").click(function() {
	        $("#d_pingGroupListNone2").hide();
	        return false;
	    });

	    var $layerTag = $("#d_layerTag");

	    $("#d_btTagPreset_span").click(function() {
	        if ($layerTag.is(":visible")) {
	            $layerTag.hide();
	            $(this).removeClass("btTagPresetDown")
				.addClass("btTagPresetUp");
	        } else {
	            $layerTag.show();
	            $(this).removeClass("btTagPresetUp")
				.addClass("btTagPresetDown");
	        }
	        return false;
	    });

	    $layerTag.find(".btClose button:eq(0)").click(function() {
	        $("#d_btTagPreset_span").click();
	        $("#d_layerTag").hide();

	        return false;
	    });

	    /**
	    $("#d_tag").find("li>a").each(function(idx) {
	    var $tag = $(this);
	    $tag.click(function() {
	    //$("#d_btTagPreset_span").click();
	    $("#d_tag>ul li").removeClass();
	    $("#d_tag>ul li").eq(idx).addClass("current");

	            var s = $tag.find("span:eq(1)").html();
	    $("#d_tagTxt").val(s.substring(1, s.length - 1));
	    //$("#d_layerTag").hide();
	    $("#d_tagSearchBtn").click();
	    $("#d_tagTxt").watermark("option", "container").hide();
	    return false;
	    });
	    });
	    **/
	}
	, destroy: function() {
	    var self = this, opts = self.options, $target = self.element;

	    $.Widget.prototype.destroy.apply(this, arguments);
	}
	, initMembers: function() {
	    var self = this, opts = self.options, $target = self.element;

	    opts.FeedGroup = 0; // 0:일반피드, 1스타피드
	    opts.TargetType = 0;
	    opts.StarTargetType = 0;
	    opts.FeedType = 0;
	    opts.MinFeedSN = 0;
	    opts.HitType = 0;
	    $(".d_day0, .d_day1, .d_day2", $target).hide();
	    opts.OwnerNexonSN = $.ProfileUserInfo.NexonSN;

	    $("#d_pingGroupListNone2").hide();
	}

	, InitTag: function() {
	    var self = this, opts = self.options, $target = self.element;
	    self.Tag = null;
	}

	, setFilteringAutoFeedOn : function( FilteringAutoFeedOn )
	{
		 var self = this, opts = self.options, $target = self.element;
		self.IsFilteringAutoFeedOn = FilteringAutoFeedOn;
	}

	, getList: function(refresh) {
	    var self = this, opts = self.options, $target = self.element;

	    if (opts.GameCode == 0) return false;

	    self.$autoFeedOnOff.parent("p").show();

	    //if (self.$writerSec.length)
	        //self.$writerSec.feedwriter();

	    if (refresh == true) {
	        self.stopListing = false;
	        opts.MinFeedSN = 0;
	        opts.MinDate = "0001-01-01";
	        $("div.chatList", $target).hide();
	        $("div.d_chatPhoto", $target).hide();
	        $("#dummyChatlist").empty();
	        self.minDate0 = "0001-01-01 00:00:00.000";
	        self.minDate1 = "0001-01-01 00:00:00.000";
	    }
	    var context = { "refresh": refresh };
	    if (opts.FeedGroup == 0) {
	        opts.GameCode = 65536;
	        if (opts.IsStatic) {
	            $("#d_chatListWrapper").ajaxNet(
			        "/xsm/method/chatmethod.aspx?_vb=GetProfileChatList"
			        , {
			            "OwnerNexonSN": opts.OwnerNexonSN
				        , "MinFeedSN": opts.MinFeedSN
				        , "MinDate": opts.MinDate
				        , "TargetType": opts.TargetType
				        , "FeedType": opts.FeedType
				        , "PageType": opts.PageType
				        , "FeedGroup": opts.FeedGroup
				        , "OpenLevel": opts.OpenLevel
				        , "IsFilteringAutoFeedOn": self.IsFilteringAutoFeedOn
				        , "HitType": opts.HitType
				        , "PageSize": opts.PageSize
			        }
			        , self._onGetListSuceeded
			        , null
			        , { "refresh": refresh, "self": self }
		        );
	        }
	        else {
	            $("#d_chatListWrapper").ajaxNet(
			        "/xsm/method/chatmethod.aspx?_vb=GetList"
			        , {
			            "OwnerNexonSN": opts.OwnerNexonSN
				        , "MinFeedSN": opts.MinFeedSN
				        , "MinDate": opts.MinDate
				        , "TargetType": opts.TargetType
				        , "FeedType": opts.FeedType
				        , "PageType": opts.PageType
				        , "FeedGroup": opts.FeedGroup
				        , "OpenLevel": opts.OpenLevel
				        , "IsFilteringAutoFeedOn": self.IsFilteringAutoFeedOn
				        , "HitType": opts.HitType
				        , "PageSize": opts.PageSize
			        }
			        , self._onGetListSuceeded
			        , null
			        , { "refresh": refresh, "self": self }
		        );
	        }
	    } else if (opts.FeedGroup == 1) {
	        opts.GameCode = 65536;
	        var gameCode = self.tagData.GameCode;
	        if (gameCode === undefined)
	            gameCode = 0;

	        $("#d_chatListWrapper").ajaxNet(
				"/xsm/method/chatmethod.aspx?_vb=GetGMFeedList"
				, {
				    "GameCode": gameCode
					, "TargetType": opts.StarTargetType
					, "HitType": opts.HitType
					, "PageType": opts.PageType
					, "FeedGroup": opts.FeedGroup
					, "OpenLevel": opts.OpenLevel
					, "IsFilteringAutoFeedOn": self.IsFilteringAutoFeedOn
					, "PageSize": opts.PageSize
					, "MinFeedSN": opts.MinFeedSN
				}
				, self._onGetListSuceeded
				, null
				, { "refresh": refresh, "self": self }
			);
	    } else if (opts.FeedGroup == 2) {
	        opts.GameCode = 65536;
	        $("#d_chatListWrapper").ajaxNet(
				"/xsm/method/chatmethod.aspx?_vb=GetFavoriteFeedList"
				, {
				    "NexonSN": $.ProfileUserInfo.NexonSN
					, "PageSize": opts.PageSize
					, "MinDate0": self.minDate0
					, "MinDate1": self.minDate1
				}
				, self._onGetListSuceeded
				, null
				, { "refresh": refresh, "self": self }
			);
	    } else if (opts.FeedGroup == 3) {// top ranked feeds
	        opts.GameCode = 65536;
	        $("#d_chatListWrapper").ajaxNet(
				"/xsm/method/chatmethod.aspx?_vb=GetTopRankedList"
				, {
				    "NexonSN": $.ProfileUserInfo.NexonSN
					, "PageSize": opts.PageSize
					, "MinDate0": self.minDate0
					, "MinDate1": self.minDate1
					, "PageType": opts.PageType
					, "FeedGroup": opts.FeedGroup
				}
				, self._onGetListSuceeded
				, null
				, { "refresh": refresh, "self": self }
			);
	    } else if (opts.FeedGroup == 4) {// taged chat
	        var tag = '';
	        if ($("#d_tagTxt").val() == undefined)
	            tag = opts.TagString;
	        else {
	            tag = $("#d_tagTxt").val().trim();
	        }
	        tag = tag.replace("[", "").replace("]", "");
	        //$("#d_tagTxt").watermark("option", "container").hide();
	        self.$autoFeedOnOff.parent("p").hide();
	        if (opts.GameCode != 0 && opts.GameCode != 65536) {
	            self.getListByGameCode(true, opts.GameCode, tag);
	            return false;
	        }
	        $("#d_chatListWrapper").ajaxNet(
				"/xsm/method/chatmethod.aspx?_vb=GetListByTag"
				, {
				    "Tag": tag
					, "PageSize": opts.PageSize
					, "MinDate": opts.MinDate
					, "MinFeedSN": opts.MinFeedSN
					, "GameCode": opts.GameCode
				}
				, self._onGetListSuceeded
				, null
				, { "refresh": refresh, "self": self }
			);
	        self.Tag = null;
	    } else if (opts.FeedGroup == 5) {// gamehistory
	        self.$autoFeedOnOff.parent("p").hide();
	        $("#d_chatListWrapper").ajaxNet(
				"/xsm/method/chatmethod.aspx?_vb=GetMoreGameFeedList"
				, {
				    "NexonSN": $.ProfileUserInfo.NexonSN
					, "PageSize": opts.PageSize
					, "MinFeedSN": opts.MinFeedSN
				}
				, self._onGetListSuceeded
				, null
				, { "refresh": refresh, "self": self }
			);
	        self.Tag = null;
	    } else if (opts.FeedGroup == 6) {// media
	        $("#d_mediaList").ajaxNet(
				"/xsm/method/chatmethod.aspx?_vb=GetMoreMediaList"
				, {
				    "NexonSN": $.ProfileUserInfo.NexonSN
					, "PageSize": opts.PageSize
					, "MinFeedSN": opts.MinFeedSN
					, "HitType": opts.HitType
				}
				, self._onGetListSuceeded
				, null
				, { "refresh": refresh, "self": self }
			);
	        self.Tag = null;
	    }
	}

	, getListByGameCode: function(refresh, GameCode, Tag) {
	    var self = this, opts = self.options, $target = self.element;
	    if (GameCode == 0) return false;
	    if (Tag == '') opts.FeedGroup = 0;
	    
	    self.$writerSec.feedwriter("setGameCode",GameCode);
	    
	    if (GameCode == 65536) {
	        self.getList(true);
	        return false;
	    }
	    self.$autoFeedOnOff.parent("p").hide();
	    opts.FeedGroup = 0;

	    //if (self.$writerSec.length)
	        //self.$writerSec.feedwriter();
	    if (refresh == true) {
	        self.stopListing = false;
	        opts.MinFeedSN = 0;
	        opts.MinDate = "0001-01-01";
	        $("div.chatList", $target).hide();
	        $("#dummyChatlist").empty();
	        self.minDate0 = "0001-01-01 00:00:00.000";
	        self.minDate1 = "0001-01-01 00:00:00.000";
	    }
	    var context = { "refresh": refresh };
	    if (opts.FeedGroup == 0) {
	        if (Tag != null && Tag != '') {
	            opts.GameCode = GameCode;
	            self.Tag = Tag;
	            $("#d_chatListWrapper").ajaxNet(
				    "/xsm/method/chatmethod.aspx?_vb=GetListByGameCodeNTag"
				    , {
				        "OwnerNexonSN": opts.OwnerNexonSN
					    , "MinFeedSN": opts.MinFeedSN
					    , "MinDate": opts.MinDate
					    , "TargetType": opts.TargetType
					    , "FeedType": opts.FeedType
					    , "PageType": opts.PageType
					    , "FeedGroup": opts.FeedGroup
					    , "OpenLevel": opts.OpenLevel
					    , "IsFilteringAutoFeedOn": self.IsFilteringAutoFeedOn
					    , "HitType": opts.HitType
					    , "PageSize": opts.PageSize
					    , "GameCode": GameCode
					    , "Tag": Tag
				    }
				    , self._onGetListSuceeded
				    , null
				    , { "refresh": refresh, "self": self }
			    );
	        }
	        else {
	            opts.GameCode = GameCode;
	            $("#d_chatListWrapper").ajaxNet(
				    "/xsm/method/chatmethod.aspx?_vb=GetListByGameCode"
				    , {
				        "OwnerNexonSN": opts.OwnerNexonSN
					    , "MinFeedSN": opts.MinFeedSN
					    , "MinDate": opts.MinDate
					    , "TargetType": opts.TargetType
					    , "FeedType": opts.FeedType
					    , "PageType": opts.PageType
					    , "FeedGroup": opts.FeedGroup
					    , "OpenLevel": opts.OpenLevel
					    , "IsFilteringAutoFeedOn": self.IsFilteringAutoFeedOn
					    , "HitType": opts.HitType
					    , "PageSize": opts.PageSize
					    , "GameCode": GameCode
				    }
				    , self._onGetListSuceeded
				    , null
				    , { "refresh": refresh, "self": self }
			    );
	        }
	    }
	}

	, getFeedInfo: function(fsn, csn) {
	    var self = this, opts = self.options, $target = self.element;
	    self.stopListing = true;
	    $("#d_chatListWrapper").ajaxNet(
			"/xsm/method/chatmethod.aspx?_vb=GetFeedInfo"
			, {
			    "FeedSN": fsn
			}
			, function(results, context) {
				$("#d_pingGroupListNone2").hide();
			    if (opts.PageType == 1) {//home, chatview
			        $("div.chatList").hide();
			        $("#d_btMoreList").hide();
			    }
			    if (opts.PageType == 4 || opts.PageType == 1) {//home, chatview
			        $("#d_permalink").show();
			    }
			    $divToBind = $("#d_permalink div.listLeft");
			    $divToBind.html(results);

			    var $feed = $divToBind.find("div.feed:eq(0)");

			    if ($("#d_permalink").is(":visible")) {
			        $feed.feedview({
			            showCommentsOnLoad: true
						, ViewFeedSN: fsn
						, ViewCommentSN: csn
			        });
			    } else {
			        $feed.feedview();
			    }
			}
		);
	}

	, _onGetListSuceeded: function(results, context) {
	    var self = context.self, opts = self.options, $target = self.element;

		if ( $("#m_ChatRecommendUser" ).size() > 0 ) {
			$("#m_ChatRecommendUser" ).hide();
		}

		$("#d_permalink").hide();
	    $("#d_noChat10").hide();
	    $("#d_noChat11").hide();
	    self.$noneChatDivs.hide();
	    // not star feed, first call, no result
	    var resultHtml = "";
	    resultHtml = results[0];
	    if (opts.FeedGroup == 2 || opts.FeedGroup == 3) {
	        if (resultHtml.trim().length > 0) {
	            self.minDate0 = results[1];
	            self.minDate1 = results[2];
	        }
	    } else if (opts.FeedGroup == 6) {
	        opts.MinFeedSN = results[1];
	    } else {
	        opts.MinFeedSN = results[1];
	        opts.MinDate = results[2];
	    }

	    if (opts.FeedGroup != 1 && opts.FeedGroup != 6 && opts.MinFeedSN <= 0 && resultHtml.trim().length <= 0) { // No record!
	        if (context.refresh == true) {
	            $("div.chatList div.listLeft, div.chatList div.listRight", $target).empty();
	        }

	        self.stopListing = true;
	        $("div.chatList", $target).hide();
	        $("#d_btMoreList").hide();
	        $("#d_noChat8").hide();
	        $("#d_noChat9").hide();
	        if (opts.GameCode != 65536 && opts.TargetType == 0 && opts.FeedGroup == 0 && (self.Tag == null || self.Tag == '')) {
	            $("#d_noChat7").hide();
	            $("#d_noChat8").show();
	        } else if (opts.TargetType == 0 && opts.FeedGroup == 0 && self.Tag != null && self.Tag != '') {
	            $("#d_noChat8").hide();
	            $("#d_noChat7").show();
	        } else if (opts.FeedGroup == 4) {
	            $("#d_noChat7").show();
	        } else if (opts.FeedGroup == 5) {
	            $("#d_noChat9").show();
	        } else if (opts.TargetType == 3) {
	            if (self.$bestPingCount > 0) {
	                self.$noneChat6.show();
	            }
	        } else if (opts.TargetType == 1) {
	            if (opts.PageType == 1) {
	                $("#d_noChat11>p span.nickname").html("<a href=\"/profile/index.aspx?sn=" + opts.OwnerNexonSN + "\" onmouseover=\"$ps.MiniProfile.open(this, '" + opts.OwnerNexonSN + "');\"></a>");
	                $("#d_noChat11>p span.nickname >a").html($("#d_pingGroupList_1 > li.current > span.nickname > a").html());
	                $("#d_noChat11").show();
	            }
	            else {
	                $("#d_noChat10").show();
	            }
	        } else if (opts.FeedType == 0 && (opts.HitType == 0 || opts.HitType == 1)) { // Type all and ping and me
	            if (opts.FeedGroup == 2) {
	                self.$noneChat5.show();
	            } else if ($.RelationInfo.IsOwner) {
					if ( typeof( $.GuideViewer ) != "undefined" ) {
						$.GuideViewer.UserRecommend( $("#d_chatListWrapper"), self.$noneChat1 );
					} else {
						self.$noneChat1.show();
					}
	            } else {
	                self.$noneChat2.show();
	            }
	        } else if (opts.FeedType == 2) {
	            self.$noneChat4.show();
	        } else if (opts.HitType == 2) {
	            self.$noneChat3.show();
	        }

	        if (opts.PageType == 5)
	            $("#profilemainemptychat").show();
	    } else {
	        var $divToBind = null;
	        if (opts.FeedGroup == 6) {
	            $divToBind = $("#d_mediaList");
	            if (context.refresh == true) {
	                $divToBind.empty();
	            }
	            $divToBind.append(resultHtml);
				var size = $divToBind.find( "span.thumbnail img" ).size();
				for ( var i = size; i > size-12; i -- )
				{
					$( "span.thumbnail img:eq(" + i + ")" ).each(function() {
						if ( !$(this).closest("span").hasClass("_noneThumbnail") )
						{
							var img = new Image();
							var $temp = $(this);
							img.onload = function() { 
								// width와 height가 모두 작을때
								if ( parseInt( $temp.attr("width"), 10 ) > parseInt( img.width, 10 ) && parseInt( $temp.attr("height"), 10 ) > parseInt( img.height, 10 ) )
								{
									$temp.css({"width":img.width,"height":img.height});
									return;
								}
								var twidth = parseInt( img.width, 10 ) - parseInt( $temp.attr("width"), 10 );
								var theight = parseInt( img.height, 10 ) - parseInt( $temp.attr("height"), 10 );
								
								// width가 클경우
								if ( twidth > theight )
								{
									$temp.attr("height", null);
									$temp.css("margin-top", ( 99 - parseInt( $temp.css("height"), 10 ) ) / 2 );
									
								}
								// height가 클경우
								else if ( twidth < theight )
								{
									$temp.attr("width", null);
									$temp.css("margin-left", ( 150 - parseInt( $temp.css("width"), 10 ) ) / 2 );
								}
								img.onload = {};
							};
							img.src = $(this).attr("src");
						}
					});
				}
	        }
	        else {
	            $divToBind = $("#dummyChatlist");
	            $divToBind.html(resultHtml);

	            // after viewcomment
	            if (opts.PageType != 5)
	                $("#d_btMoreList").show();
	            $("#d_permalink").hide();
	            var $feeds = $divToBind.find("div.feed");
	            if (context.refresh == true) {
	                $("div.chatList div.listLeft, div.chatList div.listRight, div.chatList div.d_Left0", $target).empty();
	            }

	            if (opts.PageType == 5) {
	                self._slideDownProfileMain();
	            }
	            else {
					if ( opts.PageType == 1 && opts.FeedGroup == 0 && opts.FeedType == 0 && opts.GameCode == 65536 && ( opts.HitType == 0 || opts.HitType == 1 ) ) { // Type all and ping and me
						if ($.RelationInfo.IsOwner) {
							if ( typeof($.GuideViewer) != "undefined" ) {
								$.GuideViewer.ChatGuide();
							}
						}
					}
	                self._slideDown();
				}
	        }
	    }

	    $.setPng24();
	}

	, _slideDown: function() {
	    var self = this, opts = self.options, $target = self.element;
	    var $feed = $("#dummyChatlist").find("div.feed").eq(0);
	    if (self.stopListing) {
	        $("div.chatList", $target).hide();
	    }
	    if ($feed.size() > 0 && !self.stopListing) {
	        var daysIdx = 0;

	        var left = "div.d_Left" + daysIdx;
	        var right = "div.d_Right" + daysIdx;

	        if ($(left).height() <= $(right).height()) {
	            $feed.appendTo(left).hide().slideDown(function() {
	                $feed.feedview();
	                self._slideDown();

	            });
	        } else {
	            $feed.appendTo(right).hide().slideDown(function() {
	                $feed.feedview();
	                self._slideDown();
	            });
	        }
	        $(".d_day" + daysIdx, $target).show();

	        $.each([0, 1, 2], function(i) {
	            if ($("div.d_day" + i, $target).find("div.feed").size() == 0) {
	                $(".d_day" + i, $target).hide();
	            } else {
	                $(".d_day" + i, $target).show();
	            }
	        });
	    }
	}

	, _slideDownProfileMain: function() {
	    var self = this, opts = self.options, $target = self.element;
	    var $feed = $("#dummyChatlist").find("div.feed").eq(0);
	    if (self.stopListing) {
	        $("div.chatList", $target).hide();
	    }
	    if ($feed.size() > 0 && !self.stopListing) {
	        var left = "div.d_Left0";
	        $feed.appendTo(left);
	        $feed.feedview();
	        self._slideDownProfileMain();
	    }
	}

	, onFeedCreated: function() {
	    var self = this, opts = self.options, $target = self.element;
	  
	    if (opts.IsStatic) {
	        $("div.d_Left0", $target).empty();
	        self.getList(true);
	    } else {
	        $("#d_permalink").hide();
	        self.$noneChatDivs.hide();
	        var $newFeed = null;

	        if (!$(".d_day0").is(":visible"))
	            $(".d_day0").show();

	        $("#d_noChat8").hide();
	        if ($.browser.msie && $.browser.version < 8.0)
	            $newFeed = $("div.d_Left0").prepend(results).find("div.feed").eq(0);
	        else
	            $newFeed = $("div.d_Left0").prepend(results).find("div.feed").eq(0).hide().slideDown();

	        $newFeed.feedview();
	    }
	}

	, toggleDay: function(dayIdx) {
	    var self = this, opts = self.options, $target = self.element;
	    var $base = $target.find("div.chatList").eq(dayIdx);

	    if ($("div.d_day" + dayIdx, $base).is(":visible")) {
	        $("div.d_day" + dayIdx, $base).slideUp();
	        $("h2.d_day" + dayIdx, $base).addClass("close").attr("title", "챗 펼치기");
	    } else {
	        $("div.d_day" + dayIdx, $base).slideDown();
	        $("h2.d_day" + dayIdx, $base).removeClass("close").attr("title", "챗 접기");
	    }
	}

	, getBestPingsList: function() {
	    var self = this, opts = self.options, $target = self.element;

	    $("#d_pingGroupList").ajaxNet(
			"/xsm/method/chatmethod.aspx?_vb=GetBestPingsList"
			, {}
			, function(results, context) {
			    $("#d_pingGroupList ul").hide().empty();
			    $("#d_pingGroupListTemp").html(results[0]);
			    self.$bestPingCount = parseInt(results[1]);

			    var $lis = $("#d_pingGroupListTemp li");

			    $lis.each(function() {
			        var $li = $(this);
			        $li.find(".d_profileView").click(function() {
			            $lis.removeClass("current");
			            $li.addClass("current");
			            var targetNexonSN = $li.tagdata().NexonSN;
			            self.initMembers();
			            opts.TargetType = 1;
			            //opts.PageType = 2;
			            opts.OwnerNexonSN = targetNexonSN;
			            self.getList(true);
			            try {
			                $.sendWiselog("http://www.nexon.com/wl.aspx?home=chatbestping");
			            } catch (e) { }

			            self.updateChatViewDate(targetNexonSN, $li);
			            return false;
			        });
			        if ($li.hasClass("d_bestPingEmpty")) {
			            $li.click(function() {
			                $("#d_btPingGroupSet").click();
			            });
			        }
			    });

			    self.$bestPingsPagerSec.pager("option", "currentPageNo", 1);
			    self.$bestPingsPagerSec.pager("update", results[1], 1);
			    var pageSize = self.$bestPingsPagerSec.pager("option", "pageSize");
			    for (var i = 0; i < pageSize; i++) {
			        $("#d_pingGroupList_1").append($lis.eq(i));
			    }
			    for (var i = 10; i < pageSize * 2; i++) {
			        $("#d_pingGroupList_2").append($lis.eq(i));
			    }

			    $("#d_pingGroupList_1").show();

			    if (self.$bestPingCount == 0) {
			        $("#d_pingGroupListNone2").show();
			    }

			}
		);
	    return false;
	}

	, updateChatViewDate: function(pingNexonSN, $li) {
	    var self = this, opts = self.options, $target = self.element;

	    $("#d_pingGroupList").ajaxNet(
			"/xsm/method/chatmethod.aspx?_vb=UpdateChatViewDate"
			, {
			    "PingNexonSN": pingNexonSN
			}
			, function(results, context) {
			    $li.find(".num").remove();
			}
		);
	    return false;
	}

	, movePageBestPings: function(pageNo) {
	    var self = this, opts = self.options, $target = self.element;
	    $("#d_pingGroupList ul").hide();
	    $("#d_pingGroupList_" + pageNo).show();
	    self.$bestPingsPagerSec.pager("update", self.$bestPingsPagerSec.pager("option", "totalCount"));
	    return false;
	}


    });

})(jQuery);

