var uagent    = navigator.userAgent.toLowerCase();
var is_safari = ( (uagent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var is_ie     = ( (uagent.indexOf('msie') != -1) && (!is_opera) && (!is_safari) && (!is_webtv) );
var is_ie4    = ( (is_ie) && (uagent.indexOf("msie 4.") != -1) );
var is_moz    = (navigator.product == 'Gecko');
var is_ns     = ( (uagent.indexOf('compatible') == -1) && (uagent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_safari) );
var is_ns4    = ( (is_ns) && (parseInt(navigator.appVersion) == 4) );
var is_opera  = (uagent.indexOf('opera') != -1);
var is_kon    = (uagent.indexOf('konqueror') != -1);
var is_webtv  = (uagent.indexOf('webtv') != -1);

var is_win    =  ( (uagent.indexOf("win") != -1) || (uagent.indexOf("16bit") !=- 1) );
var is_mac    = ( (uagent.indexOf("mac") != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var ua_vers   = parseInt(navigator.appVersion);

// ajax stuff
var field, isTextArea, ajaxStr, xmlHttp;
var ajaxCache = new Array();

function sideIconHoverOn(objName, str, newHead, useCache, newClass)
{
	objName.width = objName.height = 48;
	objName.src = objName.src.replace("menu/thumb/", "menu/");
	
	my_getbyid("menuicon").style.top = '166px';
	
	if(newHead)
	{
		my_getbyid("f_p_side_menu_head").innerHTML = newHead;
	}

	if(str)
	{
		change_cell_class("f_p_side_menu", newClass);
		my_show_div(my_getbyid("fo_leftnav"));
		getData(str, "f_p_side_menu", 0, useCache);
	}
}

function sideIconHoverOff(objName)
{
	my_getbyid("menuicon").style.top = '180px';

	objName.width = objName.height = 16;
	objName.src = objName.src.replace("menu/", "menu/thumb/");
}

function getData(str,fld,textarea,useCache)
{
	field = fld;
	ajaxStr = str;
	isTextArea = textarea;
		
	if(useCache && ajaxCache[str])
	{
		updateContent(ajaxCache[str]);
	}
	else
	{		
		updateContent("Loading content...");
		xmlSend("act-"+ipb_input_a+"/f-"+ipb_input_f+"/p-"+ipb_input_p+"/t-"+ipb_input_t+"/ajax-" + str, stateChanged, useCache);
	}

	return false;
}

function stateChanged()
{
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"))
	{
		ajaxCache[ajaxStr] = xmlHttp.responseText;
		
		updateContent(xmlHttp.responseText);
		
		xmlHttp.abort();
		xmlHttp = null;
	}
}

function updateContent(newText)
{
	if(isTextArea)
	{
		updateContentTextarea(newText);
	}
	else
	{
		updateContentDiv(newText);
	}
}

function updateContentTextarea(newText)
{
	field.value = newText;
	field.focus();
}

function updateContentDiv(newText)
{
	my_getbyid(field).innerHTML=newText;
}

function GetXmlHttpObject()
{
	try
	{
		return new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			return new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch(e)
		{
			return false;
		}
	}
}

function xmlerror()
{
	alert("Error. Scripting for ActiveX might be disabled");
	return;
}

function disp_menu(obj, field, tab_left)
{
	if (myMenuObj = my_getbyid(field))
	{
		myMenuObj.style.width = null;
		
		var myOffsetTop = obj.offsetTop + obj.offsetHeight;
		var myOffsetLeft = obj.offsetLeft;
		var myOffsetParent = obj.offsetParent;

		while (myOffsetParent)
		{
			myOffsetTop += myOffsetParent.offsetTop;
			myOffsetLeft += myOffsetParent.offsetLeft;
			myOffsetParent = myOffsetParent.offsetParent;
		}
		
		if(tab_left)
		{
			myMenuObj.style.width = tab_left+'px';
			myOffsetLeft-=(tab_left+10 - obj.offsetWidth);
		}

		myMenuObj.style.left = myOffsetLeft+"px";
		myMenuObj.style.top = myOffsetTop+"px";
		
		toggleview(field);
	}
}

function getScrollY()
{
	// Netscape
	if( typeof( window.pageYOffset ) == 'number' )
	{
		return window.pageYOffset;
	}
	
	// DOM
	if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
	{
		return document.body.scrollTop;
	}
	
	// IE6
	if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
	{
		return document.documentElement.scrollTop;
	}
	
	return 0;
}

//==========================================
// Get cookie
//==========================================

function my_getcookie( name )
{
	cname = ipb_var_cookieid + name + '=';
	cpos  = document.cookie.indexOf( cname );

	if ( cpos != -1 )
	{
		cstart = cpos + cname.length;
		cend   = document.cookie.indexOf(";", cstart);

		if (cend == -1)
		{
			cend = document.cookie.length;
		}

		return unescape( document.cookie.substring(cstart, cend) );
	}

	return null;
}

//==========================================
// Set cookie
//==========================================

function my_setcookie( name, value, sticky )
{
	expire = "";
	domain = "";
	path   = "/";

	if ( sticky )
	{
		expire = "; expires=Wed, 1 Jan 2020 00:00:00 GMT";
	}

	if ( ipb_var_cookie_domain != "" )
	{
		domain = '; domain=' + ipb_var_cookie_domain;
	}

	if ( ipb_var_cookie_path != "" )
	{
		path = ipb_var_cookie_path;
	}

	document.cookie = ipb_var_cookieid + name + "=" + value + "; path=" + path + expire + domain + ';';
}

function buddy_pop()
{
	window.open( ipb_var_base_url + 'buddy','BrowserBuddy','width=250,height=500,resizable=yes,scrollbars=yes');
}

function chat_pop(cw,ch)
{
	window.open( ipb_var_base_url + 'chat/pop-1','Chat','width='+cw+',height='+ch+',resizable=yes,scrollbars=yes');
}

function multi_page_jump( url_bit, total_posts, per_page )
{
	pages = 1;
	cur_st = ipb_var_st;
	cur_page  = 1;

	if ( total_posts % per_page == 0 )
	{
		pages = total_posts / per_page;
	}
	else
	{
		pages = Math.ceil( total_posts / per_page );
	}

	msg = ipb_lang_tpl_q1 + " " + pages;

	if ( cur_st > 0 )
	{
		cur_page = cur_st / per_page; cur_page = cur_page -1;
	}

	show_page = 1;

	if ( cur_page < pages )
	{
		show_page = cur_page + 1;
	}

	if ( cur_page >= pages )
	{
		show_page = cur_page - 1;
	}
 	else
 	{
 		show_page = cur_page + 1;
 	}

	userPage = prompt( msg, show_page );

	if ( userPage > 0  )
	{
		if ( userPage < 1 )     {    userPage = 1;  }
		if ( userPage > pages ) { userPage = pages; }
		if ( userPage == 1 )    {     start = 0;    }
		else { start = (userPage - 1) * per_page; }

		window.location = url_bit + "/st-" + start;
	}
}

function ShowHide(id1, id2)
{
	if (id1 != '') toggleview(id1);
	if (id2 != '') toggleview(id2);
}

function changeImgSrc(imgid, newsrc)
{
	if(myimg = my_getbyid(imgid))
		myimg.src = '/'+newsrc;
}

function my_getbyid(id)
{
	if (document.getElementById)
	{
		return document.getElementById(id);
	}
	if (document.all)
	{
		return document.all[id];
	}
	if (document.layers)
	{
		return document.layers[id];
	}

	return null;
}

function toggleview(id)
{
	if ( itm = my_getbyid(id) )
	{
		if (itm.style.display == "none")
		{
			my_show_div(itm);
		}
		else
		{
			my_hide_div(itm);
		}
	}
}

function my_hide_div(itm)
{
	itm.style.display = "none";
}

function my_show_div(itm)
{
	itm.style.display = "";
}

function change_cell_class( id, cl )
{
	if ( itm = my_getbyid(id) )
	{
		itm.className = cl;
	}
}

//==========================================
// Toggle category
//==========================================

function togglecategory( fid )
{
	if(!(itm = my_getbyid('f_'+fid)))
	{
		return;
	}
	
	saved = new Array();
	clean = new Array();

	//-----------------------------------
	// Get any saved info
	//-----------------------------------

	if ( tmp = my_getcookie('collapseprefs') )
	{
		saved = tmp.split(",");
	}

	//-----------------------------------
	// Remove bit if exists
	//-----------------------------------

	for( i = 0 ; i < saved.length; i++ )
	{
		if ( saved[i] != fid && saved[i] != "" )
		{
			clean[clean.length] = saved[i];
		}
	}

	//-----------------------------------
	// Add?
	//-----------------------------------

	if (itm.style.display != "none")
		clean[ clean.length ] = fid;
	
	toggleview('f_'+fid);
	toggleview('fc_'+fid);
	toggleview('fo_'+fid);

	my_setcookie( 'collapseprefs', clean.join(','), 1 );
}

function locationjump(url)
{
	window.location = ipb_var_base_url + url;
}

function refreshSend(text)
{
	window.location.reload();
}

function sendAndReload(url)
{
	xmlSend(url, refreshSend);
}

function xmlSend(url, myMethod, useCache)
{
	if(!useCache)url += "/sid-" + Math.random();
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange = myMethod;
	xmlHttp.open("GET", ipb_var_base_url + url , true);
	xmlHttp.send(null);
}

function chooseskin(obj)
{
	choosebox = obj.options[obj.selectedIndex].value;

	if ( choosebox != -1 && ! isNaN( choosebox ) )
	{
		location.href = '/ajax-setskin/return-true/skinid-' + choosebox;
	}
}

function chooselang(obj)
{
	choosebox = obj.options[obj.selectedIndex].value;

	sendAndReload('setlanguage-1/langid-' + choosebox);
}

function PopUp(url, name, width,height,center,resize,scroll,posleft,postop)
{
	showx = "";
	showy = "";

	if (posleft != 0) { X = posleft }
	if (postop  != 0) { Y = postop  }

	if (!scroll) { scroll = 1 }
	if (!resize) { resize = 1 }

	if ((parseInt (navigator.appVersion) >= 4 ) && (center))
	{
		X = (screen.width  - width ) / 2;
		Y = (screen.height - height) / 2;
	}

	if ( X > 0 )
	{
		showx = ',left='+X;
	}

	if ( Y > 0 )
	{
		showy = ',top='+Y;
	}

	if (scroll != 0) { scroll = 1 }

	var Win = window.open( url, name, 'width='+width+',height='+height+ showx + showy + ',resizable='+resize+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no');
}

// Array: Stacks

function stacksize(thearray)
{
	for (i = 0 ; i < thearray.length; i++ ) if (thearray[i] == "" || thearray[i] == null || thearray == 'undefined') return i;

	return thearray.length;
}

function pushstack(thearray, newval)
{
	arraysize = stacksize(thearray);
	thearray[arraysize] = newval;
}

function popstack(thearray)
{
	arraysize = stacksize(thearray);
	theval = thearray[arraysize - 1];
	delete thearray[arraysize - 1];
	return theval;
}
