/**
 * Klasa obsługuje klip audio.
 */
function AudioTrack(_parent, _id, _title, _position, _track) {
	this.parent = _parent;
	this.id = _id;
	this.title = _title;
	this.position = _position;
	this.track = _track;
	this.container = null;
	this.background = false;
	this.loop = false;
	this.player = null;
	this.paused = false;
}

AudioTrack.prototype.setContainer = function(_container) {
	this.container = _container;
}

AudioTrack.prototype.isLoop = function() {
	return this.loop;
}

AudioTrack.prototype.setLoop = function(_loop) {
	this.loop = (_loop === true);
}

AudioTrack.prototype.isBackground = function() {
	return this.background;
}

AudioTrack.prototype.setBackground = function(_background) {
	this.background = (_background === true);
	this.setLoop(this.background);
}

AudioTrack.prototype.clearContainer = function() {
	while (this.container.childNodes.length > 0) {
		this.container.removeChild(this.container.childNodes[0]);
	}
}

AudioTrack.prototype.getParent = function() {
	return this.parent;
}

AudioTrack.prototype.createPlayer = function(audioTrack) {
	var id = '#audio_' + audioTrack.container.id;
	var player = $(id).jPlayer({
		swfPath: baseURL + templatePath + 'js/',
		solution: 'flash',
		supplied: 'mp3',
		preload: 'auto',
		volume: 0.8,
		muted: false,
		backgroundColor: '#000000',
		cssSelectorAncestor: id + '_interface',
		cssSelector: {
			/*videoPlay: '.jp-video-play',*/
			play: '.jp-play',
			pause: '.jp-pause',
			stop: '.jp-stop',
			seekBar: '.jp-seek-bar',
			playBar: '.jp-play-bar',
			mute: '.jp-mute',
			unmute: '.jp-unmute',
			volumeBar: '.jp-volume-bar',
			volumeBarValue: '.jp-volume-bar-value',
			currentTime: '.jp-current-time',
			duration: '.jp-duration'
		},
		errorAlerts: false,
		warningAlerts: false,
		ready : function() {
			$(this).jPlayer("setMedia", {
				mp3: (baseURL+audioTrack.track)
			}).jPlayer("play"); 
		},
		ended: function() {
			if (audioTrack.isLoop()) $(this).jPlayer("play");
		}
	});
	audioTrack.setPlayer(player);
	return player;
}

AudioTrack.prototype.setPlayer = function(_player) {
	this.player = _player;
}

AudioTrack.prototype.getPlayer = function() {
	return this.player;
}

/**
 * Odrysowuje zdjęcie galerii.
 */
AudioTrack.prototype.draw = function(parentContainer) {
	if (!parentContainer) return;
	
	var id = 'audio_' + parentContainer.id;
	var core = document.createElement("div");
	core.id = id;
	var jpAudio = document.createElement("div");
	jpAudio.className = "jp-audio";
	var jpTypeSingle = document.createElement("div");
	jpTypeSingle.className = "jp-type-single";
	var jpInterface = document.createElement("div");
	jpInterface.id = id + '_interface';
	jpInterface.className = "jp-interface";
	var jpControls = document.createElement("ul");
	jpControls.className = "jp-controls";
	var jpPlay = document.createElement("a");
	jpPlay.className = "jp-play";
	jpPlay.href = "#";
	jpPlay.style.display = "block";
	var jpPlayLi = document.createElement("li");
	var jpPause = document.createElement("a");
	jpPause.className = "jp-pause";
	jpPause.href = "#";
	jpPause.style.display = "none";
	var jpPauseLi = document.createElement("li");
	var jpStop = document.createElement("a");
	jpStop.className = "jp-stop";
	jpStop.href = "#";
	var jpStopLi = document.createElement("li");
	var jpMute = document.createElement("a");
	jpMute.className = "jp-mute";
	jpMute.href = "#";
	var jpMuteLi = document.createElement("li");
	var jpUnmute = document.createElement("a");
	jpUnmute.className = "jp-unmute";
	jpUnmute.href = "#";
	var jpUnmuteLi = document.createElement("li");
	var jpProgress = document.createElement("div");
	jpProgress.className = "jp-progress";
	var jpSeekBar = document.createElement("div");
	jpSeekBar.className = "jp-seek-bar";
	jpSeekBar.style.width = "100%";
	var jpPlayBar = document.createElement("div");
	jpPlayBar.className = "jp-play-bar";
	jpPlayBar.style.width = "1%";
	var jpVolumeBar = document.createElement("div");
	jpVolumeBar.className = "jp-volume-bar";
	var jpVolumeBarValue = document.createElement("div");
	jpVolumeBarValue.className = "jp-volume-bar-value";
	jpVolumeBarValue.style.width = "80%";
	var jpCurrentTime = document.createElement("div");
	jpCurrentTime.className = "jp-current-time";
	var jpDuration = document.createElement("div");
	jpDuration.className = "jp-duration";
	
	jpPlayLi.appendChild(jpPlay);
	jpPauseLi.appendChild(jpPause);
	jpStopLi.appendChild(jpStop);
	jpMuteLi.appendChild(jpMute);
	jpUnmuteLi.appendChild(jpUnmute);
	jpControls.appendChild(jpPlayLi);
	jpControls.appendChild(jpPauseLi);
	jpControls.appendChild(jpStopLi);
	jpControls.appendChild(jpMuteLi);
	jpControls.appendChild(jpUnmuteLi);
	jpSeekBar.appendChild(jpPlayBar);
	jpProgress.appendChild(jpSeekBar);
	jpVolumeBar.appendChild(jpVolumeBarValue);
	jpInterface.appendChild(jpControls);
	jpInterface.appendChild(jpProgress);
	jpInterface.appendChild(jpVolumeBar);
	jpInterface.appendChild(jpCurrentTime);
	jpInterface.appendChild(jpDuration);
	jpTypeSingle.appendChild(jpInterface);
	jpAudio.appendChild(jpTypeSingle);
	
	parentContainer.appendChild(core);
	parentContainer.appendChild(jpAudio);
};

AudioTrack.prototype.drawLink = function(parentContainer) {
	var div = document.createElement("div");
	div.id = "link_audioTrack_" + this.id;
	div.className = "menuItem";
	div.onclick = this.play;
	div.innerHTML = this.title;
	if (parentContainer) {
		parentContainer.appendChild(div);
	}
}

AudioTrack.prototype.play = function() {
	var regex = /link_audioTrack_(-?\d+)/;
	if (!this.id || !this.id.match(regex)) return;
	var match = regex.exec(this.id);
	var audioTrack = null;
	if (match[1] == -1) {
		audioTrack = this;
	} else {
		audioTrack = Albums.getAudioTrack(match[1]);
	}
	if (!audioTrack) return;
	audioTrack.clearContainer();
	audioTrack.draw(audioTrack.container);
	audioTrack.createPlayer(audioTrack);
}

AudioTrack.prototype.pause = function() {
	if (this.player && !this.paused) {
		this.player.jPlayer("pause");
		this.paused = true;
	}
}

AudioTrack.prototype.resume = function() {
	if (this.player && this.paused) {
		this.player.jPlayer("play");
		this.paused = false;
	}
}

AudioTrack.prototype.getId = function() {
	return this.id;
}	
