var chatSoundsEventObserver = Class.create({
	initialize: function(observer, settings) {
		this.observer = observer;
		this.settings = settings;
		this.settings.silentChats = this.settings.silentChats.toArray();
		this.activeChat = null;
		this.init();
	},
	
	init: function() {
		soundManager.onready(function(s) {
			if (s.success) {
				soundManager.createSound('onMessage', '/audio/chat/onMessage.mp3');
				soundManager.createSound('onSentMessage', '/audio/chat/onSentMessage.mp3');
			}
			this.observer.on('sounds.onMessage', this.play.bind(this, 'onMessage'));
			this.observer.on('sounds.onSentMessage', this.play.bind(this, 'onSentMessage'));
			this.observer.on('sounds.toggleSound', this.toggleSound.bind(this));
			this.observer.on('sounds.syncSettings', this.syncSettings.bind(this))
		}.bind(this));
		
		this.observer.on('chatList.activateChat', function(chatName) {
			this.activeChat = chatName;
		}.bind(this));
		
		this.observer.on('chatWindow.closeActiveChat', function() {
			this.activeChat = null;
		}.bind(this));
	},
	
	/**
	 * обновить само число
	 */
	play: function(idSound) {
		//if (this.activeChat && this.settings.silentChats.indexOf(this.activeChat) == -1) {
		if (this.settings.silentChats.indexOf(this.activeChat) == -1) {
			soundManager.play(idSound);
		}
	},
	
	saveSettings: function() {
		this.observer.fire('sounds.saveSettings', this.settings);
	},
	
	syncSettings: function(data) {
		if (data) {
			this.settings = data;
		}
	},
	
	/**
	 * Включает/выключает звук
	 */
	toggleSound: function(v) {
		if (parseInt(v) == 1) { // включить
			this.settings.silentChats = this.settings.silentChats.without(this.activeChat);
		}
		else { // выключить
			this.settings.silentChats.push(this.activeChat);
		}
		this.saveSettings();
	}
});
