$(document).ready(function(){
	// Check for required fields
	$('.required').parent().prev().children().after('<span style="color: red;">*</span>');
	$('form').submit(function() {
		var ready = true;
		$('.required').each(function() {
			if ($(this).val() == '') {
				$(this).css('background','#FFDBDB').css('border','1px solid red').focus()
					.animate({marginLeft: '-15', marginRight: '15'}, 100)
					.animate({marginLeft: '20', marginRight: '-20'}, 100)
					.animate({marginLeft: '0', marginRight: '0'}, 100);
				ready = false;
				return false;
			}
		});
		
		if (ready == true) {
			// Go ahead
		} else {
			return false;
		}
	});
	
	// Table striping
	var classNames = {
		0: 'first',
		1: 'second',
		2: 'third'
	};
	$('table.striped tbody tr').not('[th]').each(function(index) {
		$(this).addClass(classNames[index % 3]);
	});
	
	// Simple striping
	$('.simplestriped tbody tr:odd').addClass('odd');
	$('.simplestriped tbody tr:even').addClass('even');
	
	// Simple striping
	$('.simplestripedtan tbody tr:odd').addClass('oddtan');
	$('.simplestripedtan tbody tr:even').addClass('eventan');
	
	// Simple striping
	$('.simplestripedgray tbody tr:odd').addClass('oddgray');
	$('.simplestripedgray tbody tr:even').addClass('evengray');
	
	// Call superfish navigation
	$("ul.menu").superfish();

	// Money Saved Counter
	var baseRate = 27611765.98;
	var incrementPerSec = 0.365170228;
	
	// Get the difference between baseDate and currentDate in seconds
	var baseDate = new Date("January 6, 2010 00:00:00");
	var currentDate = new Date($('.current_date_time').text());
	var baseDateMS = baseDate.getTime();
	var currentDateMS = currentDate.getTime();
	var dateDiffSec = (currentDateMS - baseDateMS) / 1000;
	
	baseRate += (dateDiffSec * incrementPerSec);
	
	var myTimer = setInterval(incrementValue, 1000);			// Increment counter every second
	
	function incrementValue() {
		baseRate += incrementPerSec;
		strBaseRate = baseRate.toString();
		var begin = strBaseRate.indexOf(".") - 3;				// Split the number apart to format it
		var cents = strBaseRate.substring(begin + 6, begin + 3);
		var formattedRate = new Array();

		for (i = begin; i > -3; i -= 3) {
			formattedRate.push(strBaseRate.substring(i, i + 3));	// Stick the pieces into an array
		}

		formattedRate.reverse();
		$('.savingscount').html('$' + formattedRate.join(",") + cents);
	}

    // Get Tow-411 RSS Feed
	/*jQuery.getFeed({
		url: 'http://www.repopartners.com/rss/proxy.php?url=http://tow411.yuku.com/feed/get/type/rss/source/domain/id/6542',
		success: function(feed) {

			var html = '';
			for(var i = 0; i < feed.items.length && i < 5; i++) {
				var item = feed.items[i];

				html += '<li><h3>'
                + '<a target="_blank" href="'
                + item.link
                + '">'
                + item.title
                + '</a>'
                + '</h3>';

                html += '<div class="updated" style="font-style: italic;">'
                + item.updated
                + '</div></li>';
            }
            jQuery('#result').append(html);
        }    
    });*/

    // Only rotate if NOT IE6
	if ($.browser.msie && $.browser.version == "6.0") {
		// Remove all but one of Top News Story
		$('.rotatingTopNews:gt(0)').remove();
	}
	else {
		// Top News Story rotator
		$('#newsContainer').cycle({
			speed: 1500,
			timeout: 15000
		});

	    // Ad rotator
		/*$('#adContainer').cycle({
			speed: 1500,
			timeout: 10000
		});*/
	}
	
	// Require login for restricted tools
	$('.members_only').click(function() {
		alert('This application is for members only.');
		return false;
	});
	$('.gold_members_only').click(function() {
		alert('You must be a Gold Level Member to access this feature.');
		return false;
	});

	// Run the VIN Decoder
	$('#btn_vin').click(function() {
		var vinNum = $('#txt_vin').val();
		var companyKey = $('#hdn_companykey').val();
		$('#vin_results').html('Loading Data...');
		$('#vin_results').load('vin_submit_2.php?vin=' + vinNum + '&companykey=' + companyKey, function() {
		});
		return false;
	});
	
	// RFI Processor
	$('.add_to_rfi').click(function() {
		var rfi_key = $(this).attr('rel');
		$.post('tiny.php', { page: 'rfi_add_remove', rfi_key: rfi_key },
		function(data) {
			var answer = confirm('Your request to get more information has been added to the inquiry list.\n\nClick "OK" to send your request.\nClick "Cancel" to continue browsing.');
			if (answer) {
				window.location = "secondary.php?page=directory_rfi";
			} else {
				return false;
			}
		});
	});
	
	// Phone Format Validation
	$('.phone').blur(function() {
		var string = $(this).val();
		var phone_expression = /^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$/i.test(string);
		if (! phone_expression && string != '') {
			alert('Phone needs to be formatted as 999-999-9999.');
			return false;
		}
	});
	
	// Credit Card Format Validation
	$('.credit_card').blur(function() {
		var string = $(this).val();
		var phone_expression = /^[0-9]{4}[-][0-9]{4}[-][0-9]{4}[-][0-9]{2,}$/i.test(string);
		if (! phone_expression && string != '') {
			alert('Credit Card needs to be formatted as 9999-9999-9999-99XX.');
			return false;
		}
	});
	
	// Keep PIN length within bounds
	$('.pin').blur(function() {
		var min = 4;
		var max = 8;
		var string_length = $(this).val().length;
		if (min <= string_length && string_length <= max) {
			// Do nothing
		} else {
			alert('PIN must be between '+min+' and '+max+' characters');
		}
	});
	
	// Coming Soon
	$('.coming_soon').click(function() {
		alert($(this).attr('rel'));
		return false;
	});
	
	// Drop Anchor
	$('.btn_drop_anchor').click(function() {
		var key = $('#hdn_company_key').val();
		var url = $('#hdn_url').val();
		$.post('tiny.php', { page: 'drop_anchor', url: url }, function(data) {
			if (data) {
				// Proceed
			} else {
				alert('Unable to drop anchor.');
				return false;
			}
		});
	});
});