function GetObj(ID){return document.getElementById(ID)}
function PostAction(formType){ //ie6 change
	obj=GetObj('POST-FORM')
	if (obj) {
		obj.className='' //ie6 issue, click twice
  	obj.className = formType //post/ask/share/intro
	}
}
myTimeEvt='';
function LogoMenu(cls){GetObj('logo-menu').className=cls}
function MenuReset(){myTimeEvt=setTimeout(function(){HIDE('logo-hover')},500);}
/*Header/Footer search*/
function searchBtn(id){
	var frm = GetObj(id+'er-search');
	var inputs = frm.getElementsByTagName('input');
	for(var i = 0; i < inputs.length; i++){
		if(inputs[i].name == 'keywords'){
			var keywords = inputs[i];
		}
	}
	if(keywords && keywords.value!=''){frm.submit();}
}
function GetObj(ID){return document.getElementById(ID)}
function uMenu(){
	n=GetObj('user-name');m=GetObj('user-menu')
	if(n.className=='hover'){n.className='';m.style.display='none'}
	else {n.className='hover';m.style.display='block'}
}
function sMenu(o){signon=0;
	n=GetObj('sign');m=GetObj('sign-drop')
	if(o){GetObj('rld').value='http://connect.manta.com/subscriptions.php';signon=1}
	if(signon){n.className='hover';m.style.display='block'}
	else if(n.className!='hover') {n.className='hover';m.style.display='block'}
	else{n.className='';m.style.display='none'}
}
function login(){
	rldval=$('#rld').val()
	if(rldval==''){$('#rld').val(document.location)}
	$('#top_login_form').submit()
}
function LOGOUT(rld){
	SetCookie('ReturnPage',document.location)
	retURL=encodeURIComponent("http://connect.manta.com/"+rld)
	document.location="http://www.manta.com/logout?rld="+retURL
}
function SetCookie (name, value) {
	var expdate = new Date()
	var argv = SetCookie.arguments; 
	var argc = SetCookie.arguments.length; 
	var expires = (argc > 2) ? argv[2] : null; 
	expdate.setDate(expdate.getDate() + expires);
	var path = (argc > 3) ? argv[3] : null; 
	var domain = '.manta.com'; 
	var secure = (argc > 5) ? argv[5] : false; 
	var cookieVal = name + "=" + escape (value) + 
		((path == null) ? "" : ("; path=" + path)) + 
		((expires == null) ? "" : ("; expires=" + expdate)) + 
		((domain == null) ? "" : ("; domain=" + domain)) + 
		((secure == true) ? "; secure" : "");
	document.cookie = cookieVal;
}

/**
* Find a member
*/
function find_username(url)
{
	popup(url, 760, 570, '_usersearch');
	return false;
}

/**
* New function for handling multiple calls to window.onload and window.unload by pentapenguin
*/
window.onload = function()
{
	for (var i = 0; i < onload_functions.length; i++)
	{
		eval(onload_functions[i]);
	}
}

window.onunload = function()
{
	for (var i = 0; i < onunload_functions.length; i++)
	{
		eval(onunload_functions[i]);
	}
}

function updatePopFollow(obj){
	var url = obj.href;
	if(url.match(/(un)?watch=(\w+)/)){
		var un = RegExp.$1;
		var mode = RegExp.$2;
		
		jQuery('.subscribe-posttype').html((mode == 'topic' ? 'Thread' : 'Topic'));
		if(un){//watching
			jQuery('.unsub').removeClass('HIDE');jQuery('.sub').addClass('HIDE');
			jQuery('.start').addClass('HIDE');jQuery('.stop').removeClass('HIDE');
		}else{
			jQuery('.sub').removeClass('HIDE');jQuery('.unsub').addClass('HIDE');
			jQuery('.stop').addClass('HIDE');jQuery('.start').removeClass('HIDE');
		}
	}
}

function toggleFollow(obj, hideContent){
	var panel = document.getElementById('subscribe-panel');
	var offset = jQuery(obj).offset();
	panel.style.top = offset.top + "px";
	panel.style.left = offset.left + "px";
	panel.style.display = 'block';
	updatePopFollow(obj);
	document.getElementById('subscribe-deny').onclick = function(){
		panel.style.display = 'none';
	};
	document.getElementById('follow-x').onclick = function(){
		panel.style.display = 'none';
	};
	document.getElementById('subscribe-confirm').onclick = function(){
		var url = obj.href;
		if(url.match(/(un)?watch=(\w+)/)){
			var un = RegExp.$1;
			var mode = RegExp.$2;
			if(mode){
				if(un){
					var newurl = url.replace('unwatch=' + mode, 'watch=' + mode);
				}else{
					var newurl = url.replace('watch=' + mode, 'unwatch=' + mode);
				}
			}
			// The extra fluff in the function is a closure to make sure newurl and obj get evaluated now, not at run-time when the ajax request completes
			jQuery.get(url, (function(node, url, un, mode){
				return(function(){
					if(url){
						node.href = url;
					}
					var content = (un != 'un' ? "You're Following <span class='stop'>Stop</span>" : "Follow This " + (mode == 'topic' ? 'Thread' : 'Topic'));
					if(hideContent) content = '&nbsp;';
					node.innerHTML = content;
					jQuery(node).removeClass('blue gold').addClass(un == 'un' ? 'blue' : 'gold');
					document.getElementById('subscribe-panel').style.display = 'none';
				});
			})(obj, newurl, un, mode));
		}
	};
}

