$(document).ready(function()
{

	// tweak colors for the user  
  $("div[id^='tweak_color_']").click(function() {
		$("#tweak_color_ajax").load("/cust/ui/tweakui.asp", { 'color': $(this).attr("rel") } );
		parent.$(".colorbar").css("background-color","#"+$(this).attr("rel"));
		parent.$("#headercell").css("background-color","#"+$(this).attr("rel"));
	});

  // opens up the tweak me lightbox
  if ($(".tweakme").length > 0) {
		$(".tweakme").fancybox({'showCloseButton':true,'hideOnOverlayClick':false,'width':330,'height':250,'titleShow':false});
	}
	
	if ($("#submit_background").length > 0) {
		$("#submit_background").click(function() {
				$("#tweak_color_ajax").load("/cust/ui/tweakui.asp", { 'background_img': $("#rKey").val() } );	
				changeBackground($("#rKey").val())
		});
	}
	if ($("#thumbs_wrapper").length > 0) {
		$("#thumbs_wrapper img").click(function() {
			$("#mainpic").attr("src",$(this).attr("src"));														 
			});
	}
	if ($("#youtube_thumbs").length > 0) {
		$("#youtube_thumbs img").click(function() {
			$("#youtube_wrapper").html($("#"+$(this).attr("rel")).html());														 
			});
	}
	if ($("#sizechartlink").length > 0) {
		$("#sizechartlink").fancybox({'showCloseButton':true,'hideOnOverlayClick':false,'width':690,'height':150});
	}
	// inventory notification
	if ($('#size_id').length > 0) {
		$('#size_id').change(function() { 
			if ( $('#size_id option:selected').val() != 0 && $('#color option:selected').val() != 0) { 
				$('#inventory_notify').load('/cust/products/ajaxhosts/inventory_check.asp', { 'size_id': $('#size_id option:selected').val(), 'color': $('#color option:selected').val(), 'product_id': $('#product_id').val() }, 
					function(data) {   if(data.indexOf('OUT OF STOCK') > 0) { $('#submit_btn').attr('disabled','disabled'); } }
				);	
			}
		});
		$('#color').change(function() { 
			if ( $('#size_id').val() != 0 && $('#color').val() !=0) {
				$('#inventory_notify').load('/cust/products/ajaxhosts/inventory_check.asp', { 'size_id': $('#size_id option:selected').val(), 'color': $('#color option:selected').val(), 'product_id': $('#product_id').val() }, 
					function(data) {  if(data.indexOf('OUT OF STOCK') > 0) { $('#submit_btn').attr('disabled','disabled');  } } 
				);	
			}
		});
	}
	
	if ($("#paypalform").length > 0) {
		$("#paypalform").submit();
	}
	if ($("#authorizedotnetform").length > 0) {
		 $("#State").change(function() {
				tax = Math.round($('#authorize_subotal').html().substr(1) * .0825 * 100)/100;
				subtotal = (Math.round($('#authorize_subotal').html().substr(1) * 100)/100).toFixed(2);
				shipping = (Math.round($('#authorize_shipping').html().substr(1) * 100)/100).toFixed(2);
				if ($(this).val() == 'CA') { 
					tax = tax.toFixed(2);
					total = parseFloat(subtotal) + parseFloat(tax) + parseFloat(shipping);
					total = total.toFixed(2);
				   $('#taxcell').html('$'+tax+'<input type="hidden" name="tax" id="tax" value="'+tax+'">');
					$('#taxrow').show();
					$('#authorize_total').html('$'+(total));
					$('#ordertotal').val(total);
				}
				else {
					total = parseFloat(subtotal) + parseFloat(shipping);
					total = total.toFixed(2);
					$('#taxcell').html('');
					$('#taxrow').hide();
					$('#authorize_total').html('$'+(total));
					$('#ordertotal').val(total);
				}			
			});
	}
	// shopping cart
	if ($('#shipping_id').length > 0) {
		$('#shipping_id').change(function() {
			if ($('#shipping_id option:selected').val() != '') {
				$('#shipping').html('$'+$('#shipping_id option:selected').attr('title'));
				$.post('/cust/shopping_cart/ajaxhosts/setshippingmethod.asp', { id: $('#shipping_id option:selected').val() }, function(data) { updateTotal(); } );
				$('#shipping_id').removeClass('chooseShipping');
			}
			else { $('#shipping').html(''); $("#total").html(''); }
		});
	}
});

function checkForShipping(x) {
		if ($('#shipping_id option:selected').val() == '') { 
			alert('Please choose a shipping destination.');
			$('#shipping_id').addClass('chooseShipping');
			return false; 
		}
		else { 
			document.location = x; 
		}
}

function showColor() {
	if ( $("#color option:selected").attr("title") != "") {
		$("#mainpic").attr("src",$("#color option:selected").attr("title"));
	}
}

function showShipping() {
		$("#shippinginfo").slideToggle();
}

function changeBackground(y) {
		var x;
		$.ajax({
			type: "POST",
			url: '/cust/ui/tweakui.asp',
			data: "rKey="+y,
			success: function(data) {
    			parent.$("body").css("background-image","url('"+ data +"')");
  			}
		});
}

// removes items from carts. 
function updateCart() {
	numrows = $("tr[id^='cartrow_']").length;
	c=0;
	$("input[name='remove']").each( function () {
		if (this.checked) { 
			c++;
			$("#cartrow_"+this.value).css('background-color','#46B5DC').load("/cust/shopping_cart/ajaxhosts/removeitemfromcart.asp", { 'id': this.value } ).fadeOut(500);
			setTimeout('$(\'#cartrow_'+this.value+'\').remove();',500);			
		}
	});
	// if cart is now empty, clear the table, else reset the price total
	if (numrows==c) {
		 setTimeout('$(\'#cart\').remove();',500);
		 setTimeout('$(\'#emptycart\').fadeIn(500).html("Your cart is empty");',500);
	}
	else {
		setTimeout('updateTotal();',1000);
	}
}

function updateTotal() {
	//total = 0
	//$(".price").each( function () {
	//	total += parseFloat(this.innerHTML.substr(1));	
	//});
	//total += parseFloat($("#shipping").html().substr(1));
	//$('#total').html('$'+total);
	$("#shipping").load("/cust/shopping_cart/ajaxhosts/updatetotal.asp",  { 'update': 'shipping' });
	$("#total").load("/cust/shopping_cart/ajaxhosts/updatetotal.asp",  { 'update': 'total' });	
}

