var isMobile = navigator.userAgent.toLowerCase().indexOf('mobile') > 0;

var loadDynamicRE = document.getElementById('loadRE');
if (loadDynamicRE)
{
	var isRealEstateAuctionPageObject = document.getElementById('RealEstateAuctionPage');
	if (isRealEstateAuctionPageObject)
	{
		var categoriesTR = isRealEstateAuctionPageObject.parentNode.parentNode.previousSibling;
		
		if (categoriesTR.nodeName != 'TR')
			categoriesTR = categoriesTR.previousSibling;
   		
   		if  (categoriesTR.firstChild.innerHTML.toLowerCase() == '<strong>categories: </strong>')
			categoriesTR.style.display = 'none';
    	
		auctionDetailsTable = categoriesTR.parentNode;
		var thisAuctionDetailsTR = auctionDetailsTable.firstChild;
		
		do
		{
			if (thisAuctionDetailsTR.nodeName == 'TR')
			{
				var thisTD = thisAuctionDetailsTR.firstChild;
				
				if (thisTD.nodeName == 'TD' && thisTD.bgColor.toLowerCase() == '#ebf6fd')
					thisTD.style.display = 'none';
			}
		}
		while (thisAuctionDetailsTR = thisAuctionDetailsTR.nextSibling);
		
		var auctionHighlightImagesObject = document.getElementById('auctionHighlightImages');
		auctionHighlightImagesObject.style.textAlign = 'center';
		
		if (auctionHighlightImagesObject)
		{
			var thisAuctionHighlightImage = auctionHighlightImagesObject.firstChild;
			do
			{
				if (thisAuctionHighlightImage.nodeName == 'IMG')
				{
					var imageSrcParts = thisAuctionHighlightImage.src.split('/');
					imageSrcParts[imageSrcParts.length - 1] = 'h' + imageSrcParts[imageSrcParts.length - 1];
					
					thisAuctionHighlightImage.src = imageSrcParts.join('/');
				}
			}
			while (thisAuctionHighlightImage = thisAuctionHighlightImage.nextSibling);
		}
	}
}

var auctionInformationWrapper = document.getElementById('auctionInformationWrapper');
var auctionTitle = document.getElementById('auction_title');
var newAuctionTitleContainer = document.getElementById('newAuctionTitleContainer');
if (auctionInformationWrapper && auctionTitle && newAuctionTitleContainer) // Hide auction table and reposition auction_title
{
	var searchArea = document.getElementById('SearchArea');
	if (searchArea)
		searchArea.style.display = 'none';
	
	auctionTitleParentTable = auctionTitle.parentNode;
	do
	{
		if (auctionTitleParentTable.nodeName == 'TABLE')
		{
			auctionTitleParentTable.style.display = 'none';
			break;
		}
	}
	while (auctionTitleParentTable = auctionTitleParentTable.parentNode);
	
	auctionInformationWrapper.style.top = '-10px';
	auctionInformationWrapper.style.marginBottom = '10px';
	
	newAuctionTitleContainer.innerHTML = '<span id="auction_title">' + auctionTitle.innerHTML + '</span>';
	newAuctionTitleContainer.style.display = '';
}

var workspace = 'mclemore';

var navLinksObject = document.getElementById('NavLinks');
if (navLinksObject)
{
    var thisLink = navLinksObject.firstChild;
    do
    {
    	if (thisLink.nodeName == 'A')
    	{
    		var hrefString = thisLink.href.toString();
    		if (hrefString.match(/\/mndetails/))
    		{
    			var registerHrefSplit = hrefString.split('?');
    			if (registerHrefSplit.length == 2 && registerHrefSplit[1] != 'mclemore')
    				workspace = registerHrefSplit[1];
    		}
    	}
    }
    while (thisLink = thisLink.nextSibling);
}