// Optional second argument "show" true/false
function toggleFindOutMore(obj){
	var $ = jQuery;
	var panel = $('#find-out-more-panel');
	var hide = typeof arguments[1] != 'undefined' ? arguments[1] ? false : true : panel.css('display') != 'none';
	$('#find-x').click(function(){
		panel.hide();
	});

	var offset = $(obj).offset();
	var css = {
		top: (parseInt(offset.top, 10) + parseInt($(obj).height(), 10) + parseInt(10, 10)) + "px",
		left: parseInt((parseInt($(window).width(), 10) / 2) - (parseInt($(obj).width(), 10) / 2) - 20, 10) + "px",
		position: 'absolute'
	};
	panel.css(css);
	hide ? panel.hide() : panel.show();
}

var panelTimers = {};
// Optionally accepts a third argument as the handler to be called after timeout. Default is to hide the given id
function beginPanelTimeout(panelId, time){
	if(panelTimers[panelId]){
		clearTimeout(panelTimers[panelId]);
		delete panelTimers[panelId];
	}
	var defaultHandler = function(){
		jQuery('#' + panelId).hide();
	};
	var handler = typeof arguments[2] == 'function' ? arguments[2] : defaultHandler;
	panelTimers[panelId] = setTimeout(handler, time);
}
function cancelPanelTimeout(panelId){
	clearTimeout(panelTimers[panelId]);
}

function menuHide(id){
	$('#' + id).addClass('hide').removeClass('show');
}
function menuShow(id){
	$('#' + id).addClass('show').removeClass('hide');
}

function sortSearch(obj){
	if(obj && obj.options && obj.selectedIndex != 'undefined'){
		if(obj.options[obj.selectedIndex]){
			var key = obj.options[obj.selectedIndex].value;
			var url = location.href;
			var newurl = url;
			if(url.match(/sk=[^\&]/)){
				newurl = location.href.replace(/sk=[^\&]/, 'sk=' + key);
			}else{
				var sep = location.href.match(/\?/) ? '&' : '?';
				newurl = location.href + sep + "sk=" + key;
			}
			if(newurl && newurl != '' && newurl != url){
				location.href = newurl;
				return true;
			}
		}
	}

	return false;
}

function reSort(){
	var url = location.href;
	if(url.match(/sd=([^\&])/)){
		var new_sort_dir = (RegExp.$1 == 'a' ? 'd' : 'a');
		var newurl = url.replace(/sd=[^\&]/, 'sd=' + new_sort_dir);
	}else{
		var newurl = url + (url.match('\\?') ? "&sd=a" : "?sd=a");
	}
	if(newurl != url) location.href = newurl;
}

function addNewPost(){
	location.href = "/posting.php?mode=post&simple=1";
}

var questionPanelShown = false;
var questionPanelChanging = false;
function toggleQuestionPanel(){
	if(questionPanelChanging) return false; // Currently doing something
	var toggleOpen = typeof arguments[0] != 'undefined' ? (arguments[0] ? true : false) : (questionPanelShown ? false : true); // Either explicitly from argument, or toggle
	var questionPanelChanging = true; // This means we're doing something, cancel any interupting events
	if(toggleOpen){
		questionPanelShown = true;
		$('#question-submit').removeClass('btn-post-my-message-continue').addClass('btn-post-my-message');
		$('#question-expand').show(1200, function(){questionPanelChanging=false;});
	}else{
		questionPanelShown = false;
		$('#question-submit').removeClass('btn-post-my-message').addClass('btn-post-my-message-continue');
		$('#question-expand').hide(400, function(){questionPanelChanging=false;});
	}
}

function grayscaleTab(id){
	jQuery('#' + id).css('background-color', '#BBBBBB !important');
	jQuery('#' + id + ' div').css('opacity', '0.3');
}

function setTextSelection(nodeId, start, end){
	var node = document.getElementById(nodeId);

	node.focus();
	if('selectionStart' in node){
		// Legitimate web browsers
		node.selectionStart = start;
		node.selectionEnd = end;
	}else{
		// Toy browsers (IE)
		var range = node.createTextRange();
		range.collapse(true);
		range.moveStart('character', start);
		range.moveEnd('character', end - start);
		range.select();
	}
}

