/*########################################
#                                        #
# RedPlayer v1.0 ;D                      #
#                                        #
# Made by Luiz Souza && Wilker Lúcio     # 
#                                        #      
########################################*/


var MyPlayer = function( args ){
	// Variables Globais
	this.sound     = new FlashInterface("Sound"); 

	this.nowPlaying      = -1;
	this.playlistJsPhp   = getRealPath() + "playlist.js.php?f=";

	this.soundPosition  = 0;               
	this.soundVolume    = 100;

	this.playerStatus   = 0;
	this.obj2Show       = ( args.elementID != undefined ) ? args.elementID : false;

	// PlayList
	this.playList = new Array();
	this.playList.Name    = "Default";
	this.playList.Random  = "NO";
	this.playList.Entries = new Array(); 

	// Template
	this.templateFile   = ( args.templateFile != undefined ) ? "templates/" + args.templateFile : "templates/default.html";
	this.tpl            = Template.load( this.templateFile );

 	// Events
	this.onPlay         = new cEvent();
	this.onPause        = new cEvent();
	this.onStop         = new cEvent();
	this.onVolumeChange = new cEvent();
	this.onLoadSound    = new cEvent();
	this.onChangeSound  = new cEvent();

	// Call The Show Function
	this.Show();

};

MyPlayer.prototype.LoadPlayList = function( playListFile ) {
	if( playListFile == undefined ){
		alert("Por favor defina a PlayList.");
		return;
	}

	var strPlayList = Ajax.open( this.playlistJsPhp + playListFile, {})
	var MyPlayList  = eval('(' + strPlayList + ')');

	this.playList.Name    = MyPlayList.name;
	this.playList.Random  = MyPlayList.random;
	this.playList.Entries = MyPlayList.entries;

	if( this.playList.Entries.length == 0 ){
		alert("Está PlayList não contem músicas.");
		return;
	}else{
		this.LoadSound( this.playList.Entries[0].soundFile, this.playList.Entries[0].soundTitle, this.playList.Entries[0].soundArtist, 0 );
	}
};

MyPlayer.prototype.LoadSound = function( soundFile, soundTitle, soundArtist, toPlay ) {
	if( soundFile == undefined ){
		alert("Por favor defina a música.");
		return;
	}
	
	this.Stop();
	this.sound = new FlashInterface("Sound");

	this.sound.callFlash("loadSound", soundFile, true);
	this.sound.listen("onSoundComplete", function() {
		this.Stop();
		this.Forward();
	}.bind(this));

	toPlay = (toPlay == undefined) ? 0 : toPlay;
	this.soundPosition = 0;
	this.nowPlaying    = toPlay;

	this.setSoundTitle( soundTitle, soundArtist );
	this.setSoundArtist( soundTitle, soundArtist );
	this.setNextSound();

	this.setPlayerStatus( 1 );
	this.setTiming();

	this.onLoadSound.fire(this);

};

MyPlayer.prototype.Play = function(){
	if( this.getPlayerStatus() == 1 )
		return;


	if( this.soundPosition == "stopped" ){
		this.sound.callFlash("start", 0, 1);
		this.soundPosition = 0;
	}else{
		this.sound.callFlash("start", this.soundPosition, 0);
		this.soundPosition = 0;
	}

	this.setPlayerStatus( 1 );
	this.onPlay.fire( this );
};

MyPlayer.prototype.Pause = function(){
	if( this.getPlayerStatus() == 3 )
		return;

	this.soundPosition = this.sound.callFlash("position")/1000;
	this.sound.callFlash("stop");

	this.setPlayerStatus( 3 );
	this.onPause.fire( this );
};

MyPlayer.prototype.Stop = function(){
	if( this.getPlayerStatus() == 2 )
		return;

	this.sound.callFlash("stop");
	this.soundPosition = "stopped";
	this.tpl.nowTime.innerHTML = "0:00";

	this.setPlayerStatus( 2 );
	this.onStop.fire( this );
};

MyPlayer.prototype.setVolume = function( volume ){
	if( volume > 100 )
		volume = 100;
	if( volume < 0 )
		volume = 0;

	if( volume != this.soundVolume ){
		this.sound.callFlash("setVolume", this.soundVolume);
		this.soundVolume = volume;
		this.onVolumeChange.fire( volume );
	}
};

MyPlayer.prototype.getVolume = function (){
	return this.soundVolume;
};

MyPlayer.prototype.setTiming = function(){
	if( this.sound.callFlash("duration") != false ){
		var secs1 = Math.floor( this.sound.callFlash("duration")/1000 );
		var mins1 = Math.floor( secs1 / 60 );

		secs1 = secs1 % 60;
		mins1 = mins1 % 60;

		secs1 = ( secs1 < 10 ) ? '0' + secs1 : secs1;
		mins1 = mins1;

		this.tpl.totalTime.innerHTML = mins1 + ":" + secs1;
	}

	if( this.sound.callFlash("position") != false && this.soundPosition != "stopped" ){
		var secs2 = Math.floor( this.sound.callFlash("position")/1000 );
		var mins2 = Math.floor( secs2 / 60 );

		secs2 = secs2 % 60;
		mins2 = mins2 % 60;

		secs2 = ( secs2 < 10 ) ? '0' + secs2 : secs2;
		mins2 = mins2;

		this.tpl.nowTime.innerHTML = mins2 + ":" + secs2;
	}

	setTimeout(this.setTiming.bind(this), 500);
};

