
// ----- WARNING ----- \\
// COPYRIGHTED CONTENT \\
//  COPYRIGHT © NERVA  \\
//   nerva@nerva.ee    \\
// All rights reserved \\
// ------------------- \\

var mbi = null;
var mbo = null;
var mbx = 0;
var mby = 0;
var mb_space_y = 30;
var mb_creatures_moved = false;

function mb_findByContentid(contentid) {
	var i, cid;
	var mbs = document.getElementsByName("mb");
	if ((mbs != null) && (mbs.length < 1)) mbs = getElementsByName_IE("table", "mb");
	if (mbs != null) {
		for (i = 0; i < mbs.length; i++) {
			cid = getclassparam(mbs[i], "mbcontentid");
      if ((cid !== false) && (cid == contentid)) {
				return mbs[i];
			  }
			}
	  }
	return false;
  }

function mb_saveposition(mb) {
	var mbsavename = getclassparam(mb, "mbsavename");
	if (mbsavename === false) return false;
	var x = 0;
	var y = 0;
	var obj = mb;
	while (obj != null) {
		x += obj.offsetLeft;
		y += obj.offsetTop;
		obj = obj.offsetParent;
		}
	setCookie("mbposition_" + mbsavename, "" + x + "," + y, 365);
  }

/*
	var cookie = getCookie(name);
	if (cookie == null) cookie = def;
	return cookie;
*/

function mbc_click(mbindex) {
	var mb = document.getElementById("mb_" + mbindex);
	if (mb == null) return false;
  if (mb.className.indexOf("mbarticle") >= 0) {
		var o = document.getElementById("mbc_" + mbindex);
		if (o == null) return false;
		var mbcontentid = getclassparam(mb, "mbcontentid");
		if (getCookieDef("mbkeep_" + mbcontentid, 0) > 0) {
			setCookie("mbkeep_" + mbcontentid, "0", 365);
			hide(mb);
			}
		else {
			o.src = root + "content/mb/images/button/close.png";
			o.title = getString("hide");
			setCookie("mbkeep_" + mbcontentid, "1", 365);
			}
	  }
	else {
		hide(mb);
	  }
  }

function mbmenu_mouse(mbmenuindex, newindex) { // OLD
	var o = null;
	for (i = 0; i <= last_mbindex; i++) {
	  o = document.getElementById("mbmenu_" + mbmenuindex + "_" + i);
		if (o == null) continue;
		o.src = ((i > newindex) ? "/pixel.gif" : root + "content/mb/images/mainmenu/" + ((i < newindex) ? "middle" : "top") + ".png");
	  }
	o = document.getElementById("mbmenu_" + mbmenuindex + "_belly");
	if (o != null) o.src = root + "content/mb/images/mainmenu/" + ((newindex >= 0) ? "middle" : "top") + ".png";
  }

function mbmenu_mouseover(object, mbmenuindex) {
	var table = document.getElementById("mbmenu_" + mbmenuindex + "_table");
	var belly = document.getElementById("mbmenu_" + mbmenuindex + "_belly");
	var head = document.getElementById("mbmenu_" + mbmenuindex + "_head");
	if ((table != null) && (belly != null) && (head != null)) {
		if (object != null) {
			var y = table.offsetTop - object.offsetTop + 6;
			head.style.top = "-" + y + "px";
			y -= 24;
			belly.height = "" + y;
		  }
		else { // reset
		  head.style.top = "-25px";
			belly.height = "1";
		  }
	  }
  }

function mb_bringtofront(o) {
	if ((o != null) && (o.style.zIndex < last_zindex)) {
		last_zindex++;
		o.style.zIndex = last_zindex;
	  }
  }

function mb_mousedown(e, index, allowmove) {
  e = e || window.event;
	var o = document.getElementById("mb_" + index);
	if (o != null) {
		mb_bringtofront(o);
		if (allowmove == 1) {
			mbo = o;
			mbi = index;
			mbx = mby = 0;
			mb_creatures_moved = false;
			var pos = get_object_pos(mbo);
			mbx = e.clientX - pos[0];
			mby = e.clientY - pos[1];
			}
	  }
  }

