//
// @(#)sns.js
//

$(function() {
	//Facebook
	window.fbAsyncInit = function() {
		FB.init({
			appId: "114972465214398",
			xfbml: true,
			cookie: true,
			status: true
		});
	};
	(function(){
	var e = document.createElement('script'); e.async = true;
	e.src = document.location.protocol + '//connect.facebook.net/ko_KR/all.js';
	document.getElementById('fb-root').appendChild(e);
	}());

	if(typeof(AA)=="undefined"){ AA={}; }
	if(typeof(AA.Sns)=="undefined"){ AA.Sns={}; }
	AA.Sns.getAnchor=function(s) {
		var anchorRegExp = new RegExp(/#(.*)$/);
		var a = s.match(anchorRegExp);
		return (a && a[1]) ? a = a[1] : "";
	};
	AA.Sns.getPopupFeature=function(w,h,f) {
		var gab = 20;
		var wx = (window.screen.width-w)/2;
		var wy = (window.screen.height-h)/2;
		if(wy > gab) {
			wy -= gab;
		}
		var features = "width="+w+",height="+h+",left="+wx+",top="+wy+",toolbar=no,menubar=no,location=no,status=no";
		if (f != null) {
			features += "," + f;
		}
		return features;
	};
	$('.sns-twitter').click(function(){
		var a = AA.Sns.getAnchor(this.getAttribute('href'));
		var w=window.open("/sns/twitter/"+a, "sns_twitter", AA.Sns.getPopupFeature(770,400,"resizable=no") );
		w.focus();
	});
	$('.sns-me2day').click(function(){
		var a = AA.Sns.getAnchor(this.getAttribute('href'));
		var w=window.open("/sns/me2day/"+a, "sns_me2day", AA.Sns.getPopupFeature(950,600,"resizable=no") );
		w.focus();
	});
	$('.sns-cyworld').click(function(){
		var a = AA.Sns.getAnchor(this.getAttribute('href'));
		var xmlUrl="http://www.archeage.com/sns/cyworld/"+a;
		var apiKey="XOvcIfKKf5RPkiIFlalTyvBBQfdfltnR";
		
		var w=window.open("http://api.cyworld.com/openscrap/post/v1/?xu="+xmlUrl+"&sid="+apiKey, "sns_cyworld", AA.Sns.getPopupFeature(450,410,"resizable=no"));
		w.focus();
	});
	$('.sns-facebook').click(function(){
		var a = AA.Sns.getAnchor(this.getAttribute('href'));
		var loginStatus = false;
		FB.getLoginStatus(function(response) {
			if (response.session) {
				loginStatus = true;
			} else {
				FB.login(function(response) {
					if (response.session) {
						alert("페이스북 로그인되었습니다. 다시한번 시도해주세요.");
					}
				});
			}
		});
		if(!loginStatus) {
			return;
		}
		$.getJSON("/sns/facebook/"+a, {"noCache":new Date().getTime()}, function(data) {
			if(data == null) {
				alert("대상을 찾을 수 없습니다.\n올바르지 않은 조작입니다.");
				return;
			}
			FB.ui({
				method: "stream.publish",
				attachment: data
			}, function(res) {
				if(res && res.post_id) {
					//
				}
			});
		});
	});
});

// EOF
