/*! script.js
 Author: Calvin Rien
 */

var player_actions = [];
var player_instances = [];

function attach_jplayer(elem, media, formats, index) {
    player_actions[index] = "stop";
    $(elem).jPlayer({
        ready: function () {
            $(this).jPlayer("setMedia", media);
            player_actions[index] = "play";
        },
        ended: function (event) {
            console.log('ended');
            $(this).jPlayer("destroy");
            attach_jplayer(this, media, formats, index);
        },
        pause: function (event) {
            console.log('pause');
            player_actions[index] = "play";
        },
        play: function (event) {
            console.log('play');
            player_actions[index] = "pause";
        },
        swfPath: "/swf",
        supplied: formats
    });
}

function play_audioname(index) {
    if (_gaq) {
        _gaq.push(['_trackEvent', 'play_audioname', index, player_actions[index]]);
    }

    player_instances[index].jPlayer(player_actions[index]);
}

var newWin = null;
function popUp(strURL, strHeight, strWidth) {
    if (newWin != null && !newWin.closed)
        newWin.close();
    var strOptions = "toolbar=0,location=1,directories=0,status=0,menubar=0,resizable=0,height=" +
            strHeight + ",width=" + strWidth;
    newWin = window.open(strURL, '_anExternalWin', strOptions);
    newWin.focus();
}

function share(service, name, url) {
    var params = {
        name: 'AudioName | ' + name,
        link: 'http://'+ window.location.host +'/' + url,
        picture: 'http://img.audioname.com/img/icon-logo.png',
        caption: 'AudioName.com' ,
        description: 'Make your conversations easier',
        message: 'I just recorded my audioname to help people pronounce my name.',
        width: 550,
        height: 370,
        win_url: ''
    };

    if (_gaq) {
        _gaq.push(['_trackEvent', 'share', service]);
    }
    
    switch (service) {
        case 'li':
            params.win_url = 'http://www.linkedin.com/cws/share?' + $.param({url:params.link});
            break;
        case 'fb':
            params.method = 'feed';

            FB.ui(params,
                    function(response) {
                        if (response && response.post_id) {
                            if (_gaq) {
                                _gaq.push(['_trackEvent', 'share', service, 'success']);
                            }

                            // was published
                        } else {
                            if (_gaq) {
                                _gaq.push(['_trackEvent', 'share', service, 'cancel']);
                            }
                            // was not published
                        }
                    });
            return;
        case 'tw':
            params.message = params.message.replace('audioname', '@audioname');

            params.win_url = 'http://twitter.com/share?' + $.param({url:params.link, text:params.message + ' Check it out here - ', related:'@audioname'});
            break;
/*
        case 'email':
            params.win_url = '/profile/send-invites';
            params.width = 778;
            params.height = 614;
            break;
*/
    }

    popUp(params.win_url, params.height, params.width)

    return false;
}

function service_logout(url, quick) {
    if (url != '') {
        popUp(url, 472, 760)
    }
    if (!!quick) {
        window.location = '/logout?q=1';
    }
    return false;
}
