
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


//alert( BrowserDetect.browser + ' ' + BrowserDetect.version );

function checkMandatoryFields( element )
{
	var error = 0;
	var message = "";
	
	if( element.name.indexOf( "picture" ) != -1 )
	{
		if( document.getElementById( 'pictureAltTag' ).value.length == 0 )
		{
			message += "Please provide alternative text for the picture. ";
			error = 1;
		}
	}
	else if( element.name.indexOf( "download" ) != -1 )
	{
		if( document.getElementById( 'downloadTitle' ).value.length == 0 )
		{
			message += "Please provide a title for the download. ";
			error = 1;
		}
	}
	
	if( message.length > 0 )
		alert( message );
		
	return( error );
}

function storeMandatoryFields( element, rowToAppendTo )
{
	if( element.name.indexOf( "download" ) != -1 )
	{
		var title = document.createElement( 'input' );
		title.type = 'hidden';
		title.value = document.getElementById( 'downloadTitle' ).value;
		title.name = element.name + '_title';
		
		rowToAppendTo.appendChild( title );
		
		document.getElementById( 'downloadTitle' ).value = "";
	}
	else if( element.name.indexOf( "picture" ) != -1 )
	{
		latestPicture = element.value;
		
		var altTag = document.createElement( 'input' );
		altTag.type = 'hidden';
		altTag.value = document.getElementById( 'pictureAltTag' ).value;
		altTag.name = element.name + '_altTag';
		
		rowToAppendTo.appendChild( altTag );
		
		document.getElementById( 'pictureAltTag' ).value = "";
	}
	else if( element.name.indexOf( "video" ) != -1 )
	{
		var video = element.value;
		
		var videoToStill = document.createElement( 'input' );
		videoToStill.type = 'hidden';
		videoToStill.value = video + "~" + latestPicture;
		videoToStill.name = element.name + '_videodetails';
		
		rowToAppendTo.appendChild( videoToStill );
	}
}

function basename(path, suffix) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ash Searle (http://hexmen.com/blog/)
    // +   improved by: Lincoln Ramsay
    // +   improved by: djmix
    // *     example 1: basename('/www/site/home.htm', '.htm');
    // *     returns 1: 'home'
 
    var b = path.replace(/^.*[\/\\]/g, '');
    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
        b = b.substr(0, b.length-suffix.length);
    }
    return b;
}

function getProjectDetail( absolutePath, webRoot, projectId )
{
	var contents = "";
	
	new Ajax.Request( webRoot + '/includes/GetProjectDetail.php', {
		method: 'get',
		parameters: 'projectId=' + projectId + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport ){
			
			contents = transport.responseText;
			var idxContainer = contents.indexOf( '<div id="container">' );
			idxContainer = transport.responseText.indexOf( '>', idxContainer );
			idxContainer++;
			
			var idxBanner = contents.indexOf( '<div id="banner">' );
			
			var newContainerContents = contents.substring( idxContainer, idxBanner );
			document.getElementById( 'container' ).innerHTML = newContainerContents;
			
			projectResize();
			
			setActiveMenu( 'PROJECTS' );
		}
	});
}

function initNav()
{
	var container = document.getElementById( 'container' );
	var width = parseInt( container.offsetWidth );
	var height = parseInt( container.offsetHeight );
		
	var xMultiple = width / 168;
	var yMultiple = height / 38;
		
	var nav = document.getElementById( "cursor" );
	Drag.init( nav, null, 0, 168, 0, 38 );
	
	nav.onDrag = function( x, y )
	{
		window.scrollTo( ( x * xMultiple ), ( y * yMultiple ) );
	}
}

function updateDisplayOrder()
{
	var list = document.getElementById( 'sections' );
	var items = list.getElementsByTagName( "li" );
	var len = items.length;
		
	var order = new Array();
		
	for( i = 0; i < len; i++ )
	{
		var html = items[i].innerHTML;
		var idxProjectId = html.indexOf( 'projectId=' );
		var idxEqual = html.indexOf( '=', idxProjectId );
		idxEqual++;
		var idxQuote = html.indexOf( '"', idxEqual );
			
		order.push( html.substring( idxEqual, idxQuote ) );
	}
		
	new Ajax.Request( webRoot + '/includes/UpdateDisplayOrder.php', {
		method: 'get',
		parameters: 'order=' + order.join( "," ) + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport ){
		}
	});
}

