$.fn.faderEffect = function(options){
	options = jQuery.extend({
		fadeType: 'fadeIn', // or fadeOut
		count: 3, // how many times to fadein
		speed: 500, // speed of fadein
		callback: false // call when done
	}, options);
	
	return this.each(function(){
		
		// if we're done, do the callback
		if (0 == options.count) 
		{
			if ( $.isFunction(options.callback) ) options.callback.call(this);
			return;
		}
		
		var callAgain = function(){
			options.count = options.count - 1; // countdown
			$(this).faderEffect(options);
		};

		// fade, and call again
		switch(options.fadeType)
		{
		case 'fadeIn':
			// hide so we can fade in
			if ( $(this).is(':visible') ) $(this).hide();
			$(this).fadeIn(options.speed, callAgain);
			break;
		case 'fadeOut':
			// show so we can fade out
			if ( !$(this).is(':visible') ) $(this).show();
			$(this).fadeOut(options.speed, callAgain);
			break;
		default:
		 // nothing
		}
	});
}

function newAlert(msg)
{
	// if ie6, position it
	if ( 'absolute' == $('#topAlert').css('position') ) $('#topAlert').css('top', $(document).scrollTop() );
	
	$('#topAlert')
	.html('<p>' + msg + '</p>')
	.showAlert();
}

$.fn.showAlert = function(){
	return this.each(function(){
		var self = this;
		
		var text = $(self).text();
		var aText = text.split(' ');
		var ms = aText.length * 500;

		$(self).faderEffect({
			callback: function(){
				setTimeout(function(){
					$(self).faderEffect({
						fadeType: 'fadeOut',
						count: 1
					});
				}, ms);
			} // callback
		});
	});
};

var fieldsToCopy = new Array('item_name', 'item_number', 'amount');

var numberCartItems = function () {
	for (var j = 0; j<$('#cart input[name^=item_number]').length; j++) {		
		for (var i = 0; i<fieldsToCopy.length; i++) {
			var thisInput = $('#cart').find('input[name^=' + fieldsToCopy[i] + ']').eq(j);
			var newName = $(thisInput).attr('name').replace(/_[0-9]+$/, '');
			newName = newName + "_" + (parseInt(j) + 1);
			$(thisInput).attr('name',  newName);
		}
	}
	if ( $('#cartList li').length ==0 ) {
		$('#cartSubmit').hide();
		$('<li id="cartIsEmpty">There are no items in your cart.</li>').appendTo('#cartList');
	} else {
		$('#cartSubmit').show();
		$('#cartIsEmpty').remove();
	}
}

$(document).ready(function() {
	$('.release form').submit(function(e) {
		if ( $('#cart input[value=' + $(this).find('input[name=item_number]').val() + ']').length > 0 ) {
			newAlert('Item is already in your cart.');
			return false;
		}
		
		var newCartItem = $('<li class="cartItem"><input type="button" class="cartRemoveItem button" value="remove" /> ' + $(this).find('input[name=item_name]').val() + ' - ' + $(this).find('input[name=amount]').val() + '</li>').appendTo('#cartList');

		$(newCartItem).find('input').click(function() {
			var confirmation = confirm('delete this item?');
			if (confirmation) $(this).parents('li').remove();
			numberCartItems();
		});
		
		for (var i = 0; i<fieldsToCopy.length; i++) {
			$(this).find('input[name=' + fieldsToCopy[i] + ']').clone(true).appendTo(newCartItem);
		}
		
		numberCartItems();

		newAlert('The item has been added to your shopping cart. <a href="#" class="cartToggle">View your Cart</a>');

		return false;
	});

	$('#cartSubmit').hide();
//	$('#cart').hide();

	$('.cartToggle').live('click', function(){
		$('#viewCart').click();
		return false;
	})
});