
/*
Plugin Name: Audio player
Plugin URI: http://wpaudioplayer.com
Description: Audio Player is a highly configurable but simple mp3 player for all your audio needs. You can customise the player's colour scheme to match your blog theme, have it automatically show track information from the encoded ID3 tags and more. Go to your Settings page to start configuring it.
Version: 2.0b6
Author: Martin Laine
Author URI: http://www.1pixelout.net

License:
Copyright (c) 2008 Martin Laine

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


/**************** List of options ********************

Tracks

Option	Default	Description

soundFile	required	comma-delimited list of mp3 files
titles	overrides ID3 information	comma-delimited list of titles
artists	overrides ID3 information	comma-delimited list of artists



Options

Option	Default	Description

autostart	no	if yes, player starts automatically
loop	no	if yes, player loops
animation	yes	if no, player is always open
remaining	no	if yes, shows remaining track time rather than ellapsed time
noinfo	no	if yes, disables the track information display
initialvolume	60	initial volume level (from 0 to 100)
buffer	5	buffering time in seconds
encode	no	indicates that the mp3 file urls are encoded
checkpolicy	no	tells Flash to look for a policy file when loading mp3 files
(this allows Flash to read ID3 tags from files hosted on a different domain)
rtl	no	switches the layout to RTL (right to left) for Hebrew and Arabic languages

Flash player options

Option	Default	Description

width	required	width of the player. e.g. 290 (290 pixels) or 100%
transparentpagebg	no	if yes, the player background is transparent (matches the page background)
pagebg	NA	player background color (set it to your page background when transparentbg is set to ‘no’)
Colour scheme options


All colour codes must be 6-digit HEX codes without ‘#’ or ‘0x’ in front.

Option	Default	Description
bg	E5E5E5	Background
leftbg	CCCCCC	Speaker icon/Volume control background
lefticon	333333	Speaker icon
voltrack	F2F2F2	Volume track
volslider	666666	Volume slider
rightbg	B4B4B4	Play/Pause button background
rightbghover	999999	Play/Pause button background (hover state)
righticon	333333	Play/Pause icon
righticonhover	FFFFFF	Play/Pause icon (hover state)
loader	009900	Loading bar
track	FFFFFF	Loading/Progress bar track backgrounds
tracker	DDDDDD	Progress track
border	CCCCCC	Progress bar border
skip	666666	Previous/Next skip buttons
text	333333	Text

*/



var AudioPlayer = function () {
	var instances = [];
	var activePlayerID;
	var playerURL = "";
	var defaultOptions = {};
	var currentVolume = -1;
	
	function getPlayer(playerID) {
		return document.all ? window[playerID] : document[playerID];
	}
	
	return {
		setup: function (url, options) {
	        playerURL = url;
	        defaultOptions = options;
	    },

		getPlayer: function (playerID) {
			return getPlayer(playerID);
		},
	    
	    embed: function (elementID, options) {
			var instanceOptions = {};
	        var key;
	        var so;
			var bgcolor;
			var wmode;
			
			var flashParams = {};
			var flashVars = {};
			var flashAttributes = {};
	
	        // Merge default options and instance options
			for (key in defaultOptions) {
	            instanceOptions[key] = defaultOptions[key];
	        }
	        for (key in options) {
	            instanceOptions[key] = options[key];
	        }
	        
			if (instanceOptions.transparentpagebg == "yes") {
				flashParams.bgcolor = "#FFFFFF";
				flashParams.wmode = "transparent";
			} else {
				if (instanceOptions.pagebg) {
					flashParams.bgcolor = "#" + instanceOptions.pagebg;
				}
				flashParams.wmode = "opaque";
			}
			
			flashParams.menu = "false";
			
	        for (key in instanceOptions) {
				if (key == "pagebg" || key == "width" || key == "transparentpagebg") {
					continue;
				}
	            flashVars[key] = instanceOptions[key];
	        }
			
			flashAttributes.name = elementID;
			flashAttributes.style = "outline: none";
			
			flashVars.playerID = elementID;
			
			swfobject.embedSWF(playerURL, elementID, instanceOptions.width.toString(), "24", "9", false, flashVars, flashParams, flashAttributes);
			
			
			instances.push(elementID);
	    },
		
		syncVolumes: function (playerID, volume) {	
			currentVolume = volume;
			for (var i = 0; i < instances.length; i++) {
				if (instances[i] != playerID) {
					getPlayer(instances[i]).setVolume(currentVolume);
				}
			}
		},
		
		activate: function (playerID) {
			if (activePlayerID && activePlayerID != playerID) {
				getPlayer(activePlayerID).close();
			}
			
			activePlayerID = playerID;
		},
		
		load: function (playerID, soundFile, titles, artists) {
			getPlayer(playerID).load(soundFile, titles, artists);
		},
		
		close: function (playerID) {
			getPlayer(playerID).close();
			if (playerID == activePlayerID) {
				activePlayerID = null;
			}
		},
		
		open: function (playerID) {
			getPlayer(playerID).open();
		},
		
		getVolume: function (playerID) {
			return currentVolume;
		}
		
	}
	
}();