MyPlayer.prototype.getRandomIndex = function(){
	var length = this.playList.Entries.length;
	var index  = Math.floor( Math.random()*length );

	if( index == this.nowPlaying ){
		index = this.getRandomIndex();
	}

	return index;
};

MyPlayer.prototype.Forward = function(){
	if( this.nowPlaying == -1 || this.nowPlaying == this.playList.Entries.length - 1 )
		return;

	var index = this.nowPlaying + 1;

	if( this.playList.Entries[index] != undefined ){
		this.LoadSound( this.playList.Entries[index].soundFile, this.playList.Entries[index].soundTitle, this.playList.Entries[index].soundArtist, index );
		this.onChangeSound.fire( this );
	}
}

MyPlayer.prototype.Rewind = function(){
	if( this.nowPlaying == -1 || this.nowPlaying == 0 )
		return;

	var index = this.nowPlaying - 1;

	if( this.playList.Entries[index] != undefined ){
		this.LoadSound( this.playList.Entries[index].soundFile, this.playList.Entries[index].soundTitle, this.playList.Entries[index].soundArtist, index );
		this.onChangeSound.fire( this );
	}
};

MyPlayer.prototype.setSoundTitle = function( title, artist ) {
	var titleSearch = URLEncode(artist) + "+-+" + URLEncode(title);
	this.tpl.soundTitle.innerHTML = title + "&nbsp;<a href=\"http://vagalume.uol.com.br/search.php?t=let&q=" + titleSearch + "&be=OK\" target=\"_blank\"><b>+</b></a>";
};

MyPlayer.prototype.setSoundArtist = function( title, artist ){
	var artistSearch = URLEncode(artist).replace(/[\+]+/g,"-");
	this.tpl.soundArtist.innerHTML = artist + "&nbsp;<a href=\"http://vagalume.uol.com.br/" + artistSearch + "/\" target=\"_blank\"><b>+</b></a>";
};

MyPlayer.prototype.setNextSound = function (){
	if( this.nowPlaying == -1 ){
		return;
	}else if( this.nowPlaying == this.playList.Entries.length -1 ){
		this.tpl.soundNext.innerHTML = "Fim da PlayList.";
		return;
	}
	
	var index  = this.nowPlaying + 1;

	if( this.playList.Entries[index] != undefined ){
		var title  = this.playList.Entries[index].soundTitle;
		var artist = this.playList.Entries[index].soundArtist;

		this.tpl.soundNext.innerHTML = artist + " / " + title + "&nbsp;<a href=\"http://vagalume.uol.com.br/search.php?t=let&q=" + URLEncode(artist + " - " + title) + "&be=OK\" target=\"_blank\"><b>+</b></a>";
	}else{
		this.tpl.soundNext.innerHTML = "Fim da PlayList.";
	}
};

MyPlayer.prototype.setPlayerStatus = function ( id ){

	if( id == 0 )
		status = "Carregando";
	else if( id == 1 )
		status = "Reproduzindo";
	else if( id == 2 )
		status = "Parado";
	else if( id == 3 )
		status = "Pausado";

	this.tpl.pStatus.innerHTML = status;
	this.playerStatus = id;
};

MyPlayer.prototype.getPlayerStatus = function (){
	return this.playerStatus;
};

MyPlayer.prototype.Show = function (){ 
	this.tpl.title.innerHTML = this.playList.Name;
 
	this.tpl.btnRewind.onclick = function (){
		this.Rewind();
	}.bind(this);
	this.tpl.btnPlay.onclick = function (){
		this.Play();
	}.bind(this);
	this.tpl.btnPause.onclick = function (){
		this.Pause();
	}.bind(this);
	this.tpl.btnStop.onclick = function (){
		this.Stop();
	}.bind(this);
	this.tpl.btnForward.onclick = function (){
		this.Forward();
	}.bind(this);

	this.tpl.btnVolDown.onclick = function (){
		this.setVolume( this.getVolume() - 10 );
	}.bind(this);
	this.tpl.btnVolUp.onclick = function (){
		this.setVolume( this.getVolume() + 10 );
	}.bind(this);

	var elementBody = document.getElementsByTagName("body").item(0);
	if( this.obj2Show == false ){
		elementBody.appendChild( this.tpl._obj );
	}else{
		var elementUser = document.getElementById( this.obj2Show );
		if( this.obj2Show == "" || elementUser == null ){
			elementBody.appendChild( this.tpl._obj );
		}else{
			elementUser.appendChild( this.tpl._obj );
		}
	}
};