function mb_content_mousedown(index) {
	var o = document.getElementById("mb_" + index);
	if (o != null) mb_bringtofront(o);
  }

function _set_overlay_position(o, sw, extray, ch, immediately) {
	var x = Math.max(10, Math.round((sw - o.offsetWidth) / 2));
	var y = extray + Math.max(10, Math.round((ch - o.offsetHeight) / 2));
	mov_move(o, x, y, immediately);
  }

function mb_move(o, suggx, suggy, immediately, resetpositions) {
	var mbsavename = getclassparam(o, "mbsavename");
	var x = suggx;
	var y = suggy;
	if (mbsavename !== false) {
		var saved = getCookie("mbposition_" + mbsavename);
		if (resetpositions) {
		  if ((saved != null) && (saved != "")) setCookie("mbposition_" + mbsavename, "", 365);
		  }
		else if ((saved != null) && (saved != "")) {
			var i = saved.indexOf(",");
			if (i > 0) {
				x = 0 + saved.substr(0, i);
				y = 0 + saved.substr(i + 1);
			  }
		  }
	  }
	mov_move(o, x, y, immediately);
  }

function refresh_mb_positions(immediately, resetpositions) {

  // Measuring:
	var left_w = 140; // 20px for margins will be added later
	var j = 0;
	for (i = 1; i <= last_mbindex; i++) {
		o = document.getElementById("mb_" + i);
		if (o == null) continue;
		if (o.className.indexOf("mbarticle") >= 0) {
      o.style.width = "1px";
      continue;
		  }
		if (o.className.indexOf("hidden") >= 0) continue;
		if (o.className.indexOf("mbtop") >= 0) continue;
		if (o.className.indexOf("mboverlay") >= 0) continue;
		// Left objects:
		j = o.offsetWidth;
		if (left_w < j) left_w = j;
	  }
	left_w += 20; // left and right margins

	// Positioning:
	var left_y = 160;
	var top_x = 160 + 260;
	var article_y = 160;
	var sw = document.body.scrollWidth;
	var ch = document.body.clientHeight;
	for (i = 1; i <= last_mbindex; i++) {
		o = document.getElementById("mb_" + i);
		if (o == null) continue;

		if (o.className.indexOf("mbarticle") >= 0) {
		  if (o.className.indexOf("hidden") >= 0) continue;
			mb_move(o, left_w, article_y, true, resetpositions);
			j = sw - o.offsetLeft - 20;
			o.style.width = "" + j + "px";
			article_y += o.offsetHeight + mb_space_y;
	    hide_creatures(o);
			continue;
		  }

		if (o.className.indexOf("mbleft") >= 0) {
		  if (o.className.indexOf("hidden") >= 0) continue;
			j = left_w - o.offsetWidth - 10;
			mb_move(o, j, left_y, immediately, resetpositions);
			left_y += o.offsetHeight + mb_space_y;
	    hide_creatures(o);
			continue;
		  }

		if (o.className.indexOf("mbtop") >= 0) {
		  if (o.className.indexOf("hidden") >= 0) continue;
			j = Math.max(10, 160 - mb_space_y - o.offsetHeight);
			mb_move(o, top_x, j, immediately, resetpositions);
			top_x += o.offsetWidth + 10;
	    hide_creatures(o);
			continue;
		  }

    // Overlay items:
		_set_overlay_position(o, sw, 0, ch, immediately);
	  }
  }

function update_articles_width() {
	var o = null;
	for (i = 1; i <= last_mbindex; i++) {
		o = document.getElementById("mb_" + i);
		if (o == null) continue;
		if (o.className.indexOf("hidden") >= 0) continue;
		if (o.className.indexOf("mbarticle") >= 0) o.style.width = "1px";
	  }
	var j = 0;
	var sw = document.body.scrollWidth;
	for (i = 1; i <= last_mbindex; i++) {
		o = document.getElementById("mb_" + i);
		if (o == null) continue;
		if (o.className.indexOf("hidden") >= 0) continue;
		if (o.className.indexOf("mbarticle") >= 0) {
			j = sw - o.offsetLeft - 20;
			o.style.width = "" + j + "px";
 	    hide_creatures(o);
		  }
	  }
  }

