// WAT

function clearLField(target)
{
	if(target.className == "loginFieldInActive")
	{
		target.value = "";
		target.className = "loginFieldActive";
	}
}

function checkLField(target, which)
{
	if(target.value == "")
	{
		switch(which)
		{
			case 1: var fieldText = "E-Mail Address"; break;
			case 2: var fieldText = "Password"; break;
		}
		target.className = "loginFieldInActive";
		target.value = fieldText;
	}
}

// FACEBOOK CONNECT STUFF BRAH
// <fb:name uid='loggedinuser' capitalize='true' useyou='false' firstnameonly='true' /></fb:name> --- <fb:name uid='loggedinuser' capitalize='true' useyou='false' lastnameonly='true' /></fb:name>

function fbconnect_login()
{
	mFloatOpen(0, 120, 'auth.php', '');
	update_user_box();
	var errorShow = setTimeout('mFloatOpen(0, 120, \'auth.php\', \'amperror=1\')', 15000);
	
	var d = new Date();
	var tsec = d.getSeconds();
	var ajaxRequest;
	try{ajaxRequest = new XMLHttpRequest(); } catch (e){try{ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try{ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");} catch (e){return true;}}}
	ajaxRequest.onreadystatechange = function()
	{
	if(ajaxRequest.readyState == 4){
		if(ajaxRequest.responseText == "1")
		{
			clearTimeout(errorShow);
			refresh_page();
		}
	}
	}
	var queryString = 'http://www.unknowndrunkeninjuries.com/inc/fbcontrol.php' + '?time=' + tsec;
	ajaxRequest.open("GET", queryString, true);
	ajaxRequest.send(null);
}

function update_user_box()
{
	document.getElementById("loginUser").innerHTML = "<span><fb:profile-pic uid='loggedinuser' facebook-logo='true'></fb:profile-pic> LOADING..."; 
	FB.XFBML.Host.parseDomTree();
}

function refresh_page() {
  window.location = 'http://www.unknowndrunkeninjuries.com/';
}
function logout_user() {
  window.location = 'http://www.unknowndrunkeninjuries.com/index.php?action=logout';
}

// MFLOAT BRAH

///////////////////////////////////////////////////
//      mFloat Interface by MarkWillis.co.uk     //
/*************************************************/
var mFloatRoot = 'http://www.unknowndrunkeninjuries.com/inc/floater/';
var mFloatViewportWidth;
var mFloatViewportHeight;
var MouseX = 0; var MouseY = 0;
function TrackingMousePosition(e)
{
	e = e || window.event;
	if (e.pageX)
	{
		MouseX = e.pageX;
		MouseY = e.pageY;
		scanViewport();
		if(MouseX + 230 > mFloatViewportWidth){MouseX = MouseX - 200;}
		
		if(document.getElementById('mFloatBlock').style.display == "block")
		{
			//document.getElementById('mFloatBlock').style.left = (MouseX + 10) + "px";
			//document.getElementById('mFloatBlock').style.top = (MouseY + 25) + "px";
		}
		if(document.getElementById('mwBubble').style.display == "block")
		{
			document.getElementById('mwBubble').style.left = (MouseX + 10) + "px";
			document.getElementById('mwBubble').style.top = (MouseY + 10) + "px";
		}
	}
	else
	{
		x = window.event.clientX+document.documentElement.scrollLeft;
		y = window.event.clientY+document.documentElement.scrollTop;
		scanViewport();
		if(x + 230 > mFloatViewportWidth){x = x - 200;}
		
		if(document.getElementById('mFloatBlock').style.display == "block")
		{
			//document.getElementById('mFloatBlock').style.left = (x + 10) + "px";
			//document.getElementById('mFloatBlock').style.top = (y + 25) + "px";
		}
		if(document.getElementById('mwBubble').style.display == "block")
		{
			document.getElementById('mwBubble').style.left = (x + 10) + "px";
			document.getElementById('mwBubble').style.top = (y + 10) + "px";
		}
	}
}
window.onmouseover = TrackingMousePosition;
window.onmousemove = TrackingMousePosition;
function scanViewport()
{
// 'normal' browsers 
if (typeof window.innerWidth != 'undefined')
{
	mFloatViewportWidth = window.innerWidth;
	mFloatViewportHeight = window.innerHeight;
}
// IE6
else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
{
	mFloatViewportWidth = document.documentElement.clientWidth;
	mFloatViewportHeight = document.documentElement.clientHeight;
}
// even older IE :O
else
{
	mFloatViewportWidth = document.getElementsByTagName('body')[0].clientWidth;
	mFloatViewportHeight = document.getElementsByTagName('body')[0].clientHeight;
}
}
scanViewport();

function mFloatOpen(useMouse, useY, command, query)
{
	// useMouse: set to 1 if you want to have Ian popup at the mouse's location (centered), set to 0 if you want manual Y pos
	// useY: manual y location of Ian popup (vertical position)
	var x = 0; var y = 0;
	var oY = useY;
	if(MouseX)
	{
		x = MouseX;
		
		if(useMouse == 1){
		y = MouseY;
		}else{
		scroll(0,(oY - 10));
		y = oY;
		}
	}
	else
	{
		x = window.event.clientX+document.documentElement.scrollLeft;
		if(useMouse == 1){
		y = window.event.clientY+document.documentElement.scrollTop;
		}else{
		scroll(0,(oY - 10));
		y = oY;
		}
	}
	document.getElementById('mFloatBlock').style.left = ((mFloatViewportWidth / 2) - 300) + "px";
	document.getElementById('mFloatBlock').style.top = (y + 10) + "px";
	document.getElementById('mFloatBlock').innerHTML = '<div class="mFloat-inner" id="mFloatInner"><div class="mFloat-logo"><div class="mFloat-close"><a href="#" onclick="return mFloatClose();"><!-- close --></a></div><div class="mFloat-title">Unknown Drunken Injuries</div><div class="mFloat-main"><h3>loading...</h3></div></div></div>';
	document.getElementById('mFloatBlock').className = 'mFloatBlock-on';
	document.getElementById('mFloatBlock').style.display = "block";
	var d = new Date();
	var tsec = d.getSeconds();
	var ajaxRequest;
	try{ajaxRequest = new XMLHttpRequest(); } catch (e){try{ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try{ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");} catch (e){return true;}}}
	ajaxRequest.onreadystatechange = function()
	{
	if(ajaxRequest.readyState == 4){
		document.getElementById('mFloatBlock').innerHTML = ajaxRequest.responseText;
	}
	}
	var addQuery = query.replace(/amp/g, '&');
	var queryString = mFloatRoot + command + '?time=' + tsec + addQuery;
	ajaxRequest.open("GET", queryString, true);
	ajaxRequest.send(null);
	return false;
}
function mFloatClose()
{
	document.getElementById('mFloatBlock').innerHTML = ''; document.getElementById('mFloatBlock').className = 'aianBlock-off'; document.getElementById('mFloatBlock').style.display = "none";
	return false;
}



// MBUBBLE TOOPTIP BRAH
// mwBubble tooltip
var overit = 0;

function showBubble(bubbleTITLE, bubbleCONTENT)
{
	document.getElementById('mwBubble').innerHTML = "<div class=\"mwBubble-inside\"><b>" + bubbleTITLE + "</b><br />" + bubbleCONTENT + "</div>";
	document.getElementById('mwBubble').className = 'mwBubble-on';
	document.getElementById('mwBubble').style.display = "block";
	var x = 0; var y = 0;
	if(MouseX)
	{
		x = MouseX;
		y = MouseY;
		if(x + 200 > mFloatViewportWidth){x = x - 200;}
	}
	else
	{
		x = window.event.clientX+document.documentElement.scrollLeft;
		y = window.event.clientY+document.documentElement.scrollTop;
		if(x + 200 > mFloatViewportWidth){x = x - 200;}
	}
	document.getElementById('mwBubble').style.left = (x + 5) + "px";
	document.getElementById('mwBubble').style.top = (y + 10) + "px";
	overit = 1;
}

function clearBubble()
{
	document.getElementById('mwBubble').innerHTML = '';
	document.getElementById('mwBubble').className = 'mwBubble-off';
	document.getElementById('mwBubble').style.display = "none";
	overit = 0;
}



//MY NOTEPAD STUFF
function openAddEnt()
{
	
	if(document.getElementById('addEnt2').style.display != "block"){document.getElementById('addEnt2').style.display = "block"; document.getElementById('addEnt1').style.display = "none";}
	else if(document.getElementById('addEnt2').style.display == "block"){document.getElementById('addEnt1').style.display = "block"; document.getElementById('addEnt2').style.display = "none";}
	return false;
}

function processAddEnt(target)
{
	var dasDate = escape(target.entDate.value);
	var dasEvent = escape(target.entEntry.value);
	var dasTimeH = escape(target.entHour.value);
	var dasTimeM = escape(target.entMin.value);
	var dasYear = escape(target.entYear.value);
	var dasMonth = escape(target.entMonth.value);
	var dasDay = escape(target.entDay.value);
	var dasSend = 1;
	document.getElementById('addEvtErr').innerHTML = '';
	if(dasEvent == "")
	{
		document.getElementById('addEvtErr').innerHTML = '<b style="color: #FF0000;">Please enter an event!</b>';
		dasSend = 0;
	}
	
	if(dasSend == 1)
	{
		document.getElementById('addEnt1').style.display = "none";
		document.getElementById('addEnt2').style.display = "none";
		document.getElementById('addEnt3').style.display = "block";
		document.getElementById('addEnt3').innerHTML = '<h2>Saving...</h2>';
		
		// Ajax
		var d = new Date();
		var tsec = d.getSeconds();
		var ajaxRequest;
		try{ajaxRequest = new XMLHttpRequest(); } catch (e){try{ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try{ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");} catch (e){return true;}}}
		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4){
				if(ajaxRequest.responseText == "1")
				{
					// All went according to plan!
					mFloatOpen(0, 200, 'calEntries.php', 'ampd=' + dasDay + 'ampm=' + dasMonth + 'ampy=' + dasYear + '');
				}else{
					// Something went wrong
					document.getElementById('addEnt3').innerHTML = ajaxRequest.responseText;
				}
			}
		}
		var queryString = mFloatRoot + 'addEntry.php?d=' + dasDate + '&e=' + dasEvent + '&h=' + dasTimeH + '&m=' + dasTimeM + '&t=' + tsec;
		ajaxRequest.open("GET", queryString, true);
		ajaxRequest.send(null);		
	}

	return false;
}

function resetAddEnt()
{
	document.getElementById('addEnt1').style.display = "none";
	document.getElementById('addEnt2').style.display = "block";
	document.getElementById('addEnt3').style.display = "none";
	return false;
}