var forumtabs = new Array(['alltab',''],['ptalktab',1],['trantalktab',2],['tkttalk',3],['nostttalk',7],['cahagtab',4],['gentalktab',5],['footballtab',10],['funtalktab',6]);
var newstabs = new Array('palnewstab','champnewstab','premnewstab');
var fixtabs = new Array('statstabletab','statsfixtab','statsresulttab');
var predicttabs = new Array('statspredicttabletab','statspredictresulttab');
var ratingtabs = new Array('statsratingoveralltab','statsratinglasttab');

function cleartabs(tabarray) {
	tabarray.each(function(s) {
		if($(s[0])) {
			$(s[0]).up().removeClassName('selected');
		} else {
			$(s).up().removeClassName('selected');
		}
	});
}

function setHomePage(url) {
  if (document.all && document.getElementById)
    oHomePage.setHomePage(url);
  else if (document.layers && navigator.javaEnabled()) {
    netscape.security.PrivilegeManager.enablePrivilege
('UniversalPreferencesWrite');
    navigator.preference('browser.startup.homepage', url);
  }
}

function addBookmark(title,url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	} else if( document.all ) {
		window.external.AddFavorite( url, title);
	} else if(window.opera && window.print) {
		return true;
	}
}

function expandtextarea(r, obj) {
	var counter = obj.value.match(/\n/gi);
	if(counter) {
		if(counter.length > r) {
			obj.rows = counter.length+2;
		} else {
			obj.rows = r;
		}
	}
}

function expandtextareas() {
	textareas = document.getElementsByTagName('textarea');
	
	for(i=0;i<textareas.length;i++) {
		eval("Event.observe(textareas[i], \"keyup\", function(e) { element = Event.element(e); expandtextarea("+textareas[i].rows+", element); });");
	}
}

