//################## Index
function swapperInit() {
	var count = 2;
	window.setInterval(function() {
		if (count % 2 === 0) {
			var item = ((count / 2) % 6) + 1;
			$('#signatureSwapper').replaceWith('<a href="signature.php?item=' + item + '" id="signatureSwapper" class="swapperImage"><img src="imgs/signature/swappers/' + item + '.png" alt="Signature Line Example" /></a>');
			var newImage = new Image();
			newImage.src = 'imgs/signature/swappers/' + String((item + 1) % 6) + '.png';
		} else {
			var item = (((count - 1) / 2) % 14) + 1;
			$('#customSwapper').replaceWith('<a href="custom.php?item=' + item + '" id="customSwapper" class="swapperImage"><img src="imgs/custom/swappers/' + item + '.png" alt="Custom Line Example" /></a>');
			var newImage = new Image();
			newImage.src = 'imgs/custom/swappers/' + String((item + 1) % 14) + '.png';
		}
		count++;
	},3000);
}

//################## Signature
function productItemInit() {
	$('.productThumb').click(function() {
		var hrefArray = $(this).attr('href').split('?')
		var type = hrefArray[0].replace('.php','');
		var item = hrefArray[1].replace('item=','');
		$.ajax({
			type:'POST',
			url:'jax/productPage.php',
			data:{type:type,item:item},
			success:function(response) {
				$('#productBlock').html(response);
				itemAddInit();
			}
		});
		return false;
	});
}
function checkQuantity(id) {
	var increment = $('#increment' + id).val();
	var quantity = $('#quantity' + id).val();
	if (quantity.search(/^\d+$/) == 0) {
		var extras = quantity % increment
		if (extras != 0) {
			if (confirm('The quantity of your order needs to be a multiple of ' + increment + '. Click "OK" to round your quantity up to ' + (quantity + increment - extras) + ' or click "Cancel" to enter a new quantity.')) {
				$('#quantity' + id).val(quantity + increment - extras);
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	} else {
		return false;
	}
}
function itemAddInit() {
	$('.itemAdder').click(addItems);
}
function addItems() {
	var checkout = $(this).hasClass('checkout');
	var ids = new Array();
	var quantities = new Array();
	$('.itemQuantity').each(function() {
		var id = $(this).attr('id').replace('quantity','');
		var quantity = Number($(this).val());
		if (quantity) {
			if (checkQuantity(id)) {
				ids.push(id);
				quantities.push(quantity);
			}
		}
	});
	if (ids.length > 0) {
		$.ajax({
			type:'POST',
			url:'jax/cartAdd.php',
			data:{ids:ids.join('#'),quantities:quantities.join('#')},
			success:function(response) {
				while (thisID = ids.pop()) {
					$('#quantity' + thisID).val('0');
					$('#addCartResp').html('The items have been added to the cart.');
				}
				if (checkout) {
					window.location = 'http://www.edwardsstone.com' + location.pathname.replace('signature','checkout');
				}
			}
		});
	} else if (checkout) {
		window.location = 'http://www.edwardsstone.com' + location.pathname.replace('signature','checkout');
	}
}


//################## Checkout
function itemDeleteInit() {
	$('.itemDeleter').click(function() {
		var id = $(this).attr('id').replace('delete','');
		deleteItem(id);
	});
}

function makeMoney(someNumber) {
	if (((someNumber * 100) % 100) == 0) {
		return String(someNumber) + '.00';
	} else if (((someNumber * 100) % 10) == 0) {
		return String(someNumber) + '0';
	} else {
		return String(someNumber);
	}
}

function deleteItem(id) {
	$.ajax({
		type:'POST',
		url:'jax/cartDelete.php',
		data:{id:id},
		success:function(response) {
			$('#item' + id).replaceWith('');
			var totalTotal = 0;
			var costCount = 0;
			$('.cost').each(function() {
				costCount++;
				totalTotal += Number($(this).text());
			});
			if (costCount == 0) {
				$('#paypalForm').replaceWith('<div id="textBlock">Your cart is empty</div>');
			} else {
				$('#itemsCost').text(makeMoney(totalTotal));
			}
		}
	});
}

function paypalSubmitInit() {
	$('#paypalForm').submit(function() {
		var itemCount = 0;
		$('.item').each(function() {
			itemCount++;
			var id = $(this).attr('id').replace('item','');
			$('#paypalFormSend').before('<input type="hidden" id="item_name_' + itemCount + '" name="item_name_' + itemCount + '" value="' + $('#name' + id).text() + '" /><input type="hidden" id="amount_' + itemCount + '" name="amount_' + itemCount + '" value="' + $('#cost' + id).text() + '" /><input type="hidden" id="quantity_' + itemCount + '" name="quantity_' + itemCount + '" value="' + $('#quantity' + id).text() + '" />');
		});
		if (itemCount == 0) {
			$('#response').html('<div id="textBlock">Your cart is empty</div>');
			return false;
		} else {
			return true;
		}
	});
}


//################## Contact
function contactFormInit() {
	$('#contactForm').submit(function() {
		$.ajax({
			type:'POST',
			url:'jax/contactJax.php',
			data:{name:$('#name').val(),email:$('#email').val(),project:$('#project').val()},
			success:function(response) {
				$('#contactForm input[type=text], #contactForm textarea').val('');
				$('#contactResponse').html(response);
			}
		});
		return false;
	});
}

//################## Ready
$(function() {
	var currPath = location.pathname;
	if ((currPath.search('/index') != -1) || (currPath == '/')) {
		swapperInit();
	} else if (currPath.search('/signature') != -1) {
		productItemInit();
		itemAddInit();
	} else if (currPath.search('/custom') != -1) {
		productItemInit();
	} else if (currPath.search('/checkout') != -1) {
		itemDeleteInit();
		paypalSubmitInit();
	} else if (currPath.search('/contact') != -1) {
		contactFormInit();
	}
});