/* 
    WPaudio WordPress MP3 Player Plugin (http://wpaudio.com) 
    Copyright 2009 Todd Iceton (t@ticeton.com)
*/

soundManager.debugMode = false;
soundManager.url = wpa_url + '/sm2/';
soundManager.nullURL = wpa_url + '/sm2/null.mp3';
soundManager.useHighPerformance = true;
soundManager.useFastPolling = false;
soundManager.waitForWindowLoad = true;

// When player is ready, add wpa clip objects to wpa array. Show error if error.
soundManager.onready(function(oStatus){
    if (oStatus.success && isset('wpa_params')) {
        for (i in wpa_params) {
            wpa.push(new wpaClip(wpa_params[i]));
            jQuery('#wpa' + i + '_placeholder').hide();
        }
    }
});
soundManager.onerror(function(){
    jQuery('.wpa_placeholder').innerHTML('<br>WPaudio Player could not load.  Do you have <a href="http://get.adobe.com/flashplayer/">Adobe Flash</a> installed?');
});

// Wpa clip object
function wpaClip(params) {
    this.id = params.id;
    this.url = params.url;
    this.text = params.text;
    this.dl = params.dl;
    this.duration = 0;
    this.player = soundManager.createSound({
        id: 'wpa' + this.id,
        url: this.url,
        // In the following functions, 'this' refers to the player
        whileplaying: function() {
            jQuery('#' + this.sID + '_bar_position').width(this.position/wpa[this.sID.substr(3)].duration*100 + '%');
            jQuery('#' + this.sID + '_position').text(wpaTimeFormat(this.position));
        },
        whileloading: function() {
            wpa[this.sID.substr(3)].duration = (this.bytesLoaded == this.bytesTotal) ? this.duration : this.durationEstimate;
            jQuery('#' + this.sID + '_bar_load').width(this.bytesLoaded/this.bytesTotal*100 + '%');
            jQuery('#' + this.sID + '_duration').text(wpaTimeFormat(wpa[this.sID.substr(3)].duration));
        },
        onid3: function() {
            if (wpa[this.sID.substr(3)].text)
                return;
            var text;
            if (this.id3.artist) text = this.id3.artist;
            if (this.id3.artist && this.id3.songname) text += ' - ';
            if (this.id3.songname) text += this.id3.songname;
            wpa[this.sID.substr(3)].textChange(text);
            this.unload();
        },
        onplay: function() {wpa[this.sID.substr(3)].buttonCheck();},
        onpause: function() {wpa[this.sID.substr(3)].buttonCheck();},
        onresume: function() {wpa[this.sID.substr(3)].buttonCheck();},
        onstop: function() {wpa[this.sID.substr(3)].buttonCheck();}
    });
    // If text is not presupplied, load to get id3
    if (!this.text) this.player.load();
    // Set the width of the bar to the width of the meta info
    this.barWidth = function() {
        jQuery('#wpa' + this.id + '_bar, #wpa' + this.id + '_sub').width(jQuery('#wpa' + this.id + '_meta').width()+50)
    };
    this.barWidth();
    // Change meta text and set bar width
    this.textChange = function(text) {
        this.text = text;
        jQuery('#wpa' + this.id + '_meta').text(text);
        this.barWidth();
    };
    // Play or pause
    this.play = function() {
        // Pause any other playing players if this player is stopped, about to play, and handle button image
        if (!this.player.playState || this.player.paused) soundManager.pauseAll();
        soundManager.togglePause(this.player.sID);
        this.show();
    };
    this.show = function() {
        var id = this.id;
        if (wpa_pref_bar) {
            jQuery('#wpa' + id + '_bar').slideDown('slow', function(){
                if (wpa_pref_sub) {
                    jQuery('#wpa' + id + '_sub').fadeIn('slow');
                }
            });
        }
    }
    // Jog track
    this.jog = function(percent) {
        if (!this.player.playState || this.player.paused) this.play();
        this.player.setPosition(this.duration * percent);
    }
    // Check button for css sprite
    this.buttonCheck = function() {
        if (!this.player.playState || this.player.paused) 
            jQuery('#wpa' + this.id + '_play').css('backgroundPosition', '0px 0px');
        else
            jQuery('#wpa' + this.id + '_play').css('backgroundPosition', '0px 15px');
    }
}

// Add click handlers for play button and jog bar
jQuery(document).ready(function() {
    jQuery('.wpa_play').click(function(){
        if (!wpa.length) {
            soundManager.reboot();
            setTimeout('wpa[' + jQuery(this).attr('id').split('_',1)[0].substr(3) + '].play();', 100);
        }
        else wpa[jQuery(this).attr('id').split('_',1)[0].substr(3)].play();
    });
    jQuery('.wpa_meta_link').click(function(){
        // If iPhone/iPod, let the link function normally
        if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i)) return true;
        if (!wpa.length) soundManager.reboot();
        wpa[jQuery(this).attr('id').split('_',1)[0].substr(3)].play();
        return false;
    });
    jQuery('.wpa_bar_click').click(function(e){ //DONE
        // Allow clicks on status bar to jog track
        var id = jQuery(this).attr('id').split('_',1)[0].substr(3);
        if (e.pageX) {
            var percent = (e.pageX - jQuery(this).offset()['left']) / jQuery(this).width();
            wpa[id].jog(percent);
        }
    });
});

function isset(varname){ // For checking user-set variables
    return(typeof(window[varname])!='undefined');
}
function wpaTimeFormat(ms) {
    var min = Math.floor(ms / 1000 / 60);
    var sec = Math.floor(ms / 1000 % 60);
    var time_string = min + ':';
    if (sec<10) time_string += '0'; // Add leading 0 to seconds if necessary
    time_string += sec;
    return(time_string);
}