

	/// functions and run-time setup methods
	/// for "live"-tabbed interfaces
	function hideTabAndTable(classToToggle, activeTabClass)
	{
		// hide anybody's that isn't mine
		// and make sure thir <li> is not an "activeGrid"
		$('.' + classToToggle).each(
			function()
			{
				$('#' + $(this).attr('ref')).hide();
				$(this).parent().removeClass(activeTabClass);
			}
		);
	}
	
	function showTabAndTable(objToToggle, activeTabClass)
	{
		if(typeof(objToToggle) == "undefined")
		{
			// nothing going on here.
			return;
		}
		// make me active now
		$(objToToggle).parent().addClass(activeTabClass);
		
		var oToToggle = $(objToToggle).attr('ref');
		var methodToCall = $(objToToggle)[0].onTabDisplay;
		
		if(oToToggle)
		{
			//$('#' + oToToggle).slideToggle('fast');
			$('#' + oToToggle).toggle();
		}
		
		if(typeof(methodToCall) == "function")
		{
			methodToCall(objToToggle);
		}
		//if(typeof(methodToCall))
	}
	
	function setupTabControls(classToToggle, activeTabClass)
	{
		// insert a folded look above these
		// $('.togglableGrid').before('^ -- click on the above header to expand your view -- ^');
		// $('.togglableGrid').hide();
		
		$('.' + classToToggle).css('cursor', 'pointer');
		
		// hide any instances of this class where i don't have any values in the ref table
		$('.' + classToToggle).each(
			function()
			{
				var objDiv = $(this).attr('ref');
				
				if($('#' + objDiv).length == 0)
				{
					$(this).hide();
				}
			}
		);
		
		$('.' + classToToggle).click(
			function()
			{
				hideTabAndTable(classToToggle, activeTabClass);
				showTabAndTable(this, activeTabClass);
				
				return false;
			}
		);
	}
	
	function processTabControls(classToToggle, activeTabClass)
	{
		// only run these if we have at least one instance of this class on the page
		if($('.' + classToToggle).length > 0)
		{
			setupTabControls(classToToggle, activeTabClass);
			hideTabAndTable(classToToggle, activeTabClass);
			// active the first, if there is one
			showTabAndTable($('.' + classToToggle + ':visible')[0], activeTabClass);
		}
	}

	function reloadCounties( stateObj )
	{
		if(!stateObj)
		{
			stateObj = $('#state');
		}
		
		if(!stateObj)
		{
			alert('no state object, failing.');
			return;
		}
		
		// get our object here
		var stateid = $(stateObj).val();
		var parent = $(stateObj).parents('fieldset').slice(0,1);

		if(stateid == 0)
		{
			return;
		}

		$(parent).block('Loading...');

		$('.countySelect').each(
			function()
			{
				//alert(($(this))[0].options[0].text);
				$(this).attr('metaOldTitle', ($(this))[0].options[0].text);
			}
		)

		// clear out
		$('.countySelect').removeOption(/./);
		$('.countySelect').addOption(0, "Loading counties...");

		var url = '/util/json_getCounties/stateid/' + stateid;
		
		if($(stateObj).is('.allowallstates'))
		{
			url += '/showall/1';
		}
		
		$.getJSON(
			url,
			function(data)
			{
				$('.countySelect').each(
					function()
					{
						$(this).addOption(0, $(this).attr('metaOldTitle'), true);
					}
				)
				
				// $('.countySelect').addOption(0, "-- Select your county --", true);
				$('.countySelect').addOption( data.counties, false );

				$(parent).unblock();
			}
		);
	}

	// these are all things we want to run
	// last on the page
	function handleSortLinkClick(obj)
	{
		var myId = $(obj).attr('id');
		
		// find my first parent div (outter div)
		var parentDiv = $(obj).parents('[class*=inplaceAjaxSortable]').slice(0,1);
		
		// the table that holds us (and holds other key information)
		var parentTable = $(obj).parents('[class*=inplaceAjaxSorter]').slice(0,1);
		
		var requestPage = $(parentTable).attr('ajaxrequesturl');
		var curSortDirection = $(parentTable).attr('cursortdir');
		var curSortCol = $(parentTable).attr('cursortcol');
		var curPage = $(parentTable).attr('curpage');
		
		var newSortBy = $(obj).attr('rel') ? $(obj).attr('rel') : curSortCol;
		var newPage = $(obj).attr('newpage') ? $(obj).attr('newpage') : curPage;
		var hasNewPage = $(obj).attr('newpage') ? true : false;
		var newSortDirection = curSortDirection == 'asc' ? 'desc' : 'asc';
		
		// special sorting logic here
		if(hasNewPage)
		{
			newSortDirection = curSortDirection;
		}
//		
//		alert(newSortBy);
//		alert(newPage);
//		alert(newSortDirection);
		
		// set the drop down box (if one exists) to this value now
		$('select[class*=sorter]').find('option').filter('[sortlink=' + myId + ']').attr('selected', 'true');
		
//		alert('newrequest');
		
		var blockContent = 'Reloading... <br /> <img src="/images/skin01/ajax-loader.gif" border="0">';
		$(parentDiv).block(blockContent);
		$.ajax(
			{
				type	: 'POST',
				url		: requestPage,
				dataType	: 'json',
//				dataType	: 'html',
				data	: {
					'sortBy' : newSortBy,
					'sortDirection'	: newSortDirection,
					'sortPage': newPage
				},
				success	:
					function(data)
					{
//						alert(data);
//						alert(data.content);
						$(parentDiv).html(data.content);
						$(parentDiv).block(blockContent);
						$(parentDiv).unblock({ fadeOut: 1, fadeTime: 1000 });
						setupDataGridScripts();
					},
				error	:
					function(data, err)
					{
						alert('Error processing request.');
						// DumperPopup(data);
						// alert(err);
						$(parentDiv).unblock();
					}
			}
		);
	}

	function setupDataGridLinks( )
	{
		var v = $(this).attr('rel');

		if(typeof(v) != 'undefined')
		{
			if(v.length > 0)
			{
				// only do this to the empty td's, not the ones with links
				// UNLESS they have the 'forceLinkTrue' class as well?
				$(this).find('td')
				.filter(
					function()
					{
						if($(this).parent().hasClass('forceLinkTrue') // tr-level
							|| $(this).parent().parent().hasClass('forceLinkTrue')) // table level
						{
							return true;
						}
						
						// this will only return true if there aren't any <a> elements
						return $(this).find('a').length > 0 ? false : true
					}
				).unbind('click').click(
					function( )
					{
						location.href = v;
					}
				);
			}
		}
	}
	
	function setupAutoLink()
	{
		var v = $(this).attr('rel');
		
		// override link?
		var aOverride = $(this).find('a');
		if(aOverride)
		{
			v = $(aOverride).attr('href');
		}

		if(typeof(v) != 'undefined')
		{
			$(this).unbind('click').click(
				function( )
				{
					location.href = v;
				}
			);
		}
	}
	
	function setupDataGridScripts()
	{
		// data grids with <tr rel=""> elsements (filtering out inactive ones)
		$('.datagridwide').find('tr').not('.inactive').each( setupDataGridLinks );
		$('.datagrid').find('tr').not('.inactive').each( setupDataGridLinks );
		$('.autolinktable').find('tr').not('.inactive').each( setupDataGridLinks );
		
		// link a single object
		$('.autolink').not('.inactive').each( setupAutoLink );
		
		$('.inplaceAjaxSorter').each(
			function()
			{
				// reset clicks
				// $(this).find('thead').find('tr').find('th').find('a').unbind('click');
				
				var curSortCol = $(this).attr('cursortcol');
				var curSortDirection = $(this).attr('cursortdir');
				
				var sortImage = curSortDirection == 'asc' ? '/images/skin01/icon_sortdown.gif' : '/images/skin01/icon_sortup.gif';
				 
				// set new click actions
				// this is for the sorting
				$(this).find('thead').find('tr').find('th').find('a').each(
					function()
					{
						if($(this).attr('rel') == curSortCol)
						{
							// do we already have an image? If so, ignore it
							if($(this).parent().find('img').length <= 0)
							{
								//$(this).css('background', 'black');
								//$(this).after(' *sortimage:' + sortImage + '* ');
								// after == after the <a> tag, append = insert at the end of the "content" of the <a> tag
								// changed, 4/10/2008
								$(this).append(' <img src=' + sortImage + '>' );
							}
						}
						
						// now setup the click method
						$(this).unbind('click').click(
							function()
							{
								handleSortLinkClick(this);
								return false;
							}
						);
					}
				);
				
				// this is for the paging
				$(this).find('.changepage').unbind('click').click(
					function()
					{
						handleSortLinkClick(this);
						
						return false;
					}
				);
			}
		);
	}
	
	
	// setup tab controls (run-time execution)
	$(
		function()
		{
			processTabControls('buyerRequestTabControl', 'activeGrid');
			processTabControls('buyerAuctionControl', 'activeGrid');
			processTabControls('sellerListingControl', 'activeGrid');
			processTabControls('offerTabControl', 'activeListToggle');
			
			processTabControls('agentLandingPageTabControl', 'activeGrid');
			
			processTabControls('buyerAuctionDetailTabControl', 'activeListToggle');
			
			processTabControls('tabControlGridLink', 'activeGrid');
			
			
			
		}
	);
	
	$(
		function()
		{
			$('.hideMeOnStartup').hide();
			$('.disabledblock_inactivesubscriber').block('Disabled for inactive subscribers.');
		}
	);
	
	$(
		function()
		{
			// 12/4/2007
			// fix the need for the last <li> in the navUtil <ul> collection
			// to have the class "lastred" in it
			$('#navUtil').find('li').slice(-1).find('a').addClass("last");
		}
	);

	$(
		function()
		{
			$('.sorter').change(
				function()
				{
					var refLinkId = $(this).find('option').filter(':selected').attr('sortlink');

					if(refLinkId)
					{
						//alert($('#' + refLinkId).length);
						handleSortLinkClick($('#' + refLinkId));
					}
				}
			);
			
			$('.linktonewwindow').attr('title', 'Click to open this auction in a new window.');
			$('.linktonewwindow').click(
				function()
				{
					var link = $(this).attr('linkref');
					
					if(link != null)
					{
						window.open(link, 'viewlink');
					}
					
					return false;
				}
			);
		}
	);
	
	// Setup the drop-downs
	$(
		function()
		{
			// slighly re-implemented by Jon using jQuery
			// 3/29/2008
			$('#navigation').find('li').mouseover(
				function()
				{
					$(this).addClass('over');
				}
			).mouseout(
				function()
				{
					$(this).removeClass('over');
				}			
			);
			
		}
	);
	
	$(
		function()
		{
			// ajax-request sort tables
			setupDataGridScripts();
		}
	);
	
	$(
		function()
		{
			$('.pmblink').click(
				function()
				{
					openPMB($(this).attr('href'));
					
					return false;
				}
			);
		}
	);

	

		
	runOnLoad(
		function()
		{
			// focus on the first error
			if($('.inputError').length > 0)
			{
				$('.inputError').slice(0,1).focus().select();
			}
			else
			{
				$('.focusOnStartup').not(':hidden').slice(0,1).focus();
			}
			
			// if there is error text, make it clickable
			$('.errorref').css('cursor', 'pointer').click(
				function()
				{
					var newNote = $('<b>&laquo;</b>');
					
					//$('#' + $(this).attr('rel')).focus();
					var o = $('#' + $(this).attr('rel'));
					
					$(o).after(newNote).focus();
					$(newNote).fadeOut(3000);
				}
			);
		}
	);
