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 sendInvitesReg(){
	if($("#cs_form").val().length > 0) {
		var contacts = $("#contact_list").val();
		var from = $("#cs_form").val();
		$("#cs_friends").hide();
		$("#cs_form_required").html("");
		$("#cs_friends_load").show();
		$("#cs_friends_load").html('<br /><br /><img src="/images/spin-loader.gif" /> Setting up the invites<br /><br />');
		$.ajax({
			url: '/global/send_invites.php',
			type: "POST",
			cache: false,
			data: "from="+from+"&contacts="+contacts,
			success: function(data) {
				$("#contact_list").val("");
				$("#cs_friends_load").html('<br /><br /> Invites are queued for delivery! <a href="Javascript:showHideInvite();">Invite More?</a><br /><br />');
			}
		});
	} else {
		$("#cs_form_required").html("You must put your name in to send invites");
	}
}
function openInviteLoggedIn(){
	$("#inviteContentBox").overlay({ 
		top: '10%', 
		expose: {
			color: 'transparent',
			loadSpeed: 200,
			opacity: 0.5
		},
		closeOnClick: true,
		api: true,
		onLoad: function() {
			$.getScript("https://api.cloudsponge.com/address_books.js",function() {
				csInit({domain_key:"U5U8KX6HS74KCW6AXWZR", textarea_id:'contact_list'});
			});
		}
	}).load();
}

function showHideInvite(){
	$("#cs_friends_load").hide();
	$("#cs_friends").show();
}
function changeLeftDisplay(toWhat)
{
	var availWindows = ['friendsOnline','friendsPlaying'];
	var numWins = availWindows.length;
	var currentWindow, previousWindow, setImage;
	
	if (toWhat == "next")
	{
		for(a=0;a<numWins;a++)
		{
			currentWindow = document.getElementById(availWindows[a]);
			if (currentWindow.style.display == "block")
			{
				if (a == numWins - 1)
				{
					toWhat = availWindows[0];
				} else {
					if (a + 1 < numWins) {
						toWhat = availWindows[a + 1];
					}
				}
				
				break;
			}
		}
	}
	for(a=0;a<numWins;a++)
	{
		currentWindow = document.getElementById(availWindows[a]);
		currentWindow.style.display = "none";
	}
	for(a=0;a<numWins;a++)
	{
		currentWindow = document.getElementById(availWindows[a]);
		if (availWindows[a] == toWhat)
		{
			currentWindow.style.display = "block";
			setImage = availWindows[a];
			setCookie("uhpLeftPref", availWindows[a], 9999);
			
			$("#"+availWindows[a]).html('<center><br /><br /><br /><img src="/images/spin-loader.gif" /><br /><br /><br /></center>');
			$.ajax({
				url: '/global/uhp_module.php?module='+availWindows[a],
				success: function(data) {
					$("#"+availWindows[a]).html(data);
				}
			});
			break;
		} else {
			currentWindow.style.display = "none";
		}
	}
	document.getElementById("leftMenuHeaderImage").src = "/images/" + setImage + ".png";
}
function lookupUser(what)
{
	what.value = "Searching...";
	$.ajax({
		url: '/global/pwnedAjax.php',
		cache: false,
		type: "POST",
		data: "u="+$("#usernameLookup").val()+"&wtd=unlookup",
		success: function(data) {
			$("#usernameReturn").html(data);
			what.value = "Search";
		}
	});
}
function changeLeftDisplayT(toWhat)
{
	var availWindows = ['friendsOnline','friendsPlaying'];
	var numWins = availWindows.length;
	var currentWindow, previousWindow, setImage;
	
	for(a=0;a<numWins;a++)
	{
		currentWindow = document.getElementById(availWindows[a]);
		if (currentWindow.style.display == "none")
		{
			if (a > 0) {
				previousWindow = document.getElementById(availWindows[a-1]);
			} else {
				previousWindow = document.getElementById(availWindows[numWins-1]);
			}

			previousWindow.style.display = "none";
			currentWindow.style.display = "block";
			setImage = availWindows[a];
			setCookie("uhpLeftPref", availWindows[a], 9999);
			
			$("#"+availWindows[a]).html('<center><br /><br /><br /><img src="/images/spin-loader.gif" /><br /><br /><br /></center>');
			$.ajax({
				url: '/global/uhp_module.php?module='+availWindows[a],
				success: function(data) {
					$("#"+availWindows[a]).html(data);
				}
			});
			break;
		}
	}
	document.getElementById("leftMenuHeaderImage").src = "/images/" + setImage + ".png";
}
function changeCenterDisplayP3(toWhat)
{
	var availWindows = ['info','liveFeed','comments'];
	var numWins = availWindows.length;
	var currentWindow, previousWindow, setImage;
	
	for(a=0;a<numWins;a++)
	{
		currentWindow = document.getElementById(availWindows[a]);
		currentWindowButton = document.getElementById(availWindows[a] + "Button");
		if (availWindows[a] != toWhat)
		{
			currentWindow.style.display = "none";
			currentWindowButton.className = "liMenu p3liMenu";
		} else {
			currentWindow.style.display = "block";
			currentWindowButton.className = "liMenu activeLiP3 p3liMenu";
		}
	}
}

