var httpreqs = new Array();
var httpcounts = 0;
var do_shout = 1;
var refresh_time = 20000;
var ajax_status = '';
var ajax_working = 1;
var post_unparsed = '';
var mapnight_timer = null;

var browser = null;
var newbrowser = true;
var check = false;

var current_scroll_height = 0;
var current_scroll_length = 0;
var current_view_height = 0;
var current_view_width = 0;
function menuOut() { hide(get_by_id('nav1'));hide(get_by_id('nav2'));hide(get_by_id('nav3'));hide(get_by_id('nav4'));hide(get_by_id('nav5')); }

function hide_switch(obj)
{
	ele = get_by_id(obj);
	if (ele.style.display == 'none')
		ele.style.display = '';

	else
		ele.style.display = 'none';
}
function bbcode_collapse(lolbox)
{
	obj = get_by_id(lolbox);
	if (obj.style.display == 'none')
		show(obj);
	else
		hide(obj);
}
function clear_field(start, obj) { if (start.toString() == obj.value.toString()) obj.value = ''; }
function unhelp() { hide(get_by_id("help")); }
function seeing_stars(name, i) { var z = 0; for(var j=1;j<i+1;j++) { star = get_by_id(name+j); star.src = "/images/star.png"; } }
function no_stars(name) { i = 5; for(var j=1;j<i+1;j++) { star = get_by_id(name+j); star.src = "/images/graystar.png"; } }
function inline_popup (loc, text, return_obj)
{
	popup = get_by_id("inlinepopup");
	hide(get_by_id("body"));
	show(popup);
	scroll(0, 0); 
	popup.innerHTML = '<div class="centered padding"><a href="javascript:destroy_popup(&#39;' + return_obj + '&#39;);"><img src="' + loc + '" alt="Image" />aaaa</a>aaa<br />'+text+'</div>';
	
}
function inline_popup_2(loc)
{
	popup = get_by_id("inlinepopup");
	hide(get_by_id("body"));
	img = get_by_id(loc).src;
	show(popup);
	scroll(0, 0); 
	popup.innerHTML = '<div class="centered padding"><a href="javascript:destroy_popup(&#39;' + loc + '&#39;);"><img src="' + img + '" alt="Image" /></a></div>';
}

function inline_popup_3(header, text, script)
{
	popup2 = get_by_id("inlinepopup2");
	show(popup2);
	popup2.innerHTML = '<div class="centered padding"><h1>'+ header + '</h1>' + text + '<br><div class="righted"><a href="javascript:destroy_popup2(&#39;&#39;);">Close</a></div></div>';
}

function destroy_popup(return_obj)
{
	hide(get_by_id("inlinepopup"));
	show(get_by_id("body"));
	arr = get_offsets(get_by_id(return_obj));
	arr['top'] -= 150;
	scroll(0, arr['top']);
}
function destroy_popup2(return_obj)
{
	hide(get_by_id("inlinepopup2"));
}

function destroy_popup2ok(script)
{
	hide(get_by_id("inlinepopup2"));
	vars = get_by_id("vars1");
	if(!vars) { vars = 0; }
	script(vars);
}

function get_offsets(obj)
{
	return_array = Array();
	return_array['top'] = obj.offsetTop;
	return_array['left'] = offleft = obj.offsetLeft;
	
	pelement = obj.offsetParent;
	while (pelement!=null)
	{
		return_array['top'] += pelement.offsetTop;
		pelement = pelement.offsetParent;
	}
	
	pelement = obj.offsetParent;
	while (pelement!=null)
	{
		return_array['left'] += pelement.offsetLeft;
		pelement = pelement.offsetParent;
	}
	return return_array;
}
function view_error(input)
{
	arr = get_offsets(get_by_id(input));
	arr['top'] -= 150;
	scroll(arr['left'], arr['top']); 
}
function help(msg, obj)
{
	help_text = msg.replace("#39;", "&#39;"); //can this be done better?
	var obj_help = get_by_id("help");
	obj_help.innerHTML = help_text;
	arr = get_offsets(obj);
	obj_help.style.left = arr['left'] + "px";
	obj_help.style.top = (arr['top'] + 20) + "px";
	show(obj_help);
}
function mapnight(box) { mapnight_timer = setTimeout("hide(get_by_id('mp_comments'));hide(get_by_id('mp_plays'));hide(get_by_id('mp_screenshots'));show(get_by_id('"+box+"'))", 650); }

