var Button = Class.create();
Button.prototype = {
  initialize: function (music_id, state, button, graphical)
	{
    this.music_id = music_id;
    this.state = (state != null) ? state : STATE.STOP;
    this.button = $(button);
    this.graphical = graphical;
  },
	equals: function (music_id) {
		return (this.music_id == music_id);
	},
	// Actualiza
	update: function (music_id, state, button, graphical) {
		if (music_id != null) this.music_id = music_id;
		if (state != null) this.state = state;
		if (button != null) this.button = button;
		if (graphical != null) this.graphical = graphical;
	}
}

var Buttons = Class.create();
Buttons.prototype = {
  initialize: function ()
	{
		this.buttons = new Array();
		this.TYPE = {
			GRAPHICAL: true,
			TEXTUAL: false
		};
	},
	// Insere ou actualiza
	insert: function (music_id, state, button, graphical) {
		var found = false;
		for (index = 0; index < this.buttons.length; index++ )
		{
			if (this.buttons[0].equals(music_id)) {
				this.buttons[0].update(music_id, state, button, graphical);
				found = true;
			}
		}
		// Se nao existir, entao cria um novo
		if (!found)
		{
			this.buttons[this.buttons.length] = new Button(music_id, state, button, graphical);
		}
	},
	setState: function (music_id, new_state)
	{
		var found = false;
		for (index = 0; index < this.buttons.length; index++ )
		{
			var but_item = this.buttons[index];
			if (but_item.equals(music_id))
			{
				var button = but_item.button;
				
				// Guarda a informação actual do botão
				if (but_item.state != null) {
					button.writeAttribute('html_' + but_item.state, button.innerHTML);
					button.writeAttribute('href_' + but_item.state, button.href);
				}
				// Se o botão tiver atributos href e html personalizados, entao mostrar esses
				var but_new_href = button.readAttribute('href_' + new_state);
				var but_new_html = button.readAttribute('html_' + new_state);
				
				// Se tiver atributos, inseri-los no botão, senão usar os default
				if (but_new_href)
				{
					button.href = but_new_href;
				}
				else
				{
					switch ( new_state ) {
					case STATE.STOP:
						button.href = 'javascript:ouvirMusica(' + music_id + ')';
						break;
					case STATE.LOADING:
					case STATE.PLAYING:
						button.href = 'javascript:pararMusica()';
						break;
					}
				}
				
				if (but_new_html)
				{
					button.innerHTML = but_new_html;
				}
				else
				{
					switch ( new_state ) {
					case STATE.STOP:
						if (but_item.graphical == this.TYPE.GRAPHICAL) 
							button.innerHTML = '<img src="templates/imagens/ouvir_ouvir.gif" title="Ouvir m&uacute;sica"><br/>Ouvir';
						else
							button.innerHTML = 'Ouvir';
						break;
					case STATE.LOADING:
						if (but_item.graphical == this.TYPE.GRAPHICAL) 
							button.innerHTML = '<img src="templates/imagens/ouvir_acarregar.gif" title="M&uacute;sica est&aacute; a carregar, carregue para parar"><br/>A carregar...';
						else
							button.innerHTML = 'A carregar...';
						break;
					case STATE.PLAYING:
						if (but_item.graphical == this.TYPE.GRAPHICAL) 
							button.innerHTML = '<img src="templates/imagens/ouvir_parar.gif" title="Parar m&uacute;sica"><br/>Parar';
						else
							button.innerHTML = 'Parar';
						break;
					}
				}
				
				but_item.state = new_state;
				found = true;
			}
		}
		return found;
	}
};
