﻿var isPlaying = false;
var isPaused = false;
var songPos = 0;
var defaultVolume = 50;
var iResume = 0;

function migMusic(url, title, artist, album) {
	this.url = url;
	this.author = title;
	this.title = artist;
	this.artist = album;
}

var isPlaying = false;
var isPaused = false;

function migMusic(url, title, artist, album) {
	this.url = url;
	this.author = title;
	this.title = artist;
	this.artist = album;
}

function playSong() {
	if (aMusic.length < 1) return;
	if (musicIndex < 0 || musicIndex >= aMusic.length) { musicIndex = 0; }
	if (!isPaused) {
		var url = aMusic[musicIndex].url;
		setCookie("MusicIndex", musicIndex, 1);
		//audio.LoadMedia(url);
		songPos = 0;
		audio.SetVolume(100);
		audio.PlayMedia(url);
		//var vol = document.getElementById("ctl00_sldMusicVolume").value;
		//if (vol < 0 || vol > 100) vol = defaultVolume;
		//audio.SetVolume(vol);
	}
	else {
		audio.PlayMedia();
	}
	isPlaying = true;
	isPaused = false;
	setCookie("MusicOn", true, 3);
}

function toggleMusic() {
	if (!isPlaying) {
		playSong();
	}
	else {
		if (!isPaused) {
			pauseSong();
		}
		else { playSong(); }
	}
}

function pauseSong() {
	if (audio != null) {
		audio.PauseMedia();
	}
}
function stopSong() {
	if (audio != null) {
		audio.StopMedia();
	}
}

function AudioFinished() {
	isPlaying = false;
	isPaused = false;
	nextSong();
}
function AudioPlayStarted() {

	isPlaying = true;
	isPaused = false;
	var vol = document.getElementById("ctl00_sldMusicVolume").value;
	//var vol = defaultVolume;
	audio.SetVolume(vol);
	setCookie("MusicOn", true, 3);
}
function AudioPaused() {
	isPaused = true;
	isPlaying = true;
	setCookie("MusicOn", false, 3);
	setCookie("MusicPos", audio.GetPosition(), 1);
}
function AudioStopped() {
	isPlaying = false;
	isPaused = false;
}
function AudioProgress(pos) {
	var oldPos = songPos;
	if ((pos - songPos) >= 1) {
		setCookie("MusicPos", pos, 1);
		songPos = pos;
	}
}

function nextSong() {
	if (audio != null) {
		musicIndex++;
		if (musicIndex >= aMusic.length) { musicIndex = 0; }
		playSong();
	}
}

function prevSong() {
	if (audio != null) {
		musicIndex--;
		if (musicIndex < 0) { musicIndex = aMusic.length - 1; }
		playSong();
	}
}

function setVolume(volume) {
	if (audio != null) {
		audio.SetVolume(volume);
		setCookie("MusicVolume", volume, 3);
	}
}
function volumeChange() {
	var vol = document.getElementById("ctl00_sldMusicVolume").value;
	setVolume(vol);
}

function AudioReady() {
	if (iResume > 0) audio.SetPosition(iResume);
	iResume = 0;
	//audio.PlayMedia();
	//alert("ready");
}

function initMusic() {
	if (aMusic.length < 1) return;
	var pos = getCookie("MusicPos", 0);
	var vol = getCookieInt("MusicVolume", defaultVolume);
	if (vol < 0 || vol > 100) { vol = defaultVolume; }
	document.getElementById("ctl00_sldMusicVolume").value = vol;
	var index = getCookie("MusicIndex", 0);
	if (index < 0 || index >= aMusic.length) { index = 0; }
	musicIndex = index;
	var bOn = getCookieBool("MusicOn", true);
	document.getElementById("ctl00_chkMusicOn").checked = bOn;
	if (bOn == true) {
		resumeSong(pos, vol);
	}
}

function resumeSong(pos, vol) {
	if (aMusic.length < 1) return;
	if (musicIndex < 0 || musicIndex >= aMusic.length) { musicIndex = 0; }
	var url = aMusic[musicIndex].url;
	setCookie("MusicIndex", musicIndex, 1);
	audio.SetVolume(100);
	audio.PlayMedia(url);
	audio.SetVolume(vol);
	iResume = pos;
	setCookie("MusicOn", true, 3);
}

function saveMusic() {
	if (audio != null) {
		var bon = document.getElementById("ctl00_chkMusicOn").checked;
		setCookie("MusicOn", bon, 3);
		setCookie("MusicVolume", GetVolume(), 3);
		setCookie("MusicIndex", musicIndex, 1);
		setCookie("MusicPos", audio.GetPosition(), 1);
	}
}
