/*
----------------------------
Javascript for FoodSourceInc.com Ingredients Page
Author: Ty Cohen
Thank you to the jQuery team!
----------------------------
*/

// Long Description Slider
// -----------------------------
// This function hides all of the long descriptions
// and toggles their visibility with the Title anchor
$(document).ready(function() {
	$('p.long').hide(); // Hide all of the Long Descriptions
	// Toggle them to slide up and down with the Title anchor
	$('div.subcategory_list a.showinfo').toggle(
		function() {
			$(this).parent().next('p.long').slideDown('slow');
			$(this).html('Hide Info');
			return false;
		}, 
		function() {
			$(this).parent().next('p.long').slideUp('slow');
			$(this).html('More Info');
			return false;
		});
});

// Quick Ingredients Search on SideBar
// --------------------------------------------
// Sets default text and clears the quick ingredients search, and more.
$(document).ready(function() {
	// Set the defaut text
	var defaultText = "Search for..."
	$("#quicksearch-keyword").val(defaultText);
	
	// When clear the search box when in focus
	$("#quicksearch-keyword").focus(function () {
		if ($("#quicksearch-keyword").val() == defaultText) {
			$(this).val("");	
		}
	})
	// When search box is blurred, check to see if a value was entered
	// If not, then put the default text back
	$("#quicksearch-keyword").blur(function() {
		if ($(this).val() == "" || $(this).val() == " ") {
			$(this).val(defaultText);		
		}
	});
});

// Fade Ingredient Subcategory
// -----------------------------
// This function fades each subcategory in and out.
$(document).ready(function() {
	var subElements = $('div.subcategory');
	$('div#subcategory-container').css('position', 'relative');
	for ( var i = 0; i < subElements.length; i++ ) {
		$(subElements[i])
			.css('z-index', String(subElements.length-i)).css('position', 'relative');
	};
	$('div.subcategory').hide(); // Hide all of the Subcategories
	// If there is a default class on one Subcategories of the show that one.
	if ($('.subcategory.default').size() > 0) {
		$('.subcategory.default').eq(0).show();
	}
	else {
	// Else show the first Subcategory of all of them.
		$('.subcategory').eq(0).show();
	}

	// Now we deal with the Subcategory Buttons
	$('#dropdown').change(function() {
			var buttonValue = $(this).val();
			// Show the loading gif
			$('#subcategory-loading').show();
			// Hide the loader in a couple seconds
			var t = setTimeout("$('#subcategory-loading').hide()", 500);
			// Fade out the visible Subcategory Div
			$('.subcategory:visible').fadeOut('normal');
			// Use the name attribute to fade in the correct Div
			$('#subcategory_'+buttonValue).fadeIn('normal');
	});
});



// This function will preform the ajax request
$(document).ready(function() {
	$('a.add_sample').bind('click',function () {
		var addLinkID = $(this).attr('id');
		var addSplitID = addLinkID.split('_');
		var theID = addSplitID[1];
		var randomnumber=Math.floor(Math.random()*110);
		$(this).after('<div class="loading"><img alt="Loading" src="/img/ajax_loading.gif"></div>')
		$(this).hide();
		$.get("/addtocart_ajax.asp",
				{ itemid : theID, smp : randomnumber },
				function(data) {
					var randomnumber=Math.floor(Math.random()*110);
					$('.loading').after('<p class="sample_added">Item Added</p><a class="view-sample-bag" href="/samplebag.asp?smp=' +randomnumber +'">View Sample Bag</a>');
					$('.loading').remove();
					var cartItemNumber = "1 item";
					if (parseFloat(data) > 1) {
						cartItemNumber = data + " items";
					}
					else {
						cartItemNumber = "1 item";
					}
					$('#summary').html('You have <a href="samplebag.asp?smp=' + randomnumber + '">' + cartItemNumber + '</a> in your sample bag.');
				});
		return false;
	});
})