﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
// MODIFIED by Alexander Sidko (mortiy@gmail.com)
/***************************/
var playStatus = ['Undefined','Остановлено','Пауза','Проигрывание','ScanForward','ScanReverse','Буферизация...','Waiting','Конец','Передача данных...','Готов','Reconnecting'];
//This is the class that interact with the interface
var player = new (function(){
	//Simulate a playlist
	this.playList = [];
	this.playHref = [];
	//The song position
	this.position = 0;
	//The current volume
	this.volume = 3;
	//Status: 0:pause, 1:play
	this.status = 1;
	//Jump to the next or first song if the it is in the last position
	this.init = function(){
		var me = this;
		$(".title").fadeOut(200, function(){
			$(this).text(me.playList[me.position]);
			document.audio.URL = base_url+me.playHref[me.position];
			$(".song").removeClass('active');
			$("#song_"+me.position).addClass('active');
			me.volume = Math.round(document.audio.settings.volume/10);
			$("#volume").css("backgroundImage", "url('/css/images/vol" +  me.volume + ".png')");
		}).fadeIn();
	}
	
	this.changePosition = function(){
		var duration = document.audio.currentMedia.duration;
		var total_width = $("#progress_bar").width();
		var sec_per_pixel = duration/total_width;
		var left_offset = parseInt($("#progress_dragger").css('left'),10);
		
		document.audio.controls.currentPosition = Math.round((left_offset+25)*sec_per_pixel);
		$("#time").html(document.audio.controls.currentPositionString);
		
	}
	
	this.updatePosition = function(){
		var duration = document.audio.currentMedia.duration;
		var total_width = $("#progress_bar").width();
		var sec_per_pixel = duration/total_width;
		var left_offset = Math.round(document.audio.controls.currentPosition/sec_per_pixel)-25;
		
		$("#progress_dragger").css('left',left_offset+'px')

	}
	
	this.changeTitle = function(title){
		$(".title").fadeOut(200, function(){
			$(this).text(title);
		}).fadeIn();
	}
	
	this.Timer = function(){
		duration = document.audio.currentMedia.duration;
		if(duration-2 <= document.audio.controls.currentPosition && duration>10 ){
			clearInterval(time);player.nextSong()
			} 
		
			
		$("#time").html(document.audio.controls.currentPositionString);
		 if (!is_dragging) {player.updatePosition()}
	}
	
	this.loadSong =  function(href,title,id){
		
		player.changeTitle(title);
		document.audio.URL = base_url+href;
		$(".song, .fav_songs").removeClass('active');
		$("#fav_"+id).addClass('active');
		
			time = setInterval(function(){ player.Timer();},500);
			player.playPause(1);

			return false;
	}
	
	this.playSong = function(id){
		
		$(".song").removeClass('active');
		$("#song_"+id).addClass('active');
		song_id = id;
		this.position = song_id;
		this.changeTitle(this.playList[song_id]);
		document.audio.URL = base_url+this.playHref[song_id];
		time = setInterval(function(){ player.Timer();},500);
		player.playPause(1);
		return false;
		
	}
	this.nextSong = function(){
		if(this.position+1 == this.playList.length)
			this.position = 0;
		else
			this.position++;
		//So, the reference to this class is not lost
		var me = this;
		clearInterval(time);
		$(".title").fadeOut(200, function(){
			$(this).text(me.playList[me.position]);
			document.audio.URL = base_url+me.playHref[me.position];
			$(".song").removeClass('active');
			$("#song_"+me.position).addClass('active');
			if(me.status == 0) {
				document.audio.controls.play()
				time = setInterval(function(){ player.Timer();},500);
			}
		}).fadeIn();
	}
	//Jump to he previous or last song if it is in the first position
	this.prevSong = function(){
		if(this.position-1 < 0)
			this.position = this.playList.length-1;
		else
			this.position--;
		
		var me = this;
		$(".title").fadeOut(200, function(){
			$(this).text(me.playList[me.position]);
			document.audio.URL = base_url+me.playHref[me.position];
			$(".song").removeClass('active');
			$("#song_"+me.position).addClass('active');
			if(me.status == 0) document.audio.controls.play();
		}).fadeIn();
	}
	//Increase the volume in one point
	this.volumeInc = function(){
		if(this.volume +1 <= 10){
			   X = document.audio.settings.volume; 
			   document.audio.settings.volume = X + 10; 
			this.volume++;
			var me = this;
			//$("#volume").fadeOut(50, function(){
			$("#volume").css("backgroundImage", "url('/css/images/vol" +  me.volume + ".png')");
			//}).fadeIn(50);
		}
	}
	//Decrease the volume in one point
	this.volumeDec = function(){
		if(this.volume -1 > 0){
			X = document.audio.settings.volume; 
			document.audio.settings.volume = X - 10; 
			this.volume--;
			var me = this;
			//$("#volume").fadeOut(50, function(){
			$("#volume").css("backgroundImage", "url('/css/images/vol" +  me.volume + ".png')");
			//}).fadeIn(50);
		}
	}
	//Toggle play & pause
	this.playPause = function(force){
		
		this.status = !this.status;
		var me = this;
		$("#play").fadeOut(200, function(){
			if(me.status == 0 || force) {
				$(this).css("backgroundImage", "url('/css/images/pause.jpg')");
				document.audio.controls.play();
				me.status = 0;
				time = setInterval(function(){ player.Timer();},500);
			}
			else {
				document.audio.controls.pause();
				clearInterval(time);
				$(this).css("backgroundImage", "url('/css/images/play.jpg')");
			}
			
		}).fadeIn(200);
		
	}
});
//Starts playing :P