/* Put a shaded out tip in an input which disappears when on focus and returns on
 * blur if the field is blank 
 *
 * $('.default').inputTip(options);
 *
 * options:
 *   tip: By default the tip text is taken from the input when the page is loaded
 *        but this can be overriden
 *   tipClass: By default the class given to the input when the tip is being displayed
 *             is 'default'.
 */
$.fn.inputTip = function(options) {
	options = options || {};
	
  this.each(function() {	
		var input = $(this);
		var tip = options.tip || input.val();
		var tipClass = options.tipClass || 'default';
		
		$(this).focus(function() {
			if($(this).val() == tip) {
				$(this).val('').removeClass(tipClass);
			}
		});
		
		$(this).blur(function() {
			if($(this).val().replace(/\s+/,'') == '') {
				$(this).val(tip).addClass(tipClass);
			}
		});
  });
  return this;
};

$(function() {
	
	$('input').each(function() { $(this).addClass($(this).attr('type')); });
	
	$('#shopping-cart li:odd').addClass('even');
	
	$('#product-list a').hover(function() {
		if (!$(this).hasClass('selected')) {
			var title = $('.title', this);
			
			if (!($(title).attr('originalColour'))) {
				$(title).attr('originalColour', $(title).css('color'));
			}
			title.stop().animate({opacity: 1.0, color: '#ffffff'});
			$('img', this).stop().animate({opacity: 1.0});
		}
	}, function() {
		if (!$(this).hasClass('selected')) {
			var title = $('.title', this);
			title.stop().animate({opacity: 0.3, color: title.attr('originalColour') });
			$('img', this).stop().animate({opacity: 0.3});
		}
	});
	
	var c = $('#product-categories');
	
	$('#product-categories li:not(.selected)').hover(function() {
		$(this).stop().animate({borderBottomColor: $(this).css('border-top-color')});
	}, function() {
		$(this).stop().animate({borderBottomColor: '#000000'});
	});
	
	var selected = $('#product-list a.selected'), listWidth = $('#product-list').width();
	if (selected.length && selected[0].offsetLeft > listWidth) {
		$('#product-list .inner').css('left', listWidth - selected[0].offsetLeft - 150);
	}
	
	
	$('.ajax-post').click(function() {
		$.ajax({
			type: 'POST',
			dataType: 'script',
			url: $(this).attr('href'),
			beforeSend: function(xhr) { xhr.setRequestHeader('Accept', 'text/javascript'); }
		});
		return false;
	});
	
	$('.default').inputTip();
	
	var acceleration = 0;
	var velocity = 0;
	var max = 25;
	
	$('#product-list .prev, #product-list .next').mousedown(function() {
		acceleration = this.className == 'prev' ? 1 : -1;
	}).mouseup(function() {
		acceleration = acceleration * -0.75;
	}).click(function() {
		return false;
	});
	
	setInterval(function() {
		var before = velocity;
		velocity += acceleration;
		if (velocity > max) velocity = max;
		if (velocity < -max) velocity = -max;
		if (velocity == 0 || (velocity > 0 && before < 0) || (velocity < 0 && before > 0)) { velocity = 0; acceleration = 0; }
		var left = parseInt($('#product-list .inner').css('left'));
		if (left > 0) { left = 0; velocity = 0; acceleration = 0; }
		$('#product-list .inner').css({left: left + velocity});
	}, 50);
	
});
