$(document).ready
(
	function()
	{
		/**
		 * Testimonials
		 */
		$.getJSON
		(
			'http://www.cheapercarinsurance.com/testimonials.php?statecode=' + getQueryStringVariable('statecode'),
			function(data)
			{
				$('#leftColumn div.you-are-in p.testimonial').html(data.text);
				$('#leftColumn div.customer p span.testimonial').text('Like ' + data.customer + ' from ' + data.city + ', ' + data.state + '.');
			}
		);
		
		
		/**
		 * Intro Text
		 */
		var currentDate = new Date();
		
		// Prepare date for intro text
		var daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
		var introDay = daysOfWeek[currentDate.getDay()];
		var introDate = parseInt(currentDate.getMonth() + 1) + '/' + currentDate.getDate() + '/' + currentDate.getFullYear();
		
		// Prepare state name
		var introState = $('#listingHeading').text().replace('Auto Insurance Providers in ', '');
		
		// Replace intro text
		var introText = 'Cheaper ' + introState + ' car insurance for ' + introDay + ', ' + introDate + ' <span>Updated 15 minutes ago</span>';
		$('#rightColumn div.content div.listing-intro p.highlight').html(introText);
		
		
		/**
		 * Search results format
		 */
		var $listingResults = $('#listingResults');
		var $listingOutput = $('<ul></ul>').appendTo('#rightColumn div.content div.listing');
		
		$('table', $listingResults).each
		(
			function()
			{
				var $rows = $('tr', this);
				
				$rows.each
				(
					function()
					{
						var $row = $(this);
						
						// Prepare table cells
						var $imageCell = $('td:first', $row);
						var $descriptionCell = $('td:nth-child(2)', $row);
						var $actionCell = $('td:last', $row);
						
						// Build a new item with proper formatting
						var $item = $('<li></li>');
						
						$('<div class="company-logo"></div>')
							.append($imageCell.children())
							.appendTo($item);
						
						$('<div class="company-info"></div>')
							.append($descriptionCell.children())
							.find('p:first')
								.removeAttr('style')
								.end()
							.find('a:first')
								.wrap('<h3></h3>')
								.end()
							.find('br')
								.remove()
								.end()
							.appendTo($item);
						
						$('<div class="action"></div>')
							.append($actionCell.children())
							.find('img')
								.attr('src', 'http://www.cheapercarinsurance.com/assets/images/lp/see-plans-and-pricing.png')
								.attr('alt', 'See Plans and Pricing')
								.end()
							.appendTo($item);
						
						// The rollover effect
						$item.hover
						(
							function()
							{
								$(this).addClass('current');
							},
							function()
							{
								$(this).removeClass('current');
							}
						);
						
						// Clickable section
						$item.click
						(
							function(event)
							{
								event.preventDefault();
								punder();
								window.open($('a:first', $(this)).attr('href'));
								return false;
							}
						);
						
						// Append new item to the list
						$item.appendTo($listingOutput);
					}
				);
			}
		);
		
		// Remove the old listings
		$listingResults.remove();
	}
);