﻿var win = null;

function OpenNewWindow(mypage, w, h, myname) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    settings = 'height=' + h + ',width=' + w + ',top=' + wint + ',left=' + winl + ',scrollbars=no,toolbar=no';
    win = window.open(mypage, myname, settings);
    if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// Social Networking
$(function() {
    var ShareURL = window.location;
    var ShareTitle = document.title;
    var popupOptions = "status=0,toolbar=0,width=800,height=550,resizable=0,location=1";

    $(".share_facebook").click(function() {
        var targetUrl = 'http://www.facebook.com/sharer.php'
                                    + '?u=' + encodeURIComponent(ShareURL)
                                    + "&t=" + encodeURIComponent(ShareTitle);
        OpenNewWindow(targetUrl, 800, 500, "fbShare");
    });
    $(".share_twitter").click(function() {
        var twitterStatus;
        twitterStatus = trimTwitterStatus(ShareTitle, ShareURL);
        var targetUrl = 'http://twitter.com/home/?status='
                            + encodeURIComponent(twitterStatus);
        OpenNewWindow(targetUrl, 800, 550, "twiShare");
    });
    $(".share_myspace").click(function() {
        var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto'
                            + '&t=' + encodeURIComponent(ShareTitle)
                            + '&c=' + encodeURIComponent('<p><strong>' + ShareTitle + '</strong></p>')
                            + '&u=' + encodeURIComponent(ShareURL) + '&l=3';
        window.open(targetUrl);
    });
    $(".share_email").click(function() {
        var winLeft = ((screen.width - 1024) / 2) - 40;
        $("#liveblog").hide('slow');
        $(".playlist").hide('slow');
        $("#emailFormResults").hide();
        // set the title
        $("#emailShareTitle").html(ShareTitle);
        $("#emailForm").show('slow');
        $("#emailFormTable").show('slow');
        $("#emailForm").css("background", "#fff").css("border", "2px solid #000").css("left", winLeft);
        //    var id = getQueryVariable("id");
        //    var url = escape(window.location);
        //    window.location = '/EmailStory.aspx?id=' + id + "&url=" + url;
    });
    $("#EmailVideoSend").click(function() {
        EmailVideo(ShareTitle, ShareURL);
    });
});


// function to trim a twitter status to 140 chars (based on title and url)
function trimTwitterStatus(title, url) {
    var result, titleLengthAllowed, fullTitle;
    fullTitle = title + ' [' + url + ']';
    if (fullTitle.length > 139) {
        // status is too long with full title and url
        // shorten title to fit
        // start with max 140 char - 3 for {space}[] - full url length
        titleLengthAllowed = 137 - (url.href.length);
        result = title.substring(0, titleLengthAllowed) + ' [' + url + ']';
    } else {
        result = title + ' [' + url + ']';
    }
    return result
}


// email a video link
function EmailVideo(title, url) {
    // retrieve user data 
    var EmailTo = $("#EmailVideoTo").val();
    var EmailFrom = $("#EmailVideoFrom").val();
    var encURL = encodeURI('Title=' + title + '&URL=' + url + '&emailTo=' + EmailTo + '&emailFrom=' + EmailFrom);

    // hide form, show results pane
    $("#emailFormTable").hide('slow');
    $("#EmailVideoResults").html("&nbsp;");
    $("#emailFormResults").show('slow');

    // send request to server as POST
    // send encoded URL parameters and set contentType as such
    // expect response in XML
    $.ajax({
        type: "POST",
        url: "http://action.afa.net/ajax/videoService.asmx/emailVideoLink",
        data: encURL,
        dataType: 'xml',
        contentType: "application/x-www-form-urlencoded",
        success: function(msg) {
            successEmailVideo(msg);
        },
        error: function(msg) {
            failedEmailVideo(msg);
        }

    });
}

// handle response on successful AJAX call
function successEmailVideo(msg) {
    var returnMsg;
    // extract responses
    $(msg).find('string').each(function() {
        returnMsg = $(this).text();
    });

    $("#EmailVideoResults").html(returnMsg);

}

// handle response on failed AJAX call
function failedEmailVideo(msg) {
    $("#EmailVideoResults").html("<span style='color:#ff0000;'>" + msg.responseText + "</span>");
}

function hideEmailForm() {
    $("#emailForm").hide('slow');
    $("#liveblog").show('slow');
    $(".playlist").show('slow');
} 
           

