function addCSSRule(sheet, selector, rule)
{
  	if (sheet) {
  		if (sheet.addRule) {
  			sheet.addRule (selector, rule, -1);
  			return true;
  		}
  		else if (sheet.insertRule) {
  			sheet.insertRule(selector + '{' + rule + '}', getCSSRules(sheet).length);
  			return true;
  		}
  	}
  	return false;
}

function getCSSRules(sheet)
{
	return (sheet.cssRules) ? sheet.cssRules : sheet.rules;
}

function getStyleSheet(title)
{
	if ( document.styleSheets ) {
		for(var i = 0; i < document.styleSheets.length; i++) {
			if( document.styleSheets[i].href.indexOf(title) != -1 ) {
				return document.styleSheets[i];
			}
		}
	}
	return null;
}

/* Theme options */

var StyleOptions = {};

function post_background_set (canvas, mode)
{
	if (canvas) {
		var color = mode != "white";
		var c0 = "#fffffe";
		var c1 = color ? "#f7f7f2" : c0;
		var c2 = color ? "#fcfcfb" : c0;

		addCSSRule(canvas, ".section-viewtopic .post",     "background-color: "+c0+";");
		addCSSRule(canvas, ".section-viewtopic .post.bg1", "background-color: "+c1+";");
		addCSSRule(canvas, ".section-viewtopic .post.bg2", "background-color: "+c2+";");
	}
}

function post_background_toggle()
{
	var mode = StyleOptions['post_bg'];
	/* default is color */
	StyleOptions['post_bg'] = (mode == 'white' ? 'color' : 'white');

	theme_script_update_sheet();
}

function userinfo_position_set(canvas, lr)
{
	if (!canvas) return false;

	if (lr == 'left') {
		addCSSRule(canvas, '.postbody', 'float: right');
		addCSSRule(canvas, '.postprofile', 'float: left');
		addCSSRule(canvas, '.postprofile', 'border-right: 1px solid #ada698');
		addCSSRule(canvas, '.postprofile', 'border-left: none');
		addCSSRule(canvas, '.pm .postprofile', 'border-right: 1px solid #ddd');
		addCSSRule(canvas, '.pm .postprofile', 'border-left: none');
		addCSSRule(canvas, '.online', 'margin-top: -5px');
		addCSSRule(canvas, '.online dt', 'margin-top: 5px');
	}
	else {
		addCSSRule(canvas, '.postbody', 'float: left');
		addCSSRule(canvas, '.postprofile', 'float: right');
		addCSSRule(canvas, '.postprofile', 'border-left: 1px solid #ada698');
		addCSSRule(canvas, '.postprofile', 'border-right: none');
		addCSSRule(canvas, '.pm .postprofile', 'border-left: 1px solid #ddd');
		addCSSRule(canvas, '.pm .postprofile', 'border-right: none');
		addCSSRule(canvas, '.online', 'margin-top: auto');
		addCSSRule(canvas, '.online dt', 'margin-top: auto');
	}
}

function userinfo_position_toggle()
{
	var mode = StyleOptions['ui_pos'];
	/* default is right */
	StyleOptions['ui_pos'] = (mode == 'left' ? 'right' : 'left');

	theme_script_update_sheet();
}

function theme_script_update_sheet()
{
	var canvas = getStyleSheet("canvas.css");
	if (canvas)
	{
		canvas.disabled = true;
		/* clear canvas */
		if (canvas.cssText)
			canvas.cssText = '';
		else
			for(var i=0; i < getCSSRules(canvas).length; i++)
				canvas.deleteRule(i);

		post_background_set   (canvas, StyleOptions['post_bg']);
		userinfo_position_set (canvas, StyleOptions['ui_pos']);

		canvas.disabled = false;
	}
	theme_script_onunload();
}

function theme_script_render()
{
	var bar = $('page-bar');
	if (bar) bar.innerHTML = '<ul class="linklist rightside">'
		+ '<li><a href="javascript:" class="theme-option postbg" onclick="post_background_toggle(); return false;"  title="Alternar los colores del fondo de los mensajes">Alternar los colores del fondo de los mensajes</a></li>'
		+ '<li><a href="javascript:" class="theme-option uipos" onclick="userinfo_position_toggle(); return false;" title="Alternar la posición de los avatares">Alternar la posición de los avatares</a></li>'
		+ '</ul>'
		;
}

function theme_script_onload()
{
	if (!topic_id) return;

	var options = readCookie('theme_options_cookie');
	if ( options ) {
		options = options.split('-');
		if (options[0])	StyleOptions['post_bg'] = options[0];
		if (options[1]) StyleOptions['ui_pos' ] = options[1];

		theme_script_update_sheet();
	}

	var bar = $('page-bar');
	if (bar) theme_script_reander();
	else     onload_functions.push('theme_script_render()');
}

function theme_script_onunload()
{
	var options = [
		StyleOptions['post_bg'],
		StyleOptions['ui_pos'],
	].join('-');

	createCookie('theme_options_cookie', options, 365);
}

//onunload_functions.push('theme_script_onunload()');

/* we can execute this now */
theme_script_onload();


// ads
function theme_ad_slot() {
	return "9216348986";
}
