$(function(){
    
    //закрытие всплывающих подсказок при нажатии вне окна
    $("body").click(function (event) {
        if('keyCode' in event) {
            var obj = event.target;
            if($(obj).closest(".desc_soc").length==0 && $(obj).closest(".social_button").length==0)
                $(".desc_soc").hide();
        }
    });


    //Twitter    
    $('#tweet').click(
        function() {
            var el = $('#company_news a.new:visible');
            var prelink = 'https://twitter.com/intent/tweet?source=webclient&';            
            if(el.get(0)) {
                var link = el.attr('href');
                if(link.indexOf('http') == -1) {
                    link = 'http://fibo.ru'+ link;
                }
                goShare(prelink, $(this), {
                        url  : link,
                        text : el.text()+'\n'
                });
            }
        }          
    );

    $('#tweet2').click(
        function() {
            var el = $('.citation_text em');
            var prelink = 'https://twitter.com/intent/tweet?source=webclient&';
            if (el.get(0)){
                var link = 'http://fibo.ru/fibogroup/cit-' + el.attr('id') + '.html';
                var text = el.text();
                if (text.length > 95){
                    text = text.substr(0, text.substr(0, 92).lastIndexOf(' ')) + '...';
                }
                var text = "Fibo.ru - Прямая речь: " + text;
                
                goShare(prelink, $(this), {
                    url :   link,
                    text:   text +'\n'
                });
            }

        }          
    );         


    ///Facebook	
    $('#facebook').click(
        function() {
            var el = $('#company_news a.new:visible');
            var prelink = 'https://facebook.com/sharer.php?';
            if(el.get(0)) {
                var link = el.attr('href');
                if(link.indexOf('http') == -1) {
                    link = 'http://fibo.ru'+ link;
                }
                goShare(prelink, $(this), {
                        u : link
                });
            }
        }          
      );

    $('#facebook2').click(
        function() {
            var el = $('.citation_text em');
            var prelink = 'https://facebook.com/sharer.php?';
            if (el.get(0)){
                var link = 'http://fibo.ru/fibogroup/cit-' + el.attr('id') + '.html';
                goShare(prelink, $(this), {
                    u : link
                });
            }
        }
    );
        

    ///Vkontakte
    $('#vkontakte').click(
        function() {
            var el = $('#company_news a.new:visible');
            var prelink = 'https://vkontakte.ru/share.php?';
            if(el.get(0)) {
                var link = el.attr('href');
                if(link.indexOf('http') == -1) {
                    link = 'http://fibo.ru'+ link;
                }
                goShare(prelink, $(this), {
                    url  : link
                });
            }
        }          
    );

    $('#vkontakte2').click(
        function() {
            var el = $('.citation_text em');
            var prelink = 'https://vkontakte.ru/share.php?';
            if (el.get(0)){
                var link = 'http://fibo.ru/fibogroup/cit-' + el.attr('id') + '.html';
                goShare(prelink, $(this), {
                    url: link
                });
            }
        }          
    );
        
    ///Google Plus
    //!!! Don't work after its last fix
    /*		  
        $('#googleplus2').click(
            function() {
                window.open('https://m.google.com/app/plus/x/?v=compose&content=123','gplusshare','width=550,height=400,left=500,top=300');
                return false;
            }
        );
        function setGooglePlusLink (tag, params) {
            var linkToGooglePlus = 'https://m.google.com/app/plus/x/';
            $.each(params, function(key, val){
                    linkToGooglePlus += '?'+ key +'='+ encodeURIComponent(val);
            });
            tag.attr('href', linkToGooglePlus);
        }
    */
})

//всплывающие подсказки
function showDesc_soc(id) {
    var obj = $('#'+id);
    var otobrajat = true;
    if(obj.is(":visible")){
        otobrajat = false;
    }
    $(".desc_soc").hide();

    if(otobrajat)
        obj.slideDown("normal");
};
function hideDesc_soc(id) {
    $('#'+id).hide();
};


function goShare(prelink, tag, params){
    var linkToShare = prelink;
    var ref = '&';
    $.each(params, function(key, val){
        linkToShare += key + '=' + encodeURIComponent(val) + ref;
    });
    linkToShare = linkToShare.substr(0, linkToShare.length-1); //убираем & в конце
    tag.attr('href', linkToShare);
}