function deleteFromOrderedList()
{
	var checkBoxGrp = document.getElementsByName( 'delete[]' );
	var len = checkBoxGrp.length;
	
	var idxOfCheck = 0;
	
	for( i = 0; i < len; i++ )
	{
		if( checkBoxGrp[i].checked )
			idxOfCheck = i;
	}
	
	var list = document.getElementById( 'sections' );
	var items = list.getElementsByTagName( "li" );
	
	var html = items[idxOfCheck].innerHTML;
	var idxValue = html.indexOf( 'value' );
	var idxStartQuote = html.indexOf( '"', idxValue );
	idxStartQuote++;
	var idxEndQuote = html.indexOf( '"', idxStartQuote );
	
	var temp = html.substring( idxStartQuote, idxEndQuote );
	
	var array = temp.split( ',' );
	
	var id = array[0];
	var absolutePath = array[1];
	var webRoot = array[2];
	var checkBoxName = array[3];
	var tableNameToColumnName = array[4];
	var thingBeingRemoved = array[5];
	var noDataRows = array[6];
	
	if( deleteFromDatabase( absolutePath, webRoot, checkBoxName, tableNameToColumnName,
		thingBeingRemoved, noDataRows, '-1' ) == '1' )
	{
		document.getElementById( 'listitem' + id ).parentNode.removeChild( document.getElementById( 'listitem' + id ) );
	}
}

function login( absolutePath, webRoot )
{
	var username = document.getElementById( 'username' ).value;
	var password = document.getElementById( 'password' ).value;
	
	new Ajax.Request( webRoot + '/clients/Login.php', {
		method: 'get',
		parameters: 'username=' + username + '&' + 'password=' + password + '&' + 'ajax=1',
  		onSuccess: function( transport ){
			
			response = transport.responseText;
			
			if( response == "" )
				window.location = "http://www.surfacelightspace.com/clients/index.php";
			else
				document.getElementById( 'username' ).parentNode.parentNode.parentNode.parentNode.style.backgroundColor = "red";
		}
	});
}

function getThreadDetail( absolutePath, webRoot, categoryId, activeLink )
{
	if( categoryId == currentCategory )
		return;
	else
		currentCategory = categoryId;
		
	var contents = "";
	
	new Ajax.Request( webRoot + '/includes/GetWeblogDetail.php', {
		method: 'get',
		parameters: 'categoryId=' + categoryId + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport ){
			
			contents = transport.responseText;
			var idxContainer = contents.indexOf( '<div id="container">' );
			idxContainer = transport.responseText.indexOf( '>', idxContainer );
			idxContainer++;
			
			var idxBanner = contents.indexOf( '<div id="banner">' );
			
			var newContainerContents = contents.substring( idxContainer, idxBanner );
			document.getElementById( 'container' ).innerHTML = newContainerContents;
			
			resize();
			
			setActiveMenu( 'WEBLOG' );
		}
	});
}

function resize()
{
	var container = document.getElementById( 'container' );
	var items = container.getElementsByTagName( 'div' );
	
	// Width is 210, 200 + 10px spacer.
	var width = 210;
	var total = 0;
	
	// Divide number of divs by two to take into account the blue spacer graphics
	// which should not be treated as 210px columns.
	var len = items.length / 2;
	
	for( i = 0; i < len; i++ )
		total = total + width;

	container.style.width = total + 'px';
}

function projectResize()
{
	var container = document.getElementById( 'container' );
	var items = container.getElementsByTagName( 'div' );
	
	// Width is 210, 200 + 10px spacer.
	var width = 400;
	var total = 0;
	
	// Divide number of divs by two to take into account the blue spacer graphics
	// which should not be treated as 210px columns.
	var len = items.length;
	
	for( i = 0; i < len; i++ )
		total = total + width;
	
	//container.style.width = total + 'px';
}

function forgottenPassword( absolutePath, webRoot )
{
	var username = document.getElementById( 'username' ).value;
	
	new Ajax.Request( webRoot + '/clients/ForgottenPassword.php', {
		method: 'get',
		parameters: 'username=' + username,
  		onSuccess: function( transport ){
			
			alert( transport.responseText );
		}
	});
}

