/**************************************************

	mLib, by Mark Willis: www.MarkWillis.co.uk
	
	mLib v1.2.1 (beta)
	(C) 2010 Mark Willis of MLW Creative.

**************************************************/

// SETTINGS
var mFloatRoot = 'http://www.mlwgames.com/bioshock2/inc/mLib/floatFiles/'; // Where are the files which you load into mFloat located?
var mSuggestRoot = 'http://www.mlwgames.com/bioshock2/inc/mLib/suggestFiles/'; // Where are the files which power mSuggest located?

/*********************/ 
/* YOU NO EDIT BELOW */

/* Set up vars */
// mFloat & mBubble
var mFloatViewportWidth;
var mFloatViewportHeight;
var MouseX = 0;
var MouseY = 0;
var mBOverit = 0;
// mSlide
var mSlideable = 1;
var mSlideOpen = 0;
var mSlideWindow = 0;

function mAjaxQuery(mTarget, scriptTarget, addQuery)
{
	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(mTarget).innerHTML = ajaxRequest.responseText;
	}
	}
	var query = addQuery.replace(/\[amp\]/g, '&');
	var queryString = scriptTarget + '?time=' + tsec + query;
	ajaxRequest.open("GET", queryString, true);
	ajaxRequest.send(null);
	return false;
}

function mBviewportScan()
{
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;
}
}

function mBTrackingMousePosition(e)
{
	e = e || window.event;
	if (e.pageX)
	{
		MouseX = e.pageX;
		MouseY = e.pageY;
		mBviewportScan();
		if(MouseX + 350 > mFloatViewportWidth){MouseX = MouseX - 300;}
		if(document.getElementById('mBubbleBlock').style.display == "block"){
			document.getElementById('mBubbleBlock').style.left = (MouseX + 10) + "px";
			document.getElementById('mBubbleBlock').style.top = (MouseY + 10) + "px";
		}
	}
	else
	{
		x = window.event.clientX+document.documentElement.scrollLeft;
		y = window.event.clientY+document.documentElement.scrollTop;
		mBviewportScan();
		if(x + 350 > mFloatViewportWidth){x = x - 300;}
		if(document.getElementById('mBubbleBlock').style.display == "block"){
			document.getElementById('mBubbleBlock').style.left = (x + 10) + "px";
			document.getElementById('mBubbleBlock').style.top = (y + 10) + "px";
		}
	}
	
}

// Get things going.
mBviewportScan();
document.onmousemove = mBTrackingMousePosition;

/* 	mBubble 				*/
/* 	Updated: 16th Dec 2009 	*/

function showBubble(bubbleTITLE, bubbleCONTENT)
{
var bubbleCONTENTrend = bubbleCONTENT.replace(/\[br\]/g, '<br />');
document.getElementById('mBubbleBlock').innerHTML = "<div class=\"mBubble-inside\"><b>" + bubbleTITLE + "</b><br />" + bubbleCONTENTrend + "</div>";
document.getElementById('mBubbleBlock').className = 'mBubble-on';
document.getElementById('mBubbleBlock').style.display = "block";
var x = 0; var y = 0;
if (MouseX)
	{
		x = MouseX;
		y = MouseY;
	}
else
	{
		x = window.event.clientX+document.documentElement.scrollLeft;
		y = window.event.clientY+document.documentElement.scrollTop;
	}
if(x + 350 > mFloatViewportWidth){x = x - 300;}
document.getElementById('mBubbleBlock').style.left = (x + 10) + "px";
document.getElementById('mBubbleBlock').style.top = (y + 10) + "px";
mBOverit = 1;
}

function clearBubble(){
document.getElementById('mBubbleBlock').innerHTML = '';
document.getElementById('mBubbleBlock').className = 'mBubble-off';
document.getElementById('mBubbleBlock').style.display = "none";
mBOverit = 0;
}

/*	mFloat					*/
/*	Updated: 16th Dec 2009	*/

