
var myOdometerVal = 0;
var myOdometerIncrement = 0;
var myOdometer;

function runOdometer() {
	calculateOdometerStart();
	var div = document.getElementById("odometerDiv");
	myOdometer = new Odometer(div, {
		value: myOdometerVal,
		digits: 10,
		tenths: false,
		digitHeight:25,
		digitWidth:17,
		digitPadding:1,
		fontStyle:"font-family: Courier New, Courier, monospace; font-weight: 900;",
		bustedness:2
	});
	updateOdometer();
	// Recalculate after 10 seconds
	setTimeout("recalculateOdometer()", 10000);
}

function updateOdometer() {
	myOdometerVal=myOdometerVal+myOdometerIncrement;
	myOdometer.set(myOdometerVal);
	// Update every tenth of a second
	setTimeout("updateOdometer()", 100);
}

function recalculateOdometer() {
	calculateOdometerStart();
	// Recalculate every 10 seconds to account for any javascript lag
	setTimeout("recalculateOdometer()", 10000);
}

function calculateOdometerStart(){
	var startingNumber = 1040000000;
	// Current date
	var currentDate = new Date();
	// Sales starting date
	var startDate = new Date('February 21, 2011 00:00:00');
	// Calculation date
	var calcDate = new Date('February 22, 2011 00:00:00');
	// Number of total sales, per the calculation date
	var numSales = 55000;
	// Calculate the differences
	var seconds_from_calcDate_to_salesDate = Math.abs((calcDate.getTime() - startDate.getTime())/1000);
	var seconds_from_startDate_to_currentDate = Math.abs((currentDate.getTime() - startDate.getTime())/1000);
	//alert(seconds_from_startDate_to_currentDate);
	var sales_per_second = numSales/seconds_from_calcDate_to_salesDate;
	//alert(sales_per_second);
	var calculated_current_sales = seconds_from_startDate_to_currentDate * sales_per_second;
	myOdometerVal = startingNumber+calculated_current_sales;
	myOdometerIncrement = sales_per_second/10;
}

// Function used to re-format the product details page
function reformatProductDetails(){
	if (location.pathname == "/ProductDetails.asp" || location.pathname.indexOf("-p/") != -1 || location.pathname.indexOf("_p/") != -1){
		// We are on a product page
		var cartForm = $('form[name="MainForm"]');
		if(cartForm.length>0){
			// Product photo column
			var prodPhotoCol = cartForm.find('table:first tr:nth-child(2)').children(':first');
			// Product short description and pricing column
			var prodPricingCol = cartForm.find('table:first tr:nth-child(2)').children().eq(1);
			// Product photo
			var prodPhoto = $('#product_photo');
			// Set css styles
			prodPhotoCol.css('padding-right','10px');
			prodPhotoCol.css('padding-bottom','10px');
			prodPhotoCol.css('width','500px');
			prodPricingCol.css('padding-bottom','10px');
			// Remove the spacing columns to right and left of the product photo.
			prodPhoto.closest('tr').children().each(function(index, value){
				if($(this).html() == "&nbsp;"){
					$(this).remove();
				}
			});
			// Remove extra spacing under pricing (any time more than two <BR/> tags appear in sequence, we delete the second one)
			var pricingExtraTable = cartForm.find('table:hidden font[class*="pricecolor"][class*="colors_productprice"]:first').closest('table');
			if(pricingExtraTable.length>0){
				var pricingContainerCell = pricingExtraTable.parent();
				pricingExtraTable.remove();
				var previousTagType = "";
				pricingContainerCell.children().each(function(index, value){
					if($(this).is('br') && previousTagType=="BR"){
						$(this).remove();
					}
					previousTagType = $(this)[0].tagName;
				});
			}
			// Fix alignment issues for quantity and add to cart button. Remove <BR/> tag between dropdowns and add to cart button
			$('input[name="QTY.PROTECTOSPLINT"]').closest('td').attr('valign','bottom').attr('align','left').attr('width','1');
			$('input[src*="/btn_addtocart.gif"]').closest('td').attr('valign','bottom').attr('align','left').closest('table').closest('td').find('br:last').remove();
			// Remove the asterisk from required items
			$('#options_table font').each(function(index, value){
				if($(this).html() == "*"){
					$(this).remove();
				}
			});
			// Move product title above the product image so that the right column will occupy
			// the space to the previously empty space to the right of the product title
			var prodTitle = cartForm.find('font[class*="productnamecolorLARGE"][class*="colors_productname"]:first');
			var catNav = prodTitle.parent();
			var prodTitleText = prodTitle.text();
			catNav.css('text-indent','-5px');
			catNav.find('img, br').remove();
			prodTitle.remove();
			prodPhoto.closest('td').prepend('<div id="content-above-product-photo"><div id="product-title-above-photo"></div></div>');
			$('#product-title-above-photo').html(prodTitleText);
			var prodTextAbovePhoto = $('#product-text-above-photo');
			prodTextAbovePhoto.appendTo($('#content-above-product-photo'));
			if(prodTextAbovePhoto.html()){
				prodTextAbovePhoto.css('display','block');
			}
			// Product long description, tech specs, etc...
			var prodLongDescTable = cartForm.find('div#ProductDetail_ProductDetails_div2').parent();
			// Product description table container
			var prodDescTableContainer = prodLongDescTable.parent().parent().parent();
			// Other right-side items (normally to the right of the product description box)
			var prodOtherRightSideItems = prodLongDescTable.parent().children('td:nth-child(3)');
			// Remove all of the "Add" links from the related products
			prodOtherRightSideItems.find('input:checkbox').closest('table').remove();
			// Center the related products
			prodOtherRightSideItems.find('a').closest('td').attr('align','center');
			// Move other right-side items under the pricing column
			prodOtherRightSideItems.appendTo(prodPricingCol);
			// Resize the width of any newly moved tables under the pricing column
			prodPricingCol.find('table.colors_lines_light').attr('width', '100%').find('table:first').attr('width', '100%');
			// Move the long description table into the photo column
			prodLongDescTable.children().appendTo(prodPhotoCol);
			// Remove the now empty product description container table
			prodDescTableContainer.remove();
			// Move all other content into the left (photo+desc) column
			prodPhotoCol.html(prodPhotoCol.html()+"<BR/>");
			$('div#content_area form[name="MainForm"]').nextAll().each(function(){
				// Stop when we reach the "My Recent History" heading
				if($(this).is('img') && $(this).attr('src').indexOf("heading_history.gif")>-1){
					return false;
				}
				// Otherwise, move content
				$(this).appendTo(prodPhotoCol);
			});
		}
	}
}


$(document).ready(function() {
	$('body').removeClass('nojs');
	$('body').addClass('hasjs');

	// Reformat the product details page (if we are on a product details page)
	reformatProductDetails();
	
	// Start odometer
	runOdometer();
	
});