function changeCenterDisplay(toWhat)
{
	var availWindows =	[
							'whosPlayingWhat',
							'liveFeed',
							'friendsFeed',
							'hotForumTopics',
							'calendar','glogs',
							'gameShouts',
							'notifications',
							'settings',
							'gameSettings',
							'profileSettings',
							'steamGames',
							'xfireFriends',
							'ps3Games',
							'xboxGames',
							'steamFriends'
						];
	var numWins = availWindows.length;
	var currentWindow, previousWindow, setImage;
	for(a=0;a<numWins;a++)
	{
		currentWindow = $("#"+availWindows[a]);
		currentWindowButton = $("#"+availWindows[a] + "Button");
		
		if (availWindows[a] != toWhat)
		{
			$(currentWindow).empty();
			$(currentWindow).hide();
			$(currentWindowButton).removeClass("liMenu activeLi").addClass("liMenu");
			$("#centerDisplayTitleText").html($("#"+toWhat +"Button > a").prop("title"));
		} else {
			$(currentWindow).show();
			$(currentWindow).empty();
			$(currentWindowButton).removeClass("liMenu activeLi").addClass("liMenu activeLi");
			$("#centerDisplayTitleText").html($("#"+toWhat +"Button > a").prop("title"));
			setCookie("uhpBottomPref", toWhat, 9999);
			$("#"+toWhat).html('<center><br /><br /><br /><img src="/images/spin-loader.gif" /><br /><br /><br /></center>');
			$.ajax({
				url: '/global/uhp_module.php?module='+toWhat,
				success: function(data) {
					$("#"+toWhat).html(data);
					$(document).unbind();
					bindBehaviors();
				}
			});
		}
	}
}

function showConfirmation(title,message)
{
	$("#confirmationBoxTitle").html(title);
	$("#confirmationBoxText").html(message);
	$("#confirmationBox").overlay({ 
		top: '20%', 
		expose: {
			color: 'transparent',
			loadSpeed: 200,
			opacity: 0.5
		},
		closeOnClick: true,
		api: true
	}).load();
	$("#confirmationBox").oneTime(3750,closeConfirmationBox);
	
}

function removeDivById(div){
	$('#'+div).fadeOut(500, function() {
		$('#'+div).remove();
	});
}
function removeDivByClass(div){
	$('.'+div).fadeOut(500, function() {
		$('.'+div).remove();
	});
}