function hide_save_shouts()
{
	for (i=0;i<document.xform.elements.length;i++)
	{
		objec = document.xform.elements[i];
		str = objec.id;
		if (str.substring(0, 4) == "save" && objec.type=='checkbox' && objec.style.visibility=='visible')
		{
			hide(objec);
		} else if (str.substring(0, 4) == "save" && objec.type=='checkbox' && objec.style.visibility=='hidden')
		{
			show(objec);
		}
	}
}

function init()
{
	if (document.layers) { layerRef="document.layers"; styleSwitch=""; visibleVar="show"; browser ="ns"; } 
	else if(document.all) { layerRef="document.all"; styleSwitch=".style"; visibleVar="visible"; browser ="ie"; } 
	else if(document.getElementById) { layerRef="document.getElementByID"; styleSwitch=".style"; visibleVar="visible"; browser="dom"; } 
	
	else { browser="none"; newbrowser = false; } 
	check = true; 
}
function attach(id)
{
	xtext = get_by_id('xtext');
	xtext.value = xtext.value + "[attach=" + id + "]"
}
function open_image(html)
{
	body = get_by_id('body_screen');
	hide(body);
	hbs = get_by_id('huge_black_screen');
	hbs.innerHTML = 'Click image to go back<br /><a href="javascript:close_image();"><img src="uploads/database/images/' + html + '"></a>';
	show(hbs);
}
function close_image()
{
	body = get_by_id('body_screen');
	show(body);
	hbs = get_by_id('huge_black_screen');
	hide(hbs);
	
}
function locations()
{
	if (window.scrollY)
	{
		current_scroll_height = window.scrollY; current_scroll_length = window.scrollX;
	}
	else if (window.pageYOffset)
	{
		current_scroll_height = window.pageYOffset; current_scroll_length = window.pageXOffset;
	}
	else if (document.documentElement.scrollTop)
	{
		current_scroll_height = document.documentElement.scrollTop; current_scroll_length = document.documentElement.scrollLeft;
	}
	if (window.innerWidth)
	{
		current_view_width = window.innerWidth;
		current_view_height = window.innerHeight;
	}
	else if (document.documentElement.offsetHeight)
	{
		current_view_height = document.documentElement.offsetHeight;
		current_view_width = document.documentElement.offsetWidth;
	}
}
function ajax_follow_start()
{ 
	if(ajax_working)
	{
	if(!ajax_status) ajax_status="Loading..."; 
	obj=get_by_id("ajax_move");
	objstatus=get_by_id("ajax_status");
	objstatus.innerHTML=ajax_status;
	obj.style.visibility='visible';
	show(obj);
	}
}

