//GENERAL FUNCTIONS
function emote(text,obj) {
	var comm = $(obj);
	if(comm) wrapSelection(comm, " "+text+" ", "");
}

function insertimg() {
	var img = prompt("Enter the image URL:","http://");
	if(img!="http://" && img!=null) wrapSelection(document.form1.post, "<img src=\""+img+"\">", "")
}
function insertlink() {
	var url = prompt("Enter the URL:","http://");
	var title = prompt("Enter optional link title:","");
	if(title=="") title=url;
	wrapSelection(document.form1.post, "<a href=\""+url+"\">"+title+"</a>", "")
}

function mozWrap(txtarea, lft, rgt) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + lft + s2 + rgt + s3;
}

function SafWrap(txtarea, lft, rgt) {
	txtarea.value = txtarea.value + lft + rgt;
}

function IEWrap(txtarea, lft, rgt) {
	strSelection = document.selection.createRange().text;
	if (strSelection!="") {
		document.selection.createRange().text = lft + strSelection + rgt;
	} else {
		txtarea.focus();
		document.selection.createRange().text = lft + strSelection + rgt;
		//txtarea.value = txtarea.value + lft + rgt;
	}
}

function wrapSelection(txtarea, lft, rgt) {
	var detect = navigator.userAgent.toLowerCase();

	if (detect.indexOf('msie')>=0) {IEWrap(txtarea, lft, rgt);}
	else if (detect.indexOf('safari')>=0) {SafWrap(txtarea, lft, rgt);}
	else {mozWrap(txtarea, lft, rgt);}
}

function togglePoll() {
	var vis = document.getElementById('visrow').style.display;
	var poll = document.getElementById('poll');
	var check = document.getElementById('check').checked;
	
	if(check==1) poll.style.display = vis;
	else poll.style.display="none";
}

function textareasize(ta,size) {
	if(ta.value.length>size) ta.value = ta.value.substring(0,size);
}


//HOME PAGE ANIMATIONS
var timeout = 0;
var loaded = new Array(4); 

function clearTimer() {
	if(timeout) clearTimeout(timeout);
}

function gotContent(showme) {
	loaded[showme] = 1; //prevents same thing from loading multiple times
	
	Element.hide("roll_teasers");
	Element.hide("roll_trivia");
	Element.hide("roll_mental");
	Element.hide("roll_games");
	Element.hide("roll_community");
	Element.show("roll_"+showme);

	$("cell_teasers").className = "ind_nohigh";
	$("cell_trivia").className = "ind_nohigh";
	$("cell_mental").className = "ind_nohigh";
	$("cell_games").className = "ind_nohigh";
	$("cell_community").className = "ind_nohigh";
	$("cell_"+showme).className = "ind_high";
	
}

function frontRollovers(showme, delay) {
	clearTimer();
		
	if(delay>0) {
		timeout = setTimeout("frontRollovers('"+showme+"',0)",delay);
		return;
	}
	
	if(showme && showme!="teasers" && !loaded[showme]) {
		new Ajax.Updater('roll_'+showme, '/includes/ajax/front.php', {method: 'post', postBody: 'showme='+showme, onSuccess: function() { gotContent(showme); }  } );

	} else { 
		Element.hide("roll_teasers");
		Element.hide("roll_trivia");
		Element.hide("roll_mental");
		Element.hide("roll_games");
		Element.hide("roll_community");
		Element.show("roll_"+showme);
	
		$("cell_teasers").className = "ind_nohigh";
		$("cell_trivia").className = "ind_nohigh";
		$("cell_mental").className = "ind_nohigh";
		$("cell_games").className = "ind_nohigh";
		$("cell_community").className = "ind_nohigh";
		$("cell_"+showme).className = "ind_high";
	}
}

//PRIVATE MESSAGES
function showSearch() {
	Element.hide("searchbtn"); 
	Element.show("pmsearch");
	return false;
}

//LOGIN AND TIPS
function saveVal(id,a,b) {
	var pars = "id="+id+"&a="+a;
	if(b!='') {
		var val = $(b).value;
		pars +="&b="+val;
		$('save').value="It has been saved";
	}
	
	new Ajax.Request( "/includes/ajax/tip.php", {method: 'post', postBody: pars} );

}

function clicked(id,a,text) {
	var pars = "id="+id+"&a="+a;
	new Ajax.Request( "/includes/ajax/tip.php", {method: 'post', postBody: pars} );
	$('choices').update('<b>'+text+'</b>');
}

function valChanged() {
	$('save').value="Save";
}