var basket_max = 8;
var shelfStatus = "down";

$(function() {
	$(".mast").show();
});

function ShrinkShelf() {
	shelfStatus = "down";
	$.session("shelfStatus", shelfStatus);
	$('#shelf').height(47);

	$('#shelf .shelfremove').hide();
	$('#ancExpandShelf').show();
	$('#ancShrinkShelf').hide();
}

function ExpandShelf() {
	shelfStatus = "up";
	$.session("shelfStatus", shelfStatus);
	$('#shelf').height(147);

	$('#shelf .shelfremove').show();
	$('#ancExpandShelf').hide();
	$('#ancShrinkShelf').show();
}

function clearBasket() {
	$('#ulBasket').html('');
	
	RemoveAllFromBasket();
}

function RemoveFromBasket(book_ID) {
	$('#ulBasket #li_' + book_ID).remove();
	ret = PageMethods.BasketRemove(book_ID, OnCompleteBasketRemove, OnError, OnTimeOut);
	return false;
}

function RemoveAllFromBasket() {
	ret = PageMethods.BasketRemoveAll(OnCompleteBasketRemoveAll, OnError, OnTimeOut);
	return false;
}

function DisplayInBasket(item) {
	// Get the Book ID and image object
	var idx = item.indexOf('_');
	var id = 0;
	var imgSrc = $('#' + item);
	
	if (idx > -1)
		id = item.substring(idx + 1);
	else
		id = item;

	// Check if the book already exists
	if ($('#li_'+id).length == 0) {
		var cnt = $('#ulBasket li').length;
		
		if (cnt < basket_max) {
			$('#ulBasket').append('<li id="li_' + id + '" class="basketItem"><a href="bookdetails.aspx?BookID=' + id + '"><img src="' + imgSrc.attr('src') + '" id="img_' + id + '" /></a> <a href="#" onclick="return(RemoveFromBasket(\'' + id + '\'));"><img src="gfx/shelf_btn_remove.png" class="shelfremove" /></a></li>');
		} else if (cnt == basket_max) {
			$('#ulBasket').append('<li id="li_more"><a href="basket.aspx" alt="View the entire Basket">View more&hellip;</a>');
		}
	}
	else {
		// imgSrc.effect('shake');	// Book exists - give feedback
		ShowMessageBox('This book is already in your basket', 1500);
	}
	
	return AddToBasket(id, 1);
}

function AddToBasket(book_ID, quantity) {
	ret = PageMethods.BasketAdd(book_ID, quantity, OnCompleteBasket, OnError, OnTimeOut);
	return false;
}

function OnCompleteBasketRemove(arg) {
	$('#spnBasket').html(arg);
	
	if ($('#ulBasket #li_more').length > 0 && arg <= basket_max)
		$('#ulBasket #li_more').remove();
}

function OnCompleteBasketRemoveAll(arg) {
	$("#spnBasket").html("0");
}

function ShowMessageBox(msg,time) {
	$('#spnMessage').html(msg);
	
	$('#divMessage').css({ position: 'absolute',
		top: parseInt(($(window).height() / 2) - ($('#divMessage').height() / 2)) + $(window).scrollTop() + 'px',
		left: parseInt(($(window).width() / 2) - ($('#divMessage').width() / 2)) + $(window).scrollLeft() + 'px'
	});
	
	$('#divMessage').show();
	setTimeout("$('#divMessage').fadeOut('slow')", time);
}

function OnCompleteBasket(arg) {
	if (arg == -1) {
		ShowMessageBox('This book already exists in your basket', 1500);
	} else {
		ShowMessageBox('Added to your basket', 1500);

		$("#spnBasket").html(arg);
	}
}

function OnTimeOut(arg) {
	alert("System warning.  The server has taken too long to add your book, please try again.");
}

function OnError(arg) {
	alert("System warning.  There has been a server error trying to add your book, please try again.");
}

