// DHTML & javscript implementation for YummyMummy

// make sure we can use both jQuery and prototype
jQuery.noConflict();

//===== START: drop down menu =====//
jQuery(document).ready(function ($) {
	var dropDown1_cursorOnParent = 0;
	var dropDown1_cursorOnChild = 0;
	
	// function for highlight #navigation_item1
	var hightlight_navitem1 = function() 
	{
		$('#navigation li.first a').css('background','#f15bab url(images/navigation_bg_onlineStore_hover.gif) no-repeat;');
	};
	// function for de-highlight #navigation_item1
	var no_hightlight_navitem1 = function() 
	{
		$('#navigation li.first a').css('background','#e798c2 url(images/navigation_bg_onlineStore_out.gif) no-repeat;');
	};
	
	// function trigger when hover into #dropDown1
	var show_dropDown1_child = function() 
	{
		dropDown1_cursorOnChild = 1;
		show_dropDown1 = 1;
		$('#dropDown1').show();
		hightlight_navitem1();
	};
	// function trigger when hover out #dropDown1
	var hide_dropDown1_child = function() 
	{
		dropDown1_cursorOnChild = 0;
		if (dropDown1_cursorOnParent==0 && dropDown1_cursorOnChild==0) 
		{
			$('#dropDown1').hide();
			no_hightlight_navitem1();
		};
	};
	
	// function trigger when hover into #navigation_item1
	var show_dropDown1_parent = function() 
	{
		dropDown1_cursorOnParent = 1;
		$('#dropDown1').slideDown('fast');
		hightlight_navitem1();
	};
	// function trigger when hover out #navigation_item1
	var hide_dropDown1_parent = function() 
	{
		dropDown1_cursorOnParent = 0;
		if (dropDown1_cursorOnParent==0 && dropDown1_cursorOnChild==0) 
		{
			$('#dropDown1').hide();
			no_hightlight_navitem1();
		};
	};
	
	// scripts execution start
	// hide dropdown by default
	$('#dropDown1').hide();
	
	// register hover event on #dropDown1. use plug-in hoverIntent to "slow down" the script
	$('#dropDown1').hoverIntent({
		sensitivity: 100, // sensitivity threshold (must be 1 or higher)
		interval: 10, // milliseconds for onMouseOver polling interval
		over: show_dropDown1_child, // onMouseOver callback function
		timeout: 100, // milliseconds delay before onMouseOut
		out: hide_dropDown1_child // onMouseOut callback function
	});

	// register hover event on #navigation_item1. use plug-in hoverIntent to "slow down" the script
	$('#navigation_item1').hoverIntent({
		sensitivity: 100, // sensitivity threshold (must be 1 or higher)
		interval: 10, // milliseconds for onMouseOver polling interval
		over: show_dropDown1_parent, // onMouseOver callback function
		timeout: 100, // milliseconds delay before onMouseOut
		out: hide_dropDown1_parent // onMouseOut callback function
	});
});
//===== START: drop down menu =====//


//===== START: bodywrap add to cart =====//
jQuery(document).ready(function () {
	// function to remove a cart item
	var bodywrap_removefromcart = function(itemNumber)
	{
		jQuery('#cart_item'+itemNumber).fadeOut(
			'normal', 
			function()
			{
				jQuery('#cart_item'+itemNumber).remove();
				// check if there is no item left
				var itemInCart = jQuery('.cartFull').length;
				if (itemInCart == 0)
				{
					jQuery('#cart_empty').show();
					jQuery('#cart_add').hide();
				}
			}
		);
	};

	// function to create a cart item
	var bodywrap_addtocart = function(itemNumber, price, quantityMax, imageFullpath)
	{
		var itemExist = jQuery('*').index(jQuery('#cart_item'+itemNumber)[0]);
		if (itemExist == -1)
		{
			// add new cart item
			var newItemHTML = 
			'<div id="cart_item'+itemNumber+'" class="cartFull">' +
				'<dl>' +
					'<dd>Cost Per Item: <span class="price">'+price+'</span></dd>' +
					'<dd class="quantiy">' +
						'<strong>Quantiy:</strong>' +
						'<select id="quantity'+itemNumber+'" name="quantity_add_'+itemNumber+'">';
			if (quantityMax > 10)
				var useQuantityMax = 10;
			else
				var useQuantityMax = quantityMax;
			for(i=1; i<=useQuantityMax; i++)
			{
				if (i==1)
					var selected = 'selected';
				else
					var selected = '';
				var optionHTML = 
							'<option value="'+i+'" '+selected+'>'+i+'</option>';
				newItemHTML = newItemHTML + optionHTML;
			};
			newItemHTML = newItemHTML +
						'</select>' +
					'</dd>' +
					'<dd class="removeItem">x <a href="javascript:foo()" id="cart_remove'+itemNumber+'" title="Remove item">Remove item</a></dd>' +
					'<dt><a href="javascript:foo()" title=""><img id="image_'+itemNumber+'" src="'+imageFullpath+'" alt="" width="80" height="80" border="0" /></a></dt>' +
				'</dl>' +
			'</div>';	 
			
			jQuery('#cart_empty').hide();
			jQuery('#cart_add').html('<input type="image" src="images/internal_button_addToCart.gif" width="107" height="23" alt="Add To Cart" />').show();
			// Note that after the fadeIn() function I hide and show the image again because there is some image cache/display issue on IE6
			jQuery('#cart_content').prepend(newItemHTML).find('#cart_item'+itemNumber).hide().fadeIn('normal', function(){jQuery('#image_'+itemNumber).hide().show()});
			
			// register the click event on the newly added cart item for remove a cart item
			jQuery('#cart_remove'+itemNumber).click(function(event) {
				bodywrap_removefromcart(itemNumber);
			});
		}
		else
		{
			// increment quantity option only if there is space to increase
			var totalOptions = jQuery('#quantity'+itemNumber)[0].length;
			var currentOption = jQuery('#quantity'+itemNumber)[0].selectedIndex + 1;
			//alert(currentOption+'/'+totalOptions);
			if (currentOption < totalOptions)
			{
				jQuery('#quantity'+itemNumber)[0].selectedIndex = currentOption;
			}
			jQuery('#quantity'+itemNumber).hide().fadeIn('normal');
		}
	};
	
	// scripts execution start
	// click on a swatch to add a cart item
	jQuery('.jTip').click(function(event) {
		var itemID = this.id;
		var itemNumber = jQuery('#'+itemID).children('span.s_itemNumber').html();
		var price = jQuery('#'+itemID).children('span.s_price').html();
		var quantityMax = jQuery('#'+itemID).children('span.s_quantityMax').html();
		var imageFullpath = jQuery('#'+itemID).children('span.s_imageFullpath').html();
		bodywrap_addtocart(itemNumber,price,quantityMax,imageFullpath);
	});
});
//===== END: bodywrap add to cart =====//


//===== START: misc function =====//
	// do nothing
	function foo() {};
	// do nothing
	function reloadPage() {
		window.location.reload(true);
	};
//===== END: misc function =====//
