1/**
2 * javascript functionality for the lastfm plugin
3 *
4 * @author Michael Klier <chi@chimeric.de>
5 */
6
7function lastfm_ajax(chart, opts) {
8    if(!document.getElementById) return;
9    if(!chart) return;
10    if(!opts) return;
11
12    var ajax = new sack(DOKU_BASE+'lib/exe/ajax.php');
13    ajax.AjaxFailedAlert = '';
14    ajax.encodeURIString = false;
15
16    ajax.setVar('call', 'plugin_lastfm');
17    ajax.setVar('plugin_lastfm_chart', chart.id);
18
19    for(var i = 0; i < opts.length; i++) {
20        ajax.setVar(opts[i].firstChild.className, opts[i].firstChild.innerHTML);
21    }
22
23    // show loader
24    lastfm_loader(chart);
25
26    // define callback
27    ajax.onCompletion = function(){
28        var data = this.response;
29        if(data === ''){ return; }
30        chart.style.visibility = 'hidden';
31        chart.innerHTML = data;
32        chart.style.visibility = 'visible';
33    };
34
35    ajax.runAJAX();
36}
37
38/**
39 * Calls the ajax function for each requested chart
40 *
41 * @author Michael Klier <chi@chimeric.de>
42 */
43function lastfm_get_charts(charts, opts){
44    if(!document.getElementById) return;
45    if(!charts) return;
46    if(!opts) return;
47
48    for(var i = 0; i < charts.length; i++) {
49        lastfm_ajax(charts[i], opts);
50    }
51}
52
53/**
54 * shows the loading image
55 *
56 * @author Michael KLier <chi@chimeric.de>
57 */
58function lastfm_loader(obj) {
59    if(!obj) return;
60    obj.innerHTML = '<img src="'+DOKU_BASE+'lib/plugins/lastfm/images/loader.gif" />';
61}
62
63// add the init event
64addInitEvent(function() {
65    var objs = getElementsByClass('plugin_lastfm', document, 'div');
66    if(!objs) return;
67    for(var i = 0; i < objs.length; i++) {
68        var opts   = getElementsByClass('plugin_lastfm_opt', objs[i], 'li');
69        var charts = getElementsByClass('plugin_lastfm_chart', objs[i], 'div');
70        lastfm_get_charts(charts, opts);
71    }
72});
73
74// vim:ts=4:sw=4:et:enc=utf-8:
75