/**
 * Created by mkhong on 2017-02-14.
 */

var Sns = function(){
    this.open_window = function (url) {
        var width = window.screen.width / 3;
        var height = window.screen.height / 2;
        var share_url = encodeURIComponent(this.url);
        var leftPosition, topPosition;
        //Allow for borders.
        leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
        //Allow for title and status bars.
        topPosition = (window.screen.height / 2) - ((height / 2) + 50);
        var windowFeatures = "status=no,height=" + height + ",width=" + width + ",resizable=yes,left=" + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY=" + topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no";
        window.open(url, 'sharer', windowFeatures);


        var setShareUrl = function (share_url) {
            this.share_url = share_url;
        }
    };

    this.getMetaData = function (property) {
        var metas = document.getElementsByTagName('meta');
        for (var i=0; i<metas.length; i++) {
            if (metas[i].getAttribute("property") == property) {
                return metas[i].getAttribute("content");
            }
        }
        return "";
    };

    this.isMobile = (function () {
        if (navigator.userAgent.match(/android/i)) {
            return 'and';
        }
        else if (navigator.userAgent.match(/(iphone)|(ipod)|(ipad)/i)) {
            return 'ios';
        }
        return false;
    })();


    this.kakaoID = "7055a5f2da2dbc3ae48f8be6b6f474ae";
    this.url = location.href;
    this.text = document.title;

    if (Kakao) {
        Kakao.init(this.kakaoID);
    }
};

Sns.prototype = {
    setUrl : function (url) {
        this.url = url;
    },
    setText : function (text) {
        this.text = text;
    },
    facebook : function() {
        this.open_window('http://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(this.url) + '&t=' + encodeURIComponent(this.text));
    },
    twitter : function(text) {
        if(!text) text = this.text;
        this.open_window('https://twitter.com/intent/tweet?url=' + encodeURIComponent(this.url) + '&text=' + encodeURIComponent(text));
    },
    kakaoTalk : function() {

        Kakao.Link.sendDefault({
            objectType: 'feed',
            content: {
                title: this.getMetaData('og:title'),
                description: this.getMetaData('og:description'),
                imageUrl: this.getMetaData('og:image'),
                link: {
                    mobileWebUrl: this.url,
                    webUrl: this.url
                }
            }
        });
    },
    kakaoStory : function() {

        if (this.isMobile == 'and' || this.isMobile == 'ios') {
            Kakao.Story.open({
                url: this.url,
                text: this.getMetaData('og:description'),
                urlInfo : {
                    title: document.title,
                    desc: [this.getMetaData('og:description')],
                    name: [this.getMetaData('og:url')],
                    images: [this.getMetaData('og:image')]
                }
            });
        } else {
            Kakao.Story.share({
                url: this.url,
                text: this.text
            });
        }

    },
    commonKakaoTalk : function() {
        Kakao.Link.sendDefault({
            objectType: 'feed',
            content: {
                title: this.getMetaData('og:title'),
                description: this.getMetaData('og:description'),
                imageUrl: this.getMetaData('og:image'),
                link: {
                    mobileWebUrl: this.url,
                    webUrl: this.url
                }
            }
        });
    },
    naverBand : function() {
        this.open_window('http://band.us/plugin/share?body=' + encodeURIComponent(this.url) + '&text=' + encodeURIComponent(this.text));
    },
    googlePlus : function() {
        this.open_window('https://plus.google.com/share?url=' + encodeURIComponent(this.url) + '&t=' + encodeURIComponent(this.text));
    },
    naverBlog : function() {
        this.open_window('http://blog.naver.com/openapi/share?url=' + encodeURIComponent(this.url) + '&t=' + encodeURIComponent(this.text));
    }
};