function loadevents() {

	expandtextareas();

	if($('newmsg')) {
		new Effect.Highlight($('newmsg'),{duration: 3.0, transition: Effect.Transitions.wobble});
		window.setTimeout("new Effect.DropOut($('newmsg'))",15000);
	}

	Event.observe($('searchbox'), 'focus', function() {
		$('searchbox').value='';
	});

	Event.observe('body', 'click', function(e) {
		if(Event.element(e).id=="morelinkspan") {
			Event.stop(e);
			$('morelinks').style.display='block';
			if($('moresub')) {
				$('moresub').style.display='none';
			}
			return false;
		} else if(Event.element(e).id=="moresublink") {
			if($('moresub')) {
				$('moresub').style.display='block';
			}
			$('morelinks').style.display='none';
			return false;
		} else {
			$('morelinks').style.display='none';
			if($('moresub')) {
				$('moresub').style.display='none';
			}
			return true;
		}
	});

	if($('regbut')) {
		Event.observe('regbut', 'click', function() {
			location.href='/page.php?id=80';
		});
	}

	if($('msgposts')) {
		forumtabs.each(function(s) {
			Event.observe(s[0],'click', function(e) {
				new Ajax.Request('/ajax/forum.php?f='+s[1], {
					method: 'get',
					onSuccess: function(transport) {
						$('msgposts').update(transport.responseText);
						cleartabs(forumtabs);
						$(s[0]).up().addClassName('selected');
					}
				});
				Event.stop(e);
			});
		});
	}

	if($('profileeditlink')) {
		var obj = {
		  fx: function(e) {
			if(($('statusmsg').innerHTML != $('logusername').innerHTML + " " + $('statusupdate').value) && ($('statusupdate').value!="")) {
				new Ajax.Request('/ajax/statusupdate.php?s='+encodeURIComponent($('statusupdate').value), {
					method: 'get',
					onSuccess: function(transport) {
						$('statustime').update("Updated a moment ago");
						statustext = transport.responseText.split('|');
						$('statusmsg').update(statustext[0]+" "+statustext[1]);
						$('statusupdate').value = statustext[1];
						$('profileeditlink').show();
					}
				});
			}
			$('changestatus').hide();
			Event.stop(e);
		  }
		};

		obj.bfx = obj.fx.bindAsEventListener(obj);

		Event.observe($('profileeditlink'),'click', function(e) {
			$('changestatus').show();
			$('statusupdate').focus();
			$('statusupdate').select();
			Event.observe($('statusupdate'),'blur', obj.bfx);
			Event.observe($('statusform'),'submit', obj.bfx);
			Event.stop(e);
		});

		Event.observe($('statusmsg'),'click', function(e) {
			$('changestatus').show();
			$('statusupdate').focus();
			$('statusupdate').select();
			Event.observe($('statusupdate'),'blur', obj.bfx);
			Event.observe($('statusform'),'submit', obj.bfx);
			Event.stop(e);
		});

		Event.observe($('cancel'),'mousedown', function(e) {
			$('changestatus').hide();
			Event.stopObserving($('statusupdate'),'blur', obj.bfx);
			Event.stopObserving($('statusform'),'submit', obj.bfx);
			Event.stop(e);
		});

		Event.observe($('clearstatus'),'mousedown', function(e) {
			new Ajax.Request('/ajax/statusupdate.php', {
				method: 'get',
				onSuccess: function(transport) {
					$('statustime').update("");
					$('statusmsg').update("Status has been cleared");
					$('statusupdate').value = "";
					$('profileeditlink').hide();
					Event.stopObserving($('statusupdate'),'blur', obj.bfx);
					Event.stopObserving($('statusform'),'submit', obj.bfx);
				}
			});
			$('changestatus').hide();
			Event.stop(e);
		});
	}

	if($('newsarticles')) {
		newstabs.each(function(s) {
			Event.observe(s,'click', function(e) {
				new Ajax.Request('/ajax/news.php?n='+s, {
					method: 'get',
					onSuccess: function(transport) {
						$('newsarticles').update(transport.responseText);
						cleartabs(newstabs);
						$(s).up().addClassName('selected');
					}
				});
				Event.stop(e);
			});
		});
	}

	if($('statshome')) {
		fixtabs.each(function(s) {
			Event.observe(s,'click', function(e) {
				new Ajax.Request('/ajax/stats.php?t='+s, {
					method: 'get',
					onSuccess: function(transport) {
						$('statshome').update(transport.responseText);
						cleartabs(fixtabs);
						$(s).up().addClassName('selected');
					}
				});
				Event.stop(e);
			});
		});

		predicttabs.each(function(s) {
			Event.observe(s,'click', function(e) {
				new Ajax.Request('/ajax/predict.php?t='+s, {
					method: 'get',
					onSuccess: function(transport) {
						$('predicthome').update(transport.responseText);
						cleartabs(predicttabs);
						$(s).up().addClassName('selected');
					}
				});
				Event.stop(e);
			});
		});

		ratingtabs.each(function(s) {
			Event.observe(s,'click', function(e) {
				new Ajax.Request('/ajax/ratings.php?t='+s, {
					method: 'get',
					onSuccess: function(transport) {
						$('ratingshome').update(transport.responseText);
						cleartabs(ratingtabs);
						$(s).up().addClassName('selected');
					}
				});
				Event.stop(e);
			});
		});
	}

	if($$('#right .friendupdates .hideshow').length > 0) {
		Event.observe($$('#right .friendupdates .hideshow')[0],'click', function(e) {
			Event.stop(e);

			if($$('#right .friendupdates')[0].hasClassName('profileclosed')) {
				$('friendupdates').show();
				$$('#right .friendupdates')[0].removeClassName('profileclosed');
				$$('#right .friendupdates .hideshow a')[0].update('Hide');
				SetCookie('friendsboxhide',0,-1);
			} else {
				$('friendupdates').hide();
				$$('#right .friendupdates')[0].addClassName('profileclosed');
				$$('#right .friendupdates .hideshow a')[0].update('Show');
				SetCookie('friendsboxhide',1,365);
			}
		});
	}

	if($('map')) {
		load();
	}

	if($('chatstatusbar')) {
		//getChatMessages();
	}
}

function getChatMessages() {
	new Ajax.Request('/chat/messages.php', {
		method: 'get',
		onSuccess: function(transport) {
			if(transport.responseText) {
				alert(transport.responseText);
			}
			getChatMessages();
		}
	});
}

function changefont(x) {
	if(x == "xsmall") {
		$('body').removeClassName('xbig');
		$('body').addClassName('xsmall');
		SetCookie('font',x,60);
	}
	if(x == "normal") {
		$('body').removeClassName('xsmall');
		$('body').removeClassName('xbig');
		SetCookie('font',x,-1);
	}
	if(x == "xbig") {
		$('body').removeClassName('xsmall');
		$('body').addClassName('xbig');
		SetCookie('font',x,60);
	}
	
	return false;
}

function SetCookie(cookieName,cookieValue,nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}