var sss_that_center_body = {'3.51': true};
var sss_that_float_menu = {'3.51': true};

function css_get_rule_by_selector(css_rule_list, selector_text)
{
	var i_rule;

	for(var i = 0; i < css_rule_list.length; i++)
	{
		i_rule = css_rule_list.item(i);

		if(i_rule.type == 3)
		{
			var rule = css_get_rule_by_selector(i_rule.styleSheet.cssRules, selector_text);

			if(rule)
			{
				return rule;
			}
		}
		else
		if(i_rule.type == 1 && i_rule.selectorText == selector_text)
		{
			return i_rule;
		}
	}
}

function css_get_selected_stylesheet()
{
	for(var i = 0; i < document.styleSheets.length; i++)
	{
		if(!document.styleSheets.item(i).disabled)
		{
			return document.styleSheets.item(i);
		}
	}
}

var viewport = document.documentElement;

function reposition_follower(fwr)
{
	if(viewport.scrollTop > fwr.origin_top_px)
	{
		fwr.ref.style.top = (viewport.scrollTop + fwr.offset_fn()) + "px";
	}
	else
	{
		fwr.ref.style.top = fwr.origin_top_px + "px";
	}
}

function setup_follower_behaviour()
{
	window.followers = {};

	var menu_fwr = setup_follower("menu_block", null, 0);
	/*var login_fwr = setup_follower("login_block", "#login_block, #login_feedback_block",
		function()
		{
			return parseInt(window.getComputedStyle(window.followers["menu_block"].ref, null).height);
		});*/

	//var usercp_fwr = setup_follower("login_feedback_block");

	window.onscroll = function()
	{
		clearTimeout(window["reposition_followers_ivl"]);
		window["reposition_followers_ivl"] = setTimeout(reposition_followers, 500);

		//reposition_followers();
	};
}

function setup_follower(id, selector_text, offset_fn)
{
	if(!selector_text) selector_text = "#" + id;

	var ref = document.getElementById(id);

	if(!ref) return;

	var css_selector = css_get_rule_by_selector(window.css_rule_list, selector_text);

	if(!css_selector)
	{
		//opera.postError("no css selector found for a follower with id '" + id + "'\n\tselector_text:" + selector_text);
		return;
	}

	var fwrs = window.followers;

	var fwr = fwrs[id] = {};

	if(!offset_fn) offset_fn = zero_offset_fn;

	fwr.offset_fn = offset_fn;
	fwr.ref = ref;

	ref.style.top = css_selector.style.top;

	fwr.origin_top_px = parseInt(window.getComputedStyle(ref, null).top);

	reposition_follower(fwr);

	return fwr;
}

function reposition_followers()
{
	for(var i in window.followers)
	{
		reposition_follower(window.followers[i]);
	}
}

function zero_offset_fn()
{
	return 0;
}

function center_body()
{
	var ss = css_get_selected_stylesheet();

	center_body[ss.title]();
}

center_body['3.51'] = function()
{
	var html = document.documentElement;
	var header = document.getElementById("header");
	var page = document.getElementById("page");

	var content_height = header.offsetHeight + page.offsetHeight + window.page_margin_top;

	//opera.postError(html.scrollHeight + "/" + html.clientHeight);
	//opera.postError(content_height + "/" + html.clientHeight);

	var body_top;

	if(content_height < html.clientHeight)
	{
		body_top = (html.offsetHeight  - window.credits_height - (content_height  - window.page_border_bottom)) / 2 - window.body_margin_top;

		if(body_top < 0) body_top = 0;
	}
	else
	{
		body_top = 0;
	}

	document.body.style.top = body_top + "px";

	var body_left = ((html.clientWidth - document.body.offsetWidth) / 2);

	if(body_left < body_left_origin) body_left = body_left_origin;

	document.body.style.left = body_left + "px";
}

window.onload = function()
{
	var current_style_sheet = document.styleSheets.item(0);

	if(!current_style_sheet) return;

	var css_rule_list = current_style_sheet.cssRules;

	if(!css_rule_list) return;

	window.css_rule_list = css_rule_list;

	var html = document.documentElement;

	if(menu_float_enabled())
	{
		setup_follower_behaviour();
	}

	if(body_center_enabled())
	{
		window.body_left_origin = parseInt(window.getComputedStyle(document.body, null).left);
		window.body_margin_top = parseInt(window.getComputedStyle(document.body, null).marginTop);
		window.page_margin_top = parseInt(window.getComputedStyle(document.getElementById('page'), null).marginTop);
		window.page_border_bottom = parseInt(window.getComputedStyle(document.getElementById('page'), null).borderBottomWidth);
		window.credits_height = parseInt(window.getComputedStyle(document.getElementById('credits_block'), null).height);

		center_body();

		window.onresize = function()
		{
			center_body();
		};
	}
};

function body_center_enabled()
{
	var ss = css_get_selected_stylesheet();

	return sss_that_center_body[ss.title];
}

function menu_float_enabled()
{
	var ss = css_get_selected_stylesheet();

	return sss_that_float_menu[ss.title];
}