function mFloatOpen(useMouse, useY, command, query, keepSize)
{	
	/* Key
		useMouse: Set to 1 if you want floating window to popup at client's mouse location (centered). Set to 0 if you want manual Y Position.
		useY: Manual Y co-ord location of floating window. (Vertical position.)
		command: the file you wish to load.
		query: any variables to pass to the file loading into floating window
		keepSize: keep the window size when loading a new file? Makes transitions between floating windows look nicer.
	*/
	
	var x = 0; var y = 0;
	var oY = useY;
	if(MouseX)
	{
		x = MouseX;
		
		if(useMouse == 1){
		//scroll(0,(MouseY - 110));
		y = MouseY - 100;
		}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;
		}
	}
	
	// Cloak
	var wrapHeight = document.getElementById('mFloatWrap').offsetHeight;
	var cloakHeight = document.documentElement.clientHeight;
	
	if(cloakHeight < wrapHeight)
	{
		cloakHeight = wrapHeight;
	}
	document.getElementById('mFloatCloak').style.height = cloakHeight + "px";
	document.getElementById('mFloatCloak').style.width = mFloatViewportWidth + "px";
	document.getElementById('mFloatCloak').style.display = "block";
	
	
	if(keepSize == 1)
	{
		var goheight = document.getElementById('mFloatInner').clientHeight;
		if(goheight == "")
		{
			goheight = document.getElementById('mFloatInner').offsetHeight;
		}
	}
	
	document.getElementById('mFloatBlock').style.left = ((mFloatViewportWidth / 2) - 440) + "px";
	document.getElementById('mFloatBlock').style.top = (y + 10) + "px";
	document.getElementById('mFloatBlock').innerHTML = '<div class="mFloat-inner" id="mFloatInner"><div class="mFloat-close"><a href="#" onclick="return mFloatClose();"><!-- close --></a></div><div class="mFloat-main"> <div class="mFloatAreaLoading"><!-- --></div> </div></div>';
	
	if(keepSize == 1)
	{
		document.getElementById('mFloatInner').style.height = goheight + "px";
	}
	
	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 + "&pos=" + y;
	ajaxRequest.open("GET", queryString, true);
	ajaxRequest.send(null);
	return false;
}
function mFloatClose()
{
	// Cloak
	document.getElementById('mFloatCloak').style.height = "1px";
	document.getElementById('mFloatCloak').style.width = "1px";
	document.getElementById('mFloatCloak').style.display = "none";
	document.getElementById('mFloatBlock').innerHTML = ''; document.getElementById('mFloatBlock').className = 'mFloatBlock-off'; document.getElementById('mFloatBlock').style.display = "none";
	return false;
}

/*	mSlide					*/
/*	Updated: 16th Dec 2009	*/

function mSlide(mTarget, mStart, mGoal, mIncrement, mReps, mDir, mAjax, mContentLoad)
{
	/* PLEASE CALL THE mSlideToggle FUNCTION (BELOW) IN YOUR HTML */
	
	if(mDir == 1)
	{
		// OPENING
		if((mStart + (mIncrement * mReps)) <= mGoal)
		{
			// Not reached goal yet...
			document.getElementById(mTarget).style.height = (mStart + (mIncrement * mReps)) + "px";
			document.getElementById(mTarget).style.display = "block";
			var st = setTimeout('mSlide(\'' + mTarget + '\', ' + mStart + ', ' + mGoal + ', ' + mIncrement + ', ' + (mReps + 1) + ', 1, \''+ mAjax +'\', \''+ mContentLoad +'\')', 1);
		}else{
			// Reached goal
			if(mAjax == 1){mAjaxQuery(mTarget, mContentLoad, '');}
			mSlideable = 1;
			mSlideOpen = 1;
		}
	}else{
		// CLOSING
		if((mStart - (mIncrement * mReps)) >= mGoal)
		{
			// Not reached goal yet...
			document.getElementById(mTarget).style.height = (mStart - (mIncrement * mReps)) + "px";
			var st = setTimeout('mSlide(\'' + mTarget + '\', ' + mStart + ', ' + mGoal + ', ' + mIncrement + ', ' + (mReps + 1) + ', 2, 0, \'\')', 1);
		}else{
			// Reached goal
			document.getElementById(mTarget).style.display = "none";
			mSlideable = 1;
			mSlideOpen = 0;
		}
	}
	
	return false;
}