function sendEmail( absolutePath, webRoot )
{
	var email = "";
	var subject = "";
	var message = "";
	
	var inputs = document.getElementsByTagName( 'input' );
	
	for( i = 0; i < inputs.length; i++ )
	{
		if( inputs[i].id == "email" && inputs[i].value != "" )
			email = inputs[i].value;
		
		if( inputs[i].id == "subject" && inputs[i].value != "" )
			subject = inputs[i].value;
	}
	
	var textareas = document.getElementsByTagName( 'textarea' );
	
	for( i = 0; i < textareas.length; i++ )
	{
		if( textareas[i].id == "message" && textareas[i].value != "" )
			message = textareas[i].value;
	}
	
	new Ajax.Request( webRoot + '/includes/SendEmail.php', {
		method: 'get',
		parameters: 'email=' + email + '&' + 'subject=' + subject + '&' + 'message=' + message,
  		onSuccess: function( transport )
		{
			
			message = '<div id="enquiry-form"><br /><br /><br /><br /><br /><p>Thank you, your email has been sent</p></div>';
				
			document.getElementById( 'shadowbox_content' ).innerHTML = message;
		}
	});
}

function openShadowbox( type, filename )
{
	Shadowbox.init();
	
	Shadowbox.open({
        type:       type,
        content:    filename
    });
}

/*
 * Use Ajax to remove a database entry after double checking with the user. checkBoxName is used to identify the
 * checkbox (along with rowReference) if the user cancels the delete and in which case the checkbox should be unchecked.
 * tableNameToColumn is used to identify the table and the table row, thingBeingRemoved is used to customise the confirm 
 * message and noDataRows is used to make certain rows visible or invisible when there are no rows left in tableName to delete.
 */

function deleteFromDatabase( absolutePath, webRoot, checkBoxName, tableNameToColumnName, thingBeingRemoved, 
	noDataRows, headingRow )
{	
	var success = 0;
	
	// If the row should be deleted.
	if( confirm( "Are you sure you want to delete this " + thingBeingRemoved + "?" ))
	{
		// Using Ajax to delete the row from the database.
		var currentPage = window.location;
		
		// Using Ajax to delete the row from the database.
		new Ajax.Request( webRoot + '/includes/DeleteFromDatabase.php', {
			method: 'get',
			parameters: 'tableNameToColumnName=' + tableNameToColumnName + '&' + 'absolutePath=' + absolutePath + 
				'&' + 'currentPage=' + currentPage,
  			onSuccess: function( transport )
			{
				var row = document.getElementById( headingRow );
				var start = row.rowIndex;
				
				var table = row.parentNode.parentNode;
	
				var rows = table.rows;
				var len = rows.length;
				var end = -1;
	
				for( i = start; i < len; i++ )
				{
					if( rows[i].innerHTML.indexOf( '<td>&nbsp;</td>' ) != -1 && end == -1 )
					{
						end = i;
						end++;
					}
				}
				
				if( end == -1 )
					end = len;
				
				for( i = start; i < end; i++ )
				{
					// Delete the table row from the HTML document.
					document.getElementById( 'contentRows' ).deleteRow( start );
					
					// Decrement the value of the variable that indicates how many rows are left in the table.
					numRows--;
				}
				
				if( numRows == 0 )
				{
					// If no rows are left retrieve the name of the row to update and an integer value to 
					// determine whether the row should be made visible or invisible.
					var rows = noDataRows.split( ',' );
					
					for( i = 0; i < rows.length; i++ )
					{
						var rowNameToStatus = rows[i].split( '~' );
						
						if( rowNameToStatus[1] == "0" )
							document.getElementById( rowNameToStatus[0] ).style.display = "none";
						else
							document.getElementById( rowNameToStatus[0] ).style.display = "";
					}
				}
			}
		});
	}
	else
	{
		var checkBoxGrp = document.getElementsByName( checkBoxName );
		var len = checkBoxGrp.length;
		
		for( i = 0; i < len; i++ )
		{
			if( checkBoxGrp[i].checked )
				checkBoxGrp[i].checked = false;
		}
	}
	
	return( 1 );
}

