/**
 * @author Bizon
 */

$(document).ready(function(){
	$('#order_custom_quantity').numeric();
	var selectedCupcake = '';
	var cupcakeDescription = '';
	var cupcakeDescriptionHolder = $('#cupcake-description');
	var priceHolder = $('#price-holder');
	var customQuantityValue = '';
	var editMode = false;
	var selectedIcing = '';
	var imageHolder = $('#choose-image-holder');
	var icingColorHolder = $('#order_cupcake_icing_color_holder');
	var customQuantityHolder = $('.custom_quantity_holder');
	var placeInCupcakeBox = false;
	var lastImageDisplayed = '';
	var firstTimeLoad = true;
	
	//////////Change Cupcake Description   /////////////////
	changeCupcakeDescription = function()
	{
		selectedCupcake = $('#order_cupcake').val();
		cupcakeDescription = arCupcakesDescription[selectedCupcake];
		cupcakeDescriptionHolder.html(cupcakeDescription);
		changeCupcakeImage();
	};
	////////////////////////////////////////////////////////
	
	//////////   Change Cupcake Icing   ///////////////////////
	changeCupcakeIcingOptions = function()
	{
		if(typeof arCupcakesIcing[selectedCupcake] != "undefined")
		{
			$('#order_cupcake_icing').find('option').remove();
			$.each(arCupcakesIcing[selectedCupcake], function(i, val)
			{
				// edit mode option
				if (editMode == true && typeof thisCupcake['icing_id'] != "undefined")
				{
					if(val.id == thisCupcake['icing_id'])
					{
						$('#order_cupcake_icing').append($("<option selected='selected'></option>").attr("value",val.id).text(val.name));
					}
					else
					{
						$('#order_cupcake_icing').append($("<option></option>").attr("value",val.id).text(val.name));
					}
				}
				else // init mode
				{
					$('#order_cupcake_icing').append($("<option></option>").attr("value",val.id).text(val.name));
				}
			});
			$('#order_cupcake_icing_holder').show();
			selectedIcing = $('#order_cupcake_icing').val();
			checkCupcakeIcingExtra();
			changeCupcakeImage();
		}
		else
		{
			$('#order_cupcake_icing').find('option').remove();
			$('#order_cupcake_icing_holder').hide();
			selectedIcing = '';
			checkCupcakeIcingExtra();
		}
	};
	////////////////////////////////////////////////////////
	
	//////////   Change Cupcake Icing   ///////////////////////
	checkCupcakeIcingExtra = function()
	{
		if(typeof arCupcakesIcingExtra[selectedIcing] != "undefined")
		{
			switch(arCupcakesIcingExtra[selectedIcing])
			{
				case 'cupcake_vanilla_frosting_colors':
				{
					icingColorHolder.show();
					break;
				}
			}
		}
		else
		{
			icingColorHolder.hide();
		}
	};
	////////////////////////////////////////////////////////
	
	//////////   Change Cupcake Total Price  ////////////////////////
	changeCupcakeTotal = function()
	{
		var totalPrice = 0;
		if(typeof arCupcakesPrice[selectedCupcake] != "undefined")
		{
			customQuantityValue = $('#order_custom_quantity').val();
			if (typeof arCupcakesPrice[selectedCupcake]['label'] != "undefined" && arCupcakesPrice[selectedCupcake]['label'] == "custom_quantity")
			{
				totalPrice = parseFloat(arCupcakesPrice[selectedCupcake]['price'])*parseFloat(customQuantityValue ? customQuantityValue : 0);
				if (placeInCupcakeBox)
				{
					totalPrice += parseFloat(cupcakeBoxPrice) * parseFloat(customQuantityValue ? customQuantityValue : 0);
				}
			}
			else
			{
				totalPrice = parseFloat(arCupcakesPrice[selectedCupcake]['price']);
			}
			
			priceHolder.html('$'+totalPrice.toFixed(2));
		}
	};
	//////////////////////////////////////////////////////
	
	///   Change Cupcake Images   ///
	changeCupcakeImage = function()
	{
		if (firstTimeLoad && editMode) // edit mode (bug) for cupcake in a box image. [FIX]
		{
			checkCupcakeBoxOption(true);
			firstTimeLoad = false;
		}
		
		if (placeInCupcakeBox)
		{
			if (lastImageDisplayed != cupcakeInABoxImageUrl)
			{
				imageHolder.fadeOut(function(){
					imageHolder.html('<img src="'+cupcakeInABoxImageUrl+'" />');
					imageHolder.fadeIn();
				});
				
				lastImageDisplayed = cupcakeInABoxImageUrl;
			}
		}
		else if (typeof arImages[selectedCupcake] != "undefined")
		{
			if (typeof arImages[selectedCupcake]["icing"] != "undefined")
			{
				if (selectedIcing != '' && typeof arImages[selectedCupcake]["icing"][selectedIcing] != "undefined")
				{
					imageHolder.fadeOut(function(){
						imageHolder.html('<img src="'+arImages[selectedCupcake]["icing"][selectedIcing]+'" />');
						imageHolder.fadeIn();
					});
					
					lastImageDisplayed = arImages[selectedCupcake]["icing"][selectedIcing];
				}
				else
				{
					imageHolder.fadeOut();
					lastImageDisplayed = '';
				}
			}
			else
			{
				imageHolder.fadeOut(function(){
					imageHolder.html('<img src="'+arImages[selectedCupcake]+'" />');
					imageHolder.fadeIn();
				});
				
				lastImageDisplayed = arImages[selectedCupcake];
			}
		}
		else
		{
			imageHolder.html('');
			lastImageDisplayed = '';
		}
	};
	/////////////////////////////////////////////////////////////////////////////////////////

	checkCustomQuantityDisplay = function()
	{
		if(typeof arCupcakesPrice[selectedCupcake]['label'] != "undefined" && arCupcakesPrice[selectedCupcake]['label'] == "custom_quantity")
		{
			customQuantityHolder.show();
		}
		else
		{
			customQuantityHolder.hide();
			if(placeInCupcakeBox)
			{
				resetCupcakeBoxOption();
				changeCupcakeImage();
			}
		}
	};
	
	checkCupcakeBoxOption = function(onlyCheck)
	{
		if ($('#order_place_in_cupcake_box')[0].checked == true)
		{
			placeInCupcakeBox = true;
		}
		else
		{
			placeInCupcakeBox = false;
		}
		
		if(!onlyCheck)
		{
			changeCupcakeImage();
		}
	};
	
	resetCupcakeBoxOption = function()
	{
		$('#order_place_in_cupcake_box').attr('checked', false);
		placeInCupcakeBox = false;
	}
	
	///   Check Mode: Init (or) Edit   ///
	checkMode = function()
	{
		if (typeof thisCupcake != "undefined")
		{
			editMode = true;
		}
	};
	////////////////////////////////////////
	
	///   Init   ///
	checkMode();
	changeCupcakeDescription();
	changeCupcakeIcingOptions();
	checkCustomQuantityDisplay();
	checkCupcakeBoxOption();
	changeCupcakeTotal();
	///////////////////////////////
	
	///   When cupcake is changed   ///
	$('#order_cupcake').change(function(){
		changeCupcakeDescription();
		changeCupcakeIcingOptions();
		checkCustomQuantityDisplay();
		changeCupcakeTotal();
	});
	///////////////////////////////////////
	
	///   When Custom Quantity is changed   ///
	$('#order_custom_quantity').keyup(function(){
		changeCupcakeTotal();
	});
	/////////////////////////////////////////////
	
	///   When Icing is changed   ///
	$('#order_cupcake_icing').change(function(){
		selectedIcing = $('#order_cupcake_icing').val();
		checkCupcakeIcingExtra();
		changeCupcakeImage();
	});
	/////////////////////////////////////////////
	
	///   When Place In Cupcake Box is checked   ///
	$('#order_place_in_cupcake_box').click(function(){
		checkCupcakeBoxOption();
		changeCupcakeTotal();
	});
});

