﻿// JScript File
//WHAT: For sharing/sending to facebook
//WHEN: 8/9/09
//WHO:  willzhan@microsoft.com

//post on wall and reflected on friends News Feed  
function sendNewsFeed(target_id, content) {
    var template_bundle_id = 93446285929;  //142478295929;
    var template_data = { 'content': content };
    if (target_id != null)
        target_id = [target_id];
    FB.Connect.showFeedDialog(template_bundle_id,
                             template_data,
                             target_id,
                             null,    //body_general
                             null,    //story_size
                             FB.RequireConnect.promptConnect);
}

function sendTVObjectNewsFeedToAllFriends(content) {
    var text = "Test of sending TV Object news feed to all friends. Check out this \'good stuff\': <a href=\"http://www.tvguide.com/tvshows/greys-anatomy/191535\">Grey\'s Anatomy</a> " + content;
    sendNewsFeed(null, text);
}

function sendCommentNewsFeedToAllFriends(comment, imgUrl, title, description) {
    if (typeof (title) == "undefined" || title == null || title == "") {
        title = document.title;
        //sometimes it may have either of the two but not both.
        if (title.indexOf("| TVGuide.com") > -1) {
            title = title.substring(0, title.length - 14);
        }
        var location = title.indexOf("-");
        if (location > 0) {
            title = title.substring(0, location - 1)
        }
    }

    if (typeof (imgUrl) == "undefined" || imgUrl == null || imgUrl == "") {
        imgUrl = "http://www.tvguide.com/images/global/tvguide_logo.gif";   //not losing a chance to promote TVGOL
    }

    var href = document.location.href;
    if (typeof (href) == "undefined" || href == null || href == "") {
        href = "http://www.tvguide.com";                                    //not losing a chance to promote TVGOL
    }
    //if (href.indexOf(".tvguide.com") < 0) {
    //    href = href.replace("/" + document.location.host + "/i", "www.tvguide.com");
    //}

    comment = stripHtmlTags(comment);

    var attachment = {
        "name": title,
        "href": href,
        "caption": "Source www.TVGuide.com",
        "description": description,
        "media": [{
            /*"caption": "Text 4 Text 4 Text 4 Text 4 Text 4 ",
            "description": "Text 5 Text 5 Text 5 Text 5 Text 5 ",
            "name": "Text 6 Text 6 Text 6 Text 6 Text 6 ", */
            "type": "image",
            "href": href,
            "src": cleanUrl(imgUrl)
}]
        };

        FB.Connect.streamPublish(comment, attachment, null, null, 'Share this comment with your friends', null);
    }

    $.fn.stripTags = function() {
        var regexp = /<("[^"]*"|'[^']*'|[^'">])*>|&nbsp;/gi;
        this.each(function() {
            $(this).html(
               $(this).html().replace(regexp, "")
       );
        });
        return $(this);
    }

    function stripHtmlTags(html) {
        var regexp = /<("[^"]*"|'[^']*'|[^'">])*>|&nbsp;|\t/gi;
        return html.replace(regexp, "");
    }

    //yahoo video has image url like http://xyz/123;size=1x2. 
    function cleanUrl(url) {
        var parts = url.split(';');
        return parts[0];
    }


