var _processing = false;

/* we have some special urls. we need to fetch and redirect */
function update ()
{
	var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('-');
	if (saved_href == null || saved_href[0] != hashes[0] || saved_href[1] != hashes[1] || saved_href[2] != hashes[2])
		prep_rocket();
}

var saved_href = null; 
function rocket (hashes) 
{ 
   var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('-');
   var _SEC = hashes[0]; var _SUB = hashes[1]; var _SID = hashes[2]; var _MSG = hashes[3]; 
   if (saved_href != null && saved_href[0] == hashes[0] && saved_href[1] == hashes[1])
   {
     if (saved_href[2] == hashes[2]) { return; }
     if (_SEC == 'bull') { bbpost(_SID); saved_href = hashes; }
     return;
   }
   // reset the bb
   if (saved_href != null && saved_href[0] == 'bull' && _SEC != 'bull') endbb();
   saved_href = hashes; 

   _SUB = _SUB != undefined ? _SUB : '0';

   switch(_SEC)
   {
      case 'about': con('about'); box(_SEC + (_SUB != undefined ? _SUB : '0')); break;
      case 'bull': 
	con('bull');
	cur_thread = null;
	if (_SUB != undefined) { bb(_SUB); }
	if (_SID != undefined) { saved_href[2] = null; prep_rocket(); }
	break;
      case 'eboard': con('eboard'); box(_SEC + (_SUB != undefined ? _SUB : '0')); break;
      case 'involve': con('involve'); box(_SEC + (_SUB != undefined ? _SUB : '0')); break;
      default: con('front'); break;
   }

	// display any server messages
	if (_MSG != null) message(_MSG);

}

function message (msg)
{
	switch (msg)
	{
		case 'posted':
			mess = 'Your message was posted successfully.';
			break;
		case 'posterr':
			mess = 'There was an error while submitting your post. Please try again.';
			break;
		case 'regexist':
			mess = 'The email you gave is already in use. Please try another one.';
			break;
		case 'regempty':
			mess = 'You left a field empty when registering. Please try again.';
			break;
		case 'logemail':
			mess = "An account doesn't exist for that email. Please try again.";
			break;
		case 'loggood':
			mess = "Logged in successfully!";
			break;
		case 'logpass':
			mess = "Your password did not match. Please try again.";
			break; 
	}
	alert(mess);
}

function debug (x)
{ document.getElementById('debug').innerHTML = x; }

function prep_rocket ()
{
   setTimeout('rocket()', 100);
}


var con_ptr = 'front';
function con (ptr)
{
     document.getElementById(con_ptr).style.display = 'none';
     document.getElementById(ptr).style.display = 'block';
     con_ptr = ptr;
}
   
var box_ptr = null;
function box (ptr)
{
     if (box_ptr != null) document.getElementById(box_ptr).style.display = 'none';
     document.getElementById(ptr).style.display = 'block';
     box_ptr = ptr;
}

var last_imp = null;
function bb (i)
{
	_processing = true;
	var imp = document.createElement('script');
	imp.setAttribute('src', 'boards/view.php?bid='+i);
	document.body.appendChild(imp);	
	last_imp = imp;
}

function endbb ()
{
	document.body.removeChild(last_imp);
}

function bbtoggle(i)
{
	if (document.getElementById('bbthread'+i).style.display != 'none')
		document.getElementById('bbthread'+i).style.display = 'none';
	else
		document.getElementById('bbthread'+i).style.display = 'block'; 
}

function bbreply (i)
{
	document.getElementById('bbnewthreadform').style.display = 'none';
	document.getElementById('bbreply').style.display = 'block';
	document.getElementById('threadid').value = i;
	document.getElementById('bbreplyform').value = '';
	document.getElementById('bbnewthread').focus();
	document.getElementById('bbreplyform').focus();
}

function bbreplycancel ()
{
	document.getElementById('bbreply').style.display = 'none';
	document.getElementById('bbnewthreadform').style.display = 'block';
}

var cur_thread = null;
function bbpost(i)
{
	document.getElementById('bbpost').innerHTML = document.getElementById('bbthread'+i).innerHTML;
	document.getElementById('bbhead'+i).setAttribute("class", "selected_post");
	if (cur_thread != null) document.getElementById('bbhead'+cur_thread).setAttribute("class", "unselected_post");
	cur_thread = i;
}

function rclean (t)
{
	t.value = '';
}

function rreset (t)
{
	if (t.value != "") return; 
	else switch (t.name)
		{
			case 'title': 	t.value = 'Thread title'; break;
			case 'message': t.value = 'Your post here...'; break;
			default:	t.value = ''; break;
		}
}

function transmission_complete() 
{
	_processing = false;
}

var queries = 0;
var MAX_QUERIES = 100;
function query_wait () 
{
	if (_processing) { queries = 0; return; } 
	if (queries >= MAX_QUERIES) { alert('Connection to server lost. Please try again.'); return; }
	setTimeout('query_wait()', 100);
}
