$(document).ready(function() {
	likes.popHtml = "		<dl>"
					+ "			<dt><strong>보관함에 추가되었습니다.</strong></dt>"
					+ "			<dd class=\"text\">담기나 좋아를 클릭하면 내 뮤직라이프의 보관함에<br>담기고 <strong>좋아 점수가 +1</strong> 됩니다.</dd>"
					+ "			<dd class=\"check\">"
					+ "			<input type=\"checkbox\" id=\"noMessage\" class=\"checkbox\" onclick=\"javscript:likes.setNoPop();\" /> <label for=\"noMessage\">앞으로 이 메시지 표시 안 함</label>"
					+ "			</dd>"
					+ "		</dl>"
					+ "		<div class=\"btnArea\">"
					+ "			<span class=\"button typeME\"><a href=\""+g_urlMe+"/library/song\" type=\"ok\">보관함 보기</a></span>"
					//+ "			<span class=\"button typeM\"><a href=\"javascript:void(0);\" onclick=\"javascript:$('div.layerPopup.noTitle.layerAddMylist').hide('blind', 'normal', {}, function() { $('div.layerPopup.noTitle.layerAddMylist').remove(); bugs.ui.modalLayer.hide(); });\">닫기</a></span>"
					+ "			<span class=\"button typeM\"><a href=\"#\" type=\"cancel\">닫기</a></span>"
					+ "		</div>";

	//likes.getPopYn();
});

	if ("undefined" == typeof likes)
	{
		likes = {
			TYPE_TRACK : "TRACK",
			TYPE_ARTIST : "ARTIST",
			TYPE_ALBUM : "ALBUM",
			TYPE_MV : "MV",
			TYPE_PLAYLIST : "PLAYLIST",
			TYPE_MEMBER : "MEMBER",
			TYPE_MY_COMMENT : "MY_COMMENT",

			draggble_count : 0,
			draggable_pos : 0,
			draggable_ids : "",
			draggable_target : 0,
			draggable_priority : 0,
			draggable_data_reset : false,
			draggable_data_from : 0,
			drag_on : false,
			popHtml : "",
			pop : false,
			popCheck : false,
			openWindow : false,
			// 뉴벅스 이벤트 ===============================================
			//eventLikesType : "",
			//eventMissionLike : false,
			//============================================================
		    add : function (id, type, callbackTrue, callbackFalse)
		    {
				//likes.eventLikesType = type;	// 뉴벅스 이벤트
				
				try{
					$.getJSON(g_urlLikes+"/add/"+type+"/"+id, {}, function(data) {
						if (data.result) {
							if (likes.pop)	likes.showAddAlert();
							else if (!likes.popCheck)	likes.getPopYn();
							//else	likes.openNbugsEventPop(true);	// 뉴벅스 이벤트
							
							if(typeof(callbackTrue) == "string") eval(callbackTrue);
							else if(typeof(callbackTrue) == "function") callbackTrue(data);
						}
						else {
							if(data.error_type == "NOT_LOGGED")	bugs.ui.showLoginLayer();
							
							if(typeof(callbackFalse) == "string") eval(callbackFalse);
							else if(typeof(callbackFalse) == "function") callbackFalse(data);
						}
					});
				} catch (e) {
					if(typeof(callbackFalse) == "string") eval(callbackFalse);
					else if(typeof(callbackFalse) == "function") callbackFalse();
				}
		    },
		    adds : function (ids, type, callbackTrue, callbackFalse)
		    {
		    	//likes.eventLikesType = type;	// 뉴벅스 이벤트
				
		    	var splitIDs = ids.split(",");
		    	if (splitIDs.length > 150) {
					bugs.ui.showAlert("1회에 150개까지 가능합니다.", {css : "layerAlert layerAdultNotice"});
					return;
		    	}
		    	
				try{
					$.post(g_urlLikes+"/adds/"+type, {"ids": ids}, function(data) {
						if (data.result) {
							if (likes.pop)	likes.showAddAlert();
							else if (!likes.popCheck)	likes.getPopYn();
							//else	likes.openNbugsEventPop(true);	// 뉴벅스 이벤트
							
							if(typeof(callbackTrue) == "string") eval(callbackTrue);
							else if(typeof(callbackTrue) == "function") callbackTrue(data);
						}
						else {
							if(data.error_type == "NOT_LOGGED")	bugs.ui.showLoginLayer();
							
							if(typeof(callbackFalse) == "string") eval(callbackFalse);
							else if(typeof(callbackFalse) == "function") callbackFalse(data);
						}
					}, "json");
				} catch (e) {
					if(typeof(callbackFalse) == "string") eval(callbackFalse);
					else if(typeof(callbackFalse) == "function") callbackFalse();
				}
		    },
		    del : function (id, type, callbackTrue, callbackFalse)
		    {
				try{
					$.getJSON(g_urlLikes+"/delete/"+type+"/"+id, {}, function(data) {
						if (data.result) {
							if(typeof(callbackTrue) == "string") eval(callbackTrue);
							else if(typeof(callbackTrue) == "function") callbackTrue(data);
						}
						else {
							if(data.error_type == "NOT_LOGGED")	bugs.ui.showLoginLayer();
							
							if(typeof(callbackFalse) == "string") eval(callbackFalse);
							else if(typeof(callbackFalse) == "function") callbackFalse(data);
						}
					});
				} catch (e) {
					if(typeof(callbackFalse) == "string") eval(callbackFalse);
					else if(typeof(callbackFalse) == "function") callbackFalse();
				}
		    },
		    getPopYn : function()
		    {
		    	$.getJSON(g_urlLikes+"/check/pop", {}, function(data) {
		    		likes.pop = data;
		    		likes.popCheck = true;
		    		
		    		if (likes.pop)	likes.showAddAlert();
		    		//else	likes.openNbugsEventPop(true);	// 뉴벅스 이벤트
				});
		    },
		    setNoPop : function ()
		    {
		    	if ($("#noMessage").attr("checked")) {
			    	$.getJSON(g_urlLikes+"/set/pop/N", {}, function(data) {
			    		likes.pop = false;
					});
		    	}
		    },
		    deletes : function (ids, type, success, error)
		    {
		    	var splitIDs = ids.split(",");
		    	if (splitIDs.length > 150) {
					bugs.ui.showAlert("1회에 150개까지 가능합니다.", {css : "layerAlert layerAdultNotice"});
					return;
		    	}
		    	
				try{
					$.post(g_urlLikes+"/list/delete/"+type, {"ids": ids}, function(data) {
						if (data.result) {
							success(data);
						} else {
							if(data.error_type == "NOT_LOGGED")	bugs.ui.showLoginLayer();
							error(data);
						}
					}, "json");
				} catch (e) {
					error();
				}
		    },
		    memCount : function (id, type, success, error)
		    {
				if(typeof error == "undefined") error = function() {};
		    	try{
					$.getJSON(g_urlLikes+"/count/members/"+type+"/"+id, {}, function(data) {
						success(data);
					});
				} catch (e) {
					error();
				}
		    },
		    count : function (type, success, error)
		    {
				if(typeof error == "undefined") error = function() {};
		    	try{
					$.getJSON(g_urlLikes+"/count/"+type, {}, function(data) {
						success(data);
					});
				} catch (e) {
					error();
				}
		    },
		    getCount : function (msrl, type, success, error)
		    {
				if(typeof error == "undefined") error = function() {};
		    	try{
					$.getJSON(g_urlLikes+"/count/"+msrl+"/"+type, {}, function(data) {
						success(data);
					});
				} catch (e) {
					error();
				}
		    },
		    showAddAlert : function ()
		    {
		    	if(!likes._successPop) {
		    		likes._successPop = new bugs.ui.popup(likes.popHtml, {css : "noTitle layerAddMylist", onInit : function(popup) {
		    			popup.layer.find("div.btnArea a:first").click(function() {
		    				if(likes.openWindow) window.open($(this).attr("href"));
		    				else document.location.href = $(this).attr("href");
		    				
		    				return false;
		    			});
		    		}});
		    	}
		    	
		    	likes._successPop.show();
		    	//likes.openNbugsEventPop(false);	// 뉴벅스 이벤트
		    },
		    setDraggable : function (JqueryObj)
		    {
		    	JqueryObj.draggable({
		    		appendTo : $("#movingArea"),
		    		cursorAt: { top: -12, left: -20 },
		    		helper: function(event) {
		    				return $('<div class=\"moveBoxWrap\"><div class=\"moveBox no\"><span class=\"mark\">-</span><span class=\"songTitle\">0곡</span></div></div>');
		    		},
		    		start : function(ev, ui) {
		    			likes.drag_on = true;
		    			
		    			if (likes.draggable_data_reset) {
			    			likes.draggble_count = 0;
			    			likes.draggable_ids = "";
			    			
		    				if (likes.draggable_data_from == 1) {
		    					$(this).parent().find("TR").each( function() { 
		    						if ($(this).attr("checked") == "checked") {
			    						likes.draggble_count++;	
			    						if (likes.draggable_ids == "")	likes.draggable_ids = $(this).find("td:first .checkbox > INPUT:first").val();
			    						else	likes.draggable_ids = likes.draggable_ids + "," + $(this).find("td:first .checkbox > INPUT:first").val();
		    						}
		    					} )
		    				}
		    			}
		    		},
				    drag: function(ev, ui) {
			            if ( likes.draggable_pos ) {
			            	if ( likes.draggable_pos == 1 || likes.draggable_pos == 2 )	$(".moveBoxWrap").html("<div class=\"moveBox yes\"><span class=\"mark\">+</span><span class=\"songTitle\">"+likes.draggble_count+"곡</span></div>");
			            	if ( likes.draggable_pos == 3 )	$(".moveBoxWrap").html("<div class=\"moveBox selectTrack\"><span class=\"mark\">↕</span><span class=\"songTitle\">"+likes.draggble_count+"곡</span></div>");
			            } else {
			            	$(".moveBoxWrap").html("<div class=\"moveBox no\"><span class=\"mark\">-</span><span class=\"songTitle\">"+likes.draggble_count+"곡</span></div>");
			            }
			    	},
			    	stop: function(ev, ui) {
			        	if ( likes.draggable_pos == 1 )
			        		likes.adds(likes.draggable_ids, "TRACK", function() {}, function() {});
			        	if ( likes.draggable_pos == 2 )
			        		playlists.add(likes.draggable_target, likes.draggable_ids, function(cnt) {
			        				if (cnt > 0) {
				        				var successPop = new bugs.ui.popup($("#success"), {
					        				onInit : function(popup) {
					        					$("#success .content .putOk > P > EM").text(cnt+"곡");
					        					$("#success .content .button > A").attr("href", g_urlMe+"/library/myalbum/"+likes.draggable_target);
					        				}
				        				});
				        				successPop.show();
				        				setTimeout(function(){ successPop.hide('blind'); }, 10000);
			        				} else {
			    						var msg = "선택된 곡은 이미 수록되어있습니다.";
			    						
			    						bugs.ui.showAlert(msg, {css : "layerAlert layerAdultNotice"});
			    						return;
			        				}
			        			}, function(ret) {
			        			if (ret.error_type == "EXCEED") {
			        				var exceedPop = new bugs.ui.popup($("#exceed"), {
				        				onInit : function(popup) {
				        					$("#exceed .content .putFailure .button > A").attr("href", g_urlMe+"/library/myalbum/"+likes.draggable_target);
				        				}
			        				});
			        				exceedPop.show();
			        				setTimeout(function(){ exceedPop.hide('blind'); }, 10000);
			        			} 
			        		});
			        	if ( likes.draggable_pos == 3 ) {
			        		if (trackPlaylists.sort != "PRIORITY" && trackPlaylists.sort_type != "ASC") {
		        				var sortPop = new bugs.ui.popup($("#sortConfirm"), {});
		        				sortPop.show();
		        				setTimeout(function(){ sortPop.hide('blind'); }, 10000);
					    	} else {
					    		playlists.setPriority(likes.draggable_target, likes.draggable_ids, likes.draggable_priority, function() { trackPlaylists.sortListByPriority(likes.draggable_priority); }, function() {});
					    	}
			        	}
		    			likes.drag_on = false;
		    			
			        	if (likes.draggable_data_reset) {
			        		likes.draggble_count = 0;
			        		likes.draggable_ids = "";
			        	}
			    	}
		    	});
		    },
		    destroyDraggable : function (JqueryObj)
		    {
		    	JqueryObj.draggable('destroy');
		    },
		    checkWinSize : function ()
		    {
		    	 if (window.innerHeight) {
		    		 return window.innerHeight;
		         }
		         else if (document.documentElement && document.documentElement.clientHeight) {
		    		 return document.documentElement.clientHeight;
		         }
		         else if (document.body.clientHeight) {
		    		 return document.body.clientHeight;
		         }
		    },
		    adultLayerShow : function ()
		    {
				var msg = "해당곡은 청소년 유해매체물로서 정보통신망<br />" +  
				"이용 촉진 및 정보보호 등에 관한 법률 및<br />" +
				"청소년보호법의 규정에 의하여<br />" + 
				"19세 미만의 청소년이 이용할 수 없습니다. ";
				
				bugs.ui.showAlert(msg, {css : "layerAlert layerAdultNotice"});
		    },
		    addLike : function (id, type, obj)
		    {
		    	likes.add(id, type, function(ret) { 
		    		obj.find("img").attr("src", g_urlFile+"/common/button/bc_likeCancelOff.gif");
		    		obj.find("img").attr("alt", "좋아");
		    		obj.unbind("click");
		    		obj.removeAttr("onclick");
		    		obj.bind("click", function() { likes.delLike(id, type, obj); });
		    	}, function() {});
		    },
		    delLike : function (id, type, obj)
		    {
		    	likes.del(id, type, function(ret) { 
		    		obj.find("img").attr("src", g_urlFile+"/common/button/bc_likeOff.gif");
		    		obj.find("img").attr("alt", "좋아취소");
		    		obj.unbind("click");
		    		obj.removeAttr("onclick");
		    		obj.bind("click", function() { likes.addLike(id, type, obj); });
		    	}, function() {});
		    },
		    addLikeGr : function (id, type, obj)
		    {
		    	likes.add(id, type, function(ret) { 
		    		obj.find("img").attr("src", g_urlFile+"/common/button/bc_likeCancelGrOff.gif");
		    		obj.find("img").attr("alt", "좋아");
		    		obj.unbind("click");
		    		obj.removeAttr("onclick");
		    		obj.bind("click", function() { likes.delLikeGr(id, type, obj); });
		    	}, function() {});
		    },
		    delLikeGr : function (id, type, obj)
		    {
		    	likes.del(id, type, function(ret) { 
		    		obj.find("img").attr("src", g_urlFile+"/common/button/bc_likeGrOff.gif");
		    		obj.find("img").attr("alt", "좋아취소");
		    		obj.unbind("click");
		    		obj.removeAttr("onclick");
		    		obj.bind("click", function() { likes.addLikeGr(id, type, obj); });
		    	}, function() {});
		    },
		    addBLike : function (id, type, obj)
		    {
		    	likes.add(id, type, function(ret) { 
		    		obj.find("img").attr("src", g_urlFile+"/common/button/bc_likeCancelBOff.gif");
		    		obj.find("img").attr("alt", "좋아");
		    		obj.unbind("click");
		    		obj.removeAttr("onclick");
		    		obj.bind("click", function() { likes.delBLike(id, type, obj); });
		    	}, function() {});
		    },
		    delBLike : function (id, type, obj)
		    {
		    	likes.del(id, type, function(ret) { 
		    		obj.find("img").attr("src", g_urlFile+"/common/button/bc_likeBOff.gif");
		    		obj.find("img").attr("alt", "좋아취소");
		    		obj.unbind("click");
		    		obj.removeAttr("onclick");
		    		obj.bind("click", function() { likes.addBLike(id, type, obj); });
		    	}, function() {});
		    },
		    openLoading : function (type)
		    {
		    	var msg = "";
		    	if (type == "TRACK")	msg = "곡";
		    	else if (type == "MV")	msg = "뮤직비디오";
		    	else if (type == "ARTIST")	msg = "뮤직비디오";
		    	else if (type == "MY_COMMENT")	msg = "한마디";
		    	else if (type == "MEMBER")	msg = "회원";
		    	else if (type == "ALBUM")	msg = "앨범";
		    	else if (type == "PLAYLIST")	msg = "앨범";

		    	if(!likes._loadingPop)
		    		likes._loadingPop = new bugs.ui.popup($("div.layerAlert.layerLoading"), {
	    				onInit : function(popup) {
		    				$("div.layerAlert.layerLoading .content > P").html(msg+" 리스트를 불러오는 중입니다.");
						}
			    	});

		    	likes._loadingPop.show();
		    },
		    closeLoading : function ()
		    {
		    	likes._loadingPop.hide();
		    },
		    closeEventWin : function(chk, type, callback) {
				if (chk) {
					var oDate = new Date();
					oDate.setDate(oDate.getDate() + 7200);
					bugs.cookie.set(type, "true", oDate, "/");
					
					callback();
				}
			},
			openAlert : function(msg) {
				var msg = "<div class=\"putFailure\"><p>"+msg+"</p><span class=\"button typeME\"><a href=\"javascript:$('div.layerPopup.layerPutSong').remove();bugs.ui.modalLayer.hide();\">확인</a></span></div>";
				
				var popup = new bugs.ui.popup(msg, {title : "LOGO", css : "layerPutSong"});
				popup.show();
			}
			/*,
			// 뉴벅스 이벤트
			openNbugsEventPop : function(setModal) {
				var openPop = bugs.cookie.get("NBUGS_LIKE");
				if (!openPop && likes.eventLikesType == "ARTIST") {
					var html = "				<div>"
						+ "				<p class=\"title\"><strong>이벤트 진행중!</strong><br />“좋아” 미션을 완료하셨습니다.</p>"
						+ "				<p><a href=\""+g_urlWww+"/event/nbugs\">이벤트 보기</a></p>"
						+ "				<p class=\"checkbox\"><input name=\"\" type=\"checkbox\" value=\"\" onclick=\"javascript:likes.closeEventWin(this.checked, 'NBUGS_LIKE', function() { $('div.layerPopup.layerEvent250').hide(); bugs.ui.modalLayer.hide(); });\" />5일동안 이 메시지 표시 안 함</p>"
						+ "				<span class=\"button typeME\"><button type=\"button\" onclick=\"javascript:$('div.layerPopup.layerEvent250').hide();";
					
					if (setModal)	html = html + "bugs.ui.modalLayer.hide();";
					
					html = html + "\">확인</button></span>"
						+ "			</div>";
	
					if (likes.eventMissionLike) {
						successPop = new bugs.ui.popup(html, {title : "LOGO", css : "layerPopup layerEvent250", modal : setModal });
				    	successPop.show();
					} else {
						$.getJSON(g_urlLikes+"/event/check", {}, function(data) {
							if (data) {
								successPop = new bugs.ui.popup(html, {title : "LOGO", css : "layerPopup layerEvent250", modal : setModal });
						    	successPop.show();
						    	likes.eventMissionLike = true;
							}
						});
					}
				}
			}
*/
		};
	}