function openWindowWithPost(url,name,keys,values, option) {
	var newWindow;
	if(typeof  option == "undefined") {
		newWindow = window.open(url, name);
	} else {
		newWindow = window.open(url, name, option);
	}
	
	if (!newWindow) return false;
	var html = "";
	html += "<html><head>";
	html += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; 
	html += "</head><body><form id='formid' method='post' action='" + url + "'>";
	if (keys && values && (keys.length == values.length))
		for (var i=0; i < keys.length; i++)
			html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";
	html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</script></body></html>";
	
	newWindow.document.write(html);
	return newWindow;
}

function openWindowWithPost1(url,name,keys,values, option) {
	var html = "<form name='"+name+"' id='formid' method='post' target='"+name+"'>";
	if (keys && values && (keys.length == values.length))
		for (var i=0; i < keys.length; i++)
			html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";
	html += "</form>"
	var frm = $(html).get(0);
	frm.action = url;
	frm.target = name;
	
	if(typeof option == "undefined") {
		frm.onsubmit = function() {
			window.open(url, name);
		};
	} else {
		frm.onsubmit = function() {
			window.open(url, name, option);
		};
	}
	frm.submit();
}

function __afterTwitterSetting() {
}

bugs.personMedia = {
		layer : null,
		setting : function() {
			if(!g_isLogged) {
				bugs.ui.showLoginLayer();
				return;
			}
	
			var url = g_urlAjax+"/external/connectionManager";
			$.get(url, function(result) {
				if(result != "") {
					if(bugs.personMedia.layer != null) {
						bugs.personMedia.layer.remove();
					}
					bugs.personMedia.layer = $(result);
					bugs.personMedia.layer.css(
							{position : "absolute", left : "50%", top : 186, "margin-left" : -227}
					);
					bugs.personMedia.layer.appendTo("body");

					/*
					__afterTwitterSetting = function() {
						alert("");
						__afterTwitterSetting();
					};
					*/
				} else {
				}
			});
		},
		twitterAuth : function() {
			bugs.personMedia.close();
			bugs.twitter.popupAuth();
		},
		cancelTwitter : function() {
			var url = g_urlAjax+"/external/twitter/cancel";
			$.post(url, function(result) {
				if(result == "200") {

					$("#twitterAuthY").hide();
					$("#twitterAuthN").show();
					bugs.ui.showAlert("연동이 해제 되었습니다.", {css : "layerAdultNotice"});
				} else {
					bugs.ui.showAlert("연동 해제가 실패 하였습니다.", {css : "layerAdultNotice"});
				}
			});
		},
		close : function() {
			
			if(bugs.personMedia.layer != null) {
				bugs.personMedia.layer.hide();
				bugs.personMedia.layer.remove();
				
			}
		}
	}



