$(document).ready(function() {

	Shadowbox.init();
	
	// body classes:
	$('body').addClass('os_' + $.client.os.toLowerCase());
	$('body').addClass('browser_' + $.client.browser.toLowerCase());
	$('body').addClass('js');
	
	// link up all the local stuff
	$.localScroll({offset:-100});

	// remove failure stuff if they start editing things.
	$('input').change(function(e) {
		$(e.currentTarget).parents('.validation_failed').removeClass('validation_failed');
	});

	add_products_animation.init();
	
	
	
	// add tooltips to the foooter 
	$('.js_member_tooltip').mouseenter(function(){
		$('.js_member_tooltip_tip').show();
	});

	$('.js_member_tooltip').mouseleave(function(){
		$('.js_member_tooltip_tip').fadeOut();
	});	
	
	
	// these two make the dropdown work!
	
	$('.js_header_range_trigger').addClass('droppedonthehead');
	

	$('.js_header_range_trigger').mouseenter(function(){
		$('.js_header_range_drop').slideDown();

	});

	$('html').click(function(e){

		// check if we're searching
		if (!$(document.activeElement).hasClass('js_stopdropcancel')){
			$('.js_header_range_drop').hide();		
		}

	});

	
	// now we do the funy thing with hte footer fun
	/*
		Not allowed to do this. AWESOME.
				$('.js_footer_sw_realised').animate({
		    opacity: 0,
		    left: '10px'
		  });
		
		$('.js_footer_sw').mouseenter(function(){
			$('.js_footer_sw_realised').animate({
		    opacity: 1,
		    left: '0'
		  });
		});
	
		$('.js_footer_sw').mouseleave(function(){
			$('.js_footer_sw_realised').animate({
		    opacity: 0,
		    left: '10px'
		  });
		});	
	*/



  $('.js_carousel').jcarousel({
	  scroll: 1,
    auto: 5,
    wrap: 'circular',
    initCallback: mycarousel_initCallback
  });

});

function mycarousel_initCallback(carousel)
{
 
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};
 

var add_products_animation = {

	init : function(){
		
		$('form.js_add_to_cart').bind('submit', function(e){
			// don't prevent default here, as we actually do want it to submit
			e.preventDefault();
			$('#add_to_cart_buttons').addClass('submitting');
		});	
	}
}


var ajax_handlers = {
	global: {
		// this is global
		start: function(message, data) {
			if ($('.ticker').length > 0)
			{
				$('.ticker').show();
			}
		},

		// this is global, ie over all ajax requests.
		stop: function(message, data) {
			if ($('.ticker').length > 0)
			{
				$('.ticker').hide();
			}
		},
		
		error: function(message, data) {

		}
	},
	
	local: {
		complete: function(event, xhr, ajax_options) {

		},

		success: function(data, code, xhr, form) {
			// first lets do the add to cart. 
			if (data.data.action == 'add_to_cart')
			{
				$('#add_to_cart_buttons').removeClass('submitting');
				$('#add_to_cart_buttons').addClass('submitted');
				$('#add_to_cart_buttons').find('.followon').hide().fadeIn(500);

				/* change again if necessary */
				$('#quantity_input').change(function(e){
					$('#add_to_cart_buttons').removeClass('submitted');
					$('#add_to_cart_buttons').find('.followon').hide();
				});
			}

		},

		error: function(message, data) {

		}
	}
}
