jQuery(document).ready(function()
{
	//base dialog object
	var d = jQuery('#dialog').dialog({
		modal: true,
		bgiframe: true,
		autoOpen: false,
		overlay: {opacity: 0.5, background: "black"},
		close: function(event, ui){jQuery(this).text('').html('').dialog({buttons: {}})}
	});
	
	jQuery.ajaxSetup(
	{
		type: 'POST',
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			d.text('').html('<p>We are sorry there was an error processing your request. Please email <a href="mailto:support@rballiance.com">support@rballiance.com</a> or call 585-256-4605 and provide as much information as possible so we can try to prevent this error in the future.</p><p>Thank you-<br/>Rochester Business Alliance Web Support Team</p>');
			setOKButton();
			d.dialog('open');
		}
	});
	
	var num, popUpName, popUpField, strFilter;
	var strSort = 'RANK';
	var strOrder = 'ASC';
	var strSPName = 'Custom_' + jQuery('#top100Year').val() + 'Top100List';
	var imgSortDir = jQuery('#sortDir');
	
	function getList(dataString)
	{
		d.text('Please wait').dialog('open');
		jQuery.ajax(
		{
			url: 'top100List.ashx',
			data: dataString,
			dataType: 'json',
			success: function(data)
			{
				var rowElement, cellElement, linkElement, divElement, thisRank, thisWebsite, thisCompany, thisCEO, thisAddlCEO, thisAddress, thisCityState, thisPhone, thisDesc, clsName;
				var numRecords = 0;
				var table = document.getElementById('resultsTable').getElementsByTagName('tbody')[0];
				while (table.firstChild)
				{
					table.removeChild(table.firstChild);
				}
				jQuery.each(data, function()
				{
					thisRank = this.rank;
					thisIndustry = this.industry;
					thisCompany = this.company;
					thisCEO = this.ceo;
					thisAddlCEO = this.addlCeo;
					thisAddress = this.address;
					thisCityState = this.cityState;
					thisPhone = this.Phone;
					thisDesc = this.description;
					
					clsName = numRecords == 0 || numRecords % 2 == 0 ? "blueRow" : "greyRow";
					rowElement = document.createElement('tr');
					rowElement.className = clsName;
					
					cellElement = document.createElement('td');
					cellElement.appendChild(document.createTextNode(thisRank));
					rowElement.appendChild(cellElement);
					cellElement = null;
					
					cellElement = document.createElement('td');
					cellElement.appendChild(document.createTextNode(this.prevRank));
					rowElement.appendChild(cellElement);
					cellElement = null;
					
					cellElement = document.createElement('td');
					linkElement = document.createElement('a');
					if (thisWebsite != 'None')
					{
						linkElement.setAttribute('target', '_blank');
						linkElement.setAttribute('href', 'http://' + this.website);
					}
					else
					{
						linkElement.setAttribute('href', '#');
					}
					linkElement.setAttribute('id', 'link' + thisRank);
					linkElement.onmouseover = function(){popUp(this)};
					linkElement.onmouseout = function(){popDown(this)};
					thisCompany = thisCompany.replace(/&#44;/g, String.fromCharCode(44));
					linkElement.appendChild(document.createTextNode(thisCompany));
					cellElement.appendChild(linkElement);
					rowElement.appendChild(cellElement);
					linkElement = null;
					cellElement.appendChild(document.createElement('br'));
					//create tooltip with company information
					divElement = document.createElement('div');
					divElement.setAttribute('id', 'desc' + thisRank);
					divElement.className = 'companyInfoCell';
					thisCEO = thisCEO.replace(/&#44;/g, String.fromCharCode(44));
					divElement.appendChild(document.createTextNode('CEO: ' + thisCEO));
					divElement.appendChild(document.createElement('br'));
					if (thisAddlCEO != 'None')
					{
						thisAddlCEO = thisAddlCEO.replace(/&#44;/g, String.fromCharCode(44));
						divElement.appendChild(document.createTextNode('Addl. CEO: ' + thisAddlCEO));
						divElement.appendChild(document.createElement('br'));
					}
					thisAddress = thisAddress.replace(/&#44;/g, String.fromCharCode(44));
					divElement.appendChild(document.createTextNode(thisAddress));
					divElement.appendChild(document.createElement('br'));
					thisCityState = thisCityState.replace(/&#44;/g, String.fromCharCode(44));
					divElement.appendChild(document.createTextNode(thisCityState));
					divElement.appendChild(document.createElement('br'));
					divElement.appendChild(document.createTextNode('Phone Number: ' + this.phone));
					divElement.appendChild(document.createElement('br'));
					thisDesc = thisDesc.replace(/&#44;/g, String.fromCharCode(44));
					divElement.appendChild(document.createTextNode(thisDesc));
					divElement.appendChild(document.createElement('br'));
					divElement.appendChild(document.createTextNode('Industry: ' + thisIndustry));
					divElement.appendChild(document.createElement('br'));
					divElement.appendChild(document.createTextNode('Founded: ' + this.founded));
					divElement.appendChild(document.createElement('br'));
					cellElement.appendChild(divElement);
					rowElement.appendChild(cellElement);
					divElement = null;
					cellElement = null;
					cellElement = document.createElement('td');
					cellElement.appendChild(document.createTextNode(this.employees));
					rowElement.appendChild(cellElement);
					cellElement = null;
					cellElement = document.createElement('td');
					cellElement.appendChild(document.createTextNode(thisIndustry));
					rowElement.appendChild(cellElement);
					table.appendChild(rowElement);
					rowElement = null;
					numRecords++;
				});
			}
		});
		d.dialog('close');
	}
	
	function setOKButton()
	{
		d.dialog({buttons: {'OK': function(){d.dialog('close')}}});
	}
	
	getList('spName=' + strSPName + '&sort=' + strSort + '&order=' + strOrder);
	
	//filter list
	jQuery('#filterOptions a').click(function()
	{
		switch (jQuery(this).attr('title'))
		{
			case "Show all companies":
				strFilter = "All";
				break;
				
			case "Show companies in construction":
				strFilter = "Construction";
				break;
				
			case "Show companies in financial services":
				strFilter = "Financial Services";
				break;
				
			case "Show companies in manufacturing":
				strFilter = "Manufacturing";
				break;
				
			case "Show companies in retail":
				strFilter = "Retail";
				break;
				
			case "Show companies in service":
				strFilter = "Service";
				break;
				
			case "Show companies in technology":
				strFilter = "Technology";
				break;
				
			case "Show companies in wholesale and distribution":
				strFilter = "Wholesale/Distribution";
				break;
				
			case "Show companies in e-Commerce":
				strFilter = "e-Commerce";
				break;
				
			case "Show all small companies":
				strFilter = "Small-sized Companies";
				break;
				
			case "Show all medium companies":
				strFilter = "Medium-sized Companies";
				break;
			
			case "Show all large companies":
				strFilter = "Large-sized Companies";
				break;
		}
		jQuery('#resultsTable caption').text('Now Showing '+ strFilter);
		getList('spName=' + strSPName + '&sort=' + strSort + '&order=' + strOrder + '&filter=' + strFilter);
	});
	
	//sort list
	jQuery('#resultsTable thead a').click(function()
	{
		if (strSort == jQuery(this).attr('id'))
		{
			if (strOrder == 'ASC')
			{
				strOrder = 'DESC';
				imgSortDir.attr('src', 'triangleDown.gif');
			}
			else
			{
				strOrder = 'ASC';
				imgSortDir.attr('src', 'triangleUp.gif');
			}
		}
		else
		{
			strSort = jQuery(this).attr('id');
			strOrder = 'ASC';
			imgSortDir.detach();
			jQuery(this).append(imgSortDir);
			imgSortDir.attr('src', 'triangleUp.gif');
		}
		getList('spName=' + strSPName + '&sort=' + strSort + '&order=' + strOrder + '&filter=' + strFilter);
	});
});

	
function popUp(link)
{
	var linkLeft = link.style.left;
	var linkBottom = link.style.bottom;
	num = link.id.substring(4, link.id.length);
	popUpName = "desc" + num;
	popUpField = document.getElementById(popUpName);
	popUpField.style.top = linkBottom;
	popUpField.style.left = linkLeft;
	popUpField.style.display = "block";
}

function popDown(link)
{
	num = link.id.substring(4, link.id.length);
	popUpName = "desc" + num;
	popUpField = document.getElementById(popUpName);
	popUpField.style.display = "none";
}

