/**
	bookId is a global variable to hold the book id of the last clicked book cover
*/
var bookId;

/** BELIEVE IT OR NOT THIS BREAKS IE
const BUY_PAGE = "audiobooks_buy.php";
*/

var BUY_PAGE = "audiobooks_buy.php";
var TOP_PAGE = "audiobooks.php";

/**
	Mutator method for this.bookId
*/
function setBookId(id) {
	this.bookId = id;
}

/**
	Adds a book to the cart in $_SESSION
*/
function addBook(bookId) {
	scriptPath = window.location.pathname;
	scriptPage = scriptPath.substring(scriptPath.lastIndexOf('/') + 1);
	if (scriptPage == TOP_PAGE) reaction = f_redirectBuyPage; 
	else reaction = f_drawCart;
	doGetHttpRequest("cart/cartajax.php", "func=addBook&bookId="+bookId, reaction);
}

/**
	Redirects to audiobooks_buy.php page
*/
var f_redirectBuyPage = function() {
	window.location = BUY_PAGE;
}

/**
	Decreases the quantity of a book by 1. Removes the book entirely if it has only 1 quantity.
*/
function removeBook(bookId) {
	doGetHttpRequest("cart/cartajax.php", "func=removeBook&bookId="+bookId, f_drawCart);
}

/**
	Removes a book from the cart entirely.
*/
function clearBook(bookId) {
	makeInvisible('checkoutButton');
	doGetHttpRequest("cart/cartajax.php", "func=clearBook&bookId="+bookId, f_drawCart);
}

/**
	Function to be called when a book is dropped into the cart.
*/
function bookDropped() {
	addBook(this.bookId);
}

/**
	Completly empties the cart.
*/
function emptyCart() {
	makeInvisible('checkoutButton');
	doGetHttpRequest("cart/cartajax.php", "func=emptyCart", f_drawCart);
}

/**
	"Re-Draws" the cart
	Fills the cartitems div with the resp of the last HttpRequest
*/
var f_drawCart = function() {
	fillElementHTML('cartItems', getResp());
}

/**
	"Re-Draws" the cart based on what the current cart inside $_SESSION is set to
	This function is called on page load to check if there are already items in the cart.
*/
function drawCart() {
	doGetHttpRequest("cart/cartajax.php", "func=cartToXml", f_drawCart);
}

/**
	Toggles the visibility of the cart.
	
	(Related reaction function: f_toggleCart)
	(Helper functions: showCart, hideCart)
*/
function toggleCart() {
	doGetHttpRequest("cart/cartajax.php", "func=toggleCart", f_toggleCart);
}
var f_toggleCart = function() {
	if (getResp() != 0) showCart();
	else hideCart();
}

/**
	Gets the current visibility of the cart.
*/
function getCartVisibility() {
	doGetHttpRequest("cart/cartajax.php", "func=getCartVisibility", f_toggleCart);
	/*
	var ajax = new AjaxRequest();
	ajax.doGetHttpRequest("cart/cartajax.php", "func=getCartVisibility", function() {
		if (ajax.getResp() != 0) showCart(); else hideCart();
		fillElementHTML('cartItems', ajax.getResp());
	});
	*/
}

function showCart() {
	displayBlock('expandedCart');
	displayNone('collapsedCart');
	var divs = document.getElementsByTagName('div');
	for (i=0; i<divs.length; i++) if (divs[i].id.substring(0, 4)=="book") divs[i].style.width = "500px";
	f_drawCart.call();
}

function hideCart() {
	displayBlock('collapsedCart');
	displayNone('expandedCart');
	var divs = document.getElementsByTagName('div');
	for (i=0; i<divs.length; i++) if (divs[i].id.substring(0, 4)=="book") divs[i].style.width = "720px";
}

/**
	Toggles the visibility of items which are out of stock.
	
	(Related reaction function: f_reloadPage)
*/
function toggleHideNonStocked() {
	doGetHttpRequest("manageAjax.php", "func=toggleHideNonStocked", f_reloadPage);
}
var f_reloadPage = function() {
	window.location.reload();
}

/**
	Toggles sorting items by popularity while within another genre.
	
	(Related reaction function: f_reloadPage)
*/
function toggleSortByPopularity() {
	doGetHttpRequest("manageAjax.php", "func=toggleSortByPopularity", f_reloadPage);
}

/**
	Toggles showing items by media type.
	
	(Related reaction function: f_reloadPage)
*/
function setShowMedia(mediaType) {
	doGetHttpRequest("manageAjax.php", "func=setShowMedia&mediaType="+mediaType, f_reloadPage);
}
	
/**
	Processes the order information for the checkout process.
	
	(Related reaction function: f_processOrder)
*/
function processOrder() {
	doGetHttpRequest("cart/cartajax.php", "func=cartToForm", f_processOrder);
}
var f_processOrder = function() {
	if (getResp()!=0) {
		makeVisible('processOrder');
		fillElementHTML('cartItems', "<div class='processmsg'>We are now processing your order. Please wait at least 30 seconds...</div><br />");
		fillElementHTML('fb', getResp());
		pushVars('cf');
	}
}

function super_init() {
	init_dragDrop();
	getCartVisibility();
}

// Prepares the drag and drop elements for the page
function init_dragDrop()
{	
	var drop = $('cart');
	
	$$('.bookStock').each(function(book) 
	{
		book.addEvent('mousedown', function(e) 
		{
			e = new Event(e).stop();
			
			var clone = this.clone()
				.setStyles(this.getCoordinates())
				.setStyles({'position':'absolute'})
				.addEvent('emptydrop', function() 
				{
					this.remove();
					drop.removeEvents();
				}).inject(document.body);

			drop.addEvents({
				'drop': function() {
					drop.removeEvents();
					clone.remove();
					bookDropped();
				}
			});
			

			var drag = clone.makeDraggable({
			droppables: [drop]
			});
	 
			drag.start(e);
		});
	});
}
/*
$(document).ready(function() {

	$(".dragBook").draggable({
		helper: 'clone',
		appendTo: 'body'
	});
	
	$(".cartbody").droppable({
		accept: ".dragBook",
		drop: function(ev, ui) {
			bookDropped();
		}
	});

});
*/