/*----------------------------------------------------------
Clear inputs on focus (finds all inputs with a title tag) 
------------------------------------------------------------*/

(function ($) {



$.fn.hint = function (blurClass) {
    if (!blurClass) blurClass = 'blur';

    return this.each(function () {
        // get jQuery instance of 'this'
        var $$ = $(this); 

        // get it once since it won't change
        var title = $$.attr('title'); 

        // only apply logic if the element has the attribute
        if (title) { 

            // Note this is a one liner

            // on blur, set value to title attr if text is blank
            $$.blur(function () {
                if ($$.val() == '') {
                    $$.val(title).addClass(blurClass);
                }
            })

            // on focus, set value to blank if current value matches title attr
            .focus(function () {
                if ($$.val() == title) {
                    $$.val('').removeClass(blurClass);
                }
            })

            // clear the pre-defined text when form is submitted
            .parents('form:first').submit(function () {
                if ($$.val() == title) {
                    $$.val('').removeClass(blurClass);
                }
            }).end()

            // now change all inputs to title
            .blur();

            // counteracts the effect of Firefox's autocomplete stripping the blur effect
            if ($.browser.mozilla && !$$.attr('autocomplete')) {
                setTimeout(function () {
                    if ($$.val() == title) $$.val('');
                    $$.blur();
                }, 10);
            }
        }
    });
};

})(jQuery);


$(function(){ 
			    // find all the input elements with title attributes
				$('input[title!=""]').hint();
			});

$(document).ready(function(){
	$("ul.primary li:last").addClass("last");
	$("ul.primary li:first").addClass("first");
	
});
function findValue(li) {
	if( li == null ) return alert("Sorry, you need to select a location from the list");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
	
	$('#suggestValue').val(sValue);
	//alert (sValue);
}
function productJump()
{
	if ($('#suggestValue').val()>0)
	{
		var theUrl="http://www.steenbergs.co.uk/product" + "/" + $('#suggestValue').val() + "/search";
		window.location = theUrl;
	} else
	{
		alert ("Sorry, you need to select a product from the list");
	}
		
}

function selectItem(li) {
	findValue(li);
	productJump();
}
function selectRecipe(li) {
	findValue(li);
	recipeJump();
}
jQuery(function() {
	$("#searchtxt").autocomplete(
		"/product/suggest/",
		{
			onItemSelect:selectItem,
			onFindValue:findValue,
			
			delay:100,
			minChars:3
			
		}
	);
	$("#recipes_search").autocomplete(
		"/recipes/suggest/",
		{
		   
			onItemSelect:selectRecipe,
			onFindValue:findValue,
			
			delay:100,
			minChars:3
			
		}
	);
	
});

$(function(){

	// Recipes - Ingredients add to cart
	$('div.recipes ul.shop_links input').click(function(){
		
		$list = $(this).parents('ul');
		$num_checked = $list.find('input:checked').length;
		
		if($num_checked > 0){
			$list.siblings('input.button.add').show();
		}else{
			$list.siblings('input.button.add').hide();
		}
		
	})

})


jQuery(function() {
	
	$("ul.shop_links li:odd").addClass("odd");
	$("#search_results dl:even").addClass("left");
	$("div.link_section dl:nth-child(odd)").addClass("left");
	
});

jQuery(function() {
	
	
	$("input.cancel").click(function(){
		$("#log_in").slideUp();
		return false;
	});
	$('#source_specify').hide();
	$('#source select option').click(function(){
		if( $(this).attr('class') == 'specify' ){
			$('#source_specify').slideDown();
		}else{
			$('#source_specify').slideUp();
		}
	});
	
	$("a.remove").click(function(e){
        e.preventDefault();
        var $this = $(this), link = $this.attr('href');
        $this.parent("td").parent("tr").fadeOut(function(){
                window.location = link;
        });
    }); 
	
	$("td.details a:last").click(function(){
		$(this).parent("td").parent("tr").slideup();
		return false;
	});
	$("a.button.update").click(function(){
		$("form#basketConf").submit();
	});
});

//jQuery(function() {
//	$('input.quantity').change(function(){
//		$('div.update').slideDown();
//	});
//});

jQuery(function() {
	
	$('.billing_form input.cancel').click(function(){
		$('div.billing_form').slideToggle();
		$('div.billing_address').slideToggle();
		return false;
	});
	$('.shipping_form input.cancel').click(function(){
		$('div.shipping_form').slideToggle();
		$('div.shipping_address').slideToggle();
		return false;
	});
	$('#shippingID').change(function(){
		$('form#shippingUpdate').submit();
	});
});

// ADDED in MARCH AMENDS
jQuery(function() {
	//$('dd.tooltip').hide();
	$('ul.products li dl:not([class*="open"])').hide();
	
	$('ul.products li a.clickme').click(function(){
		$(this).parent('li').siblings('li').children('dl:visible').hide();
		$(this).next('dl').show();
		return false;
	});
});

jQuery.extend(jQuery.easing,{
	easeInOutExpo: function(x,t,b,c,d){
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});