function getNumRows()
{
	var contents = document.body.innerHTML.toLowerCase();
	
	var idxStartTable = contents.lastIndexOf( '<table' );
	var idxEndTable = contents.indexOf( '</table>', idxStartTable );
	contents = contents.substring( idxStartTable, idxEndTable );
	
	var currentIdx = 0;
	var numRows = 0;
	
	var idxDelete = contents.indexOf( 'deletefromdatabase', currentIdx );
	
	while( idxDelete != -1 )
	{
		numRows++;
		currentIdx = idxDelete;
		currentIdx++;
		idxDelete = contents.indexOf( 'deletefromdatabase', currentIdx );
	}
	
	currentIdx = 0;
	
	var idxBlank = contents.indexOf( '<td>&nbsp;</td>', currentIdx );
	
	while( idxBlank != -1 )
	{
		numRows++;
		currentIdx = idxBlank;
		currentIdx++;
		idxBlank = contents.indexOf( '<td>&nbsp;</td>', currentIdx );
	}
	
	return( numRows );
}

function newLink( cmsImages )
{
	// Get the URL entered by the user.
	var url = document.getElementById( 'url' ).value;
	
	if( url.length == 0 )
	{
		alert( "Please provide the web address for the link" );
		return;
	}
	
	// How many links need to be added when the form is submitted?
	var images = document.getElementById( 'linksToBeAdded' ).parentNode.getElementsByTagName( 'img' );
	var len = images.length;
	
	var newLinksCount = 0;
	
	for( i = 0; i < len; i++ )
	{
		// We are only interested in delete images that belong to rows related to headingRow
		if( images[i].id.indexOf( 'newLinkDeleteImage' ) != -1 )
			newLinksCount++;
	}
	
	// A table row should be created for each new link following a heading row.
	var row = document.getElementById( 'linksToBeAdded' );
	var rowIndex = row.rowIndex;
	rowIndex++;
		
	var table = row.parentNode.parentNode;
	
	// Create new row immediately after heading row.
	var row = table.insertRow( rowIndex );
	var rowIdName = 'new_link_' + newLinksCount;
	row.setAttribute( 'id', rowIdName );
	
	// Display the URL of the link in the first column of the new row.
	var column1 = row.insertCell( 0 );
	column1.setAttribute( 'width', '129' );
	var span = column1.appendChild( document.createElement( 'span' ) );
	span.className = 'green';
	span.appendChild( document.createTextNode( url ) );
	column1.appendChild( span );
	
	// The above lines simply display the fact that a link has been added with a URL of ...
	// In order for the link to be added to the thread so that it appears on the website
	// it must be stored in an HTML input element. Data stored in input elements is available
	// to PHP when the thread changes are saved. The above data is not.
	var inputLink = document.createElement( 'input' );
	inputLink.setAttribute( 'type', 'hidden' );
	inputLink.setAttribute( 'id', 'new_link_url' + newLinksCount );
	inputLink.setAttribute( 'name', 'new_link_url' + newLinksCount );
	inputLink.setAttribute( 'value', url );
	column1.appendChild( inputLink );
		
	// The second column of the row contains a delete button that when clicked removes the
	// row. The delete button comprises an image, a link to change the image (rollover) and a link
	// to perform the actual row deletion.
	var column2 = row.insertCell( 1 );
	column2.setAttribute( 'width', '341' );
		
	// Create image element that will act as a button.
	var new_row_button = document.createElement( 'img' );
	new_row_button.setAttribute( 'src', cmsImages + '/ButtonDeleteOut.png' );
	new_row_button.setAttribute( 'width', '45' );
	new_row_button.setAttribute( 'height', '15' );
	new_row_button.setAttribute( 'border', '0' );
	
	// Give each button a unique id/name so we know which delete button was clicked when a row is deleted.
	var deleteButtonIdName = 'newLinkDeleteImage' + newLinksCount;
	new_row_button.setAttribute( 'id', deleteButtonIdName );
	new_row_button.setAttribute( 'name', deleteButtonIdName );
	
	// Onmouseover and onmouseout events change the button image appropriately to show whether it is active.
	// On click of the button use the deleteTableRow Javascript function to remove the link from the list of
	// links to be added.
	var button_link = document.createElement( 'a' );
	button_link.className = "rollover";
	button_link.onclick = function() { deleteTableRow( 'linksToBeAdded', rowIdName, 'newLinkDeleteImage' ); };
	
	// The links should surround the image of the button.
	button_link.appendChild( new_row_button );
	// Add link and image to row column.
	column2.appendChild( button_link );
	
	// Add new columns to new row.
	row.appendChild( column1 );
	row.appendChild( column2 );
		
	// Should row heading.
	document.getElementById( 'linksToBeAdded' ).style.display = "";
	
	// Update the list of event listeners for rollover images.
	setupRollovers();
		
	// If this is the first link being added then add a blank row to separate the link data from the rest
	// of the form.
	
	if( newLinksCount == 0 )
	{
		rowIndex++;
		
		var blankRow = document.getElementById( 'linksToBeAdded' ).parentNode.insertRow( rowIndex );
		var firstColumn = blankRow.insertCell(0);
		var secondColumn = blankRow.insertCell(1);
		firstColumn.nodeValue = "&nbsp;";
		secondColumn.nodeValue = "&nbsp;";
	}
	
	// Clear URL text box to make it quicker to add another link.
	document.getElementById( 'url' ).value = "";
}

