$('document').ready(function() {
	$('#nav-img a').click(function () {
		$('#slider').slideToggle(300);
	});
	$('.add-to-cart').live('click', function () {
		var item = $(this).attr('id').substr(5);
		var obj = $(this).siblings('.add-notice');
		var prc = $(this).siblings('.buy-cell-price');
		var but = $(this);
		$.ajax({
			async: false,
			type: "POST",
			dataType: "json",
			url: "/script/ajax/add-to-cart.php",
			//<![CDATA[
			data: "item="+item+"&task=add",
			//]]>
			success: function(data) {
				prc.hide();
				obj.show('slide','','slow',function(){
					setTimeout(function(){
						$('#cart-count a').html(data.count);
						$('#cart-empty').html(data.empty);
						obj.hide('slide','','slow',function(){
							if (data.instock == false) {
								but.next('br').remove();
								but.replaceWith('<p id="'+item+'" style="font-size:.6em;margin:0;">Out of Stock</p>');
							}
							prc.show();
						});
					}, 500);
				});
			}
		})
	});
	$('.cart-update').live('keydown', function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);4
		if (!((code >= 48 && code <=57) || (code == 9) || (code == 13) || (code == 8) || (code == 127) ||
				((code >= 37) && (code <= 40)))) {
			return false;
		}
	});
	$('.cart-update').live('change', function () {
		var amt = $(this).attr('value');
		var item = $(this).attr('name');
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "/script/ajax/add-to-cart.php",
			//<![CDATA[
			data: "item="+item+"&amount="+amt+"&task=update",
			//]]>
			success: function(data) {
				$('#cart-labels').nextAll().remove();
				$('#cart-labels').after(data.cart);
				$('#cart-count').html(data.count);
				$('#cart-empty').html(data.empty);
			}
		})
	});
	$('#cart-empty').click(function () {
		var item10 = $('.buy-cell').eq(1).children('div').children('p').not('.buy-cell-price').attr('id');
		var but10 = $('.buy-cell').eq(1).children('div').children('p').not('.buy-cell-price');
		var item01 = $('.buy-cell').eq(2).children('div').children('p').not('.buy-cell-price').attr('id');
		var but01 = $('.buy-cell').eq(2).children('div').children('p').not('.buy-cell-price');
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "/script/ajax/add-to-cart.php",
			//<![CDATA[
			data: "task=empty&item10="+item10+"&item01="+item01,
			//]]>
			//data: "task=empty",
			success: function(data) {
				$('#cart-count a').html(data.count);
				$('#cart-empty').html(data.empty);
				$('#cart-list').html('Your cart is empty.');
				if ((data.instock10 != false) && item10) {
					but10.next('br').remove();
					but10.replaceWith('<input type="button" name="buy-ten" id="'+item10+'" class="add-to-cart" value="Add to Cart"/><br/>');
				}
				if ((data.instock01 != false) && item01) {
					but01.next('br').remove();
					but01.replaceWith('<input type="button" name="buy-ten" id="'+item01+'" class="add-to-cart" value="Add to Cart"/><br/>');
				}
			}
		})
	});
	$('.cart-delete').live('click', function () {
		var item = $(this).siblings().children('input').attr('name');
		var amt = $(this).siblings().children('input').val();
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "/script/ajax/add-to-cart.php",
			//<![CDATA[
			data: "item="+item+"&amount="+amt+"&task=delete",
			//]]>
			success: function(data) {
				$('#cart-labels').nextAll().remove();
				$('#cart-labels').after(data.cart);
				$('#cart-count').html(data.count);
				$('#cart-empty').html(data.empty);
			}
		})
	});
	$('#nav-left div input[type="text"], #nav-left div input[type="password"]').keypress(function(e) {
		if ((e.which && e.which == '13') || (e.keyCode && e.keyCode == '13')) {
			$('#nav-left div input[type="button"]').click();
		}
	});
	$('#nav-left div input[type="button"]').live('click', function() {
		$('#ajax-pic').show();
		var type = $(this).attr('id');
		var sibs = $(this).parent('div').siblings('div').children('input, select');
		var num = $(this).parent('div').siblings('div').children('input, select').length;
		var query = '';
		for (i=0;i<num;i++) {
			var obj = $(this).parent('div').siblings('div').eq(i).children('input, select');
			var name = obj.attr('name');
			var inputtype = obj.attr('type');
			if (inputtype == 'checkbox') {
				var value = obj.is(':checked');
			} else {
				var value = obj.val();
			}
			query += '&'+name+'='+value;
		}
		if (type =='check-out-payment') {
			var total = $('div.total').html();
			var subtotal = $('div.subtotal').filter('.cart-total-amount').html();
			var shipping = $('div.shipping').html();
			var tax = $('div.tax').html();
			//<![CDATA[
			query += '&total='+total+'&subtotal='+subtotal+'&tax='+tax+'&shipping='+shipping;
			//]]>
			$('#check-out-payment').ajaxStart(function(){
				$(this).attr('value', 'Processing...');
			});
		}
		$.ajax({
			type: "POST",
			url: "/script/ajax/user-form.php",
			dataType: "json",
			data: "type="+type+query,
			start: function() {
				$('#ajax-pic').show();
			},
			complete: function() {
				$('#ajax-pic').hide();
			},
			success: function(data) {
				$("#ajax-pic").hide();
				if (data.error) {
					$('#nav-right p.error').remove();
					$('#nav-right').append('<p class="error">'+data.error+'</p>');
					if (data.input) {
						$('#nav-left div input').filter('name', data.input).css('background-color', '#fff;');
						$('#nav-left div input[name="'+data.input+'"]').css('background-color', '#99ff99');
						$('#check-out-payment').attr('value', 'Submit Your Payment');
					}
				} else {
					location = (data.href);
				}
			}
		});
	});
	$('#new-user').live('keyup', function() {
		var user = $(this).val();
		$.ajax({
			type: "POST",
			url: "/script/ajax/duplicate-user.php",
			data: "user="+user,
			success: function(data) {
				$('#nav-right').html('');
				$('#nav-right').append('<p class="error">'+data+'</p>');
			}
		});
	});
	$('select.country').live('change', function() {
		var country = $(this).val();
		$.ajax({
			type: "POST",
			url: "/script/ajax/state-form.php",
			dataType: "html",
			data: "country="+country,
			success: function(data) {
				$('div.state').replaceWith(data);
			}
		});
	});
	$('.logout').live('click', function() {
		$.ajax({
			type: "POST",
			url: "/script/ajax/user-logout.php",
			dataType: "json",
			data: "type=logout",
			success: function(data) {
				if (data.error) {
					$('#nav-right').html('');
					$('#nav-right').append('<p class="error">'+data.error+'</p>');
				} else {
					location = (data.href);
				}
			}
		});
	});
//SHIP-METHOD WAS HERE
	$('#logout-slider p').click(function() {
		$.ajax({
			type: "POST",
			url: "/script/ajax/user-logout.php",
			dataType: "json",
			data: "type=logout&home=1",
			success: function(data) {
				if (data.error) {
					$('#nav-right').html('');
					$('#nav-right').append('<p class="error">'+data.error+'</p>');
				} else {
					location = (data.href);
				}
			}
		});
	});
});