﻿//////////////////////////////////////////////// feedview /////////////////////////////////////////////

/*
ID: feedlist.js
History
	0.1(2011-03-22) 파일생성 - maninblu

Description
	피드 리스팅 컨트롤
	
	interface
		$( {피드 리스트 영역} ).feedview( 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.feedview", {
        options: {
            CommentPageSize: 10
		, HitPageSize: 10
		, ViewFeedSN: 0 // view 페이지인 경우
		, ViewCommentSN: 0 // view 페이지인 경우
		, $Container: null //ajax 호출시 막아야 하는 기준 영역
		, $writerSec: null // feedwriter 
		, showCommentsOnLoad: false
		, methodPath : "/xsm/method/chatmethod.aspx?_vb="
        }

	, tagData: {}

	, _create: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    self.tagData = $feed.tagdata();
	    if( opts.$Container == null )
			opts.$Container = $("#d_chatListWrapper");
		if( opts.$writerSec == null )
			opts.$writerSec = $("#d_chatWrite");
	    self._bindEventsToFeed();
	    self._createWatermark();

	    var $pagerSec = $(".pagerSec", $feed);
	    $pagerSec.pager({
	        oldpage: 1,
	        pagecut: 0,
	        blockSize: 5,
	        pageSize: opts.CommentPageSize,
	        reverse: true,
	        callback: function(pageNo) {
	            self.getCommentList();
	        }
	    });

	    var $hitpagerSec = $("p.hitAction", $feed);
	    $hitpagerSec.pager({
	        oldpage: 1,
	        pagecut: 0,
	        blockSize: 1,
	        pageSize: opts.HitPageSize,
	        firstBtn: '',
	        lastBtn: '',
	        reverse: true,
	        callback: function(pageNo) {
	            self.getHitList();
	        }
	    });

	    if (opts.showCommentsOnLoad)
	        $(".action .cmnt button", $feed).click();

	    //------------------------Home Chat Filter section -----------------------------
	}

	, destroy: function() {
	    var self = this, opts = self.options, $target = self.element;

	    $.Widget.prototype.destroy.apply(this, arguments);
	}

	, _bindEventsToFeed: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    var data = $feed.tagdata();

	    if ($.LoginUserInfo.IsLogin) {
	        if ($.PlugUtil.IsOnline(data.OriginalNexonSN)) {
	            $feed.find(".infoWriter .date").before(
			 $('<span class="plug" />').html(
				  '<img src="http://s.nx.com/S2/p2/icon/ico_plug_b.gif" width="10" height="14" alt="plug" /><span>대화</span>'
				  ).click(function() {
				      $.PlugUtil.OpenChatWindow(data.OriginalNexonSN);
				  })
			);
	        }
	    }

	    $feed.bind("mouseenter", function() {
	        $(this).unbind("focusout")
			.children(".feedOut")
				.addClass("feedOver")
				.children(".admin")
				.css("display", "block");
	        return false;

	    }).bind("mouseleave", function() {
	        $(this).unbind("focusin")
			.children(".feedOut")
				.removeClass("feedOver")
				.children(".admin")
				.css("display", "none");
	        return false;
	    }).bind("focusin", function() {
	        $(this).children(".feedOut")
				.addClass("feedOver")
				.children(".admin")
				.css("display", "block");
	        return false;

	    }).bind("focusout", function() {
	        $(this).children(".feedOut")
				.removeClass("feedOver")
				.children(".admin")
				.css("display", "none");
	        return false;
	    });


	    $(".admin", $feed).bind("mouseenter focusin", function(event) {
			$("li.d_clipboard", $feed).clipboard({
				text: "http://www.nexon.com/profile/chatview.aspx?sn=" + data.OriginalNexonSN + "&feedsn=" + data.OriginalFeedSN
			});
			$("li.d_clipboardnote", $feed).clipboard({
				text: "http://www.nexon.com/profile/note.aspx?sn=" + data.OriginalNexonSN + "&feedsn=" + data.OriginalFeedSN
			});
			
			$(this).addClass("adminCurrent");
	    }).bind("mouseleave focusout", function(events) {
			$("li.d_clipboard", $feed).clipboard("destroy");
	        $(this).removeClass("adminCurrent");
	    });

	    $("div.admin .d_static", $feed).click(function(event) {
	        self.staticFeed();
	    });

	    $("div.admin .d_staticcancel", $feed).click(function(event) {
	        self.staticFeedRemove();
	    });

	    $("div.admin .d_remove", $feed).click(function(event) {
	        self.removeFeed();
	    });

	    $("div.admin .d_removePing", $feed).click(function(event) {
	        self.removePing();
	    });

	    $("div.admin .d_hide", $feed).click(function(event) {
	        self.hideFeed();
	    });

	    $("div.admin .d_addBestPing", $feed).click(function(event) {
	        self.addBestPing();
	    });

	    $(".action .hit button", $feed).click(function(event) {
	        self.hit();
	    });
	    $(".action .hitNum button", $feed).click(function(event) {
	        self.toggleHitSec();
	    });
	    $(".action .cmnt button", $feed).click(function(event) {
	        self.toggleCommentSec();
	    });
	    $(".action .scrap button", $feed).click(function(event) {
	        self.toggleFavorite(event.target);
	    });

	    $(".btMore button", $feed).click(function(event) {
	        $(".action .cmnt button", $feed).trigger("click");
	    });

	    $(".commentWrite button", $feed).click(function(event) {
	        self.createComment();
	    });

	    $("span.hit button", $feed).bind("mouseover focusin", function() {
	        $(this).parent().addClass("hitOn");
	    }).bind("mouseout focusout", function() {
	        $(this).parent().removeClass("hitOn");
	    });

	    $("a.tagTxt", $feed).click(function(e) {
	        if (document.URL.indexOf("square.nexon.com/issue") != -1) {
				var tag = decodeURIComponent($(this).attr("tag"))
				$("#d_inputLeftTagToSearch").val(tag);				
				$("#d_inputLeftTagToSearch").watermark("option", "container").hide();
				$("#d_btnLeftTagToSearch").click();	
				$("#d_issueSideSec").find("li").removeClass("hsOn").removeClass("isOn").removeClass("on").removeClass("d_selected");						
	        }else{	        
				if (!$.LoginUserInfo.IsLogin) {
					$.Login.confirmLogin();
					return false;
				}

				var $etarget = $(e.target);
				var s = $etarget.html();

				$h.a2s.sendClickLog("Chat_Tag", "{\"tag\":\"" + $(this).attr("tag") + "\"}");

				if (document.URL.indexOf("/home/index.aspx") != -1) {
					$("#d_tag>ul li").removeClass("current");
					var tag = decodeURIComponent($(this).attr("tag"))
					$("#d_tag").find(".d_tag").each(function() {
						if ("[" + tag + "]" == $(this).text()) {
							$(this).parent().parent().addClass("current");
						}
					});
					$("#d_tagTxt").val(tag);
					$("#d_tagSearchBtn").click();
				} else {
					$.movePage( "http://www.nexon.com/home/index.aspx?Tag=1&tagstring=" + $(this).attr("tag") );
				}
	        }
	        return false;
	    });
	    
	    self._bindEventToComment();

	}

	, _createWatermark: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    var data = $feed.tagdata();
	    $("textarea", $feed).watermark({
	        overlayCss: {
	            opacity: ""
	        },
	        checkHidden: function() {
	            if (!$.LoginUserInfo.IsLogin) {
	                $.Login.confirmLogin();
	                return false;
	            }
	            return true;
	        }
	    });

	    $("fieldset.commentWrite > textarea", $feed).bind("focus focusin", function() {
	        $(this).parent().addClass("commentWriteCurrent");
	        $(this).watermark("resize");

	        // ToDo :  이거 왜 두 번 호출 되는 걸까?
	        $h.a2s.sendClickLog("Comment_TextClick", "{\"feedSN\":\"" + data.OriginalFeedSN + "\"}");
	    });

	    if (!$.browser.webkit) {
	        $("fieldset.commentWrite > textarea", $feed).bind("blur focusout", function() {
	            if ($(this).val().trim().length == 0) {
	                $(this).parent().removeClass("commentWriteCurrent");
	                $(this).watermark("resize");
	            }
	        });
	    }

	    $("fieldset.commentWrite > button", $feed).bind("focus focusin", function() {
	        $(this).parent().addClass("commentWriteCurrent");
	        $(this).siblings("textarea").watermark("resize");
	    }).bind("blur focusout", function() {
	        if ($(this).siblings("textarea").val().trim().length == 0) {
	            $(this).parent().removeClass("commentWriteCurrent");
	            $(this).siblings("textarea").watermark("resize");
	        }
	    });
		
	    $("li", $feed).hover(
        function() {
            $(this).find("> button").addClass("current");
        },
        function() {
            $(this).find("> button").removeClass("current");
        }
    );

	    if (opts.$writerSec.is(":visible")) {
	        $(".d_feedCall", $feed).click(function() {
	            $h.a2s.sendClickLog("Chat_CallWrite", "{\"NickName\":\"" + data.OriginalNickName + "\"}");
	            opts.$writerSec.feedwriter("appendUserTag", data.OriginalNickName);
	            return false;
	        });
	    } else {
	        $(".d_spanCall", $feed).hide();
	    }
	}

	, staticFeed: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    var feedSN = $feed.tagdata().OriginalFeedSN;

	    $h.a2s.sendClickLog("Chat_SetStatic", "{\"feedSN\":\"" + feedSN + "\"}");

	    $.confirm("대표챗을 설정하면 다른 대표챗 설정이 사라져요.<br/> 정말로 설정하시겠어요?", function() {
	        $h.a2s.sendClickLog("Chat_SetStaticOk", "{\"feedSN\":\"" + feedSN + "\"}");
	        opts.$Container.ajaxNet(
				opts.methodPath + "SetStaticFeed"
				, {
				    "FeedSN": feedSN
				}
				, function(results, context) {
				    $.alert("설정했어요!");
				    if ($("#d_chatListWrapper .feedHold").length > 0) {
				        $("#d_chatListWrapper").feed("getList", true);
				    }
				}
			);
	    });
	}

	, staticFeedRemove: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    var feedSN = $feed.tagdata().OriginalFeedSN;

	    $h.a2s.sendClickLog("Chat_RemoveStatic", "{\"feedSN\":\"" + feedSN + "\"}");

	    $.confirm("대표챗 설정을 취소하시겠어요?", function() {
	        $h.a2s.sendClickLog("Chat_RemoveStaticOk", "{\"feedSN\":\"" + feedSN + "\"}");
	        opts.$Container.ajaxNet(
				opts.methodPath + "RemoveStaticFeed"
				, {
				    "FeedSN": feedSN
				}
				, function(results, context) {
				    $.alert("대표챗 설정을 취소했어요!");
				    if ($("#d_chatListWrapper .d_day0").is(":visible")) {
				        $("#d_chatListWrapper").feed("getList", true);
				    }
				}
			);
	    });
	}

	, removeFeed: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    var feedSN = $feed.tagdata().OriginalFeedSN;
	    $h.a2s.sendClickLog("Chat_Remove", "{\"feedSN\":\"" + feedSN + "\"}");

	    $.confirm("정말 삭제하시겠어요?<br/>(이 챗과 연결된 노트가 있을 경우 노트 원문까지 삭제돼요!)", function() {
	        $h.a2s.sendClickLog("Chat_RemoveOk", "{\"feedSN\":\"" + feedSN + "\"}");
	        opts.$Container.ajaxNet(
				opts.methodPath + "RemoveFeed"
				, {
				    "FeedSN": feedSN
				}
				, function(results, context) {
				    if ($.browser.msie && $.browser.version < 8.0) {
				        $feed.remove();
				    } else {
				        $feed.slideUp(function() {
				            $feed.remove();
				        });
				    } ////
				}
			);
	    });
	}
	, hit: function() {

	    if (!$.LoginUserInfo.IsLogin) {
	        $.Login.confirmLogin();
	        return false;
	    }

	    var self = this, opts = self.options, $feed = self.element;
	    var feedSN = $feed.tagdata().OriginalFeedSN;

	    $h.a2s.sendClickLog("Chat_Hit", "{\"feedSN\":\"" + feedSN + "\"}");

	    $feed.ajaxNet(
			opts.methodPath + "Hit",
			{
			    "TargetFeedSN": feedSN
			},
			function(results, context) {
			    var $num = $("span.hitNum span.num", $feed);
			    var num = parseInt($num.text()) + 1;
			    $num.text(num);
			    $.alert($.LoginUserInfo.NexonName + "의 추천!<br/>추천한 챗은 내 퐁들에게도 보여져요.");
			}
		);
	}
	, toggleHitSec: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    var $hitSec = $("div.hitList", $feed);

	    if ($hitSec.is(":visible")) {
	        $h.a2s.sendClickLog("Chat_HitHide", "{\"feedSN\":\"" + $feed.tagdata().OriginalFeedSN + "\"}");
	        $hitSec.slideUp();
	    } else {
	        $h.a2s.sendClickLog("Chat_HitShow", "{\"feedSN\":\"" + $feed.tagdata().OriginalFeedSN + "\"}");
	        self.getHitList();
	        $hitSec.slideDown();
	    }
	}
	, getHitList: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    var feedSN = $feed.tagdata().OriginalFeedSN;
	    var $pagerSec = $("p.hitAction", $feed);
	    var $hitList = $("ul.d_hitList", $feed);



	    var curPageNo = $pagerSec.pager("option", "currentPageNo");
	    $feed.ajaxNet(
			opts.methodPath + "GetHitList",
			{
			    "FeedSN": feedSN
				, "PageNo": curPageNo
				, "PageSize": opts.HitPageSize
				, "PageNo_Old": $pagerSec.pager("option", "oldpage")
				, "HitSN_BlockStart_Old": $pagerSec.pager("option", "pagecut")
			},
			function(results, context) {
			    $hitList.html(results[0]);

			    $pagerSec.pager("update", results[1]);
			    $pagerSec.pager("option", "pagecut", results[2]);
			    $pagerSec.pager("option", "oldpage", curPageNo);

			    if (results[1] > opts.HitPageSize) {
			        $pagerSec.show();
			        $hitList.addClass("full");
			    }

			    var $num = $("span.hitNum span.num", $feed);
			    $num.text(results[1]);
			}
		);
	}

	, toggleCommentSec: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    var $commentSec = $(".commentList", $feed);
	    var isOpen = $commentSec.data("isOpen");
	    if (isOpen == null) {
	        $commentSec.data("isOpen", false);
	    }

	    //ToDo: 글이 없는 경우 왜 두번씩 호출되는가?
	    $h.a2s.sendClickLog("Chat_Comment" + (isOpen ? "Hide" : "Show"), "{\"feedSn\":\"" + $feed.tagdata().OriginalFeedSN + "\"}");

	    if (isOpen) {
	        $commentSec.slideUp();
	        $feed.find(".action .cmnt").addClass("cmntClose").removeClass("cmntOpen");
	        $commentSec.data("isOpen", false);
	    } else {
	        self.getCommentList();
	    }
	}

	, getCommentList: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    var feedSN = $feed.tagdata().OriginalFeedSN;
	    var $pagerSec = $(".pagerSec", $feed);
	    var $commentSec = $(".commentList", $feed);
	    var $commentList = $("ul.d_commentList", $feed);

	    var curPageNo = $pagerSec.pager("option", "currentPageNo");
        
	        $feed.ajaxNet(
			    opts.methodPath + "GetCommentList",
			    {
			        "FeedSN": feedSN
				    , "CommentSNToSearch": opts.ViewCommentSN
				    , "PageNo": curPageNo
				    , "PageSize": opts.CommentPageSize
				    , "PageNo_Old": $pagerSec.pager("option", "oldpage")
				    , "CommentSN_BlockStart_Old": $pagerSec.pager("option", "pagecut")
			    },
			    function(results, context) {
			        $commentList.html(results[0]);
			        $feed.find(".btMore").remove();
			        if (opts.ViewCommentSN > 0) {//댓글로 페이지 찾아갈 경우
			            $pagerSec.pager("option", "currentPageNo", results[3]);
			        }
			        $pagerSec.pager("update", results[1]);
			        $pagerSec.pager("option", "pagecut", results[2]);
			        $pagerSec.pager("option", "oldpage", curPageNo);
			        if (results[1] > 0) {
			            $pagerSec.show();
			            $commentSec.slideDown();
			            $(".commentWrite", $feed).show();
			            $feed.find(".action .cmnt").addClass("cmntOpen").removeClass("cmntClose");
			            $commentSec.data("isOpen", true);


			            var $num = $("span.cmnt span.num", $feed);
			            $num.text(results[1]);

			        } else {
			            $(".action .cmnt button", $feed).trigger("click");
			        }

			        self._bindEventToComment();
			    }
		    );

	}

	, _bindEventToComment: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    $("div.commentList li", $feed).each(function() {
	        var $li = $(this);
	        $(".delete button", $li).click(function(event) {
	            $h.a2s.sendClickLog("Comment_Remove", "{\"feedSN\":\"" + $feed.tagdata().OriginalFeedSN + "\",\"CommentSN\":\"" + $li.tagdata().CommentSN + "\"}");
	            $.confirm({contents:"정말로 삭제하시겠어요?", blockUI:true, confirmAfter : function() {
	                $h.a2s.sendClickLog("Comment_RemoveOk", "{\"feedSN\":\"" + $feed.tagdata().OriginalFeedSN + "\",\"CommentSN\":\"" + $li.tagdata().CommentSN + "\"}");
	                $feed.ajaxNet(
						opts.methodPath + "RemoveComment",
						{
						    "FeedSN": $feed.tagdata().OriginalFeedSN
							, "CommentSN": $li.tagdata().CommentSN
						},
						function(results, context) {
						    var $pagerSec = $(".pagerSec", $feed);
						    var curPageNo = $pagerSec.pager("option", "currentPageNo")

						    if (curPageNo > 1 && $("div.commentList li", $feed).size() == 1) {
						        $pagerSec.pager("option", "currentPageNo", curPageNo - 1);
						    }
						    self.getCommentList($(".action .cmnt button", $feed).get(0));
						    var $num = $("span.cmnt span.num", $feed);
						    var num = parseInt($num.text()) - 1;
						    $num.text(num);
						    //$pagerSec.pager("option", "currentPageNo", "1");								
						}
					);
	            }}); //end of confirm
	            return false;
	        });

	        $li.bind("mouseenter focusin", function() {
	            $(this).addClass("current");
	        }).bind("mouseleave focusout", function() {
	            $(this).removeClass("current");
	        });

	        // @댓글콜
	        $(".call button", $li).click(function(event) {
	            var $ta = $("textarea.textarea01", $feed);
	            $ta.val($ta.val() + "@" + $li.tagdata().NickName + "@ ");

	            $h.a2s.sendClickLog("Comment_CallWrite", "{\"NickName\":\"" + $li.tagdata().NickName + "\"}");

	            $ta.focus().putCursorAtEnd();
	            $ta.watermark("option", "container").hide();
	            return false;
	        });

	        if (opts.ViewCommentSN > 0 && opts.ViewCommentSN == $li.tagdata().CommentSN) {
	            $li.addClass("selected");
	            opts.ViewCommentSN = 0;
	        }

	        if ($.LoginUserInfo.IsLogin) {
	            var writerSN = $li.tagdata().NexonSN;
	            if ($.PlugUtil.IsOnline(writerSN)) {
	                $li.find(".action").before(
				 $('<span class="plug" />').html(
					  '<img src="http://s.nx.com/S2/p2/icon/ico_plug_b.gif" width="10" height="14" alt="plug" /><span>대화</span>'
					  ).click(function() {
					      $.PlugUtil.OpenChatWindow(writerSN);
					  })
				);
	            }
	        }

	    }); //end of each
	}

	, createComment: function(initContent) {
	    if (!$.LoginUserInfo.IsLogin) {
	        $.Login.confirmLogin();
	        return false;
	    }

	    var self = this, opts = self.options, $feed = self.element;
	    var feedSN = $feed.tagdata().OriginalFeedSN;
	    var $pagerSec = $(".pagerSec", $feed);
	    
		var $input = $("textarea.textarea01", $feed);

		var content = ( initContent == undefined ) ? $input.val() : initContent		

	    if (content.length <= 0) {
	        $.alert("앗! 내용이 없어요."); return false;
	    }
	    if (content.length > 140) {
	        $.alert("아앗! 내용이 너무 길어요.<br/>140자 이내로 작성해 주세요."); return false;
	    }

	    $h.a2s.sendClickLog("Comment_Write", "{\"feedSN\":\"" + $feed.tagdata().OriginalFeedSN + "\"}");
	    $feed.ajaxNet(
			opts.methodPath + "CreateComment",
			{
			    "FeedSN": feedSN
				, "Content": content
			},
			function(results, context) {
			    $pagerSec.pager("option", "currentPageNo", "1");
			    self.getCommentList();
			    $input.val("").trigger("blur");

			    var $num = $("span.cmnt span.num", $feed);
			    var num = parseInt($num.text()) + 1;
			    $num.text(num);

			    $feed.find("textarea").trigger("focusout");
			}
		);
	}
	, toggleFavorite: function(elem) {
	    var self = this, opts = self.options, $feed = self.element;
	    var feedSN = $feed.tagdata().OriginalFeedSN;
	    var $elem = $(elem);
	    if ($elem.data("IsFavorite") === undefined)
	        $elem.data("IsFavorite", $feed.tagdata().IsFavorite);

	    if (!$elem.data("IsFavorite")) {
	        $h.a2s.sendClickLog("Chat_FavoriteAdd", "{\"feedSN\":\"" + feedSN + "\"}");
	        $feed.ajaxNet(
				opts.methodPath + "CreateFavorite",
				{
				    "FeedSN": feedSN
				},
				function(results, context) {
				    $elem.data("IsFavorite", true);
				    $elem.parent()
						.removeClass("scrap1")
						.addClass("scrapOn");
				    $.alert("챗을 찜 했어요!<br/>찜 한 챗은 프로필의 찜 메뉴에서 모아 볼 수 있어요.<br/>유용한 챗은 보는 즉시 바로 찜! 해놓으세요.<br/>");
				}
			);
	    } else {
	        $h.a2s.sendClickLog("Chat_FavoriteRemove", "{\"feedSN\":\"" + feedSN + "\"}");
	        $feed.ajaxNet(
				opts.methodPath + "RemoveFavorite",
				{
				    "FeedSN": feedSN
				},
				function(results, context) {
				    $elem.data("IsFavorite", false);
				    $elem.parent()
						.removeClass("scrapOn")
						.addClass("scrap1");
				    $.alert("찜을 취소했어요!");
				}
			);
	    } //end of else
	}

	, removePing: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    var nexonSN = $feed.tagdata().OriginalNexonSN;
	    var nickName = $feed.tagdata().OriginalNickName;
	    $.confirm("흑흑~ 정말로 핑을 끊으시겠어요? <br/> 더 이상 소식을 못 받는데도요?", function() {
			$feed.removePing( nexonSN, {
				success: function() {
					if ($("#d_chatListWrapper .d_day0").is(":visible")) {
				        $("#d_chatListWrapper").feed("getList", true);
				    }

				    if ($.LeftProfile === undefined) {
				        var objNum = $("div.sideSec > div.profile > dl.ping > dd");
				        objNum.text(objNum.text().parseInt() - 1);
				    } else {
				        $.HeaderProfile.UpdateRemovePingSec(nexonSN);
				    }
				}
			} );
	    });
	}
	, hideFeed: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    var nexonSN = $feed.tagdata().NexonSN;
	    var nick = $feed.tagdata().NickName;
	    $h.a2s.sendClickLog("Chat_Hide", "{\"nexonSN\":\"" + nexonSN + "\"}");

	    $.confirm(nick + "님의 챗을 오늘 하루만 안보시겠어요? 다시 보시고 싶으실 때는 <br/>프로필 > 설정에서 변경할 수 있어요.", function() {
	        $feed.ajaxNet(
				"/config/blockusermethod.aspx?_vb=AddToHiddenList",
				{
				    "NexonSN_ToHide": nexonSN
				},
				function(results, context) {
				    if ($("#d_chatListWrapper .d_day0").is(":visible")) {
				        $("#d_chatListWrapper").feed("getList", true);
				    }
				}
			);
	    });
	}

	, addBestPing: function() {
	    var self = this, opts = self.options, $feed = self.element;
	    var OriginalNexonSN = $feed.tagdata().OriginalNexonSN;
	    var OriginalNickName = $feed.tagdata().OriginalNickName;
	    $feed.ajaxNet(
		  "/xsm/method/chatmethod.aspx?_vb=AddFavorite",
		  {
		      "PingNexonSN": OriginalNexonSN
		  },
		  function(results, context) {
		      var contents = "";
		      contents += "<p class=\"msg\"><span class=\"nickname\">" + OriginalNickName + "</span>님을 친핑으로 등록했어요.<br/>친핑의 소식은 내홈에서 모아볼 수 있어요.</p>";
		      contents += "<p class=\"userImg\"><img src=\"http://photo.nexon.com/ShowUserThumbnail.nhs?Key=" + OriginalNexonSN + "&Size=50\" width=\"50\" height=\"50\" alt=\"" + OriginalNickName + "\" /></p>";

		      $.alert({
		          title: "",
		          contents: contents,
		          closeButton: "<p class=\"btClose\"><button type=\"button\"><span>레이어 닫기</span></button></p>",
		          confirmButton: "",
		          confirmAfter: null
		      })
			.attr("class", "layer1 layerPingGroup");
		  }
	  );
	}



    });

})(jQuery);


$.Video = {			
	play: function( feedSN, a2soi ) {
		$h.a2s.sendClickLog( a2soi, "{\"feedSN\":\"" + feedSN + "\"}" );
		
		$.ajaxNet(
			opts.methodPath + "GetVideoTag"
			, {
			    "FeedSN": feedSN
			}
			, function(results, context) {
			    if (results[0] == "Link") {
			        window.open(results[1]);
			    } else {
			        var wrapper = $("<div />").addClass("layerVideo"),
					  closeButton = $("<div class='btCloseDeco'></div><p class='btClose'><button type='button'><span>레이어 닫기</span></button></p>"),
					  layerOption = {
					      blockUI: true,
					      alignCenter: true,
					      className: "d_VideoPlayLayer"
					  };

			        closeButton.bind("click", function() {
			            wrapper.find("embed").remove();
			            wrapper.empty();
			            wrapper.gLayer("close");
			        });

			        wrapper.css({
			            width: results[2],
			            height: results[3]
			        })
				  .bind('mouseenter',
					  function() {
					      closeButton.stop()
						.css({ opacity: 1 })
						.hide()
						.show("fade", {}, 500, null);
					  }
				  )
				  .bind('mouseleave',
					function() {
					    closeButton.stop()
					  .css({ opacity: 0 })
					  .show()
					  .hide("fade", {}, 500, null);
					}
				  )
				  .append(closeButton)
        		  .append($(results[1]))
        		  .gLayer(layerOption).gLayer("open")
        		  ;
			    }


			} // end of fuction
		);
    }
    ,
    noteplay: function(feedSN, a2soi) {
        $h.a2s.sendClickLog(a2soi, "{\"feedSN\":\"" + feedSN + "\"}");

        $.ajaxNet(
			opts.methodPath + "GetNoteVideoTag"
			, {
			    "FeedSN": feedSN
			}
			, function(results, context) {
			    if (results[0] == "Link") {
			        window.open(results[1]);
			    } else {
			        var wrapper = $("<div />").addClass("layerVideo"),
					  closeButton = $("<div class='btCloseDeco'></div><p class='btClose'><button type='button'><span>레이어 닫기</span></button></p>"),
					  layerOption = {
					      blockUI: true,
					      alignCenter: true,
					      className: "d_VideoPlayLayer"
					  };

			        closeButton.bind("click", function() {
			            wrapper.find("embed").remove();
			            wrapper.empty();
			            wrapper.gLayer("close");
			        });

			        wrapper.css({
			            width: results[2],
			            height: results[3]
			        })
				  .bind('mouseenter',
					  function() {
					      closeButton.stop()
						.css({ opacity: 1 })
						.hide()
						.show("fade", {}, 500, null);
					  }
				  )
				  .bind('mouseleave',
					function() {
					    closeButton.stop()
					  .css({ opacity: 0 })
					  .show()
					  .hide("fade", {}, 500, null);
					}
				  )
				  .append(closeButton)
        		  .append($(results[1]))
        		  .gLayer(layerOption).gLayer("open")
        		  ;
			    }


			} // end of fuction
		);
    }    
};

$.Media = {
    mediaview: function(feedSN) {
        $.ajaxNet(
			"/xsm/method/chatmethod.aspx?_vb=GetVideoTag"
			, {
			    "FeedSN": feedSN
			}
			, function(results, context) {
			    if (results[0] == "Link") {
			        window.open(results[1]);
			    } else {
			        $("#mediaMovieView").html(results[1]);
					if ( $("div.d_mediadecomovie object").size() > 0 ) 
					{
						$("div.d_mediadecomovie object").each(function() {
							var restrict_length = 640;
							var xHeight = 426;
							var width = $(this).attr("width");
							var height = $(this).attr("height");

							var ratio = width / height;

							var newwidth = 0;
							var newheight = 0;
							if (width > restrict_length) {
								newheight = Math.round(restrict_length / ratio);
								width = restrict_length;
								height = newheight;
							}

							if (height > xHeight) {
								newwidth = Math.round(xHeight * ratio);
								width = newwidth;
								height = xHeight;
							}

							$(this).attr("width", width).attr("height", height);
							$(this).parent("div.deco").attr("style", "position:absolute;left:" + (429 - width / 2) + "px; top:" + (274 - height / 2) + "px; text-align: center;");
						});
					}
					else if ( $("div.d_mediadecomovie embed").size() > 0 )
					{
						$("div.d_mediadecomovie embed").each(function() {
							var restrict_length = 640;
							var xHeight = 426;
							var width = $(this).attr("width");
							var height = $(this).attr("height");

							var ratio = width / height;

							var newwidth = 0;
							var newheight = 0;
							if (width > restrict_length) {
								newheight = Math.round(restrict_length / ratio);
								width = restrict_length;
								height = newheight;
							}

							if (height > xHeight) {
								newwidth = Math.round(xHeight * ratio);
								width = newwidth;
								height = xHeight;
							}

							$(this).attr("width", width).attr("height", height);
							$(this).parent("div.deco").attr("style", "position:absolute;left:" + (429 - width / 2) + "px; top:" + (274 - height / 2) + "px; text-align: center;");
						});
					} else if ( $("div.d_mediadecomovie iframe").size() > 0 ) {

						$("div.d_mediadecomovie iframe").each(function() {
							var restrict_length = 640;
							var xHeight = 426;
							var width = $(this).attr("width");
							var height = $(this).attr("height");

							var ratio = width / height;

							var newwidth = 0;
							var newheight = 0;

							if (width > restrict_length) {
								newheight = Math.round(restrict_length / ratio);
								width = restrict_length;
								height = newheight;
							}

							if (height > xHeight) {
								newwidth = Math.round(xHeight * ratio);
								width = newwidth;
								height = xHeight;
							}

							$(this).attr("width", width).attr("height", height);
							$(this).parent("div.deco").attr("style", "position:absolute;left:" + (429 - width / 2) + "px; top:" + (274 - height / 2) + "px; text-align: center;");
						});
					}
			    }
			} // end of fuction
		);
    }
    , medialayer: function(nexonSN, feedSN, a2soi) {
        $("#mediaMovieView").html('');
        $h.a2s.sendClickLog(a2soi, "{\"feedSN\":\"" + feedSN + "\"}");
        $.ajaxNet(
			"/xsm/method/chatmethod.aspx?_vb=GetMediaListForView"
			, {
			    "NexonSN": nexonSN
			    , "FeedSN": feedSN
			    , "HitType": 1
			    , "PageSize": 1
			}
			, function(results, context) {
			    $("div.layerPhotoNVideo").gLayer("close");
			    $("div.layerPhotoNVideo").remove();
			    var wrapper = $("<div />").addClass("layerPhotoNVideo"),
				  closeButton = $("<p class='btClose'><button type='button'><span>레이어 닫기</span></button></p>"),
				  layerOption = {
				      blockUI: true,
				      top: 90,
				      alignCenter: true,
				      scrollEnable: false,
				      className: "d_layerPhotoNVideo",
				      minZIndex: 8000
				  };

			    closeButton.bind("click", function() {
			        $("#mediaMovieView").html('');
			        wrapper.empty();
			        wrapper.gLayer("close");
			    });

			    wrapper.append(closeButton)
    		    .append($(results))
    		    .gLayer(layerOption).gLayer("open");
				wrapper.gLayer("getBlock" ).bind("click", function() { closeButton.click(); } );
				$.setPng24();
			    $("#mediaLayer").find(".d_feed:eq(0)")
                .feedview({ showCommentsOnLoad: true, ViewFeedSN: feedSN })
			    $("div.viewerPrev>ul li").each(function(idx) {
			        $(this).removeClass("current");
			        if (feedSN == $(this).tagdata().FeedSN) {
			            $(this).addClass("current");
			            if (idx == 0) {
			                $("div.viewerNavi > a.prev").removeClass().addClass("pagerBt prevNone");
			            }
			            if (idx == $("div.viewerPrev>ul li").size() - 1) {
			                $("div.viewerNavi > a.next").removeClass().addClass("pagerBt nextNone");
			            }
			        }
			    });

			    $("div.d_mediadecophoto>a img", wrapper).each(function() {
			        var restrict_length = 640;
					var xHeight = 426;
			        $(this).load(function() {
			            var width = $(this).attr("width");
			            var height = $(this).attr("height");

			            var ratio = width / height;

			            var newwidth = 0;
			            var newheight = 0;

			            if (width > restrict_length) {
			                newheight = Math.round(restrict_length / ratio);
			                width = restrict_length;
			                height = newheight;
			            }

			            if (height > xHeight) {
			                newwidth = Math.round(xHeight * ratio);
			                width = newwidth;
			                height = xHeight;
			            }

			            $(this).attr("width", width).attr("height", height);
			            $(this).parent().parent("div.deco").attr("style", "position:absolute;left:" + (429 - width / 2) + "px; top:" + (274 - height / 2) + "px; text-align: center;");
			        });
			    });

			} // end of fuction
		);
    }
    , medialayerPrev: function(nexonSN, feedSN) {
        var arr = new Array();
        $("div.viewerPrev>ul li").each(function(idx) {
            arr[idx] = $(this).tagdata().FeedSN;
            if (feedSN == $(this).tagdata().FeedSN) {
                if (idx > 0) {
                    $("#mediaMovieView").html('');                
                    $.Media.medialayer(nexonSN, arr[idx - 1], 'a2sToDo');
                }
                else {
                    $.alert('사진/동영상이 더이상 없어요!');
                }
            }
        });
    }
    , medialayerNext: function(nexonSN, feedSN) {
        var arr = new Array();
        $("div.viewerPrev>ul li").each(function(idx) {
            arr[idx] = $(this).tagdata().FeedSN;
        });
        $("div.viewerPrev>ul li").each(function(idx) {
            if (feedSN == $(this).tagdata().FeedSN) {
                if (idx < arr.length - 1) {
                    $("#mediaMovieView").html('');                
                    $.Media.medialayer(nexonSN, arr[idx + 1], 'a2sToDo');
                }
                else {
                    $.alert('사진/동영상이 더이상 없어요!');
                }
            }
        });
    }
};