/*
 * Add a comment entered by Surface Light Space or a user to the database and immediately show the comment on the page.
 */
 
function addComment( absolutePath, webRoot, articleId, colour, date, fullName, username )
{
	// Get the comment entered by the user.
	var comment = document.getElementById( 'newcomment' ).value;
	
	if( comment.length == 0 )
	{
		alert( "Please type a comment into the comment textbox before clicking the 'LEAVE COMMENT' button" );
		return;
	}
	
	var radioGrp = document.getElementsByName( 'email' );
	
	var email;
	
	// Identify whether emails should be sent or not following the leaving of a comment.
	for( var i = 0; i < radioGrp.length; i++ )
	{	
		if( radioGrp[i].checked )
			email = radioGrp[i].value;
	}
	
	// Use Ajax to update the database now.
	new Ajax.Request( webRoot + '/clients/AddComment.php', {
		method: 'get',
		parameters: 'comment=' + comment + '&' + 'articleId=' + articleId + '&' + 'username=' + username +
			'&' + 'email=' + email + '&' + 'absolutePath=' + absolutePath + '&' + 'fullName=' + fullName,
  		onSuccess: function( transport ){
			
			var ul = document.getElementById( 'comment-list' );
			var currentLIs = ul.getElementsByTagName( 'li' );
			var firstNode = currentLIs[0];
			
			var li = document.createElement( 'li' );
			
			var span = document.createElement( 'span' );
			span.setAttribute( 'color', colour );
			span.appendChild( document.createTextNode( date ) );
			span.appendChild( document.createElement( 'br' ) );
			span.appendChild( document.createTextNode( username ) );
			
			var p = document.createElement( 'p' );
			p.innerHTML = comment;
			
			li.appendChild( span );
			li.appendChild( p );
			ul.insertBefore( li, firstNode );
			
			// Clear the comment textbox so that another one can be provided.
			document.getElementById( 'newcomment' ).value = "";
		}
	});
}

/*
 * When deleting a row from a table it is sufficient just to have the id or name of the table row since
 * the table it belongs to can be found using parentNode. The removeChild function can then be used to
 * remove the table row from the table element. However, when adding rows it is necessary to identify
 * where in the table the rows should be inserted. headingRow does just this. It also explains what
 * data follows and therefore should be displayed if data exists and not if it doesn't. Once a row is
 * removed a count of the number of rows is computed and if this is zero the appendTo/header row is no
 * longer displayed. It can easily be displayed again if new data is added. The third argument to this
 * function is used to determine how many rows and left and when the header row should no longer be displayed.
 */
 