var searchAreaWrapper = document.getElementById('SearchArea');
var selectCategoryWrapper = document.getElementById('SelectCat');
var tableTopWrapper = document.getElementById('TableTop');
if (!auctionInformationWrapper && searchAreaWrapper && document.forms.searchform
	&& (workspace == 'mclemore120' || workspace == 'mclemore122'))
{
	var registerToBidHeaderLinkObject = document.getElementById('RegisterToBidHeaderLink');
	
	if (registerToBidHeaderLinkObject && registerToBidHeaderLinkObject.nodeName == 'A')
	    registerToBidHeaderLinkObject.href = 'https://bid.mclemoreauction.com/cgi-bin/mnregister.cgi?' + workspace;
	
	var editProfileHeaderLinkObject = document.getElementById('EditProfileHeaderLink');
	
	if (editProfileHeaderLinkObject && editProfileHeaderLinkObject.nodeName == 'A')
	    editProfileHeaderLinkObject.href = 'https://bid.mclemoreauction.com/cgi-bin/mnregup.cgi?' + workspace;
	
	searchAreaWrapper.style.display = 'none';
	
	if (tableTopWrapper)
	{
		var currentCategoryLabelObject = tableTopWrapper.firstChild;
		do
		{
			if (currentCategoryLabelObject.firstChild)
				currentCategoryLabelObject = currentCategoryLabelObject.firstChild;
			else
				currentCategoryLabelObject = currentCategoryLabelObject.nextSibling;
		}
		while (currentCategoryLabelObject && currentCategoryLabelObject.nodeName != 'TD');
		
		var currentCategoryLabel = currentCategoryLabelObject.innerHTML.replace(/ records/, '').replace(/Keywords: /, 'Search: ');
		if (currentCategoryLabel.match(/<a/)) currentCategoryLabel = null;
		
		var currentAuctionTitleObject = tableTopWrapper.lastChild;
		do
		{
			if (currentAuctionTitleObject.lastChild)
				currentAuctionTitleObject = currentAuctionTitleObject.lastChild;
			else
				currentAuctionTitleObject = currentAuctionTitleObject.previousSibling;
		}
		while (currentAuctionTitleObject && currentAuctionTitleObject.nodeName != 'TD');
		
		var currentAuctionTitle = currentAuctionTitleObject.innerHTML;
		var currentAuctionTitleParts = currentAuctionTitle.split(' - ');
		
		var currentAuctionDate = (currentAuctionTitleParts.length > 1) ?
			'<a href="/cgi-bin/mndetails.cgi?' + workspace + '#AuctionEnding">' + currentAuctionTitleParts.pop() :
			null;
		
		currentAuctionTitle = currentAuctionTitleParts.join(' - ') + '</a>';
		
		currentAuctionTitleObject.innerHTML = currentAuctionTitle + (currentAuctionDate ? '<br/>' + currentAuctionDate : '');
	}
	
	var currentCategoryCode = 'No Category Code';
	if (currentCategoryLabel && currentCategoryLabel.match(/Category: /))
	{
		currentCategoryCode = currentCategoryLabel.replace(/Category: /, '').replace(/ \(.*?\)/, '').replace(/ /g, '-');
		currentCategoryLabel = wordToUpper(currentCategoryLabel).replace(/ And /ig, ' and ');
	}
	
	if (currentCategoryLabel)
	{
		document.title = currentCategoryLabel.replace(/(&amp;)/g, '&').replace(/(&nbsp;)/g,' ') + ' | ' + document.title;
		
		currentCategoryLabel = currentCategoryLabel.replace(/: /, ': <b>') + '</b>';
		currentCategoryLabelObject.innerHTML = currentCategoryLabel;
	}
	
	if (currentAuctionTitle)
		document.title = currentAuctionTitle.replace(/(&amp;)/g, '&').replace(/(&nbsp;)/g,' ').replace(/<.*?>/g, ' ') + ' | ' + document.title;
	
	var searchKeywords =  '';
	if (document.forms.searchform.keyword)
		searchKeywords =  document.forms.searchform.keyword.value;
	
	var searchType =  'ANY';
	if (document.forms.searchform.stype)
		searchType =  document.forms.searchform.stype.value;
	
	var newCategoryList =
		'<div class="category_select_list">' +
		((workspace == 'mclemore') ?
			'<a href="http://www.mclemoreauction.com/"><b>Current Auctions</b></a>' :
			'<b>' + ((currentAuctionTitle) ? currentAuctionTitle + (currentAuctionDate ? currentAuctionDate : '') : '<a href="/cgi-bin/mndetails.cgi?' + workspace + '">Auction Information</a>') + '</b>'
		) +
		'<div style="clear: both;"></div>' +
		'<div class="category_select_label">' +
			'<form action="http://bid.mclemoreauction.com/cgi-bin/mnlist.cgi" name="searchform" method="post">' +
				'<label style="margin-right: 8px;" for="search_input">Search Auction:</label>' +
				'<input type="hidden" value="' + workspace + '" name="auction">' +
				'<input type="text" style="margin-right: 5px;" value="' + searchKeywords + '" size="60" id="search_input" name="keyword">' +
				'<select name="stype">' +
					'<option value="ANY"' + ((searchType == 'ANY') ? ' selected' : '') + '>Any of the words</option>' +
					'<option value="ALL"' + ((searchType == 'ALL') ? ' selected' : '') + '>All of the words</option>' +
					'<option value="PHRASE"' + ((searchType == 'PHRASE') ? ' selected' : '') + '>Exact phrase</option>' +
					'<option value="CONTAIN"' + ((searchType == 'CONTAIN') ? ' selected' : '') + '>Contains</option>' +
				'</select>' +
				'<input type="submit" style="margin-left: 5px;" value="Search" name="search">' +
			'</form>' +
		'</div>';
	
	if (selectCategoryWrapper)
	{
		newCategoryList +=
			'<div style="clear: both;"></div>' +
			'<div class="category_select_label">Categories:</div>';
		
		selectCategoryWrapper.style.display = 'none';
		
		var selectCategoryOptions = selectCategoryWrapper.firstChild;
		do
		{
			if (selectCategoryOptions.firstChild)
				selectCategoryOptions = selectCategoryOptions.firstChild;
			else
				selectCategoryOptions = selectCategoryOptions.nextSibling;
		}
		while (selectCategoryOptions && selectCategoryOptions.nodeName != 'SELECT');
		
		var thisCategoryOption = selectCategoryOptions.firstChild;
		var categoryCount = 0;
		
		do
		{
			if (thisCategoryOption.nodeName == 'OPTION')
			{
				var thisCategoryTitle = thisCategoryOption.innerHTML;
				var thisCategoryURL = thisCategoryOption.value;
				
				var thisCategoryURLParts = thisCategoryURL.split('/');
				var thisCategoryCode = thisCategoryURLParts[thisCategoryURLParts.length - 1];
				
				if (thisCategoryTitle != 'Select category')
				{
					categoryCount++;
					
					if (categoryCount == 8 && (currentCategoryCode == 'No Category Code' || currentCategoryCode == 'ALL'))
						newCategoryList +=
							'<span style="display: none;" id="hidden_categories">';
					
					newCategoryList +=
						'<a href="' + thisCategoryURL + '"' +
						((currentCategoryCode == thisCategoryCode) ?
							' class="category_selected"' :
							'') +
						'>' +
						((thisCategoryCode == 'ALL') ? '<b>' : '') +
						wordToUpper(thisCategoryTitle).replace(/ And /ig, ' and ').replace(/ /g, '&nbsp;').replace(
							/\(/, '<span class="category_select_list_lot_count">(').replace(/\)/, ')</span>') +
						((thisCategoryCode == 'ALL') ? '</b>' : '') +
						'</a>';
				}
			}
		}
		while (thisCategoryOption = thisCategoryOption.nextSibling);
	}
	else
		newCategoryList +=
			'<div style="clear: both;"></div>' +
			'<b><a href="/cgi-bin/mnlist.cgi?' + workspace + '/category/ALL">View Catalog</a></b>';
	
	if (categoryCount >= 7 && (currentCategoryCode == 'No Category Code' || currentCategoryCode == 'ALL'))
		newCategoryList +=
			'</span>' +
			'<a href="javascript:showAllCategories();" id="show_all_categories_link"><b>Show All ' + categoryCount + ' Categories&hellip;</b></a>';
	
	newCategoryList += '<div style="clear: both;"></div></div>';
	
	var lotInformationObject = document.getElementById('lotInformation');
	var lotInformationObject = document.getElementById('lotInformation');
	var auctionInformationWrapperObject = document.getElementById('auctionInformationWrapper');
	var lotDescriptionObject = document.getElementById('lotDescription');
	if (lotInformationObject && !auctionInformationWrapperObject && lotDescriptionObject)
		newCategoryList += '<a href="#DataTable" id="lotTitle">' + lotDescriptionObject.innerHTML + '</a>';
	
	searchAreaWrapper.innerHTML = newCategoryList;
	searchAreaWrapper.style.display = 'block';
}

var lotInformationObject = document.getElementById('lotInformation');
if (isMobile && !lotInformationObject && !auctionInformationWrapper && window.location.toString().match(/\/mnlist/))
{
	// Do something here to make lot list look better on phones
	
	//var primary = document.getElementById('primary');
	//if (primary) primary.style.width = '700px';
}

function showAllCategories()
{
    var hiddenCategoriesObject = document.getElementById('hidden_categories');
    if (hiddenCategoriesObject) hiddenCategoriesObject.style.display = '';
    
    var showAllLink = document.getElementById('show_all_categories_link');
    if (showAllLink) showAllLink.style.display = 'none';
}

// http://www.richardcastera.com/blog/javascript-capitalize-each-word-in-a-string
function wordToUpper(strSentence)
{	
	return strSentence.toLowerCase().replace(/\b[a-z]/g, convertToUpper);
	
	function convertToUpper()
	{
		return arguments[0].toUpperCase();
	}
}
