/* used to display table finder results */

function showPage( page, urlStr, onFirstPage, design )
{

	$("div#extra-info").hide();

	$("div#loader").show();

	if( ! urlStr )
	{
		urlStr = '';

		$("#table-finder select").each(function() {
			if( $(this).val() )
			{
				urlStr += '&' + $(this).attr('name') + '=' + $(this).val().replace( '+', '%2b' );
			}
		});

		if( ! page )
			page =  $("input[name=page]").val();

		urlStr += '&page' + '=' +  page;

		urlStr += '&sort' + '=' +  $("input[name=sort]").val();

		urlStr += '&advanced' + '=' +  $("input[name=advanced]").val();
	}

	urlStr += '&design=' + design;

	//$("div#table-finder-content").fadeTo("slow", 0.01);


	ajaxUrl = '/ajax/table-finder/search';

	if ( onFirstPage == 'tool')
	{
		ajaxUrl += '-tool';
	}
	else if( onFirstPage )
	{
		ajaxUrl += '-fp';
	}

	$.get( ajaxUrl + "?" + urlStr, function( data ){
		$("div#table-finder-content").html( data );
		//$("div#table-finder-content").fadeIn("slow");
		$("div#loader").hide();

		$("th").mouseover(function() {

			oldTitle = '';

			if( $(this).attr('title') )
			{

				$('<div id="extra-info"></div>').appendTo('body').hide();

				$("div#extra-info").css( "min-height", "30px" );
				$("div#extra-info").css( "height", "30px" );
				$("div#extra-info").css( "top", (mouseY + 5) + "px" );
				$("div#extra-info").css( "left", (mouseX - 5) + "px" );
				$("div#extra-info").show();
				$("div#extra-info").html(  $(this).attr('title') );

				oldTitle =  $(this).attr('title');

				$(this).attr('title', '' );
			}

		});

		$("th").mouseout(function() {
			if( typeof(oldTitle) != 'undefined' && oldTitle )
			{
				$(this).attr( 'title', oldTitle );
				$("div#extra-info").hide();
			}

		});


		$("th").mousemove(function() {
			$("div#extra-info").css( "top", (mouseY + 5) + "px" );
			$("div#extra-info").css( "left", (mouseX + 15) + "px" );
		});

	});

	return false;
}

function tableInfo( contentId, tableData, isTool )
{
	if($.browser.msie)
	{
		$('#tablefinder-page #content select').hide();
	}

	if(document.getElementById('table-finder-info'))
	{
		$('#table-finder-info').remove();
	}

	$('<div id="table-finder-info" class="stone-box">Loading...</div>').appendTo('body').hide();

	$("div#loader").show();

	ajaxUrl = '/ajax/table-finder/info';

	if( isTool )
	{
		ajaxUrl += '-tool';
	}

	$.get(ajaxUrl + "?content_id=" + contentId + "&table_data=" + tableData, function( data ){

	if( isTool )
	{
		$("#table-finder-info").html( data ).fadeIn("slow");
	}
	else
	{
		$("#table-finder-info").html( data ).show().center();
	}

		$('div#loader').hide();

		$('a.table-finder-info-close').click(function()
		{
			$("#table-finder-info").remove();

			if($.browser.msie)
			{
				$('#tablefinder-page #content select').show();
			}

			return false;
		});
	});

	return false;
}

function nextPage()
{
	page = parseInt( $("input[name=page]").val() );
	return showPage( page + 1 );
}

function prevPage()
{
	page = parseInt( $("input[name=page]").val() );
	return showPage( page - 1 );
}

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

document.onmousemove = getMouseXY;

// Variables to hold mouse x-y pos.s
var mouseX = 0
var mouseY = 0
document.onmousemove = getMouseXY;
// Main function to retrieve mouse x-y pos.s

function getMouseXY(e)
{
  if( IE && document.documentElement && document.documentElement.scrollTop ) // Explorer 6 Strict
  {
    mouseX = event.clientX + document.documentElement.scrollLeft;
    mouseY = event.clientY + document.documentElement.scrollTop;
  }
  else if( IE ) // grab the x-y pos.s if browser is IE
  {
    mouseX = event.clientX + document.body.scrollLeft;
    mouseY = event.clientY + document.body.scrollTop;
  }
  else  // grab the x-y pos.s if browser is NS
  {
  	mouseX = e.pageX;
    mouseY = e.pageY;
  }
  // catch possible negative values in NS4
  if (mouseX < 0){mouseX = 0;}
  if (mouseY < 0){mouseY = 0;}

  return true;
}