var Apollo = new Class({
	Implements: [Options],
	initialize: function(sSelector, cOptions){
		this.setOptions(cOptions);
		
		// objects
		this.barLoading = null;
		this.barPlaying = null;
		this.barProgress = null;
		this.barVolume = null;
		this.barVolumeValue = null;
		this.buttonNext = null;
		this.buttonPause = null;
		this.buttonPlay = null;
		this.buttonPrevious = null;
		this.captionProgress = null;
		this.currentTrackID = this.options.startTrack;
		this.isPlaying = false;
		this.listTrack = {title: [], url: []};
		this.player = $(sSelector);
		this.titleTrack = null;
		this.track = null;
		this.volumeSlider = null;
		
		
		if(this.options.autoRender) this.render();
		
		soundManager.url = this.options.swf;
		soundManager.flashVersion = this.options.flashVersion;
		soundManager.debugMode = false;
		soundManager.onload = function(){
			if(this.listTrack.title.length > 0){
				this.loadTrack(this.listTrack.title[this.currentTrackID], this.listTrack.url[this.currentTrackID]);
				this.options.onLoad();
			}
		}.bind(this);
		
	},
	
	buttonControl: function(){
		if(this.currentTrackID == 0 && this.options.startTrack == 0){
			this.buttonPrevious.setStyle('visibility', 'hidden');
		}else this.buttonPrevious.setStyle('visibility', 'visible');
			
		if(this.currentTrackID == (this.listTrack.title.length - 1)){
			this.buttonNext.setStyle('visibility', 'hidden');
		}else this.buttonNext.setStyle('visibility', 'visible');
	},
	
	loadTrack: function(sTitle, sFile){
		
		var me = this;
		var nPlayProgress = 0;
		
		if(this.track) this.track.destruct();
		
		this.titleTrack.set('html', sTitle);
		this.currentTrackID = this.listTrack.url.indexOf(sFile);
		this.track = soundManager.createSound({
			id: 'apolloTrack',
			url: sFile,
			volume: me.options.volume,
			onfinish: function(){
				me.next();
			},
			whileloading: function(){
				me.barLoading.setStyle('width', (this.bytesLoaded * 100 / this.bytesTotal) * (me.barProgress.getSize().x / 100) - 2);
			},
			whileplaying: function(){
				nPlayProgress = ((this.position / (me.track.duration / 100)) + 1) * (me.barLoading.getSize().x / 100) - 5;
				me.barPlaying.setStyle('width', nPlayProgress);
				me.setTime(this.position);
			},
			volume: this.options.volume
		});
		if(this.options.autoPlay || this.isPlaying) this.play();
	},

	next: function(){
		if(this.currentTrackID < this.listTrack.title.length){
			this.currentTrackID++;
			this.loadTrack(this.listTrack.title[this.currentTrackID], this.listTrack.url[this.currentTrackID]);
			this.options.onNext();
		}else{
			this.track.destruct();
			me.options.onFinish();
		}
		this.buttonControl();
	},
	
	options: {
		autoPlay: false,
		autoRender: true,
		flashVersion: 9,
		onFinish: function(){},
		onLoad: function(){},
		onNext: function(){},
		onPlay: function(){},
		onPrevious: function(){},
		startTrack: 0,
		swf: '/img/',
		volume: 80,
		volumeImage: '/img/volume.png'
	},
	
	play: function(){
		if(this.track.playState != 1){
			this.buttonPlay.removeClass('button-play').addClass('button-pause');
			this.track.play();
			this.isPlaying = true;
		}else{
			this.isPlaying = this.track.paused;
			if(this.track.paused){
				this.buttonPlay.removeClass('button-play').addClass('button-pause');
			}else{
				this.buttonPlay.removeClass('button-pause').addClass('button-play');
			}
			this.track.togglePause();
		}
		this.buttonControl();
		this.options.onPlay();
	},
	
	playTrack: function(nTrackID){
		this.isPlaying = true;
		this.loadTrack(this.listTrack.title[nTrackID], this.listTrack.url[nTrackID]);
	},
	
	previous: function(){
		if(this.currentTrackID > 0) this.currentTrackID--;
		if(this.currentTrackID >= 0){
			this.loadTrack(this.listTrack.title[this.currentTrackID], this.listTrack.url[this.currentTrackID]);
			this.options.onPrevious();
		}else this.track.destruct();
		this.buttonControl();
	},
	
	render: function(){
		this.buttonPlay = new Element('div').addClass('button-play').addEvent('click', function(){
			this.play();
		}.bind(this)).inject(this.player, 'bottom');
		this.titleTrack = new Element('h1').inject(this.player, 'bottom');
		this.captionProgress = new Element('div').addClass('caption-progress').inject(this.player, 'bottom');
		this.barProgress = new Element('div').addClass('bar-progress').addEvent('click', function(e){
			var nPos = ((e.page.x - this.barProgress.getPosition().x) * 100) / this.barProgress.getSize().x;
			if(this.track.readyState == 3) this.track.setPosition(this.track.duration / 100 * nPos);
		}.bind(this)).inject(this.player, 'bottom');
		this.barLoading = new Element('div').addClass('loading').inject(this.barProgress, 'bottom');
		this.barPlaying = new Element('div').addClass('playing').inject(this.barProgress, 'bottom');
		new Element('div').setStyle('clear', 'both').inject(this.player, 'bottom');
		
		this.buttonPrevious = new Element('div').addClass('button-previous').addEvent('click', function(e){
			this.previous();
		}.bind(this)).inject(this.player, 'bottom');
		this.barVolume = new Element('div').addClass('bar-volume').addEvent('click', function(e){
			var nPos = ((e.page.x - this.barVolume.getPosition().x) * 100) / this.barVolume.getSize().x;
			this.barVolumeValue.setStyle('width', (this.barVolume.getSize().x / 100) * nPos);
			this.options.volume = nPos;
			this.track.setVolume(nPos);
		}.bind(this)).inject(this.player, 'bottom');
		this.barVolumeValue = new Element('div').addClass('value').setStyle('width', (this.barVolume.getSize().x / 100) * this.options.volume).inject(this.barVolume, 'bottom');
		this.buttonNext = new Element('div').addClass('button-next').addEvent('click', function(e){
			this.next();
		}.bind(this)).inject(this.player, 'bottom');
		new Element('div').setStyle('clear', 'both').inject(this.player, 'bottom');
		this.player.setStyle('visibility', 'visible');
		this.buttonControl();
	},
	
	setTime: function(sPos){
		var sTimeSecs = new String(parseInt(this.track.duration / 1000) % 60);
		sTimeSecs = sTimeSecs.length == 1 ? '0' + sTimeSecs : sTimeSecs;
		var sPlayingSecs = new String(parseInt(sPos / 1000) % 60);
		sPlayingSecs = sPlayingSecs.length == 1 ? '0' + sPlayingSecs : sPlayingSecs;
		
		var sTime = parseInt(this.track.duration / 1000 / 60) + ':' + sTimeSecs;
		var sPlaying = parseInt(sPos / 1000 / 60) + ':' + sPlayingSecs;
		this.captionProgress.set('html', sPlaying + ' / ' + sTime);
	}
	
});
