(function ($) {
    jQuery.fn.playit = function (options) {
        var opts = $.extend({},
        $.fn.playit.defaults, options); //console.log(window.soundManager);
        if (!window.soundManager) { //console.log('SoundManager - Missing');
            _load_missing_js("/js/soundmanager2.js");
            soundManager.url = "/js/soundmanager2.swf";
			//soundManager.debug=true;
            //soundManager.consoleOnly = true;
            soundManager.onload = function () {};
        } //console.log('SoundManager - Here');
        var $this = $(this);
        $this.data('opts', opts);
        return this.each(function (i, e) {
            var $t = jQuery(this); 
			
			
			
			//IMPORTANT !!!
            //Prevent MULTIPLE Binding !!
            //IN case when they was already Binded!
            jQuery(e).unbind('click.playit'); //Bind Click 
            jQuery(e).bind('click.playit', function (event) {    
			
			var self = this;
		
                //event.preventDefault();
				//console.log('Az te ebavam !!!');
                var orig_title = $t.attr('title');
                var sound_id = $t.attr('sound_id');
		//		console.log('sound_id = ' + sound_id);
                var file_url = opts.sound_location + sound_id; //console.log('file_url = ' + file_url);
                //                $(".sound_progress").css('border', 'thin solid red'); //$t.wrap("<span class='sc-player'>[</span>");
                soundManager.stopAll();
				
				//soundManager.destroySound(sound_id);
				
                soundManager.createSound({
                    id: sound_id,
                    url: file_url,
                    whileloading: function () { 
					//					console.log('sound '+this.sID+' loading, '+this.bytesLoaded+' of '+this.bytesTotal);
                        //if(this.bytesLoaded!=this.bytesTotal){
                        //percent = round((this.bytesLoaded / this.bytesTotal) * 100); //
						//
						
                        //}
                        //$('.sound_progress').text('Load : ' + Math.round(this.bytesLoaded / this.bytesTotal  * 100 )+' %');
						//$kifla.attr('title', 'Load '+ Math.round(this.bytesLoaded / this.bytesTotal  * 100 )+' %');
						//console.log(Math.round(this.bytesLoaded / this.bytesTotal  * 100 ));
                    },
					whileplaying:function (){
					var percent = (Math.round(this.position / this.duration  * 100 ));
					//console.log(self);
					$(self).attr('title', Math.round(this.position / this.duration  * 100 ));
					
			//
			$('.sound_progress').text('play ' + percent+' %');
					//self.attr('title', Math.round(this.position / this.duration  * 100 )+' %');
					},
                    onfinish: function () { //
					//alert(this.sID+' finished playing');
					$('.sound_progress').text('finish');
                    }
                });
               // $t.attr('title', 'loading file: ' + file_url);

                soundManager.play(sound_id);
				
            });
        })
    } //defaults
    jQuery.fn.playit.defaults = {
        sound_location: "http://spokenrecall.controlnet.com/upload/audio/split/"
    }; //private part
    function _load_missing_js($file_name) {
        $.getScript($file_name, function () {
            alert('Loaded');
        });
    }
})(jQuery);
