jQuery(function($) {
	$('.rel_prodrow:first-child').show().addClass('active');
	var rel_prods = setInterval(function(){
		$('#related_product_cont div.active').fadeOut(500, function(){
			if($(this).is(':last-child')) {
				$(this).removeClass('active');
				$('.rel_prodrow:first-child').addClass('active').fadeIn(500);
			} else {
				$(this).removeClass('active').next('div.rel_prodrow').addClass('active').fadeIn(500);
			}
		});
	}, 5000);
	$('#related_product_list').hover(function(){
		clearInterval(rel_prods);
	}, function(){
		rel_prods = setInterval(function(){
		$('#related_product_cont div.active').fadeOut(500, function(){
			if($(this).is(':last-child')) {
				$(this).removeClass('active');
				$('.rel_prodrow:first-child').addClass('active').fadeIn(500);
			} else {
				$(this).removeClass('active').next('div.rel_prodrow').addClass('active').fadeIn(500);
			}
		});
		}, 5000);
	});
	
	// VARIABLE ASSOCIATED WITH BOTH WHOLESALE AND RETAIL CALCULATING VARIABLES 
	// FOR THE PRODUCT DETAIL PAGES.
	
	var form_id = $('.addtocart_form').attr('id');
	var quanomg_id = $('select', '#' + form_id).attr('id');
	var quan_id = form_id + '--quantity';	
	var prod_id = $('#' + form_id + '--optall').val();
	var wholesale_default = $('input#wholesaleprice_default').val();
		
	// START SECTION FOR THE WHOLESALE CALCULATE BUTTON FUNCTIONALITY	
	
	// CHECK VALUE OF THE WHOLESALE PRICE IF NO CLICK ON CALCULATE BUTTON
	if (typeof wholesale_default != "undefined") {
	
		var calculated_default = parseFloat($('#calculated_default').val());
		calculated_default = calculated_default.toFixed(2);
		$('.wholesaleprice').val(calculated_default);

			$('#' + quan_id).keyup(function() {
				
				// VALUE OF INPUT BOX
				var quan = parseInt($('#' + $('.addtocart_form').attr('id') + '--quantity').val());	
								
				
				// DO NOT ALLOW ZERO VALUE
				if (quan == 0) {
					quan = 1;
					$('#' + quan_id).val(quan);
				}
				
				var price_level_1 = parseFloat($('#price_level_1').val());
				var level_1_range = parseInt($('#level_1_range').val());			
				var price_level_2 = parseFloat($('#price_level_2').val());
				var level_2_range = parseInt($('#level_2_range').val());			
				var price_level_3 = parseFloat($('#price_level_3').val());
				var level_3_range = parseInt($('#level_3_range').val());			
				var price_level_4 = parseFloat($('#price_level_4').val());
				var level_4_range = parseInt($('#level_4_range').val());
				
				
				// CHANGE BOX QUANTITY
				var boxquan =  parseInt($('#boxquantity_default').val());
				var numcapsules = quan * boxquan;
				
				$('#no_caps').html(numcapsules);
				
				// CALCULATE PRICE	
				var startprice 		= parseFloat($('#calculated_default').val());
				var originalprice 	= parseFloat($('#original_price').val());
				var matches 		= $('#matches').text();	
				var newprice;
				var totalprice;
				var yousave;				
				
				
				// CONDITIONS TO CALCULATE PRICES BASED ON THE WHOLESALE SECTION IN THE PRODUCT TABLE
				if(quan > 0 && quan <= level_1_range) {				
					
					totalprice = (numcapsules / 1000) * originalprice;
					
					// WHOLESALE PRICE PER 1000
					displayPrice = price_level_1 / 100;				
					displayPrice = displayPrice.toFixed(2);
					$('#wholesale_price').html('$' + displayPrice);
					
					// YOUR CALCULATED PRICE
					newprice = parseFloat(price_level_1 * quan);
					newprice = newprice.toFixed(2);
					
					if (matches) 
						$('#calculated').html('<del>$' + newprice + '</del>');
					else
						$('#calculated').html('$' + newprice);
					
					$('.wholesaleprice').val(newprice);
					
					
					// CALCULATED DISCOUNT
					var you_save = newprice - (newprice * .3);
					var new_save = Math.floor(((you_save - .01)) * 100 + .5) * .01;
					var discount = new_save.toFixed(2);
					$('#new_discount').html('$' + discount);
					
					// TOTAL SAVINGS
					savings = totalprice - newprice;
					savings = savings.toFixed(2);
					$('#total_savings').html('$' + savings);
					
					// YOU SAVE PER 1000
					yousave = originalprice - (price_level_1 / 100);
					yousave = yousave.toFixed(2);							
					$('#you_save').html('$' + yousave);
					
					
				} else if ( (quan > level_1_range) && (quan <= level_2_range) ) {				
					
					totalprice = (numcapsules / 1000) * originalprice;
					
					// WHOLESALE PRICE PER 1000
					displayPrice = price_level_2 / 100;				
					displayPrice = displayPrice.toFixed(2);
					$('#wholesale_price').html('$' + displayPrice);
					
					// YOUR CALCULATED PRICE
					newprice = parseFloat(price_level_2 * quan);
					newprice = newprice.toFixed(2);
					if (matches) 
						$('#calculated').html('<del>$' + newprice + '</del>');
					else
						$('#calculated').html('$' + newprice);
					
					$('.wholesaleprice').val(newprice);
					
					// CALCULATED DISCOUNT
					var you_save = newprice - (newprice * .3);
					var new_save = Math.floor(((you_save - .01)) * 100 + .5) * .01;
					var discount = new_save.toFixed(2);
					$('#new_discount').html('$' + discount);
					
					// TOTAL SAVINGS
					savings = totalprice - newprice;
					savings = savings.toFixed(2);
					$('#total_savings').html('$' + savings);
					
					// YOU SAVE PER 1000
					yousave = originalprice - (price_level_2 / 100);
					yousave = yousave.toFixed(2);							
					$('#you_save').html('$' + yousave);	
					
				} else if (quan > level_2_range && quan <= level_3_range) {				
					
					totalprice = (numcapsules / 1000) * originalprice;
					
					// WHOLESALE PRICE PER 1000
					displayPrice = price_level_3 / 100;				
					displayPrice = displayPrice.toFixed(2);
					$('#wholesale_price').html('$' + displayPrice);
					
					// YOUR CALCULATED PRICE
					newprice = parseFloat(price_level_3 * quan);
					newprice = newprice.toFixed(2);
					if (matches) 
						$('#calculated').html('<del>$' + newprice + '</del>');
					else
						$('#calculated').html('$' + newprice);
					
					$('.wholesaleprice').val(newprice);
					
					// CALCULATED DISCOUNT
					var you_save = newprice - (newprice * .3);
					var new_save = Math.floor(((you_save - .01)) * 100 + .5) * .01;
					var discount = new_save.toFixed(2);
					$('#new_discount').html('$' + discount);
					
					// TOTAL SAVINGS
					savings = totalprice - newprice;
					savings = savings.toFixed(2);
					$('#total_savings').html('$' + savings);
					
					// YOU SAVE PER 1000
					yousave = originalprice - (price_level_3 / 100);
					yousave = yousave.toFixed(2);							
					$('#you_save').html('$' + yousave);
					
				} else if (quan > level_3_range && quan <= level_4_range) {				
					
					totalprice = (numcapsules / 1000) * originalprice;
					
					// WHOLESALE PRICE PER 1000
					displayPrice = price_level_4 / 100;				
					displayPrice = displayPrice.toFixed(2);
					$('#wholesale_price').html('$' + displayPrice);
					
					// YOUR CALCULATED PRICE
					newprice = parseFloat(price_level_4 * quan);
					newprice = newprice.toFixed(2);
					if (matches) 
						$('#calculated').html('<del>$' + newprice + '</del>');
					else
						$('#calculated').html('$' + newprice);
					
					$('.wholesaleprice').val(newprice);
					
					// CALCULATED DISCOUNT
					var you_save = newprice - (newprice * .3);
					var new_save = Math.floor(((you_save - .01)) * 100 + .5) * .01;
					var discount = new_save.toFixed(2);
					$('#new_discount').html('$' + discount);
					
					// TOTAL SAVINGS
					savings = totalprice - newprice;
					savings = savings.toFixed(2);
					$('#total_savings').html('$' + savings);
					
					// YOU SAVE PER 1000
					yousave = originalprice - (price_level_4 / 100);
					yousave = yousave.toFixed(2);			
					$('#you_save').html('$' + yousave);
				}			
			});
		}
		
		// END SECTION FOR THE WHOLESALE CALCULATE BUTTON FUNCTIONALITY
		
		// START SECTION FOR THE SALE PRICE CALCULATION - ONLY ON THE NON WHOLESALE PAGES
//		var form_id = $('.addtocart_form').attr('id');
//		var quanomg_id = $('select', '#' + form_id).attr('id');
//		var quan_id = form_id + '--quantity';
		
		var sale_price = parseFloat($('#sale_price').val());
		var reg_price = parseFloat($('#reg_price').val());
		
		$('#' + quanomg_id).change(function() {
			
			var selval 	= $(this).val();
			var matches = $('#matches').text();			
			
			if(selval == '') {
				$('#' + quanomg_id + ' option:eq(1)').attr('selected', 'selected');
				selval = $('#' + quanomg_id + ' option:eq(1)').val();
			}
			
			var amount = $('input#' + selval).val();
			var quan = $('#' + quan_id).val();			
			
			if (typeof amount == 'undefined') {
				amount = 0;
			}
			
			amount = parseFloat(amount);
			quan = parseInt(quan);
			
			var update_sale_price = (amount + sale_price) * quan;	
			
			update_sale_price = update_sale_price.toFixed(2);
			
			var update_reg_price = (amount + reg_price) * quan;
			update_reg_price = update_reg_price.toFixed(2);
			
			var you_save = update_sale_price - (update_sale_price * .3);
			var new_save = Math.floor(((you_save - .01)) * 100 + .5) * .01;
			you_save 	 = new_save.toFixed(2);
			if (matches)
				$('.price').html('<del>$' + update_sale_price + '</del>');
			else
				$('.price').html('$' + update_sale_price);				
				
			$('.priced').html('$' + you_save);
			$('.pricex').html('$' + update_reg_price);
			
		});
		
		// START UPDATE PRICES ON KEY UP - ONLY ON THE NON WHOLESALE PAGES		
		$('#' + quan_id).keyup(function() {
			
			var quan = parseInt($(this).val());
		    var matches = $('#matches').text();	
		    
			// ONLY ALLOW A VALUE OF ONE FOR THE SPECIFIED PRODUCTS
			switch (prod_id) {
				case '339':
				case '340':
				case '341':
				case '342':
				case '343':
				case '344':
				case '345':
				case '346':
				case '347':
				case '348':
				case '349':
				case '350':
				case '351':
				case '352':
				case '353':
				case '354':
				case '355':
				case '356':
				case '357':
				case '358':
					if (quan != 1) {
						quan = 1;
						$('#' + quan_id).val(quan);
					}
					break;
			}
			
			if (quan == 0 || isNaN(quan)) {
				quan = 1;
				$('#' + quan_id).val(quan);
			}
						
			var selval = $('#' + quanomg_id + ' option:selected').val();
			if(selval == '') {
				$('#' + quanomg_id + ' option:eq(1)').attr('selected', 'selected');
				selval = $('#' + quanomg_id + ' option:eq(1)').val();
			}
			
			var amount = $('input#' + selval).val();
						
			if (typeof amount == 'undefined') {
				amount = 0;
			}
			
			
						
			amount = parseFloat(amount);			
			
			var update_sale_price = (amount + sale_price) * quan;
			update_sale_price = update_sale_price.toFixed(2);
			
			var update_reg_price = (amount + reg_price) * quan;
			update_reg_price = update_reg_price.toFixed(2);
			
			var you_save = update_sale_price - (update_sale_price * .3);
			var new_save = Math.floor(((you_save - .01)) * 100 + .5) * .01;
			you_save 	 = new_save.toFixed(2);
			if (matches)
				$('.price').html('<del>$' + update_sale_price + '</del>');
			else
				$('.price').html('$' + update_sale_price);				
				
			$('.priced').html('$' + you_save);
			$('.pricex').html('$' + update_reg_price);
			
		});
		// END UPDATE PRICES ON KEY UP - ONLY ON THE NON WHOLESALE PAGES
		
			
		// START CHECKOUT SECTION
		
		// START CHECKOUT CHECK CUSTOM SHIPPING SECTION
		$('#ecom--coform--customtwo, select=[name=ecom--coform--customthree], select=[name=ecom--coform--customfour]').bind('keyup change', function() {
			var checkval = 0;
			$('.checkouterror').remove();
			checkval = checkShippingInfo();
			var delgroup = $('#delgroup').val();			
			
			if (checkval == 0) {				
				$('#ecom--coform--SUBMIT').attr('disabled', 'disabled');	
				$('#ecom--coform--' + delgroup + '--1').attr('checked','');
				$('#ecom--coform--' + delgroup + '--1').parent().show();
				$('#ecom--coform--' + delgroup + '--2').attr('checked','checked');
				$('#ecom--coform--' + delgroup + '--2').parent().show();		
				$('#ecom--coform--SUBMIT').before('<p class="checkouterror">You must complete ALL of your own shipping information including a valid account number or none to complete your order!</p>');
				$('#ecom--coform--SUBMIT').val('Complete Shipping Info...');
			} else if  (checkval == 1) {
				$('#ecom--coform--SUBMIT').removeAttr('disabled', 'disabled');
				$('.checkouterror').hide();
				$('#ecom--coform--' + delgroup + '--1').removeAttr('checked');
				$('#ecom--coform--' + delgroup + '--1').parent().hide();
				$('#ecom--coform--' + delgroup + '--2').removeAttr('checked');
				$('#ecom--coform--' + delgroup + '--2').parent().hide();
				$('#ecom--coform--SUBMIT').val('Continue');				
			} else if  (checkval == 2) {
				$('#ecom--coform--SUBMIT').removeAttr('disabled', 'disabled');
				$('#ecom--coform--' + delgroup + '--1').attr('checked','');
				$('#ecom--coform--' + delgroup + '--1').parent().show();
				$('#ecom--coform--' + delgroup + '--2').attr('checked','checked');
				$('#ecom--coform--' + delgroup + '--2').parent().show();
				$('#ecom--coform--SUBMIT').val('Continue');		
			}
		});		
		// END CHECKOUT CHECK CUSTOM SHIPPING SECTION
		
		// START DISCOUNT CODE CHECK FOR FREE SHIPPING
		var checklength = $('#ecom--coform--disccode').attr('value');
		if (checklength == undefined)
			checklength = 0;
		var delgroup = $('#delgroup').val();
		
		if (checklength.length > 0) {
			$('#ecom--coform--' + delgroup + '--1').attr('checked','');
			$('#ecom--coform--' + delgroup + '--1').parent().hide();
			$('#ecom--coform--' + delgroup + '--2').attr('checked','checked');
		} else
			$('#ecom--coform--' + delgroup + '--1').attr('checked','checked');
		
	    $('#ecom--coform--disccode').keyup(function () {
	    	var checklength = $('#ecom--coform--disccode').attr('value');
			var delgroup = $('#delgroup').val();
			var value = $(this).val();
			
			if (checklength.length > 0) {
				$('#ecom--coform--' + delgroup + '--1').attr('checked','');
				$('#ecom--coform--' + delgroup + '--1').parent().hide();
				$('#ecom--coform--' + delgroup + '--2').attr('checked','checked');
			} else {
				checkval = checkShippingInfo();
				if (checkval) {
					$('#ecom--coform--' + delgroup + '--1').removeAttr('checked');
					$('#ecom--coform--' + delgroup + '--1').parent().hide();
					$('#ecom--coform--' + delgroup + '--2').removeAttr('checked');
					$('#ecom--coform--' + delgroup + '--2').parent().hide();	
				}
				
				$('#ecom--coform--' + delgroup + '--2').attr('checked','');
				$('#ecom--coform--' + delgroup + '--1').attr('checked','checked');
				$('#ecom--coform--' + delgroup + '--1').parent().show();				
			}			
	    });
		// END DISCOUNT CODE CHECK FOR FREE SHIPPING
		
		// END CHECKOUT SECTION
		
		
		// HOME PAGE BANNER JQUERY
	    $('#cycle').cycle({
		    fx:     'uncover',
		    speed:   2500,
		    timeout: 4000,
		    pause:   1,
		    next:	 '#rightarrow',
		    prev:	 '#leftarrow'
		});    	
				
		// START COLOR PICKER SCRIPT			
		jQuery('#colorSelector').ColorPicker({
			color: '#b1b1b2',
			onShow: function (colpkr) {
				$(colpkr).fadeIn(500);
				return false;
			},
			onHide: function (colpkr) {
				$(colpkr).fadeOut(500);
				return false;
			},
			onChange: function (hsb, hex, rgb) {
				$('#colorSelector div').css('backgroundColor', '#' + hex);
				$('#capsuletop').val('#' + hex);
				$('#' + form_id + '--209_cap_color').val('#' + hex);
				$('#capsule_top').css('backgroundColor', '#' + hex);
			}
		}); 
		
		jQuery('#colorSelector2').ColorPicker({
			
			color: '#d6d8da',
			onShow: function (colpkr) {
				$(colpkr).fadeIn(500);
				return false;
			},
			onHide: function (colpkr) {
				$(colpkr).fadeOut(500);
				return false;
			},
			onChange: function (hsb, hex, rgb) {
				$('#colorSelector2 div').css('backgroundColor', '#' + hex);
				
				$('#' + form_id + '--209_body_color').val('#' + hex);
				
				$('#capsulebottom').val('#' + hex);
				$('#capsule_btm').css('backgroundColor', '#' + hex);
			}
		});
		// END COLOR PICKER SCRIPT
		
});	

