var optionsPerColumn = 10;
var columns = 3;

function resetInput(input)
{
	$(input).val(input.defaultValue);
}

function resetInputIfBlank(input)
{
	if($.trim($(input).val()) == '')
	{
		resetInput(input);
		$(input).css('color', '#aaa');
	}
}

function clearDefaultValue(input)
{
	clearValue(input, input.defaultValue);

	$(input).css('color', '#333');
}

function clearValue(input, valueToClear)
{
	if($(input).val() == valueToClear)
		$(input).val('');
}

function showAllOptions(arrayName, selectedGenders, selectedHouses, selectedBrands, selectedTypes, selectedStock, selectedSizes, priceLower, priceUpper, minimumStockLevel)
{
	var title;
	var options;

	switch(arrayName)
	{
		case 'house':
			options = house;
			title = 'Select House';
			break;
		case 'brand':
			options = brand;
			title = 'Select Brand';
			break;
		case 'type':
			options = type;
			title = 'Select Type';
			break;
		case 'category':
			options = category;
			title = 'Select Category';
			break;
		case 'sizes':
			options = sizes;
			title = 'Select Size';
			break;
	}

	var totalOptions = options.length;

	var optionToShowPerColumn = Math.max(optionsPerColumn, Math.ceil(totalOptions / columns));

	// build the html, each column is a list with each item in the column a list item
	var html = '<form method="post">\n\
		<input type="hidden" name="gender" value="'+selectedGenders+'" />';

	if(arrayName != 'house')
		html += '<input type="hidden" name="house" value="'+selectedHouses+'" />';

	if(arrayName != 'brand')
		html += '<input type="hidden" name="brand" value="'+selectedBrands+'" />';

	if(arrayName != 'type')
		html += '<input type="hidden" name="type" value="'+selectedTypes+'" />';
	
	if(arrayName != 'size')
		html += '<input type="hidden" name="size" value="'+selectedSizes+'" />';

	html += '<input type="hidden" name="stock" value="'+selectedStock+'" />';
	html += '<input type="hidden" name="priceLower" value="'+priceLower+'" />';
	html += '<input type="hidden" name="priceUpper" value="'+priceUpper+'" />';
	html += '<input type="hidden" name="stockLevel" value="'+minimumStockLevel+'" /><ul>';

	var option;
	var optionCount = 0;

	for(var i in options)
	{
		if(optionCount == optionToShowPerColumn)
		{
			html += '</ul><ul>';
			optionCount = 0;
		}

		option = options[i];

		html += '<li><label><input type="checkbox" name="'+arrayName+'[]" value="'+option.ID+'"'+(option.IsSelected ? ' checked="checked"' : '')+' />'+option.Name+' ('+option.Count+')</label></li>';

		optionCount++;
	}

	html += '</ul></form>';

	viewAllDialog.dialog({
			'title': title
		})
		.html(html)
		.dialog('open');
}

// standard error handler for all ajax calls
$(document).ajaxError(function(event, xhr, options, error) {
	addBadMessage('ERROR: '+xhr.responseText);
});

function addToFavourites(id, sku)
{
	var data = {
			'action': 'addToFavourites',
			'id': id,
			'sku': sku
		};

	$.get('/ajax.php', data, function(data, textStatus, xhr) {addToFavouritesSuccess(data, textStatus, xhr);}, 'html');
}

function addToFavouritesSuccess(data, textStatus, xhr)
{
	// set the alt and title values for the star image
	$('#favouriteStarImage').attr({
			'title': 'Favourite SKU',
			'alt': 'Favourite SKU'
		});

	// hide the add to favourites link, show the remove link
	$('#addFavourite').css('display', 'none');
	$('#removeFavourite').css('display', 'inline');

	// update the favourite skus link to show the updated number
	$('#favouriteSkus a span').html(data);

	addGoodMessage('The SKU has been added to your favourite SKUs');
}

function removeFromFavourites(id, sku)
{	
	var data = {
			'action': 'removeFromFavourites',
			'id': id,
			'sku': sku
		};

	$.get('/ajax.php', data, function(data, textStatus, xhr) {removeFromFavouritesSuccess(data, textStatus, xhr);}, 'html');
}

function removeFromFavouritesSuccess(data, textStatus, xhr)
{
	// set the alt and title values for the star image
	$('#favouriteStarImage').attr({
			'title': 'Add to Favourites',
			'alt': 'Add to Favourites'
		});

	// show the add to favourites link, hide the remove link
	$('#addFavourite').css('display', 'inline');
	$('#removeFavourite').css('display', 'none');

	// update the favourite skus link to show the updated number
	$('#favouriteSkus a span').html(data);

	addGoodMessage('The SKU has been removed from your favourite SKUs');
}

function addGoodMessage(msg)
{
	// remove any good/bad messages
	$('#middle .goodMsg, #middle .badMsg').remove();

	$('#middle #right').after('<div class="goodMsg">'+msg+'</div>');
}

function addBadMessage(msg)
{
	// remove any good/bad messages
	$('#middle .goodMsg, #middle .badMsg').remove();

	$('#middle #right').after('<div class="badMsg">'+msg+'</div>');
}

function showHideAddressInput(input)
{
	var display = ($(input).val() == '-1' ? '' : 'none');

	$('#newDeliveryAddress').css('display', display);
}

function showHideDeliveryAddressSelect(value)
{
	var display = (value == 0 ? '' : 'none');

	$('#delivery').css('display', display);
}