function ajax(script, vars)
{
	if (!ajax_working) { ajax_working = 1; get_by_id("ajax_move").style.width = "300px"; }
	eval ("var cla = new ajax_"+script+"();");
	/*
	if (script == "shoutbox") var cla = new ajax_shoutbox();
	else if (script=="mark") var cla = new ajax_mark();
	else if (script=="multimod") var cla = new ajax_multimod();
	else if (script=="report") var cla = new ajax_report();
	else if (script=="more_posts") var cla = new ajax_more_posts();
	else if (script=="postedit") var cla = new ajax_postedit();
	else if (script=="postdelete") var cla = new ajax_postdelete();
	else if (script=="shout_delete") var cla = new ajax_shout_delete();
	else if (script=="mapnight_rating") var cla = new ajax_mapnight_rating();
	else if (script=="karma") var cla = new ajax_karma();
	else if (script=="get_unparsed_post") var cla = new ajax_get_unparsed_post();
	else if (script=="postdelete_minor") var cla = new ajax_postdelete_minor();
	else if (script=="scp_member_upgrade_ignore") var cla = new ajax_scp_member_upgrade_ignore();
	else if (script=="scp_member_upgrade_promote") var cla = new ajax_scp_member_upgrade_promote();
	*/
	cla.limit();
	httpcounts++;
	i = httpcounts;
	httpreqs[i] = ajax_object();
	var params = cla.params(ajaxsession, ajaxmid, vars);
	httpreqs[i].onreadystatechange = function()
	{
		ajax_status = get_ajax_status(httpreqs[i]);
		if (cla.no_global_start != 1)
			ajax_follow_start();
		if (ajax_status == "Completed!")
		{
			errored = httpreqs[i].responseText.substr(0,5);
			//alert(errored);
			// alert(httpreqs[i].responseText);
			if (httpreqs[i].responseText.substr(0, 8) == '&Error: ')
			{
			// if(errored.toString() == 'Error')
				ajax_working = 0;
				get_by_id("ajax_move").style.width = "600px";
				get_by_id("ajax_status").innerHTML = httpreqs[i].responseText.substr(1);
				eval("setTimeout(\"ajax_close('"+i+"');\", 5000);");
			}
			else
			{	
				// alert (httpreqs[i].responseText);

					cla.parser(httpreqs[i].responseText, vars);

				if (cla.no_global_start != 1)
					eval("setTimeout(\"ajax_close('"+i+"');\", 1700);");
			}
		}
		cla.status(ajax_status);
	}
	httpreqs[i].open("POST", cla.url(), true);
	httpreqs[i].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpreqs[i].setRequestHeader("Content-length", params.length);
	httpreqs[i].setRequestHeader("Connection", "close");
	httpreqs[i].send(params);
	eval("setTimeout(\"clear('"+script+"');\", 2500);");
}
function shoutbox_enable_refresh(t) { refresh_time=t*1000;eval("setTimeout(\"shoutbox_refresh('"+refresh_time+"');\", "+refresh_time+");");}
function shoutbox_refresh(t) { eval("setTimeout(\"shoutbox_refresh('"+refresh_time+"');\", "+refresh_time+");"); do_shout=0; ajax("shoutbox"); }
function ajax_close(k) { delete httpreqs[k]; ajax_status=''; obj=get_by_id("ajax_move"); obj.style.visibility='hidden';}
function clear(script) { if (script=="shoutbox") var cla = new ajax_shoutbox; else if (script=="multimod") var cla = new ajax_multimod; else return; cla.cleared(); }
function ajax_object() { var http_object = null; try { http_object = new XMLHttpRequest(); } catch (e) { try { http_object = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { http_object = new ActiveXObject("Microsoft.XMLHTTP"); } } return http_object; }
function get_ajax_status(tmp)
{
 if (tmp.readyState == 0) return "Uninitialized";
 else if (tmp.readyState == 1) return "Loading...";
 else if (tmp.readyState == 2) return "Loaded..."
 else if (tmp.readyState == 3) return "Interacting..."
 else if (tmp.readyState == 4) { if (tmp.status == 200) return "Completed!" }
}

function ajax_shout_delete()
{
	this.params=params;this.parser=parser;this.status=status;this.url=url;this.limit=limit;this.cleared=cleared;
	function status(tmp) {}
	function limit() {}
	function cleared() {}
	function url() { return "/ajax/shoutbox_delete.php"; }
	function params(ze, amid, vars)
	{
		return "ze="+ze+"&ajaxmid="+amid+"&id="+vars;
	}
	function parser(html)
	{
		if (html != 0)
			hide(get_by_id("shout_"+html));
		else
			alert("error?");
	}
}

function ajax_getmembers()
{
	this.params=params;this.parser=parser;this.status=status;this.url=url;this.limit=limit;this.cleared=cleared;
	function status(tmp) {}
	function limit() {}
	function cleared() {}
	function url() { return "/ajax/get_members.php"; }
	function params(ze, amid, vars)
	{
		return "ze="+ze+"&ajaxmid="+amid+"&id="+vars;
	}
	function parser(html)
	{
		if (html != 0)
			get_by_id("members").innerHTML = html;
		else
			alert("error?");
	}
}

function ajax_mapnight_rating()
{
	this.params=params;this.parser=parser;this.status=status;this.url=url;this.limit=limit;this.cleared=cleared;
	function status(tmp) {}
	function limit() {}
	function cleared() {}
	function url() { return "/ajax/mapnight_rating.php"; }
	function params(ze, amid, vars)
	{
		return "ze="+ze+"&ajaxmid="+amid+vars;
	}
	function parser(html)
	{
		if (html != 0)
			hide(get_by_id("ss_rating_"+html));
		else
			alert("error?");
	}
}
function ajax_shoutbox()
{
	this.params=params;this.parser=parser;this.status=status;this.url=url;this.limit=limit;this.cleared=cleared;
	this.no_global_start = 1;
	function status(tmp) { get_by_id('shoutbox_status').innerHTML = tmp; }
	function limit() { sb_refresh = get_by_id("shout_refresh");sb_submit = get_by_id("shout_submit");sb_refresh.className = sb_submit.className = "disabled";sb_refresh.disabled = sb_submit.disabled = "disabled"; }
	function cleared() { sb_refresh = get_by_id("shout_refresh");sb_submit = get_by_id("shout_submit");sb_refresh.className = sb_submit.className = "submit";sb_refresh.disabled = sb_submit.disabled = ""; }
	function url() { return "/ajax/shoutbox.php"; }
	function params(ze, amid, vars)
	{
			shout_from = get_by_id("shout_from").value;
		if (do_shout)
		{

			textual = encodeURIComponent(get_by_id("shout_text").value);
			get_by_id("shout_text").value = "";
			shout_color = get_by_id("shout_color").value;
			site = get_by_id("shout_site").value;
		}
		else { textual=""; shout_color=""; site="";}
		do_shout = 1;
		return "ze="+ze+"&ajaxmid="+amid+"&itime="+shoutbox_last+"&shout_from="+shout_from+"&color="+encodeURI(shout_color)+"&channel="+site+"&shout="+textual;
	}
	function parser(html)
	{
		temp = html.indexOf(";");
		shoutbox_last = html.substring(0, temp);
		data = html.substring(temp+1);
		get_by_id("shoutbox_return").innerHTML = data + get_by_id("shoutbox_return").innerHTML;
	}
}
function hover(div_id, var_name) { tmp = get_by_id(div_id); tmp.innerHTML = eval(var_name);	}
function form_submit(xid) { eval("document."+xid+".submit()"); }
function over(name, n) { get_by_id(name).className = 'tdlink' + n; }
function hide(obj) { obj.style.display = 'none'; obj.style.visibility = 'hidden'; }
function show(obj) { obj.style.display = ''; obj.style.visibility = 'visible'; }
function cleartext(obj) { obj.value = ''; }
function disable_reclick(mbutton) { alert (mbutton); }
function check_all()
{
	for (var i=0;i<document.xform.elements.length;i++)
	{
		var objec = document.xform.elements[i];
		if ((objec.name != 'checkall') && (objec.type=='checkbox'))
		{
			objec.checked = document.xform.selectall.checked;
		}
	}
}
function small_window(location, name, width, height)
{
	if (!width) {width = 400;}
	if (!height) {height = 250;}
	if (!name) {name = location}
	url = href_base+"small.php?p="+location+"&skin="+skin_base;
	dimensions = "width="+width+",height="+height;
	window.open(url, name, dimensions + 'resizable=yes, scrollbars=yes'); 
}

function eat_cookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    $temp = dc.substring(begin + prefix.length, end);
    if ($temp)
    	return $temp;
    else
    	return false;
}
function give_cookie(name, value, sticky) { document.cookie = name + "=" + value + ";expires=" + cookie_expire_date +" 12:00:00 UTC;domain=" + cookie_domain + ";path=/"; }
// function give_cookie(name, value, sticky) { document.cookie = name + "=" + value + ";expires=Fri, 3 Aug 2009 20:47:11 UTC;domain=127.0.0.1;path=/"; }
function get_by_id(name)
{
	object = null;
	if (document.getElementById) object = document.getElementById(name);
	else if (document.all) object = document.all[name];
	else if (document.layers) object = document.layers[name];
	return object;
}
function check_id_for_existance(in_parent, id_name)
{
	if(in_parent)
	{
		if (window.opener.document.getElementById(id_name) != null)
			return true;
		else
			return false;
	}
	else
	{
		if (document.getElementById(id_name) != null)
			return true;
		else
			return false;
	}
}
function collapse(id, type, tablets, save)
{
	mycurrent = new Array();
	newcookie = new Array();
	tablet_arr = new Array();
	do_tabs = false;
	if (temp = eat_cookie('collapsed'))
	{
		mycurrent = temp.split(",");
	}
	if (tablets != "")
	{
		do_tabs = true;
		tablet_arr = tablets.split(",");
	}
	if (type == "expand")
	{
		if (save)
		{
			for(i = 0;i < mycurrent.length;i++)
			{
				if ( mycurrent[i] != id && mycurrent[i] != "" )
				{
					newcookie[newcookie.length] = mycurrent[i];
				}
			}
		}
		hide(get_by_id('hide_'+id));
		show(get_by_id('show_'+id));
		if (do_tabs)
		{
			for(i = 0;i < tablet_arr.length;i++)
			{
				show(get_by_id('tab_'+tablet_arr[i]));
			}
		}
	}
	else
	{
		if (save)
		{
			newcookie = mycurrent;
			newcookie[newcookie.length] = id;
		}
		show(get_by_id('hide_'+id));
		hide(get_by_id('show_'+id));
		if (do_tabs)
		{
			for(i = 0;i < tablet_arr.length;i++)
			{
				hide(get_by_id('tab_'+tablet_arr[i]));
			}
		}
	}
	if (save)
	{
		give_cookie('collapsed', newcookie.join(','), 1);
	}
}
function show_attachments(vars)
{
	show = prompt('Attachment BBCode:', '[attach='+vars+']');
	return false;
}

function test() {
	document.getElementById("shout_text").style.color = document.getElementById("shout_color").value;   
	document.getElementById("shout_color").style.color = document.getElementById("shout_color").value;   
	document.getElementById("shout_text").focus();
}