// song.js
Event.observe(window, 'load', function() {
    enableTooltipsByClassName("bubble-tooltip");
});

function enableTooltipsByClassName(className) {
    var elems = document.getElementsByClassName(className);
    for (var i = 0; i < elems.length; i++) {
        enableTooltips(elems[i].id);
    }
}

var Song = {
    rate_it: function(song_id, rate) {
        new Ajax.Request('/api/rate', {
            method: 'post',
            parameters: 'song_id=' + song_id + '&rate=' + rate,
            onComplete: function(req) {
                try {
                    var result = req.responseText.evalJSON();
                    if (result.success) {
                        if ($('rating') && $('rating-done'))
                            $('rating').update($('rating-done').innerHTML);
                    } else {
                        throw new Error();
                    }
                } catch(e) {
                    if ($('rating') && $('rating-error'))
                        $('rating').update($('rating-error').innerHTML);
                }
            }
        });
    },
    playerWindowStyle: $H({
        width: 300,
        height: 355,
        toolbar: 'no',
        status: 'no',
        scrollbars: 'no',
        location: 'no',
        directories: 'no',
        resizable: 'yes'
    }),
    play: function(artist_id, song_id, base) {
        var url = '/song/' + artist_id + '/' + song_id + '/play';
        if (base) { url = base + url; }
        var styles = [];
        Song.playerWindowStyle.each(function(pair) {
            styles.push(pair.key + '=' + pair.value);
        });
        window.open(url, '_blank', styles.join(','));
        return false;
    },
    codeWindowStyle: $H({
        width: 400,
        height: 240,
        toolbar: 'no',
        scrollbars: 'yes',
        location: 'no',
        directories: 'no',
        resizable: 'yes'
    }),
    openCode: function(artist_id, song_id, type) {
        var url = '/song/' + artist_id + '/' + song_id + '/code' + '#' + type;
        var styles = [];
        Song.codeWindowStyle.each(function(pair) {
            styles.push(pair.key + '=' + pair.value);
        });
        window.open(url, '_blank', styles.join(','));
        return false;
    }
};

