$(function(){
	
/*
	$('#nav ul').superfish({
		animation:{height:'show'},
	});
*/

	// Scrolling basket
	var scrollWrapper = $('#basket-column-wrapper');
	var scrollWrapperContainer = scrollWrapper.parents('.container_16');
	if (scrollWrapper.length) {
		// Handle the scrolling scroller!
		var msie6 = $.browser == 'msie' && $.browser.version < 7;
		if (!msie6) {
		    var top = scrollWrapper.offset().top - parseFloat(scrollWrapper.css('margin-top').replace(/auto/, 0));
		    var origCssTop = parseFloat(scrollWrapper.css('top'));
		    var containerHeight = scrollWrapperContainer.height();
		    
		    $(window).scroll(function (event) {
		      	// what the y position of the scroll is
		      	var y = $(this).scrollTop();

		      	// whether that's below the form
		      	if ((y + scrollWrapper.outerHeight()) + origCssTop >= containerHeight) {
		      		scrollWrapper.removeClass('fixed');
							scrollWrapper.css('top', function(){ return (containerHeight - top) - scrollWrapper.outerHeight(); });
		  			} else if (y >= top) {
		  				// if so, ad the fixed class
							scrollWrapper.attr('style', '');
		        	scrollWrapper.addClass('fixed');
		      	} else {
		        	// otherwise remove it
		        	scrollWrapper.removeClass('fixed');
		      	}
		    });
		}
	}
	
	
	// AJAX add to basket
	$('.ajax-add-to-basket').click(function(e){
	
		e.preventDefault();
		
		var submitButton = $(this);
		
		var data = {
			basket_item: {
				product_id: $(this).val(),
				qty: $(this).prev('.quantity').val()
			}
		};
		
		$.ajax({
			url: '/basket/add_item',
			type: 'POST',
			data: data,
			dataType: 'json',
			success: function(response) {
				
				// Shrinks the number in the basket widget, updates it and expands it back.
				$('div#basket_left').hide('clip', function(){
					$('div#basket_left').html(response.basket_items).show('clip');
				});
				
				// Change button to Added! and then bring it back after 2 seconds.
				submitButton.hide('fade', function(){
					submitButton.attr('src', '/images/small_added.png').show('fade').delay(2000).hide('fade', function(){
						submitButton.attr('src', '/images/small_addtobasket.png').show('fade');
					});
				});
				
			}
		});
	});
	
	
	// Contact form
	$('#enquiry_form').submit(function(e){
		e.preventDefault();
		
		var form = $(this);
		
		var data = {
			name: $('#name').val(),
			email: $('#email').val(),
			enquiry: $('#enquiry').val()
		};
		
		$.ajax({
			url: '/send_email.php',
			type: 'POST',
			data: data,
			beforeSend: function(){
				$('.ajax_loader').show();
			},
			success: function(response){
				if (response == '')
				{
					form.slideUp(600, function(){
						$('#enquiry_sent').slideDown(600);
					});				
				}
				else
				{
					alert('Please check the details entered. You must provide a Name, valid Email Address and details of your enquiry.');
				}
			},
			complete: function(){
				$('.ajax_loader').hide();
			}
		});
	});
	 
});
	
