var smokyAudioPlayer = new Class({
	
	Implements: [Options],
	
	options: {
		controlButtonSelector: 'div.contolButton',
		timelineSelector: 'div.timeline',
		timeIndecatorSelector: 'div.timeIndicator',
		knobSelector: 'div.knob',
		playlistItems: new Array(),
		mp3URL: '',
		autoPlay: false
	},
		
	initialize: function(playerId, swfId, options) {
		this.setOptions(options);
		this.audioPlayer = document.id(playerId);
		if(!this.audioPlayer){return;}
		
		this.setElements(playerId, swfId);
		this.builtPlaylist();
		this.builtSlider();
		this.builtListener();		
		this.embedSWF();
		this.builtContolButton();				
	},

	setElements: function(playerId, swfId){
		var a = this.audioPlayer;
		var o = this.options;
		this.swfId = swfId;
		this.controlButton = a.getElement(o.controlButtonSelector);
		this.timeline = a.getElement(o.timelineSelector);
		this.timeIndicator = a.getElement(o.timeIndecatorSelector);
		this.knob = a.getElement(o.knobSelector);
	},
	
	builtSlider: function(){
		this.slider = new Slider(this.timeline, this.knob, {
			range: [0, 100],
			offset: 5,
			obj: this,
			onChange: function(value){
				o = this.obj;			
				if(!o){return;}	
				var el = document.id(o.swfId);
				if (value && el){
					if(this.isDragging){
						el.SetVariable("method:setPosition", Math.round((value/100)*o.listener.duration));
					}
				}
			}
		});
		this.slider.element.addEvent('mousedown', function(){
			var el = document.id(this.obj.swfId);
			el.SetVariable("method:setPosition", Math.round((this.step/100)*this.obj.listener.duration));
		}.bind(this.slider));
		
		this.slider.obj = this;
	},
	
	builtListener: function(){
		this.inited = false;
		this.startMp3 = this.playlist.length > 0 ? this.playlist[0].href : this.options.mp3URL;
		this.listener = LAMBRINIDIS['audioListener_'+this.swfId]  = {
			obj: this
		};
					
		this.listener.onUpdate = function(){
			var o = this.obj;
			if(!o.inited){
				o.inited = true;
				this.position = 0;
				var el = document.id(o.swfId);
				el.SetVariable("method:setUrl", o.startMp3);
				if(o.options.autoPlay){
					el.SetVariable("method:play", "");
				}
			}
			if(!o.slider.isDragging){
				o.slider.set(Math.round(100*(this.position / this.duration)));
				o.timeIndicator.set('text', o.getTimeFromMilli(this.position)+' / '+ o.getTimeFromMilli(this.duration));
				if(this.isPlaying == "true"){
					o.controlButton.addClass('pause');
				}
				else{
					o.controlButton.removeClass('pause');
				}
			}
		};
		
	},
	
	embedSWF: function(){
		var params = {allowScriptAccess: "always", wmode: 'transparent'};
		var args = {};
		var flashVars = {listener: 'LAMBRINIDIS.audioListener_'+this.swfId, interval: 100, enabled: true, useexternalinterface: true}
		swfobject.embedSWF('swf/player_mp3_js.swf', this.swfId, 1, 1, "8", null, flashVars, params, args);
	},
	
	builtPlaylist: function(){
		this.playlist = this.options.playlistItems;
		this.playlist.each(function(item, index){
			item.addEvent('click', function(ev){
				var el = document.id(this.swfId);
				el.SetVariable("method:stop", "");
				el.SetVariable("method:setUrl", ev.target.href);
				el.SetVariable("method:play", "");
			}.bind(this));
		}.bind(this));
	},
	
	builtContolButton: function(){
		this.controlButton.addEvent('click', function(ev){
			el = ev.target;
			document.id(this.swfId).SetVariable("method:"+ (el.hasClass('pause') ? "pause" : "play"), "");
		}.bind(this));
	},
	
	getTimeFromMilli :function(m){
		if(m > 0){
			var minutes = Math.floor((m % (1000*60*60)) / (1000*60));
			if(minutes < 10 ){
				minutes = '0' + minutes;
			}
			var seconds = Math.floor((((m % (1000*60*60)) % (1000*60)) / 1000));
			if(seconds < 10 ){
				seconds = '0' + seconds;
			}
			return minutes+':'+ seconds;
		}
		else{
			return '00:00';	
		}
	}
	
});
