$(document).ready(function() {
	$('.product_slide').cycle({
		fx: 'scrollHorz',
		prev: '.prev',
		next: '.next',
		speed: '5000',
		timeout: 0
	});
	
	$('#mod_bakery_wrapper_f').fadeIn('slow');
	
	//handle checkbox clicks
	$('.itemcheck').click(function(){
		//clear notification
		$(".notification").html('');
		$(".notification").animate({
			opacity: 0.5
		}, 500 );
		
		//clear price
		$('.price_btn').html('').animate({
			opacity: 0.5
		}, 500 );
		
		//blur order btn
		$('.order_btn').animate({
			opacity: 0.7
		}, 500 ).css('cursor', 'default');
		
		//set select input to default
		$(".mod_bakery_main_select_f > option[value='']").attr('selected', true);
		
		//control select boxes
		var item_id = $(this).attr("name");
		$('.itemcheck').attr('checked', false);
		$(this).attr('checked', true);
		
		//enable selectinput for selected checkbox
		$('.mod_bakery_main_select_f').attr('disabled', true);
		$(this).prevAll().removeAttr('disabled');
	});
	
	//handle submit btn click
	$('.order_btn').click(function(event){
		event.preventDefault();
		
		//check if id is set
		if($('#itemid').val() != ""){
			validate_item = true;
		}
		
		//check if option is set
		if($('#option').val() != ""){
			validate_option = true;
		}
		
		//submit form
		if(validate_item == true && validate_option == true){
			$(".master_order_form").submit();
		}
	});
});

function count_up(price_current_units,price_end_units,price_current_tenths,price_end_tenths) {
	
	percent1 = price_end_units / 100;
	
	percent10 = percent1 * 10;
	percent30 = percent1 * 30;
	percent50 = percent1 * 50;
	percent70 = percent1 * 70;
	percent90 = percent1 * 90;
	percent100 = price_end_units;
	
	switch(true){
		case (price_current_units < percent10 && (price_current_units + 500) <= price_end_units):
			addition_units = 500;
		break;
		case (price_current_units < percent30 && (price_current_units + 30) <= price_end_units):
			addition_units = 30;
		break;
		case (price_current_units < percent50 && (price_current_units + 20) <= price_end_units):
			addition_units = 20;
		break;
		case (price_current_units < percent70 && (price_current_units + 10) <= price_end_units):
			addition_units = 10;
		break;
		case (price_current_units < percent90 && (price_current_units + 5) <= price_end_units):
			addition_units = 5;
		break;
		case (price_current_units < percent100 && (price_current_units + 1) <= price_end_units):
			addition_units = 1;
		break;
	}
	
	percent1 = price_end_tenths / 100;
	
	percent10 = percent1 * 10;
	percent30 = percent1 * 30;
	percent50 = percent1 * 50;
	percent70 = percent1 * 70;
	percent90 = percent1 * 90;
	percent100 = price_end_tenths;
	
	switch(true){
		case (price_current_tenths < percent10 && (price_current_tenths + 6) <= price_end_tenths):
			addition_tenths = 6;
		break;
		case (price_current_tenths < percent30 && (price_current_tenths + 5) <= price_end_tenths):
			addition_tenths = 5;
		break;
		case (price_current_tenths < percent50 && (price_current_tenths + 4) <= price_end_tenths):
			addition_tenths = 4;
		break;
		case (price_current_tenths < percent70 && (price_current_tenths + 3) <= price_end_tenths):
			addition_tenths = 3;
		break;
		case (price_current_tenths < percent90 && (price_current_tenths + 2) <= price_end_tenths):
			addition_tenths = 2;
		break;
		case (price_current_tenths < percent100 && (price_current_tenths + 1) <= price_end_tenths):
			addition_tenths = 1;
		break;
	}
	
	$('.price_btn').html('&euro; '+price_current_units+","+price_current_tenths);
	
	if(price_current_units < price_end_units || price_current_tenths < price_end_tenths){
		
		if(price_current_units < price_end_units){
			price_current_units = price_current_units + addition_units;
		}
		
		if(price_current_tenths < price_end_tenths){
			price_current_tenths = price_current_tenths + addition_tenths;
		}
		
		$(".itemcheck").attr('disabled', true);
		
		setTimeout("count_up("+price_current_units+","+price_end_units+","+price_current_tenths+","+price_end_tenths+")", 1);
		
		if(price_end_tenths == 0){
			$('.price_btn').append('0');
		}
	} else {
		if(price_end_tenths == 0){
			$('.price_btn').append('0');
		}
		$(".itemcheck").attr('disabled', false);
		$(".order_btn").animate({
			opacity: 10
		}, 1500 );
		$(".order_btn").css('cursor', 'pointer');
	}
}