function checkShippingInfo() {
	var custom2 = $('input=[name=ecom--coform--customtwo]').val();
	var custom3 = $('select=[name=ecom--coform--customthree] option:selected').val();
	var custom4 = $('select=[name=ecom--coform--customfour] option:selected').val();
		
	if (custom2 != '' && custom3 != '' && custom4 != '' && custom2.length > 5) {
		return 1;
	} else if (custom2 == '' && custom3 == '' && custom4 == '' && custom2.length == 0) {
		return 2;
	} else {
		return 0;	
	}
	return 0;
}


jQuery(function($){
				
	// Dialog			
	$('#dialog').dialog({
		autoOpen: true,
		width: 880,
		modal: true,
		buttons: {
			"Ok": function() { 
				
				var form	 = $('#surveyform');
				var formurl  = form.attr('action');
				var formtype = form.attr('method');
				var formdata = form.serialize();
				
				$.ajax({
					url: formurl,
					type: formtype,
					data: formdata,
					success: function(r) {						
						if (r == 1) {
							$('#surveymsg').show().html('<h2 style="font-size: 18px; color: green; font-weight: bold;">Your survey has been successfully submitted.</h2>');
						} else {
							$('#surveymsg').show().html('<h2 style="font-size: 18px; color: red; font-weight: bold;">Your survey has not been submitted. Please contact Capsuline.</h2>');
						}
					}
				});
				 
				$(this).dialog("close"); 
			}, 
			"Cancel": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
	
	// Dialog Link
	$('#dialog_link').click(function(){
		$('#dialog').dialog('open');
		return false;
	});
	
	//hover states on the static widgets
	$('#dialog_link, ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
	
});
