/* Copyright 2003-2009 Emergent Music LLC  All rights reserved.
$Id$
*/
FLYFI.shareTrack_Title = 'Cool FlyFi Music';
FLYFI.shareTrack_Comment = 'Check out this tune I found on FlyFi';
FLYFI.emailTrack_Title = 'Send this track to a friend';
FLYFI.emailTrack_Message = "Here's a track I found at FlyFi that I thought you'd want to hear...";

FLYFI.sharePlaylist_Title = 'My FlyFi Playlist';
FLYFI.sharePlaylist_Comment = 'Check out my playlist on FlyFi';
FLYFI.emailPlaylist_Title = 'Send this playlist to a friend';
FLYFI.emailPlaylist_Message = "Here's a playlist I built at FlyFi that I thought you'd want to hear...";

FLYFI.sharePage_Title = 'FlyFi Deeper Music Discovery';
FLYFI.sharePage_Comment = 'Check out this page at FlyFi';
FLYFI.emailPage_Title = 'Send this page to a friend';
FLYFI.emailPage_Message = "Here's a page I found at FlyFi that I thought you'd want to see...";

FLYFI.shareDailyBlog_Title = 'FlyFi Music Discovery Blog';
FLYFI.shareDailyBlog_Comment = 'Check out the Daily Blog at FlyFi';
FLYFI.emailDailyBlog_Title = 'Send the FlyFi Daily Blog to a friend';
FLYFI.emailDailyBlog_Message = "Here's the FlyFi Daily Blog which I thought you'd want to see...";

FLYFI.site_facebook = 'facebook';
FLYFI.site_myspace = 'myspace';
FLYFI.site_twitter = 'twitter';

FLYFI.share_track = 'track';
FLYFI.share_playlist = 'playlist';
FLYFI.share_page = 'page';

FLYFI.serverURL = function() {
    var a = ['http://'];
    if (window.dev) {
        a.push(window.dev);
    }
    a.push('www.flyfi.com/');
    
    if (window.level && window.level != 'live') {
        a.push(window.level);
        a.push('/');
    }
    
    return a.join('');
};

FLYFI.shareURLForTrack = function(trackDict) {
    var a = [FLYFI.serverURL()];

    a.push('shared/track/');
    a.push(trackDict.trackID);
    a.push('/');
    
    var videoID = FLYFI.videoYouTubeID(trackDict);
    if (videoID) {
        a.push('videoid/');
        a.push(videoID);
        a.push('/');
    }
    
    if (FLYFI.loggedIn()) {
        a.push('sender/');
        a.push(FLYFI.current_user.nickname);
        a.push('/');
    }

    return escape(a.join(''));
};

FLYFI.shareURLForMyPlaylist = function(libraryDict) {
    var a = [FLYFI.serverURL()];
    
    a.push('libraryid/');
    a.push(libraryDict.libraryID);
    a.push('/');
    
    return a.join('');
};

FLYFI.shareURLForThisPage = function() {
    return window.location;
};

FLYFI.shareURLForDailyBlog = function() {
    var a = [FLYFI.serverURL()];

    a.push('blog/');
    return a.join('');
};

FLYFI.setEventForShare = function(event, url) {
    // don't use open() since it is blocked as a pop-up.  Instead set the href and target of the clicked event
    $(event.target).attr('target', 'FlyFiShare').attr('href', url);
};

// Facebook

FLYFI.shareAtFacebook = function(title, shareURL) {
    var a = ['http://facebook.com/sharer.php?'];
    a.push('t=' + encodeURIComponent(title) );
    a.push('&u=');
    a.push(shareURL);
    return a.join('');
};

// MySpace

FLYFI.shareAtMySpace = function(title, comment, shareURL) {
    var a = ['http://www.myspace.com/index.cfm?fuseaction=postto&'];
    a.push('t=' + encodeURIComponent(title) );
    a.push('&c=' + encodeURIComponent(comment) );
    a.push('&u=');
    a.push(shareURL);
    a.push('&l=6'); // 'Music'
    return a.join('');
};

// Twitter

FLYFI.shareAtTwitter = function(title, shareURL) {
    var a = ['http://twitter.com/home?'];
    a.push('status=' + encodeURIComponent(title + ' &#9835; ') );
    a.push(shareURL);
    return a.join('');
};

// Email