function update_overlay_position(o) {
	if ((o != null) && (o.className.indexOf("mboverlay") >= 0)) {
		//Following 3 lines are for browser compatibility.
		var extray = document.documentElement.scrollTop;
		if (extray < 1) extray = window.pageYOffset;
		if ((extray == null) || (extray < 1)) extray = 0;
		_set_overlay_position(o, document.body.scrollWidth, extray, document.body.clientHeight, true);
		}
  }

function overlay_callback(content) {
	if (content.substr(0, 6) == 'ERROR:') {
    alert(getString("error") + ": " + content.substr(6));
	  }
	else {
		addhtml(content);
		show_new_overlays();
		}
  }

function show_new_overlays() {
	var o = null;
	for (i = 1; i <= last_mbindex; i++) {
		o = document.getElementById("mb_" + i);
		if (o == null) continue;
	  if (o.className.indexOf("mboverlay") < 0) continue;
		if (o.style.zIndex == 0) {
			mb_bringtofront(o);
		  update_overlay_position(o);
			if (i == mbindex_contents) setFocusSoon("contentsearch");
		  }
	  }
  }

function show_globals() {
	unhide(document.getElementById("mb_" + mbindex_globals), true);
  }

/* moving creatures */

function move_creature_object(o, visibleonly, friendsof, hide) {
	if ((o == null) || (visibleonly && (o.offsetTop < 0))) return false;
	if (hide) {
		var mov = mov_find(o);
		if (mov == null) return false;
		if ((friendsof == null) || (mov.friend == friendsof)) {
			mov.movenow(Math.round(Math.random() * (document.body.clientWidth - mov.w)), -100, -100, null);
		  }
		return true;
	  }
	if (mov_anyactive()) return false;
	var i;
	var r = null;
	var mb = null;
	while (true) {
		i = Math.round(Math.random() * last_mbindex);
		mb = document.getElementById("mb_" + i);
		if ((mb == null) || (mb == mbo)) continue;
		if (mb.className.indexOf("hidden") >= 0) continue;
		r = get_object_pos(mb);
		if (r[1] < mb_space_y) continue;
		if (mb.className.indexOf("mbtransparent") >= 0) {
			r[0] += 24;
			r[1] += 24;
			r[2] -= 48;
		  }
		break;
		}
	if (r == null) return false; // could not find any mb
	var mov = mov_find(o);
	if ((mov != null) && (friendsof != null) && (mov.friend != friendsof)) return false;
	if (mov == null) {
		mov = mov_add(o);
		mov.speed = 1;
	  }
	mov.move(mov.emptyx(r[0], r[0] + r[2], r[1]), r[1] - mov.h + 1, r[1], mb);
  }

function move_creature(index, visibleonly, friendsof, hide) {
	move_creature_object(document.getElementById("article_creature_" + index), visibleonly, friendsof, hide);
  }

function move_creaturesoon(index) {
	move_creature(index, true);
  }

function move_random_creature_loop() {
	if (mbo == null) move_creature(Math.round(Math.random() * creature_max), false);
  move_creatures_soon();
  }

function move_creatures_soon() {
	if (galleryshow_running()) {
		setTimeout(move_creatures_soon, 10000);
	  }
	else {
		setTimeout(move_random_creature_loop, Math.random() * 12000);
	  }
  }

function move_creatures(visibleonly, friendsof) {
	var i;
	for (i = 0; i <= creature_max; i++) {
		move_creature(i, visibleonly, friendsof);
	  }
  }

function hide_creatures(friendsof) {
	var i;
	for (i = 0; i <= creature_max; i++) {
		move_creature(i, false, friendsof, true);
	  }
  hide_comments_candies();
	show_comments_candies_soon();
  }

