// JS utilities for www.fredmiranda.com
// 9/2008 2/2009 Written by Etadam (fredmiranda.com member)
// Free usage - keep above comments

String.prototype.trim = function() 
{
	 return this.replace(/^\s+/, '').replace(/\s+$/, '');
}

function get_object (nid)
{
	if (document.all)
		return document.all[nid];
	else if (document.getElementById)
		return document.getElementById(nid);
	return null;
}

var q_message = null;
var q_start = '[quote]';
var q_end   = '[/quote]';
var q_flash = null;
var q_flash_n,q_flash_cols,q_flash_col,q_flash_tint;

function t_flash()
{
	if (q_flash)
	{
		q_flash.style.color = (q_flash_n & 1 ? q_flash_cols:q_flash_col);
	
		if (--q_flash_n > 0) setTimeout ('t_flash();', q_flash_tint);
	}
}

function re_flash(tinit, o, col, n, tint)
{
	if (!o || n<=0) return;

	q_flash_cols = o.style.color;
	q_flash_col  = col;
	q_flash_tint = tint;
	q_flash   = o;
	q_flash_n = n;

	setTimeout ('t_flash();', tinit);
}

function set_quoting(btn,txt)
{
	var n,b,need=false;
	q_message = get_object(txt);
	if (q_message && (n = q_message.value.indexOf(q_start)) >= 0)
		if (q_message.value.indexOf(q_start,n+1) >= 0)
			need = true;
		
	if ( ! need) return;
	
	if (b = get_object(btn))
	{
		b.style.visibility = 'visible';
		re_flash(3000, b, '#ffdd55', 4, 150);
	}
}


function re_quoting()
{
	if (q_message)
	{
		var s=q_message.value.trim();
		var p,q,t,gen = '';
		while ((q = s.indexOf(q_end)) >= 0) 
			if ((p = s.lastIndexOf(q_start, q)) >= 0)
		{
			t = (p+q_start.length < q ? s.substring(p+q_start.length, q).trim():'');
			if (t) gen += (gen ? "\n":'') + q_start + t + "\n" + q_end;
			q += q_end.length;
			s = (p ? s.substring(0, p).trim() + "\n":'') +
				 (q < s.length ? s.substring(q).trim() + "\n":'');
		}
		else
		{
			alert("Missing '[quote]', or there is an extra '[/quote]'.\nPlease check your text");
			return;
			// or, 2nd version (no error alert)
			// s = q_start + "[b]???[/b]\n" + s;
		}
		
		q_message.value = gen + (gen && s ? "\n":'') + s;
	}
}


var h_list = null;
var h_hidden = null;
var h_timer = null;

function show_time(o, divid, tds)
{
	var od = h_hidden = get_object(divid);
	
	if (od)
	{
		var i,x=0,y=o.offsetHeight-12;

		while (o)
		{
			x += o.offsetLeft;
			y += o.offsetTop;
			o = o.offsetParent;
		}

		od.style.left  = String(x) + "px";
		od.style.top   = String(y) + "px";
		od.onmouseout = function()
			 {
				  if (h_timer) clearTimeout(h_timer);
				  h_timer = setTimeout("close_time();", 1000);
			 }
		od.onmouseover = function()
			 {
				  if (h_timer) clearTimeout(h_timer);
				  h_timer = null;
			 }
		od.onmouseover();


		var h='<table style="padding:3px 6px;">';
		for (i=0 ; i<tds.length ; i++) h += '<tr>'+tds[i]+'</tr>';

		h+='<tr><td colspan="2" style="background-color:#4c4c4c;text-align:center;cursor:pointer"'+
			 ' onclick="close_time();">'+
			 'Close</a></td></tr>';
		h+='</table>';

		od.innerHTML = h;
		od.style.visibility = 'visible';
	}
}

function close_time()
{
	if (h_hidden)
	{
		if (h_timer) clearTimeout(h_timer);
		h_timer = null;
		h_hidden.style.visibility='hidden';
	}
}

function post_hidden(divid, hidden, shown)
{
	if ( ! h_list) h_list = new Object();
	h_list[divid] = new Array(hidden, unescape(shown));
}

function post_hide (divid, hs)
{
	var o = get_object(divid);
	var h = h_list[divid];
	if (o && h) o.innerHTML = h[hs ? 0:1];
}

function post_hiddenlist(o, divid, maxh, url, backurl)
{	
	if (hid_list)
	{
		var a=new Array();

		a.push('<td colspan="2" align="center"><b>Hidden posts members</b></td>');

		if (maxh) a.push('<td colspan="2" align="center">(List full)</td>');

		var u='';
		for (i in hid_list)
		{
			a.push('<td>'+hid_list[i]+'</td><td>'+
					 '<a class="linkgray" href="'+url+i+'&Action=unhideuserposts'+'">Unhide</a>'+
					 '</td>');
		}
		a.push('<td colspan="2" align="center">'+
				 '<a class="linkgray" href="'+url+'-1&Action=unhideallusers'+'">'+
				 'Unhide all members</a></td>');
		a.push('<td colspan="2"> </td>');

		show_time(o,divid,a);
	}
}