function deleteTableRow( headingRow, rowToDelete, deleteImage )
{
	var table = document.getElementById( rowToDelete ).parentNode;
	
	// Remove the row chosen by the user by navigating to the parent of the table row, the table, and then
	// using the removeChild function.
	table.removeChild( document.getElementById( rowToDelete ) );
	
	// Calculate how many rows are left following the headingRow. Each row has a delete button which
	// is a unique id but whose id has a common part for all rows belonging to the append to row.
	
	// Initially find all images on the page.
	var images = table.getElementsByTagName( 'img' );
	var len = images.length;
	
	var count = 0;
	
	for( i = 0; i < len; i++ )
	{
		// We are only interested in delete images that belong to rows related to headingRow
		if( images[i].id.indexOf( deleteImage ) != -1 )
			count++;
	}
	
	// If there aren't any rows hide the headingRow row so that we are not describing data that doesn't
	// exist.
	if( count == 0 )
	{
		var row = document.getElementById( headingRow );
		
		var rowIndex = row.rowIndex;
		rowIndex++;
		
		table.deleteRow( rowIndex );
		
		row.style.display = "none";
	}
}

/*
 * Delete a picture, download or link given the parent div to which each div is appended.
 */
 
function deleteDiv( divIdToRemove, headingRow )
{
	var ul = document.getElementById( divIdToRemove ).parentNode.parentNode;
	var li = document.getElementById( divIdToRemove ).parentNode;
	
	ul.removeChild( li );
	
	var listItems = ul.getElementsByTagName( 'li' );
	
	if( listItems.length == 0 )
	{
		var row = document.getElementById( headingRow );
		var table = row.parentNode;
		
		var rowIndex = row.rowIndex;
		table.deleteRow( rowIndex );
		table.deleteRow( rowIndex );
		table.deleteRow( rowIndex );
	}
}

function buildMenu( absolutePath, webRoot, variableName, variableValue, currentPage, pageTotal, numRowsToRetrieve,
	nextReference )
{
	var previousId = "";
	var nextId = "";
	
	if( nextReference.indexOf( 'next' ) != -1 )
	{
		currentPage++;
		variableValue = parseInt( variableValue ) + parseInt( numRowsToRetrieve );
		
		previousId = nextReference.replace(/next/,"prev");
		nextId = nextReference;
	}
	else
	{
		currentPage--;
		variableValue = parseInt( variableValue ) - parseInt( numRowsToRetrieve );
		
		previousId = nextReference;
		nextId = nextReference.replace(/prev/,"next");
	}
	
	new Ajax.Request( webRoot + '/includes/BuildNavigationMenu.php', {
		method: 'get',
		parameters: variableName + '=' + variableValue + '&' + 'ajax=1' + '&' + 'absolutePath=' + absolutePath,
  		onSuccess: function( transport )
		{
			var response = transport.responseText;
			
			var className = "";
			
			if( currentPage != 1 && currentPage != pageTotal )
				className = "both";
			
			if( currentPage != 1 )
			{
				var previousPage = currentPage;
				previousPage--;
				
				var previousClass = "previousOnly";
				
				if( className == "both" )
					previousClass = "previous";
					
				response += "<a class=" + '"' + previousClass + '"';
				
				response += "\" id=\"" + previousId + "\" href=\"javascript:buildMenu('" + absolutePath + 
					"','" + webRoot + "','" + variableName + "','" + variableValue + "','" + currentPage +
					"','" + pageTotal + "','" + numRowsToRetrieve + "','" + previousId + "')" + '" ' + 
					"target=\"_self\"></a>";
			}
			
			if( currentPage != pageTotal )
			{	
				var nextClass = "nextOnly";
				
				if( className == "both" )
					nextClass = "next";
					
				response += "<a class=" + '"' + nextClass + '"';
					
				response += "\" id=\"" + nextId + "\" href=\"javascript:buildMenu('" + absolutePath + 
					"','" + webRoot + "','" + variableName + "','" + variableValue + "','" + currentPage +
					"','" + pageTotal + "','" + numRowsToRetrieve + "','" + nextId + "')" + '" ' + 
					"target=\"_self\"></a>";
			}
			
			var container = document.getElementById( 'menu' );
			var divs = container.getElementsByTagName( 'div' );
			var len = divs.length;
			
			for( i = 0; i < len; i++ )
			{
				if( divs[i].className == "content" && divs[i].style.visibility == "visible" )
					divs[i].innerHTML = response;
			}
		}
	});
}
