var sqNotices = {
	init: function () {
		this.sounds = [];

		this.sounds['error'] = new Howl({
			src: ['../sounds/error.ogg', '../sounds/error.mp3'],
			autoplay: false,
			loop: false,
		});
		this.sounds['success'] = new Howl({
			src: ['../sounds/success.ogg', '../sounds/success.mp3'],
			autoplay: false,
			loop: false,
		});
		this.sounds['notice'] = new Howl({
			src: ['../sounds/notice.ogg', '../sounds/notice.mp3'],
			autoplay: false,
			loop: false,
		});

		window.sqNoticesA = [];
		window.sqNoticesLastId = 1;

		$(function () {
			sqNotices.showNotices();
		});

		$(document).on('click', '#sq-notices .sq-notices-head > div', function () {
			var noticeId = $(this).data('id');
			var noticesContainer = sqNotices.getContainer();
			var noticesContainerHead = noticesContainer.find('.sq-notices-head');
			var noticesContainerBody = noticesContainer.find('.sq-notices-body');
			noticesContainerHead.children().removeClass('active');
			noticesContainerBody.children().removeClass('active');
			$(this).addClass('active');
			noticesContainerBody.children('div[data-id=' + noticeId + ']').addClass('active');
		});

		$(document).on('click', '#sq-notices .sq-notices-body .sq-notices-close', function () {
			var noticeId = $(this).parent().parent().data('id');
			for (noticeSingleKey in window.sqNoticesA) {
				var noticeSingle = window.sqNoticesA[noticeSingleKey];
				if (noticeSingle.id == noticeId) {
					noticeSingle.closed = true;
					sqNotices.showNotices();
					break;
				}
			}
		});

		$(document).on('mouseover', '#sq-notices', function () {
			var noticeId = $(this).children('div.active').data('id');
			$(this).children('div.active').addClass('activeover');
			for (noticeSingleKey in window.sqNoticesA) {
				var noticeSingle = window.sqNoticesA[noticeSingleKey];
				clearTimeout(noticeSingle.timer); continue;
				if (noticeSingle.id == noticeId) {
					clearTimeout(noticeSingle.timer);
					break;
				}
			}
		});

		$(document).on('mouseout', '#sq-notices', function () {
			var noticeId = $(this).children('div.activeover').data('id');
			$(this).children('div.activeover').removeClass('activeover');
			for (noticeSingleKey in window.sqNoticesA) {
				var noticeSingle = window.sqNoticesA[noticeSingleKey];
				noticeSingle.timer = setTimeout(sqNotices.closeNotice, 3000, noticeSingle.id); continue;
				if (noticeSingle.id == noticeId) {
					noticeSingle.timer = setTimeout(sqNotices.closeNotice, 3000, noticeSingle.id);
					break;
				}
			}
		});
	},
	shouldNoticesMakeSound: function () {
		if (window.isSoundAllowed === false || window.parent.isSoundAllowed === false) {
			return false;
		}
		return true;
	},
	closeNotice: function (noticeId) {
		/*
				// REMOVE DEV!!!
				return;
		*/
		var noticesContainer = sqNotices.getContainer();
		var noticesContainerHead = noticesContainer.find('.sq-notices-head');
		var noticesContainerBody = noticesContainer.find('.sq-notices-body');

		for (noticeSingleKey in window.sqNoticesA) {
			var noticeSingle = window.sqNoticesA[noticeSingleKey];
			if (noticeSingle.id == noticeId) {
				noticeSingle.closed = true;
				break;
			}
		}

		noticesContainerHead.find('div[data-id=' + noticeId + ']').fadeOut(300, function () {
			$(this).remove();
			sqNotices.autoHideContainer();
		});
		noticesContainerBody.find('div[data-id=' + noticeId + ']').fadeOut(300, function () {
			$(this).remove();
			sqNotices.autoHideContainer();
		});

	},
	addNotice: function (nType, nText) {
		// nType: 'error', 'success', 'notice'
		// nText: text of the notice message

		if (nType != 'error' && nType != 'success' && nType != 'notice') {
			console.log('SQ notices ERROR: undefined type ' + nType);
			return;
		}

		var newNotice = {
			'type': nType,
			'details': nText,
			'id': window.sqNoticesLastId,
			'closed': false,
			'timer': setTimeout(sqNotices.closeNotice, 5000, window.sqNoticesLastId)
		};
		window.sqNoticesLastId++;
		window.sqNoticesA.push(newNotice);
		if (this.shouldNoticesMakeSound()) {
			this.sounds[nType].play();
		}
		if (document.readyState === 'complete') {
			this.showNotices();
		}
	},
	getContainer: function () {
		var noticesContainer = $('#sq-notices');
		if (noticesContainer.length < 1) {
			noticesContainer = $('<div id="sq-notices" style="display:none;"><div class="sq-notices-head"></div><div class="sq-notices-body"></div></div>');
			$('body').append(noticesContainer);
		}
		return noticesContainer;
	},
	autoHideContainer: function () {
		var noticesContainer = this.getContainer();
		var isAllClosed = true;
		for (noticeSingleKey in window.sqNoticesA) {
			var noticeSingle = window.sqNoticesA[noticeSingleKey];
			if (noticeSingle.closed == false) {
				isAllClosed = false;
				break;
			}
		}

		if (isAllClosed) {
			noticesContainer.hide();
		}
	},
	showNotices: function () {
		var noticesContainer = this.getContainer();
		var noticesContainerHead = noticesContainer.find('.sq-notices-head');
		var noticesContainerBody = noticesContainer.find('.sq-notices-body');

		for (noticeSingleKey in window.sqNoticesA) {
			var noticeSingle = window.sqNoticesA[noticeSingleKey];
			if (noticesContainerBody.find('div[data-id=' + noticeSingle.id + ']').length < 1) {
				if (noticeSingle.closed) continue;
				var noticeHeadNew = $('<div data-id="' + noticeSingle.id + '" class="sq-notices-' + noticeSingle.type + '" style="display:none;"><i class="sqnico-' + noticeSingle.type + '"></i></div>');
				var noticeBodyNew = $('<div data-id="' + noticeSingle.id + '" class="sq-notices-' + noticeSingle.type + '" style="z-index:' + noticeSingle.id + ';display:none;"><div class="sq-notices-title">' + noticeSingle.type + '<i class="sq-notices-close sqnico-close"></i></div><div class="sq-notice-detail"><i class="sq-notices-type-ico sqnico-' + noticeSingle.type + '"></i>Details:<br/>' + noticeSingle.details + '</div></div>');
				noticesContainerHead.prepend(noticeHeadNew);
				noticesContainerBody.prepend(noticeBodyNew);
				noticeHeadNew.fadeIn(300, function () {
				});
				noticeBodyNew.fadeIn(300, function () {
				});
			}
			else {
				if (noticeSingle.closed) {
					noticesContainerHead.find('div[data-id=' + noticeSingle.id + ']').fadeOut(300, function () {
						$(this).remove();
					});
					noticesContainerBody.find('div[data-id=' + noticeSingle.id + ']').fadeOut(300, function () {
						$(this).remove();
					});
				}
			}

		}

		if (noticesContainerHead.children('div').length > 0) {
			noticesContainerHead.children('div').removeClass('active');
			noticesContainerBody.children('div').removeClass('active');
			noticesContainerHead.children('div').eq(0).addClass('active');
			noticesContainerBody.children('div').eq(0).addClass('active');
			noticesContainer.fadeIn(300);
		}

		sqNotices.autoHideContainer();

	}
};

$(function () {
	sqNotices.init();
});