/***
*  jMP3 v0.2.1 - 10.10.2006 (w/Eolas fix & jQuery object replacement)
* an MP3 Player jQuery Plugin (http://www.sean-o.com/jquery/jmp3)
* by Sean O
*
* An easy way make any MP3 playable directly on most any web site (to those using Flash & JS),
* using the sleek Flash Single MP3 Player & the fantabulous jQuery.
*
* SIMPLE USAGE Example:
* $(youridorclass).jMP3();
*
* ADVANCED USAGE Example:
* $("#sounddl").jmp3({
*   showfilename: "false",
*   backcolor: "000000",
*   forecolor: "00ff00",
*   width: 200,
*   showdownload: "false"
* });
*
* HTML:
* <span class="mp3">sound.mp3</span>
*
* NOTE: filename must be enclosed in tag.  Various file paths can be set using the filepath option.
*
* Copyright (c) 2006 Sean O (http://www.sean-o.com)
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
***/
jQuery.fn.jmp3 = function(passedOptions){
	// hard-wired options
	var playerpath = "js/";					// SET THIS FIRST: path to singlemp3player.swf

	// passable options
	var options = {
		"filepath": "",										// path to MP3 file (default: current directory)
		"backcolor": "",									// background color
		"forecolor": "ffffff",								// foreground color (buttons)
		"width": "25",										// width of player
		"repeat": "no",										// repeat mp3?
		"volume": "50",										// mp3 volume (0-100)
		"autoplay": "true",								// play immediately on page load?
		"showdownload": "true",								// show download button in player
		"showfilename": "true"								// show .mp3 filename after player
	};
	
	// use passed options, if they exist
	if (passedOptions) {
		jQuery.extend(options, passedOptions);
	}
	
	// iterate through each object
	return this.each(function(){
		// filename needs to be enclosed in tag (e.g. <span class='mp3'>mysong.mp3</span>)
		var filename = options.filepath + jQuery(this).html();
		// do nothing if not an .mp3 file
		var validfilename = filename.indexOf(".mp3");
		if (validfilename == -1) { return false; }
	var uniqueid = "player" + filename.substring(filename.indexOf("/")+1, filename.indexOf(".mp3")) ;
		//var uniqueid = "mp3_player" ;
	var url = window.location.href;
	var urlsegment = url.indexOf("/index.php");
	var urlbegin = url.substring(0, url.indexOf("index.php")) ;

		// build the player HTML
		var mp3html = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
		mp3html += ' width="' + options.width + '" height="20" ';
    mp3html += ' id="' + uniqueid + '" name="' + uniqueid + '" ';
		mp3html += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">';
		mp3html += '<param name="movie" value="' + playerpath + 'singlemp3player.swf?';
		mp3html += 'showDownload=' + options.showdownload + '&file=' + filename + '&autoStart=true';
		mp3html += '&backColor=' + options.backcolor + '&frontColor=' + options.forecolor;
		mp3html += '&repeatPlay=' + options.repeat + '&songVolume=' + options.volume + '" />';
		mp3html += '<param name="wmode" value="transparent" />';
		mp3html += '<embed id="'+uniqueid+'" name="'+uniqueid+'" wmode="transparent" width="' + options.width + '" height="20" ';
		mp3html += 'src="'+ playerpath + 'singlemp3player.swf?';
		mp3html += 'showDownload=' + options.showdownload + '&file=' + filename + '&autoStart=true';
		mp3html += '&backColor=' + options.backcolor + '&frontColor=' + options.forecolor;
		mp3html += '&repeatPlay=' + options.repeat + '&songVolume=' + options.volume + '" ';
		mp3html += 'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		mp3html += '</object>';

		
		if(jQuery.browser.msie){
      mp3html =  '';
    }



		mp3html1 =  '<object type="application/x-shockwave-flash"';
		mp3html1 += ' width="' + options.width + '" height="20" ';
    mp3html1 += ' id="' + uniqueid + '" name="x' + uniqueid + '" ';
		mp3html1 += ' data="' + urlbegin  + filename + '" >';
		mp3html1 += '<param name="movie" value="' + urlbegin + 'js/player.swf"/>';
		mp3html1 += '<param name="src" value="'  + urlbegin  + 'js/player.swf"/>';
		mp3html1 += '<param name="autoStart" value="1" />';
 	  mp3html1 += '<param name="autoplay" value="true" />';
		mp3html1 += '<param name="FlashVars" value="playerID=' + uniqueid + '&amp;soundFile='+ urlbegin + filename + '&amp;autostart=yes"/>';
		mp3html1 += '<param name="quality" value="high"/><param name="menu" value="false"/><param name="wmode" value="transparent"/>';
    mp3html1 += '<p><a href="http://www.adobe.com/go/getflashplayer">Install Adobe Flash For Richer Experience</a></p>';
		mp3html1 += '</object>';

    mp3html1 =  '<object id="' + uniqueid + '" width="' + options.width + '" height="20" data="' + urlbegin + filename +'"  type="application/x-shockwave-flash"> ';
    mp3html1 += ' <param name="movie" value="' + urlbegin + 'js/player.swf" />  ';
    mp3html1 += ' <param name="autoStart" value="1" />';
    mp3html1 += ' <param name="controller" value="true" /> ';
    mp3html1 += ' <param name="autoplay" value="true" />'; 
    mp3html1 += ' <param name="src" value="'+ urlbegin  +  'js/player.swf" />';   
    mp3html1 += ' <param name="Flashvars"  value="soundFile=' + urlbegin  + filename + '&autoStart=true&playerID=' + uniqueid+'" />'; 
		mp3html1 += '</object>';


    mp3html1 =  '<script language="JavaScript" src="' + urlbegin + 'js/audio-player.js"></script>';
		mp3html1 += '<object type="application/x-shockwave-flash" data="'+ urlbegin  +  'js/player.swf" id="' + uniqueid +'" height="20" width="200">';
		mp3html1 += '<param name="movie" value="'+ urlbegin  +  'js/player.swf">';
		mp3html1 += '<param name="FlashVars" value="playerID=' + uniqueid +'&amp;slider=0xFF8A01&amp;soundFile=' + urlbegin + filename + '&amp;autostart=yes"/>';
		mp3html1 += '<param name="autoStart" value="1" />';
		mp3html1 += '<param name="autoplay" value="true" />'; 
    mp3html1 += '<param name="quality" value="high"/>';
		mp3html1 += '<param name="menu" value="false"/>';
		mp3html1 += '<param name="wmode" value="transparent"/>';
		mp3html1 += '<param name="src" value="'+ urlbegin  +  'js/player.swf" />';   
		mp3html1 += '</object>';
		
		if(jQuery.browser.msie){
      mp3html1 =  '<script language="JavaScript" src="' + urlbegin + 'js/audio-player.js"></script>';
		  mp3html1 += '<embed src="' + urlbegin + 'js/player.swf" flashvars="soundFile=' + urlbegin + filename + '" width="200" height="20" autostart="true" autoplay="true" loop="false"  controller="true" ></embed> ';
    }


		mp3html1 = '<script type="text/javascript" language="javascript" src="'+ urlbegin + 'js/niftyplayer.js"></script>';
		mp3html1 += '<object class="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="165" height="38" id="' + uniqueid + '" align="" name="' + uniqueid + '">';
		mp3html1 += '<param name=movie value="'+ urlbegin + 'js/niftyplayer.swf?file='+urlbegin+filename+'&as=1">';
		mp3html1 += '<param name=quality value=high>';
		mp3html1 += '<param name=bgcolor value=#FFFFFF>';
		mp3html1 += '<embed src="'+ urlbegin + 'js/niftyplayer.swf?file='+urlbegin+filename+'&as=1" quality=high bgcolor=#FFFFFF width="165" height="38" name="' + uniqueid + '" id="' + uniqueid + '" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">';
		mp3html1 += '</embed>';
		mp3html1 += '</object>';

//alert (mp3html);
		// don't display filename if option is set
		if (options.showfilename == "false") { jQuery(this).html(""); }
		jQuery(this).prepend(mp3html+"&nbsp;");
		
		// Eolas workaround for IE (Thanks Kurt!)
		if(jQuery.browser.msie){ this.outerHTML = this.outerHTML; }
	});
};