function sEp()
{
	$("#editProfile").slideToggle(/*"slow"*/);
}
function gameShoutToggle()
{
	$("#newGameShout").slideToggle(/*"fast"*/);
}
function setGame(id,type,div) {
	document.getElementById(div).value = "Adding Game...";
	$.ajax({
		type: "POST",
		url: "/global/user_games.php",
		data: "gameid="+id+"&type="+type,
		success: function(msg){
			if(type != "deny") {
				$.jGrowl(msg + "has been added to your profile!", { header: "Game Added To Your Profile" });
				document.getElementById(div).value = "Game Added!";
			} else {
				document.getElementById(div).value = "Won't Be Recommended";
			}
		}
	});
}
function sendMessage(subject,message,id,buttonId){
	buttonId.value = 'Sending..';
	$.ajax({
		url: '/global/pwnedAjax.php',
		cache: false,
		type: "POST",
		data: "toUser="+id+"&subject="+subject+"&message="+message+"&wtd=sendpm",
		success: function(data) {
			showConfirmation("Message Sent", "Your message has been sent.");
			buttonId.value = 'Sent';
			$("#sendmessagebox").overlay({}).close();
		}
	});
}
function sendMessageBox() {
	$("#sendmessagebox").overlay({ 
		top: '20%', 
		expose: {
			color: 'transparent',
			loadSpeed: 200,
			opacity: 0.5
		},
		closeOnClick: true,
		api: true
	}).load();
}
function gameShoutBox() {
	$("#newGameShout").overlay({ 
		top: '10%', 
		expose: {
			color: 'transparent',
			loadSpeed: 200,
			opacity: 0.5
		},
		closeOnClick: true,
		api: true
	}).load();
}
function closeGameShout()
{
	$("#newGameShout").overlay({}).close();
}
function sendGameShout(shout, gameId)
{
	$.ajax({
		url: '/global/pwnedAjax.php',
		cache: false,
		type: "POST",
		data: "gameId="+gameId+"&shout="+shout+"&wtd=gameshout",
		success: function(data) {
			$("#selectedGSGame").val("");
			$("#selectedGSGameDiv").html("No Game Selected");
			showConfirmation("Game Shout Posted", "Your game shout has been posted.");
			$("#newGameShout").overlay({}).close();
		}
	});
}
function manageBlocked() {
	$("#blockedMembersBox").overlay({ 
		top: '20%', 
		expose: {
			color: 'transparent',
			loadSpeed: 200,
			opacity: 0.5
		},
		closeOnClick: true,
		api: true
	}).load();
}
function closeConfirmationBox()
{
	$("#confirmationBox").overlay({}).close();
}

function changeDefaultPic() {
	$("#changeDefaultPic").overlay({ 
		top: '20%', 
		expose: {
			color: 'transparent',
			loadSpeed: 200,
			opacity: 0.5
		},
		closeOnClick: true,
		api: true
	}).load();
	
}

function simpleAdd(str, file) { 
	if (file == "friends") { where = 'addtofriends'; file = "friends"; }
	else if(file == "clan") { where = 'addtoclan';  file = "clan"; }
	else if(file == "block") { where = 'addblock';  file = "block"; } else {file = "";}	
	$.ajax({
		url: "/global/" + file + ".php",
		type: "GET",
		data: "id="+str + "&ms=" + new Date().getTime(),
		success: function(data) {
			showConfirmation("Request Sent", "Your request has been sent.");
		}
	});
}

function simpleAddReg(str,id) { 
	$("#addtofriends"+str).val('Sending Request..');
	$.ajax({
		url: "/global/friends.php",
		type: "POST",
		data: "id=" + str,
		success: function(data) {
			$("#addtofriends"+str).val('Request Sent!');
		}
	});
}

function killNotification() {
	$("#notificationNumber").html("0");
}

var notificationCount = 1;
function loadMoreNotifications() {
	$("#loadMoreNotifications").html('<div id="loadingNotifications"><center><img src="/images/spin-loader.gif" /></center></div>');
	$.ajax({
		url: '/global/uhp_module.php?module=notifications&start='+notificationCount,
		success: function(data) {
			$('#loadMoreNotifications').remove();
			$("#notifications").append(data);
			notificationCount++;
			$("#tooltip_profile").remove();
			$(document).unbind();
			bindBehaviors();
		}
	});
}

var liveFeedCount = 1;
function loadMoreLiveFeeds(lastIdUsed) {
	$("#loadMoreNotifications").html('<div id="loadingNotifications"><center><img src="/images/spin-loader.gif" /></center></div>');
	$.ajax({
		url: '/global/uhp_module.php?module=liveFeed&start='+lastIdUsed,
		success: function(data) {
			$('#loadMoreNotifications').remove();
			$("#liveFeed").append(data);
			liveFeedCount++;
			$("#tooltip_profile").remove();
			$(document).unbind();
			bindBehaviors();
		}
	});
}