FLYFI.shareViaEmail_libraryID = null; // needed if emailing a playlist
FLYFI.shareViaEmail = function(title, message, serverError) {
    var dialog = $('#email_dialog');
    dialog.find('.to_err').hide();
    if (serverError) {
        dialog.find('.server_err').css("display", "block");
    } else {
        dialog.find('.server_err').hide();
    }
    
    var name = '';
    if (FLYFI.loggedIn()) {
        name = FLYFI.current_user.nickname;
    }
    dialog.find('#email_dialog_fromName').val(name);
    var email = '';
    if (FLYFI.current_user && FLYFI.current_user.email) {
        email = FLYFI.current_user.email;
    }
    dialog.find('#email_dialog_replayEmail').val(email);
    dialog.find('#email_dialog_message').val(message);
    
    $('#email_link').attr('title', title)
                    .click(); 
};
FLYFI.onClickOK_email = function() {
    var dialog = $('#email_dialog');
    var senderName = dialog.find('#email_dialog_fromName').val();
    var replyTo = dialog.find('#email_dialog_replayEmail').val();
    var to = dialog.find('#email_dialog_to').val();
    var message = dialog.find('#email_dialog_message').val();
    
    if (!to) {
        dialog.find('.to_err').css("display", "block");
        return;
    }
    
    tb_remove(); // close the dialog
    
    var url = '';
    var videoYouTubeID = '';
    switch(FLYFI.shareDialogItem) {
        case FLYFI.shareDialogItem_Track:
            if (FLYFI.isContest()) {
                url = FLYFI.emailURLForCurrentContestEntry();
            } else {
                var libraryID = FLYFI.shareViaEmail_libraryID;
                url = '/' + window.level + '/email/playlist/' + libraryID + '/track/' + FLYFI.trackDialogTrackDict.trackID + '/';
                videoYouTubeID = FLYFI.videoYouTubeID(FLYFI.trackDialogTrackDict);
            }
            break;
            
        case FLYFI.shareDialogItem_Playlist:
            if (FLYFI.isContest()) {
                url = FLYFI.emailURLForCurrentContestCategory();
            } else {
                var libraryID2 = FLYFI.shareViaEmail_libraryID;
                url = '/' + window.level + '/email/playlist/' + libraryID2 + '/';
            }
            break;
            
        case FLYFI.shareDialogItem_Page:
            url = '/' + window.level + '/email/page/' + window.location;
            break;
            
        case FLYFI.shareDialogItem_DailyBlog:
            url = '/' + window.level + '/email/dailyblog/';
            break;

        default:
            break;
    }
    
    var data = {'senderName': senderName, 
                 'message': message,
                 'replyTo': replyTo, 
                 'to': to
                };
    if (videoYouTubeID) {
        data.videoYouTubeID = videoYouTubeID;
    }
    
    FLYFI.postJSON(url, data,
                        FLYFI.onServer_email,
                        function () { FLYFI.onServer_email('Network Error'); }
                    );
};
FLYFI.onServer_email = function(json) {
    if (!json) { // null is success
        FLYFI.shareViaEmail_libraryID = null;
    } else {
        switch(FLYFI.shareDialogItem) {
            case FLYFI.shareDialogItem_Track:
                setTimeout(FLYFI.onError_emailTrack, 500); // give dialog time to close before we re-open it
                break;
                
            case FLYFI.shareDialogItem_Playlist:
                setTimeout(FLYFI.onError_emailMyPlaylist, 500); // give dialog time to close before we re-open it
                break;
                
            case FLYFI.shareDialogItem_Page:
                setTimeout(FLYFI.onError_emailThisPage, 500); // give dialog time to close before we re-open it
                break;
                
            case FLYFI.shareDialogItem_DailyBlog:
                setTimeout(FLYFI.onError_emailDailyBlog, 500); // give dialog time to close before we re-open it
                break;

            default:
                break;
        }

    }
};
FLYFI.onError_emailTrack = function(json) {
    FLYFI.emailTrack(FLYFI.trackDialogTrackDict, FLYFI.shareDialogItem_Track, true);
};
FLYFI.onError_emailMyPlaylist = function(json) {
    FLYFI.emailMyPlaylist(FLYFI.currentPlaylistDict, true);
};
FLYFI.onError_emailThisPage = function(json) {
    FLYFI.emailThisPage(true);
};
FLYFI.onError_emailDailyBlog = function(json) {
    FLYFI.emailDailyBlog(true);
};

FLYFI.emailTrack = function(trackDict, libraryID, serverError) {
    FLYFI.shareDialogItem = FLYFI.shareDialogItem_Track; 
    FLYFI.shareViaEmail_libraryID = libraryID;
    FLYFI.prepTrackDialog('#email_dialog', trackDict);
    var title = FLYFI.emailTrack_Title;
    var message = FLYFI.emailTrack_Message;
    if (FLYFI.isContest()) {
        title = FLYFI.emailContestEntry_Title;
        message = FLYFI.emailContestEntry_Message;
    }
    FLYFI.shareViaEmail(title, message, serverError);
};
FLYFI.emailMyPlaylist = function(libraryDict, serverError) {
    FLYFI.shareDialogItem = FLYFI.shareDialogItem_Playlist;
    FLYFI.shareViaEmail_libraryID = libraryDict.libraryID;
    var title = FLYFI.emailPlaylist_Title;
    var message = FLYFI.emailPlaylist_Message;
    if (FLYFI.isContest()) {
        title = FLYFI.emailContestCategory_Title;
        message = FLYFI.emailContestCategory_Message;
    }
    FLYFI.shareViaEmail(title, message, serverError);
};
FLYFI.emailThisPage = function(serverError) {
    FLYFI.shareDialogItem = FLYFI.shareDialogItem_Page;
    FLYFI.shareViaEmail(FLYFI.emailPage_Title, FLYFI.emailPage_Message, serverError);
};
FLYFI.emailDailyBlog = function(serverError) {
    FLYFI.shareDialogItem = FLYFI.shareDialogItem_DailyBlog;
    FLYFI.shareViaEmail(FLYFI.emailDailyBlog_Title, FLYFI.emailDailyBlog_Message, serverError);
};

FLYFI.onLibrarySelect_Share = function(event) {
    var playlistItems = $('#player_tabs .contenttoolbar li.share_main li.playlist, #insidepage_rightcol .contenttoolbar li.share_main li.playlist, #player_tabs .contenttoolbar li.share_main li.getwidget, #insidepage_rightcol .contenttoolbar li.share_main li.getwidget');
    if (FLYFI.currentPlaylistDict) {
        playlistItems.removeClass('unavailable');
    } else {
        playlistItems.addClass('unavailable');
    }
};

$(document).ready(function() {
    $('#email_dialog .ok').click(FLYFI.onClickOK_email);
    $(window).bind(FLYFI.MSG_LibrarySelect, FLYFI.onLibrarySelect_Share);
});


