/**
 * SoundPlayer Class
 * 
 * Sample :
 *   var SP = new SoundPlayer( document.getElementById( 'player' ), 'soundPlayer.swf' );
 *   SP.setVolume( 50 ); // Not necessary
 *   SP.setLoop( true ); // Not necessary
 *   SP.setStream( true ); // Not necessary
 *   SP.play();
 * 
 * @Version:    0.1
 * @License:    GPL License.(http://www.gnu.org/licenses/gpl.html)
 * @LastUpdate: 2010/02/24
 * @Author:     Freesale.Inc
 */

//Constructor
function SoundPlayer ( elm, swf ) {
	this.elm = elm;
	this.swf = swf;
}

//Method setVolume
SoundPlayer.prototype.setVolume = function( volume ) {
	this.volume = volume;
};

//Method setLoop
SoundPlayer.prototype.setLoop = function( bool ) {
	this.loop = bool;
};

//Method setStream
SoundPlayer.prototype.setStream = function( bool ) {
	this.stream = bool;
};

//Method play
SoundPlayer.prototype.play = function( opt ) {
	if ( !opt )
		var opt = {};
	
	var flashVars = {}
	if ( this.volume )		flashVars.volume = this.volume;
	if ( this.loop )		flashVars.loop   = 1;
	if ( this.stream )		flashVars.stream = 1;
	
	for ( var prop in flashVars ) {
		opt.flashVars += prop + '=' + flashVars[ prop ] + '&';
	}
	opt.flashVars = opt.flashVars.replace(/\&$/, '');
	
	this.flash( opt );
};

//Private method flash
SoundPlayer.prototype.flash = function( opt ) {
	if ( this.getFlashVersion().version[0] >= 8 ) {
		var width  = this.elm.offsetWidth;
		var height = this.elm.offsetHeight;
		
		for ( var prop in opt ) {
			switch ( prop ) {
				case 'width':  width  = opt[ prop ]; break;
				case 'height': height = opt[ prop ]; break;
			}
		}
		
		if ( window.navigator.userAgent.match(/MSIE\s[5-7]/) ) {
			var embed = document.createElement('embed');
			this.setAttributes( embed, {
				src: this.swf,
				width: width,
				height: height,
				salign: 'TL',
				wmode: 'transparent',
				flashVars: opt.flashVars,
				pluginspage: 'http://www.macromedia.com/go/getflashplayer'
			});
			this.elm.appendChild( embed );
		} else {
			var object = document.createElement('object');
			this.setAttributes( object, {
				data: this.swf,
				width: width,
				height: height,
				type: 'application/x-shockwave-flash'
			});
			
			var params = {
				movie: this.swf,
				salign: 'TL',
				wmode: 'transparent',
				flashVars: opt.flashVars
			};
			
			for ( var prop in params ) {
				var param = document.createElement('param');
				this.setAttributes( param, {
					name: prop,
					value: params[ prop ]
				});
				object.appendChild( param );
			}
			
			this.elm.appendChild( object );
		}
	} else {
		return false;
	}
};

//Private method setAttributes
SoundPlayer.prototype.setAttributes = function( elm, attributes ){
	if ( elm ) {
		for ( var prop in attributes ) {
			elm.setAttribute( prop, attributes[ prop ] );
		}
	}
};

//Private method getFlashVersion
//Originally sourced by 'http://d.hatena.ne.jp/HolyGrail/20090106/1231256465'
SoundPlayer.prototype.getFlashVersion = function() {
	var o = {installed:0, version:[]}, description, oActiveX, nMajor, nMinor;
	if (navigator.plugins && typeof navigator.plugins["Shockwave Flash"] == "object") {
		o.pluginType = "npapi";
		description = navigator.plugins["Shockwave Flash"].description;
		if (typeof description != "undefined") {
			description = description.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
			nMajor = parseInt(description.replace(/^(.*)\..*$/, "$1"), 10);
			nMinor = /r/.test(description) ? parseInt(description.replace(/^.*r(.*)$/, "$1"), 10) : 0;
			o.version = [nMajor, nMinor];
			o.installed = 1;
		}
	} else if (window.ActiveXObject) {
		o.pluginType = "ax";
		try {
			oActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		} catch (e) {
			try {
				oActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				o.version = [6, 0];
				o.installed = 1;
				oActiveX.AllowScriptAccess = "always";
			} catch (e) {
				if (o.version[0] == 6) {o.installed=1; return; }
			}
			try {
				oActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			} catch (e) {
			}
		}
		if (typeof oActiveX == "object") {
			description = oActiveX.GetVariable("$version");
			if (typeof description != "undefined") {
				description = description.replace(/^\S+\s+(.*)$/, "$1").split(",");
				o.version = [parseInt(description[0], 10), parseInt(description[2], 10)];
				o.installed = 1;
			}
		}
	}
	return o;
};