var friendsFeedCount = 1;
function loadMoreFriendsFeeds(lastIdUsed) {
	$("#loadMoreNotifications").html('<div id="loadingNotifications"><center><img src="/images/spin-loader.gif" /></center></div>');
	$.ajax({
		url: '/global/uhp_module.php?module=friendsFeed&start='+lastIdUsed,
		success: function(data) {
			$('#loadMoreNotifications').remove();
			$("#friendsFeed").append(data);
			friendsFeedCount++;
			$("#tooltip_profile").remove();
			$(document).unbind();
			bindBehaviors();
		}
	});
}

var personalActivityCount = 1;
function loadMorePersonalFeeds(id) {
	$("#loadMoreNotifications").html('<div id="loadingNotifications"><center><img src="/images/spin-loader.gif" /></center></div>');
	$.ajax({
		url: '/global/uhp_module.php?module=profileActivity&start='+personalActivityCount+'&id='+id,
		success: function(data) {
			$('#loadMoreNotifications').remove();
			$("#personalActivity").append(data);
			personalActivityCount++;
			$(document).unbind();
			bindBehaviors();
		}
	});
}

var gameShoutCount = 1;
function loadMoreGameShouts() {
	$("#loadMoreNotifications").html('<div id="loadingNotifications"><center><img src="/images/spin-loader.gif" /></center></div>');
	$.ajax({
		url: '/global/uhp_module.php?module=gameShouts&start='+gameShoutCount,
		success: function(data) {
			$('#loadMoreNotifications').remove();
			$("#personalActivity").append(data);
			gameShoutCount++;
			$("#tooltip_profile").remove();
			$(document).unbind();
			bindBehaviors();
		}
	});
}
var personalGameShoutCount = 1;
function loadMorePersonalGameShouts(id) {
	$("#loadMoreNotifications").html('<div id="loadingNotifications"><center><img src="/images/spin-loader.gif" /></center></div>');
	$.ajax({
		url: '/global/uhp_module.php?module=profileGameShouts&start='+personalGameShoutCount+'&id='+id,
		success: function(data) {
			$('#loadMoreNotifications').remove();
			$("#personalActivity").append(data);
			personalGameShoutCount++;
			$(document).unbind();
			bindBehaviors();
		}
	});
}
function acceptFR(id,div){
	$("#"+div).html('<div id="loadingNotifications"><center><img src="/images/spin-loader.gif" /></center></div>');
	$.ajax({
		url: '/global/friendrequest.php',
		type: 'POST',
		data: 'id='+id+'&action=accept',
		success: function(data) {
			$('#'+div).remove();
		}
	});
}
function declineFR(id,div){
	$("#"+div).html('<div id="loadingNotifications"><center><img src="/images/spin-loader.gif" /></center></div>');
	$.ajax({
		url: '/global/friendrequest.php',
		type: 'POST',
		data: 'id='+id+'&action=deny',
		success: function(data) {
			$('#'+div).remove();
		}
	});
}

function enterLottery() {
	$.ajax({
		url: '/global/enterlottery.php',
		type: 'POST',
		success: function(data) {
			alert("You have entered the lottery!");
		}
	});
}

function updateStatus() {
	$('#statusUpdateBtn').val("Posting..");
	$.ajax({
		url: '/global/update_status.php',
		type: 'POST',
		data: 'status='+$('#updatestatus').val(),
		success: function(data) {
			showConfirmation("Status Updated", "Your status has been updated.");
			$('#statusUpdateBtn').val("Updated!");
		}
	});
}

function updateStatusHome() {
	$('#statusUpdateBtn').val("Posting..");
	$.ajax({
		url: '/global/update_status.php',
		type: 'POST',
		data: 'status='+$('#frontPageShareLIText').val(),
		success: function(data) {
			showConfirmation("Status Updated", "Your status has been updated.");
			$(".frontPageLIShare").show();
			$("#frontPageShareTextDiv").hide();
		}
	});
}