function mSlideToggle(slideWindow, slideTarget, slideStartHeight, slideGoalHeight, callAjax, contentLoad)
{
	/* Guide
	
		slideWindow			YOUR ID for the slider's content. This controls multi-windowed slider content. Set to 1 if you are not using AJAX.
		slideTarget			The Element ID that you wish the slide open and close.
		slideStartHeight	The starting height of the element that you wish to open/close.
		slideStartHeight	The goal height of the element that you wish to open/close.
		callAjax			If you wish to use AJAX Method to load data into the sliding area. 0 = No, 1 = Yes.
		contentLoad			Relative URL for the page you wish to load into the sliding area once it opens (given callAjax == 1)
		
	*/
	
	
	if(mSlideable == 1)
	{
	
	if(mSlideWindow == slideWindow || mSlideWindow == 0)
	{
	
	if(mSlideOpen == 0)
	{
		// Open
		// Populate with loader.
		//clearBubble();
		//alert("sliding open...");
		if(callAjax == 1){document.getElementById(slideTarget).innerHTML = '<div class="mSlideAreaLoading"><!-- --></div>';}
		mSlide(slideTarget, slideStartHeight, slideGoalHeight, 10, 0, 1, callAjax, contentLoad);
		mSlideable = 0;
		mSlideWindow = slideWindow;
	}else if(mSlideOpen == 1)
	{
		// Close
		//clearBubble();
		//alert("sliding close...");
		
		mSlide(slideTarget, slideGoalHeight, 0, 10, 0, 2, 0, '');
		mSlideable = 0;
		mSlideWindow = 0;
	}
	
	}else{
		// Bypass sliding, just change content.
		if(callAjax == 1)
		{
			document.getElementById(slideTarget).innerHTML = '<div class="mSlideAreaLoading"><!-- --></div>';
			mAjaxQuery(slideTarget, contentLoad, '');
		}
		mSlideWindow = slideWindow;
	}
	
	
	
	}
		
	return false;
}

/*	mSuggest				*/
/* 	Updated 27th Jan 2010	*/

var boxactive = 0;

function mHtmlspecialchars(p_string)
{
	p_string = p_string.replace(/&/g, '&amp;');
	p_string = p_string.replace(/</g, '&lt;');
	p_string = p_string.replace(/>/g, '&gt;');
	p_string = p_string.replace(/"/g, '&quot;');
	p_string = p_string.replace(/'/g, '&#039;');
	return p_string;
}

function mShowSugBox()
{
	if(document.mSuggestForm.mSearchSuggestion.value == "Search..."){document.mSuggestForm.mSearchSuggestion.value = '';}
	document.getElementById('mSuggestBox').style.display = "block";
	var theWidth = (document.getElementById('mSearchSuggestion').offsetWidth - 2) + 'px';
	document.getElementById('mSuggestBox').style.width = theWidth;
	return false;
}
	
function mHideSugBox()
{
	if(boxactive == 0){
	document.getElementById('mSuggestBox').style.display = "none";}
	return false;
}
	
function mSuggestData(fileLoc)
{
	var userInput = mHtmlspecialchars(document.getElementById('mSearchSuggestion').value);
	mSuggestGo(userInput, fileLoc);
}

function mSuggestGo(userInput, fileLoc)
{
	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){
	var resulten = ajaxRequest.responseText;
	if(resulten == "")
	{
		document.getElementById('mSuggestBox').innerHTML = '<div class="mSuggestContent">No matches found.</div>';
	}else{
		document.getElementById('mSuggestBox').innerHTML = '<div class="mSuggestContent">' + resulten + '</div>';
	}
	}
	}
	var queryString = mSuggestRoot + fileLoc + '?data=' + userInput + '&time=' + tsec;
	ajaxRequest.open("GET", queryString, true);
	ajaxRequest.send(null);
}

function mActivateBox(){boxactive = 1;}
function mDeactivateBox(){boxactive = 0;}

/* mSuggest actions trigger */
function mSearchAction(aid, fileLoc)
{
	switch(aid)
	{
		case 1: mShowSugBox(); break;
		case 2: mHideSugBox(); break;
		case 3: mSuggestData(fileLoc); break;
		case 4: mActivateBox(); break;
		case 5: mDeactivateBox(); break;
	}
}