var the_timeout = false;
var timestamps_timeout = false;
function ajax_start()
{
		var XMLHttpRequestObject = false; 
		if (window.XMLHttpRequest)
		{
			XMLHttpRequestObject = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			XMLHttpRequestObject = new ActiveXObject('Microsoft.XMLHTTP');
		}	
		return XMLHttpRequestObject;
}
function display_ajax_html(page, args, div_id, XMLHttpRequestObject, callback, callback_args, args2)
{
	if(XMLHttpRequestObject)
	{
		var params = args;

		XMLHttpRequestObject.open('POST', '/ajax.php?do='+page, true); 
		XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		XMLHttpRequestObject.onreadystatechange = function() 
			{ 
				if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200 && XMLHttpRequestObject.responseText.substring(0,6) != "<html>")
				{ 
					if(callback)
						callback(XMLHttpRequestObject.responseText, callback_args, args2);
					else
						document.getElementById(div_id).innerHTML = XMLHttpRequestObject.responseText;
				} 
				else if(XMLHttpRequestObject.readyState == 4)
				{
					if(callback)
						callback('Must be logged in', callback_args, args2);
					else
						document.getElementById(div_id).innerHTML = 'Must be logged in';
				}
			} 
		XMLHttpRequestObject.send(params); 
	}
}
function switch_tab(id, type)
{
	if(!type)
		type = 'profile';
	//Ugly, oh well
	if(type == 'profile')
	{
		document.getElementById('profile').style.display = 'none';
		document.getElementById('blog').style.display = 'none';
		document.getElementById('settings').style.display = 'none';
		document.getElementById(id).style.display='block';
		document.getElementById('profile_tab').className = 'unselected';
		document.getElementById('blog_tab').className = 'unselected';
		document.getElementById('settings_tab').className = 'unselected';
		document.getElementById(id+'_tab').className = 'selected';
	}
	else if(type == 'links')
	{
		document.getElementById('view').style.display = 'none';
		document.getElementById('assign').style.display = 'none';
		document.getElementById(id).style.display='block';
		document.getElementById('view_tab').className = 'unselected';
		document.getElementById('assign_tab').className = 'unselected';
		document.getElementById(id+'_tab').className = 'selected';		
	}
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function load_matchup(id)
{
	XMLHttpRequestObject = ajax_start();
	display_ajax_html('grab_matchup', '&id='+id, 'contactArea', XMLHttpRequestObject);

	centerPopup(id);
	var title = "Matchup Details";
	var content = '<div style="width:100%;text-align:center;"><img src="/images/match_load.gif" alt="" /> <br />Loading...</div>';
	loadPopup(title, content);	
}
function edit_pic()
{
	XMLHttpRequestObject = ajax_start();
	display_ajax_html('change_pic', '', 'contactArea', XMLHttpRequestObject);

	centerPopup();
	var title = "Change Your Pic";
	var content = '<div style="width:100%;text-align:center;"><img src="/images/match_load.gif" alt="" /> <br />Loading...</div>';
	loadPopup(title, content);	
}
function delete_stream(id, type)
{
	var answer = confirm("Are you sure you want to delete this message?");
	if(answer)
	{
		XMLHttpRequestObject = ajax_start();
		display_ajax_html('delete_stream', '&id='+id+'&type='+type, '', XMLHttpRequestObject, do_nothing);
		var d = document.getElementById('message_'+id);
		if(d)
			d.parentNode.removeChild(d);
	}
}
//Awesome
function do_nothing()
{
	return false;
}
function add_friend(usrid)
{
	XMLHttpRequestObject = ajax_start();
	display_ajax_html('add_friend', '&usrid='+usrid, 'add_friend', XMLHttpRequestObject);	
}
function view_friends(usrid)
{
	XMLHttpRequestObject = ajax_start();
	display_ajax_html('view_friends', '&usrid='+usrid, 'contactArea', XMLHttpRequestObject);

	centerPopup(false, 'friends');
	var title = "View Friends";
	var content = '<div style="width:100%;text-align:center;"><img src="/images/match_load.gif" alt="" /> <br />Loading...</div>';
	loadPopup(title, content);		
}
function send_message(usrid)
{
	XMLHttpRequestObject = ajax_start();
	display_ajax_html('send_message', '&usrid='+usrid, 'contactArea', XMLHttpRequestObject);

	centerPopup();
	var title = "Send Message";
	var content = '<div style="width:100%;text-align:center;"><img src="/images/match_load.gif" alt="" /> <br />Loading...</div>';
	loadPopup(title, content);		
}
function send_msg()
{
	var to = encodeURIComponent(document.getElementById('to').value);
	var subject = encodeURIComponent(document.getElementById('subject').value);
	var message = encodeURIComponent(document.getElementById('pmessage').value);
	XMLHttpRequestObject = ajax_start();
	display_ajax_html('send_msg', '&to='+to+'&subject='+subject+'&message='+message, 'contactArea', XMLHttpRequestObject);
}
function edit_profile()
{
	XMLHttpRequestObject = ajax_start();
	display_ajax_html('edit_profile', '', 'contactArea', XMLHttpRequestObject);

	centerPopup();
	var title = "Edit Your Profile";
	var content = '<div style="width:100%;text-align:center;"><img src="/images/match_load.gif" alt="" /> <br />Loading...</div>';
	loadPopup(title, content);		
}
function save_profile()
{
	var args = new Array();
	var inputs = new Array('firstname', 'lastname', 'twitter', 'facebook', 'faceplate', 'skype', 'af', 'blurb');
	for(var i in inputs)
	{
		args[inputs[i]] = encodeURIComponent(document.getElementById(inputs[i]).value);
	}
	var arg = '';
	for(var id in args)
	{
		arg += '&'+id+'='+args[id];
	}
	XMLHttpRequestObject = ajax_start();
	display_ajax_html('save_profile', arg, '', XMLHttpRequestObject, update_profile);	
}
function update_profile(json)
{
	//Success, reload
	if(json == 'success')
	{
		window.location.reload();
		document.getElementById('contactArea').innerHTML = "<p>Success! Reloading...</p>";
	}
}
function post_to(usrid)
{
	//If content
	var message = trim(document.getElementById('message').value);
	if(message.length > 0)
	{
		document.getElementById('button').value = 'Posting...';
		document.getElementById('button').disabled = true;
		XMLHttpRequestObject = ajax_start();
		display_ajax_html('post_to', '&usrid='+usrid+'&message='+encodeURIComponent(message), '', XMLHttpRequestObject, add_post);
	}
	else
		document.getElementById('message').value = '';
}
function add_post(json)
{
	//Get rid of filler message
	var d = document.getElementById('filler');
	if(d)
		d.parentNode.removeChild(d);
	document.getElementById('button').value = 'Post';
	document.getElementById('button').disabled = false;
	document.getElementById('message').value = '';
	var json_array = eval('(' + json + ')');
	var message = json_array['message'];
	var id = json_array['id'];
	document.getElementById('stream').innerHTML = message + document.getElementById('stream').innerHTML;
	$('#message_'+id).effect('highlight', {}, 2000);	
}
function show_about()
{
	centerPopup();
	var title = "About the Brackets";
	var content = "<div style='font-weight:400;'><p>ILoveHit's new bracket system works a lot like an NCAA bracket.</p><p>Each day you face a new opponent -- the surfer who surfs the most pages in that day wins.</p><p>The next day you will have a new opponent, and by the end of the week, only one surfer will have won every game. This 1st place user is the winner, and gets 66% of the pot, while 2nd and 3rd place get 25% and 9% respectively. You can tell who is undefeated by the yellow border around their matchup.</p><p>The pot is based on how many pages ILoveHits users as a whole surf that week, the more surfing done, the higher the pot gets. Additionally, the total pot is split among the divisions with the higher divisions (1, 2, 3) getting the most credits in the pot</p><p>The division you are put in is based on the number of pages you surfed in the previous week. There's a whole bunch of seeding involved too, but no need for you to worry about all that.</p></div>";
	loadPopup(title, content);			
}
/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup without jQuery magic!
function loadPopup(title, content, count){
//loads popup only if it is disabled
if(popupStatus==0){
$("#backgroundPopup").css({
	"opacity": "0.7"
});
$('#backgroundPopup').fadeIn("slow");
$('#popupContact').fadeIn("slow");
document.getElementById('popup_title').style.display = 'block';
document.getElementById('contactArea').style.display = 'block';
replace_html(document.getElementById('popup_title'), title);
replace_html(document.getElementById('contactArea'), content);
popupStatus = 1;
}
}
//disabling popup with jQuery magic!
function disablePopup(){
//disables popup only if it is enabled
if(popupStatus==1){
$('#backgroundPopup').fadeOut("slow");
$('#popupContact').fadeOut("slow");
popupStatus = 0;
}
}

//centering popup
function centerPopup(id, id2){
//request data for centering

var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("#popupContact").height();
var popupWidth = $("#popupContact").width();

if(id)
var pos = findPos(document.getElementById('matchup_'+id))
else if(id2)
var pos = findPos(document.getElementById(id2))
else
var pos = 170;
pos = pos-100;
if(pos<0)
pos = windowHeight/2-popupHeight/2;
//centering
$("#popupContact").css({
"position": "absolute",
"top": pos,
"left": windowWidth/2-popupWidth/2
});
//only need force for IE6

$("#backgroundPopup").css({
"height": windowHeight
});

}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

//CLOSING POPUP
//Click the x event!
$("#popupContactClose").click(function(){
disablePopup();
});
//Click out event!
$("#backgroundPopup").click(function(){
disablePopup();
});
//Press Escape event!
$(document).keypress(function(e){
if(e.keyCode==27 && popupStatus==1){
	disablePopup();
}
});
});
function findPos(obj) 
{
var curtop = 0;
if(obj)
{
if (obj.offsetParent) 
{
	do {
		curtop += obj.offsetTop;
	} while (obj = obj.offsetParent);
}
}
return curtop;
}
function findPosX(obj) 
{
var curtop = 0;
if (obj.offsetParent) 
{
do {
	curtop += obj.offsetLeft;
} while (obj = obj.offsetParent);
}
return curtop;
}
function replace_html(el, html) {
if( el ) {
        var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
        //var oldEl = el;
        var newEl = document.createElement(oldEl.nodeName);

        // Preserve any properties we care about (id and class in this example)
        newEl.id = oldEl.id;
        newEl.className = oldEl.className;

        //set the new HTML and insert back into the DOM
        newEl.innerHTML = html;
        if(oldEl.parentNode)
	        oldEl.parentNode.replaceChild(newEl, oldEl);
        else
        oldEl.innerHTML = html;

        //return a reference to the new element in case we need it
        return newEl;
}
};	
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}