function updateShare() {
	$('#shareUpdateBtn').val("Sharing..");
	$.ajax({
		url: '/global/update_share.php',
		type: 'POST',
		data: 'status='+$('#share_content').val(),
		success: function(data) {
			showConfirmation("Item Shared", "You've just shared with your friends!");
			$('#shareUpdateBtn').val("Shared!");
		}
	});
}

function expandActivityComment(id) {
	$("#activityCommentBox"+id).toggle();
	if($("#expandTxt"+id).html() == "Show") {
		$("#expandTxt"+id).html("Hide");
		$("#toggleFeedLI"+id).hide();
	} else {
		$("#expandTxt"+id).html("Show");
		$("#toggleFeedLI"+id).show();
	}
}

function expandNestedCommentReply(id) {
	$("#commentAddContainerNested"+id).slideToggle();
}

function addActivityComment(type,id,comment) {
	if($('#caLI'+id).val().length > 2) {
		$("#submitB"+id).val("Adding...");
		$.ajax({
			url: '/global/comment_feed.php',
			type: 'POST',
			data: 'type='+type+'&typeId='+id+'&comment='+comment,
			success: function(data) {
				$('#displayComments'+id).append(data);
				$('#caLI'+id).val("");
				$("#submitB"+id).val("Add Comment");
			}
		});
	}
}

function addComment(type,id,comment,parent){
	if($('#commadd').val().length > 2) {
		$("#submitB"+id).val("Adding...");
		$.ajax({
			url: '/global/comment_add.php',
			type: 'POST',
			data: 'type='+type+'&typeId='+id+'&comment='+comment,
			success: function(data) {
				$(parent).prepend(data);
				$('#commadd').val("");
			}
		});
	}
}
function addNestedComment(type,id,comment,parent){
	if($('#commadd'+id).val().length > 2) {
		$("#submitN"+id).val("Adding...");
		$.ajax({
			url: '/global/comment_nested.php',
			type: 'POST',
			data: 'type='+type+'&typeId='+id+'&comment='+comment,
			success: function(data) {
				$(parent).append(data);
				$('#commadd'+id).val("");
				$("#submitN"+id).val("Add Comment");
				$("#commentAddContainerNested"+id).slideToggle();
				showConfirmation("Comment Posted", "Your comment has been posted.");
			}
		});
	}
}

function destroyComment(id,type,tid,o){
	var makeSure = confirm("Do you really want to delete this comment?");
	if (makeSure) {
		$.ajax({
			url: '/global/remove_comment.php',
			type: 'POST',
			data: 'id='+id+'&type='+type+'&tid='+tid+'&o='+o,
			success: function(data) {
				removeDivById('comment'+id);
				showConfirmation("Comment Deleted", "The comment has been deleted.");
			}
		});
	}
}


function eventTextBox(){
	$("#eventTextBox").overlay({ 
		top: '20%', 
		expose: {
			color: 'transparent',
			loadSpeed: 200,
			opacity: 0.5
		},
		closeOnClick: true,
		api: true
	}).load();
}

function notifySMS(type,type_id,current){
	$.ajax({
		url: '/global/notify_sms.php',
		type: 'POST',
		data: 'phone='+$(".phone-format").val()+'&type='+type+'&type_id='+type_id+'&time='+current+"&remind="+$("#timebefore").val() ,
		success: function(data) {
			showConfirmation("Notification Success", "Your text message notification has been added to the queue");
			return false;
		}
	});
}

function myspaceConnect() {
	window.open("/global/myspace_auth.php","MySpace","menubar=no,width=790,height=500,toolbar=no");
}
function twitterConnect() {
	window.open("/global/twitter_auth.php","Twitter","menubar=no,width=790,height=500,toolbar=no");
}
function twitterDoneRegister() {
	$("#twitterConnectID").html("<strong class=\"colorc7\">Twitter Account Connected</strong>");
}

function facbeookConnect(url) {
	window.open(url,"Facebook","menubar=no,width=1000,height=600,toolbar=no");
}
function facebookDoneRegister() {
	$("#facebookConnectID").html("<strong class=\"colorc7\">Facebook Account Connected</strong>");
}
