$(function() {
	$(".cart_button").click(function () {

		// get row ID for this product:
		var button_id = $(this).attr('id');
		var button_arr = button_id.split('_');
		var id = button_arr[1];
		var product_type = button_arr[0];

		var previous_text = $('#'+button_id).html();

		if(product_type == 'apparel'){ // this is apparel, so get size, qty, and add to cart
			$( "#select-size-error" ).fadeOut();
			var product_code = $('#size_select_'+id).val();
			var quantity = $('#qty_select_'+id).val();
			if(product_code == 'Size' || quantity == 'Qty'){
				$( "#select-size-error" ).fadeIn();
				return(false);
			}
			$('#loading').toggle();
			// now we post this so we can add it to the cart
			$.post("/cart/add.php", { product_code: product_code, quantity: quantity },
			function(data){
				$('#'+button_id).fadeOut( function() {
					$('#'+button_id).html('in cart').css('background-color', '#F83920');
				}).fadeIn('slow').fadeOut( function() {
					$(this).html( previous_text ).css('background-color', '#478A9B').fadeIn( function () { $(this).attr( 'style', '' ); } );
				})
				$.post("/cart/refresh.php", { refresh: '1' },
				function(data){
					eval(data);
					$('#loading').toggle();
				});
			});
		}

		if(product_type == 'album'){
			var product_code = id;
			// we have to get the part of the product code that tells us which album format this is.
			// that way we can keep the MP3 link disabled
			// ** at this time, we wont fade in for format 3 (MP3)
			var album_format_arr = product_code.split('-');
			var album_format = album_format_arr[2];
			var quantity = $('#qtyselect_'+product_code).val();
			$('#loading').toggle();
			// now we post this so we can add it to the cart
			$.post("/cart/add.php", { product_code: product_code, quantity: quantity },
			function(data){
				$('#'+button_id).text('adding').animate({ backgroundColor: "#F83920" }, 600);
				$.post("/cart/refresh.php", { refresh: '1' },
				function(data){
					if(album_format != '3'){
						$('#'+button_id).animate({ backgroundColor: "#478AAB" }, 600, function () { $(this).attr( 'style', '' ); }).text(  previous_text );
					}else{
						$('#'+button_id).animate({ backgroundColor: "#478AAB" }, 600 );
					}
					eval(data);
					$('#loading').toggle();
				});
			});
		}

		if(product_type == 'digitalalbum'){
			var product_code = id;
			// we have to get the part of the product code that tells us which album format this is.
			// that way we can keep the MP3 link disabled
			// ** at this time, we wont fade in for format 3 (MP3)
			var album_format_arr = product_code.split('-');
			var album_format = album_format_arr[2];
			var quantity = '1';
			$('#loading').toggle();
			// now we post this so we can add it to the cart
			$.post("/cart/add.php", { product_code: product_code, quantity: quantity, digital: '1' },
			function(data){
				$('#'+button_id).text('adding').animate({ backgroundColor: "#F83920" }, 600);
				$.post("/cart/refresh.php", { refresh: '1' },
				function(data){
					$('#'+button_id).animate({ backgroundColor: "#478AAB" }, 600).fadeOut( function() {
						var td = $('#'+button_id).parent();
						$(td).html('<span class="disabled-button">in cart</span');
					});
					eval(data);
					$('#loading').toggle();
				});
			});
		}

		if(product_type == 'mp3track'){
			var product_code_plus = id;
			var arr = product_code_plus.split('-');
			var product_code = arr[0] + '-' + arr[1];
			var track_id = arr[3];
			var quantity = 1;
			$('#loading').toggle();
			$.post("/cart/add.php", { product_code: product_code, quantity: quantity, track_id: track_id, format: 'mp3', digital: '1' },
			function(data){
				$('#'+button_id).text('adding').animate({ backgroundColor: "#F83920" }, 600);
				$.post("/cart/refresh.php", { refresh: '1' },
				function(data){
					$('#'+button_id).animate({ backgroundColor: "#478AAB" }, 600).fadeOut( function() {
						var td = $('#'+button_id).parent();
						$(td).html('<span class="disabled-button">in cart</span');
					});
					eval(data);
					$('#loading').toggle();
				});
			});
		}

		if(product_type == 'flactrack'){
			var product_code_plus = id;
			var arr = product_code_plus.split('-');
			var product_code = arr[0] + '-' + arr[1];
			var track_id = arr[3];
			var quantity = 1;
			$('#loading').toggle();
			$.post("/cart/add.php", { product_code: product_code, quantity: quantity, track_id: track_id, format: 'flac', digital: '1' },
			function(data){
				$('#'+button_id).text('adding').animate({ backgroundColor: "#F83920" }, 600);
				$.post("/cart/refresh.php", { refresh: '1' },
				function(data){
					$('#'+button_id).animate({ backgroundColor: "#478AAB" }, 600).fadeOut( function() {
						var td = $('#'+button_id).parent();
						$(td).html('<span class="disabled-button">in cart</span');
					});
					eval(data);
					$('#loading').toggle();
				});
			});
		}

		if(product_type == 'merch'){

			var product_code = id;
			var quantity = $('#qty_select').val();
			$('#loading').toggle();
			// now we post this so we can add it to the cart
			$.post("/cart/add.php", { product_code: product_code, quantity: quantity },
			function(data){
				$('#'+button_id).text('adding').animate({ backgroundColor: "#F83920" }, 600);
				$.post("/cart/refresh.php", { refresh: '1' },
				function(data){
					if(album_format != '3'){
						$('#'+button_id).animate({ backgroundColor: "#478AAB" }, 600, function () { $(this).attr( 'style', '' ); }).text( previous_text );
					}else{
						$('#'+button_id).animate({ backgroundColor: "#478AAB" }, 600);
					}

					eval(data);
					$('#loading').toggle();
				});
			});
		}

		if(product_type == 'bundle'){

			var product_code = id;
			var quantity = $('#qty_select').val();
			var options = [];
			$( '.bundle-item' ).each( function ( index ) { options.push( $( "#bundle-option-" + $(this).val() ).val() ); } );
			$('#loading').toggle();
			// now we post this so we can add it to the cart
			$.post("/cart/add.php", { product_code: product_code, quantity: quantity, bundle: true, options: options },
			function(data){
				$('#'+button_id).text('adding').animate({ backgroundColor: "#F83920" }, 600);
				$.post("/cart/refresh.php", { refresh: '1' },
				function(data){
					if(album_format != '3'){
						$('#'+button_id).animate({ backgroundColor: "#478AAB" }, 600, function () { $(this).attr( 'style', '' ); }).text( previous_text );
					}else{
						$('#'+button_id).animate({ backgroundColor: "#478AAB" }, 600);
					}

					eval(data);
					$('#loading').toggle();
				});
			});
		}

		return(false);
	});

	$(".remove_from_cart").click(function () {
		$('#loading').toggle();
		var prod_id = $(this).attr('id');
		$.post("/cart/remove.php", { item: prod_id},
		function(data){
			$('[id="tr_'+prod_id+'"]').fadeOut();
			$.post("/cart/refresh.php", { refresh: '1'},
			function(data){
				eval(data);
				$('#loading').toggle();
			});
		});
	});

	$(".qty_details_select").change(function () {
		var prod_id = $(this).attr('id');
		var new_qty = $(this).val();
		$('#loading').toggle();
		$.post("/cart/change_qty.php", { item: prod_id, new_qty: new_qty },
		function(data){
			$.post("/cart/refresh.php", { refresh: '1', prod_id: prod_id},
			function(data){
				eval(data);
				$('#loading').toggle();
			});
		});

	});

	// clear all cart contents
	$('#empty_cart_link').click(function(){
		$('#loading').toggle();
		$.post("/cart/empty_cart.php", { go: 1 },
		function(data){
			$('.cart_row').fadeOut(function(){
				$('#empty_cart_tr').show();
				$('#empty_cart_link').hide();
				$('.calculation').hide();
				$('#checkout_button').hide();
				$.post("/cart/refresh.php", { refresh: '1'},
				function(data){
					eval(data);
					$('#loading').toggle();
				});
			});

		});
		return(false);
	});


	// checkout shipping choice stuff
	$('.usps_shipping_choice').click(function () {
		$('#loading').toggle();
		var choice = $(this).val();
		$.post("/cart/shipping_choice_refresh.php", { choice: choice, carrier: 'USPS'},
		function(data){
			$('#loading').toggle();
			eval(data);
			$('#select_shipping').fadeOut(function(){
				$('#shipping_totals').fadeIn();
			});

		});
	});

	$('.ups_shipping_choice').click(function () {
		$('#loading').toggle();
		var choice = $(this).val();
		$.post("/cart/shipping_choice_refresh.php", { choice: choice, carrier: 'UPS'},
		function(data){
			$('#loading').toggle();
			eval(data);
			$('#select_shipping').fadeOut(function(){
				$('#shipping_totals').fadeIn();
			});
		});
	});


	// alt images functions

	$(".alt_img img").click(function () {
		var img_src = $(this).attr('src');
		var img_alt = $(this).attr('alt');
		var main_img = $('#main_img').attr('src');
		if(main_img != img_src.replace('w=20','w=210')){
			$('#main_img').fadeOut(100, function(){
				$(this).attr('src', img_src.replace('w=20','w=210')).load(function(){
					$('#main_img').fadeIn(100);
				});
			});
			//$('#main_img').fadeOut().attr('src', img_src.replace('w=20','w=210')).fadeIn();
			$('.enlarge').hide();
			$('#'+img_alt).parent().show();
		}
		return false;
	});

	

	// end alt images

	$("#final_submit").click(function () {
		$('#error_div').fadeOut();
		$('#cc_error_div').fadeOut();
		$('#inventory_error_div').fadeOut();
		$('#loading').toggle();
		$("#final_submit").attr('disabled', 'true');
		// gather variables that we'll post

		var cc_type = $('#cc_type').val();
		var cc_num = $('#cc_num').val();
		var cc_exp_month = $('#cc_exp_month').val();
		var cc_exp_year = $('#cc_exp_year').val();
		var cc_security_code = $('#cc_security_code').val();

		// we can do some preliminary validations with javascript here
		var errors = 0;
		if(cc_type == 'Select'){
			var errors = 1;
			$("label[for='cc_type']").addClass('error');
			$("#final_submit").removeAttr('disabled');
		}
		if(cc_num == ''){
			var errors = 1;
			$("label[for='cc_num']").addClass('error');
			$("#final_submit").removeAttr('disabled');
		}
		if(cc_exp_month == 'Month'){
			var errors = 1;
			$("label[for='cc_exp_month']").addClass('error');
			$("#final_submit").removeAttr('disabled');
		}
		if(cc_exp_year == 'Select'){
			var errors = 1;
			$("label[for='cc_exp_year']").addClass('error');
			$("#final_submit").removeAttr('disabled');
		}
		if(cc_security_code == ''){
			var errors = 1;
			$("label[for='cc_security_code']").addClass('error');
			$("#final_submit").removeAttr('disabled');
		}

		if(errors == 1){
			$('#loading').toggle();
			$('#error_div').fadeIn();
			return false;
		}else{
			$.post("/cart/DoDirectPaymentReceipt.php", { cc_type: cc_type, cc_num: cc_num, cc_exp_month: cc_exp_month, cc_exp_year: cc_exp_year, cc_security_code: cc_security_code},
			function(data){
				$('#loading').toggle();
				$("#final_submit").removeAttr('disabled');
					var $response=$(data);
					var result = $response.filter('#result').text();
					if(result == 'error'){
						$('#cc_error_div').fadeIn();
					}
					else if ( result == 'inventory' ) {
						$( '#inventory_error_help' ).html( $response.filter('#help').html() );
						$('#inventory_error_div').fadeIn();
					} else{
						// success! move them where they need to go.
						window.location='/store/checkout/confirmation.php?oc=1';
					}
			});
		}
	});

	$('#go_search').click(function (){
		$('#s_f').submit();
		return(false);
	});

});

