/**
 * jSpeaker Plugin for jQuery JavaScript Library
 * http://www.ruig.com/jquery/jSpeaker
 *
 * Copyright (c) 2011 RUIG.com
 * Dual licensed under the MIT and GPL licenses.
 *  - http://www.opensource.org/licenses/mit-license.php
 *  - http://www.gnu.org/copyleft/gpl.html
 *
 *	Author: Daantje Eeltink
 *	Version: 1.0.0
 *	Date: Fri, 08 Apr 2011 13:44:07 +0200
 * 
 *	My first plugin, so it can be mess below ;) This plugin will speak the text thats in the selected object.
 *	It uses the google translate speak/listen function. Keep in mind that this is based on a little hack, so
 *	when google changes something, this plugin may break or explode. I'm not responsable to any harm that may
 *	cause this plugin.
 *
 *	Dependancies:
 *		jPlayer plugin: http://www.jplayer.org (> v2.0.0)
 *
 *	Options:
 *		swfPath:	Path to jPlayer plugin
 *		lang:		Language to speak. (eg.: nl, en, de, es...)
 *		line:		Speak only this line (eg.: 0 speaks only the first line)
 *
 *	Examples:
 *	
 *		$('#someSelector').jSpeaker(); //when jPlayer path is correct in jSpeaker plugin. Else set swfPath!
 *
 *		$('#someSelector').jSpeaker({line:1});
 *
 *		$('#someSelector').jSpeaker({swfPath:'/path/to/jPlayer/plugin',lang:'en'});
 *
 *	Todo:
 *		Find a way to cache the lines to speak as mp3 into memory while we're bussy and call them when needed
 *		to kill the pauzes between lines. But this may not be possible.
 */

(function($){

	//globals
	var settings = {
		swfPath: "/jquery/jQuery.jPlayer.2.0.0",
		lang: "nl",
		line: false
	}
	var jSpeakerLines;
	var jSpeakerLine = 0;

	//the speaker init function
	$.fn.jSpeaker = function(options){
		
		if(options){ 
			$.extend(settings, options);
		}
	  
		//get the selected text...
		txt = $(this).text();
		if(txt){
			//explode in jSpeakerLines of max 100 chars, the limit of google
			jSpeakerLines = txt.match(/[a-z0-9 ]{1,100}[^a-z0-9]/gi);
			if(jSpeakerLines.length){
				//add holder for player
				if(!$('#jSpeaker_player_holder').length){
					$('body').append('<div id="jSpeaker_player_holder" style="height:0px; width:0px;"></div>');
				}
				
				//init jPlayer
				$('#jSpeaker_player_holder').jPlayer({
					ready: function(){
						//do only the requested line
						if(settings['line'] !== false){
							jSpeakerLine = settings['line'];
						}
						setTimeout(function(){$('#jSpeaker_player_holder')._jSpeakerSay();},500);
					},
					swfPath: settings['swfPath'],
					supplied: "mp3",
					ended: function(){
						//when ended, speak next line...
						if(jSpeakerLine < jSpeakerLines.length){
							if(settings['line'] === false){
								$('#jSpeaker_player_holder')._jSpeakerSay();
							}
						}else{
							//done, remove player
							$('#jSpeaker_player_holder').remove();
						}
					}
				});
			}
		}
	};
	
	//speak one jSpeakerLine...
	$.fn._jSpeakerSay = function(){
		$('#jSpeaker_player_holder').jPlayer("setMedia", {
			mp3: "http://translate.google.com/translate_tts?tl=" + settings['lang'] + "&q=" + escape($.trim(jSpeakerLines[jSpeakerLine]))
		}).jPlayer("play");
		jSpeakerLine++;
	}
	
})(jQuery);
