(function () {

var $banner;

function queue_roll(bit) {
	var n = this.allRolls.sent++;
	var info = this.allRolls[n];
	if (!info)
		info = this.allRolls[n] = {};
	info.bit = bit;
	info.$tag = $(this.callback(safe('<strong>')));
	this.strong = true;
	var audio = undefined;
	if(bit =='#loli' && info.dice) {
		this.callback(safe("<a href='"+info.dice[0]+"' target='_blank' rel='noopener noreferrer'><img src='"+info.dice[1]+"'></img></a>"));
	}
	else if (info.dice && (audio = readable_audio(bit, info.dice))) {
		this.callback(safe("<a style='text-decoration:none;' href='javascript:void(0);' onclick='this.nextSibling.play();'>"+bit+"</a>"));
		this.callback(safe("<audio src='"+audio_root+"/"+audio+"'></audio>"));
	}
	else this.callback(info.dice ? readable_dice(bit, info.dice) : bit);
	this.strong = false;
	this.callback(safe('</strong>'));
}

oneeSama.hook('imouto', function (imouto) {
	imouto.dice = GAME_BOARDS.indexOf(BOARD) >= 0;
	imouto.queueRoll = queue_roll;
	imouto.allRolls = {sent: 0, seen: 0};
});

oneeSama.hook('insertOwnPost', function (extra) {
	if (!postForm || !postForm.imouto || !extra || !extra.dice)
		return;
	var rolls = postForm.imouto.allRolls;
	for (var i = 0; i < extra.dice.length; i++) {
		var n = rolls.seen++;
		var info = rolls[n];
		if (!info)
			info = rolls[n] = {};
		info.dice = extra.dice[i];
		if (info.$tag) {
			var audio = readable_audio(info.bit, info.dice);
			if (audio)
			{
				var $audio = $("<a style='text-decoration:none;' href='javascript:void(0);' onclick='this.nextSibling.play();'>"+info.bit+"</a>");
				var $aaudio = $("<audio src='"+audio_root+"/"+audio+"'></audio>");
				info.$tag.text('');
				info.$tag.append($audio);
				info.$tag.append($aaudio);
				if(info.$tag.children && info.$tag.children[0]) //wha? works for chakai
					info.$tag.children[0].text(info.bit);

			}
			else if(info.bit == "#loli")
				info.$tag.html("<a href='"+info.dice[0]+"' target='_blank' ref='noopener noreferrer'><img src='"+info.dice[1]+"'></img></a>");
			else info.$tag.text(readable_dice(info.bit, info.dice));
		}
	}
});

var bannerExtra = null; //$.parseHTML('<b>Other stream info</b>');

dispatcher[UPDATE_BANNER] = function (msg, op) {
	msg = msg[0];
	if (!$banner) {
		var dest;
		if (THREAD == op)
			dest = '#lock';
		else {
			var $s = $('#' + op);
			if ($s.is('section'))
				dest = $s.children('header');
		}
		if (dest)
			$banner = $('<span id="banner"/>').insertAfter(dest);
	}
	if ($banner) {
		if (Array.isArray(msg)) {
			construct_banner(msg);
			if (bannerExtra)
				$banner.append(' / ', bannerExtra);
		}
		else if (msg) {
			$banner.text(msg);
			if (bannerExtra)
				$banner.append(' / ', bannerExtra);
		}
		else if (bannerExtra) {
			$banner.empty().append(bannerExtra);
		}
		else {
			$banner.remove();
			$banner = null;
		}
	}
};

function construct_banner(parts) {
	$banner.empty();
	parts.forEach(function (part) {
		if (part.href)
			$('<a></a>', _.extend({target: '_blank', rel: 'noopener noreferrer'}, part)
					).appendTo($banner);
		else
			$banner.append(document.createTextNode(part));
	});
}

dispatcher[EXECUTE_JS] = function (msg, op) {
	if (THREAD != op)
		return;
	try {
		eval(msg[0]);
	}
	catch (e) {
		/* fgsfds */
	}
};

window.flash_bg = function (color) {
	var $b = $('body');
	var back = $b.css('background');
	$b.css('background', color);
	setTimeout(function () { $b.css('background', back); }, 200);
};

})();