bugs.twitter = {
	logId : 201,
	popupAuth : function(text, isSave) {
		if(typeof isSave == "undefinde") isSave = true;
	
		var url = g_urlWww + '/external/twitter/authorize?decorator=blank';
		if(isSave == false) {
			url = url + "&save=false";
		}
		if(!bugs.valid.empty(text)) {
			url = url + "&text=" + encodeURIComponent(text); 
			//var win = openWindowWithPost1(url, "_twitter", ["text"], [text], 'width=800,height=400,top=100,left=100,resizable=yes,scrollbars=no');
			//win.focus();
		} else {
//			var win = window.open(url,'_twitter','width=800,height=400,top=100,left=100,resizable=yes,scrollbars=no');
//			win.focus();
		}
		var win = window.open(url,'_twitter','width=800,height=400,top=100,left=100,resizable=yes,scrollbars=no');
		win.focus();

	},
	popupStatus : function(text) {
		if(typeof text == "undefined") return;
		var url =  "http://twitter.com/home/?status=" + encodeURIComponent(text);
		var win = window.open(url,'_twitterStatus','width=820,height=600,top=100,left=100,resizable=yes,scrollbars=no');
		win.focus();
	},
	checkAuth : function(success, fail) {
		var url = g_urlAjax+'/external/twitter/checkAuth';
		$.get(url, function(result) {
			bugs.log.log(result);
			if(typeof result !="undefined" && result == "1") {
				success();
			} else {
				fail();
			}
		});
	},
	textTemplate : '<h2 class="twitter">트위터로 보내기</h2>' +
		'<div class="sendCode">' +
		'	<p><%=text%></p>' +
		'</div>' +
		'<div class="btnArea">' +
		'	<span class="button typeME"><a href="javascript:void(0);">확인</a></span>' +		
		'	<span class="button typeM"><a href="javascript:void(0);">취소</a></span>' +
		'</div>',
	connectTemplate : '<p class="textAdminC">'+
		'	트위터 인증정보를 저장하시면<br />보다 쉽게 공유할 수 있습니다.'+
		'</p>'+
		'<div class="btnArea">'+
		'	<span class="button typeME"><a href="javascript:void(0);">확인</a></span>'+
		'	<span class="button typeM"><a href="javascript:void(0);">취소</a></span>'+
		'</div>',
	updateLayer : null,
	updateStatus : function(text) {
		bugs.twitter.popupStatus(text);
		return;
		if(!g_isLogged) {
			bugs.twitter.statusPopup(text, function() {
				bugs.twitter.popupAuth(text);
			});
			//bugs.twitter.popupStatus(text);
			return;
		} else {
			bugs.twitter.checkAuth(
				function() { // success
					bugs.twitter.statusPopup(text, function() {
						var updateUrl = g_urlAjax+"/external/twitter/updateStatus";
						$.post(updateUrl, {text : text}, function(result) {
							bugs.twitter.updateLayer.hide();
							bugs.twitter.updateLayer.remove();

							if(200 == result) {
								bugs.ui.showAlert("트위터에 보내기 성공 하였습니다.", {css : "layerAdultNotice"});
								return;
							} else {
								bugs.ui.showAlert("트위터에 보내기가 실패 하였습니다.<br/>잠시 후에 다시 시도하여 주세요.", {css : "layerAdultNotice"});
								return;
							}
							
						});
					});
				}, 
				function() { // fail
					bugs.twitter.connectPopup(text);
				}
			);			
		}
	},
	statusPopup : function(text, initOk) {
		var tmp = new bugs.template(bugs.twitter.textTemplate, {text : text});
		bugs.twitter.updateLayer = new bugs.ui.popup(tmp.html(), {
			title : "보내기", css : "layerSendContent" 
		});
		bugs.twitter.updateLayer.layer.find(".btnArea span:first a").click(function() {
			initOk();
			bugs.twitter.updateLayer.hide();
			bugs.twitter.updateLayer.remove();
		}).end().find(".btnArea span:last a").click(function() {
			bugs.twitter.updateLayer.hide();
			bugs.twitter.updateLayer.remove();
		});
		
		bugs.twitter.updateLayer.show();
	},
	popup : null,
	connectPopup : function(text) {
		var connectPopup = new bugs.ui.popup(bugs.twitter.connectTemplate, {
			title : "트위터 연동하기", css : "layerAdminMsg" 
		});
		bugs.twitter.popup = connectPopup;
		bugs.twitter.popup.layer.find(".btnArea span:first a").click(function() {
			bugs.twitter.popupAuth(text);
			bugs.twitter.popup.hide();
			bugs.twitter.popup.remove();
		}).end().find(".btnArea span:last a").click(function() {
			bugs.twitter.popupAuth(text, false);
			//bugs.twitter.popupStatus(text);
			bugs.twitter.popup.hide();
			bugs.twitter.popup.remove();
		});
		bugs.twitter.popup.show();
	},
	artist : function(artistId, artistNm) {
		var url = bugs.url.artistPage(artistId);
		if(typeof url == "undefined") return;
		var text = url + " " + artistNm + " #bugs";
		bugs.twitter.updateStatus(text);
		bugs.logging.add(bugs.twitter.logId, ["twitter", "artist"], [artistId]);
	},
	album : function(albumId, title, artistNm) {
		var url = bugs.url.albumPage(albumId);
		if(typeof url == "undefined") return;
		var text = url + " " + title + " - " + artistNm + " #bugs";
		bugs.twitter.updateStatus(text);
		bugs.logging.add(bugs.twitter.logId, ["twitter", "album"], [albumId]);
	},
	track : function(trackId, title, artistNm) {
		var url = bugs.url.trackPage(trackId);
		if(typeof url == "undefined") return;
		var text = url + " " + title + " - " + artistNm + " #bugs";
		bugs.twitter.updateStatus(text);
		bugs.logging.add(bugs.twitter.logId, ["twitter", "track"], [trackId]);
	}, 
	chart : function(chart, type, cn, date1, date2) {
		if(chart != "bugs") return;
		var text = "";
		var url = g_urlMusic + "/chart/" + chart + "/" + type + "?cn=" + cn;
		if(type=="realtime") {
			text = "벅스 실시간 차트("+date1+") " + url + " #bugs";
		} else if(type=="top1000") {
			text = "벅스 주간 차트("+date1+"~"+date2+" ) " + url + " #bugs";
		}
		bugs.twitter.updateStatus(text);
		bugs.logging.add(bugs.twitter.logId, ["twitter", "chart", type], [cn]);
	},
	cancel : function() {
	}
}

bugs.cyworld = {
		sid : "s0300048",
		popup : function(xu) {
			if(typeof xu == "undefined") return;
			var url = "http://api.cyworld.com/openscrap/post/v1/";
			url += "?xu=" + escape(xu);
			url += "&sid=" + bugs.cyworld.sid;
			var win = window.open(url, 'cyopenscrap', 'width=450,height=410');
			win.focus();
		},
		artist : function(artistId) {
			if(typeof artistId == "undefined") return;
			var xu = "http://www.bugs.co.kr/external/cyworld/artist/" + artistId;
			bugs.cyworld.popup(xu);
			bugs.logging.add(bugs.twitter.logId, ["cyworld", "artist"], [artistId]);
		},
		album : function(albumId) {
			if(typeof albumId == "undefined") return;
			var xu = "http://www.bugs.co.kr/external/cyworld/album/" + albumId;
			bugs.cyworld.popup(xu);
			bugs.logging.add(bugs.twitter.logId, ["cyworld", "album"], [albumId]);
		},
		track : function(trackId) {
			if(typeof trackId == "undefined") return;
			var xu = "http://www.bugs.co.kr/external/cyworld/track/" + trackId;
			bugs.cyworld.popup(xu);
			bugs.logging.add(bugs.twitter.logId, ["cyworld", "track"], [trackId]);
		},
		chart : function(chart, type, cn) {
			if(typeof type == "undefined") return;
			if(typeof cn == "undefined") return;
			var xu = "http://www.bugs.co.kr/external/cyworld/chart/" + type + "/" + cn;
			bugs.cyworld.popup(xu);
			bugs.logging.add(bugs.twitter.logId, ["cyworld", "chart", type], [cn]);
		}
}
