﻿// Cookie functions
jQuery.cookie = {
    save: function(n, v, d) {
        var e = '';
        if (d) {
            var date = new Date();
            date.setTime(date.getTime() + (d * 86400000));
            e = '; expires=' + date.toGMTString();
        }
        document.cookie = n + '=' + v + e + '; path=/';
    },
    load: function(n) {
        n = n + '=';
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') { c = c.substring(1, c.length); }
            if (c.indexOf(n) == 0) { return c.substring(n.length, c.length); }
        }
        return null;
    },
    erase: function(n) { $.cookie.save(n, '', -1); }
};


// The variable used by the shopping list
var shoppingList = []; // shopping list (page id's) [[id,name,bggId],[...

// On page ready init shopping list and set cursor focus to search
$(function() {
	// Get shopping list from cookie
	var cookie = $.cookie.load('shopping_list_cookie');
	if (cookie != null && cookie != '') {
		shoppingList = cookie.split('#/#');
		if (shoppingList.length >= 0) {
			for (var x = 0; x < shoppingList.length; x++) {
				shoppingList[x] = shoppingList[x].split('#,#');
				shoppingList[x][1] = unescape(shoppingList[x][1]);
			}
		}
	}

	// If this is a game page show shoppinglist button
	if ((typeof (gameData) !== 'undefined') && (typeof (gameData.id) !== 'undefined')) { $('#shoppingListButton').bind('click', clickCart); }

	// delete buttons for the shoppingList list games
	$('div.shoppingListGame').live('click', function() { removeIdFromList($(this).attr('id').replace('game_', '')); });

	// Show the shopping list
	showCart();

	// Start cursor in searchbox
	$('#ctl00_searchTextBox')[0].focus();
});

// removeIdFromList code
//shoppingList = $.grep(shoppingList, function(i) { return i != gameId; });


// Handle click on "shopping cart"
function clickCart(event) {
	// Prevent submit
	event.preventDefault();

	// Remove or add game to shopping list
	var gameInList = false;
	for (var i = 0; i < shoppingList.length; i++) { if (gameData.id == shoppingList[i][0]) { gameInList = true; } }
    if (!gameInList) {
    	shoppingList.push([gameData.id, gameData.name, '']);
	}
    // Save cookie with shopping list
	$.cookie.save('shopping_list_cookie', getShoppingListForCookie(), 14);

	// Update cart
    showCart();
}
// Handle showing link to shopping list "cart"
function showCart() {
	if (shoppingList.length > 0) {
		var str = '';
		for (var x = 0; x < shoppingList.length; x++) { str += '<li><div id="game_' + shoppingList[x][0] + '" class="shoppingListGame sprite icon delete cursor">&shy;</div><a href="/game.aspx?id=' + shoppingList[x][0] + '">' + shoppingList[x][1] + '</a></li>' }
		$('#shoppingListContent').html('<ul>' + str + '</ul><p>&nbsp;</p><p><a href="/list.aspx">Find the best place to buy all games in shopping list</a></p>');
	} else {
		$('#shoppingListContent').html('<em>The shopping list is empty</em>.<br /><br />Add games you found to the shopping list by clicking the "Add to shopping list" button.');
	}
}
// Handle remove calls from the shopping list page
function removeIdFromList(id) {
	shoppingList = $.grep(shoppingList, function(i) { return i[0] != id; });
	$.cookie.save('shopping_list_cookie', getShoppingListForCookie(), 14);

	// Update cart
	showCart();
}
// Convert the shoppin glist to cookie format
function getShoppingListForCookie() {
	var str = '';
	if (shoppingList.length >= 0) {
		for (var x = 0; x < shoppingList.length; x++) {
			if (x != 0) {str += '#/#';}
			str += shoppingList[x][0] + '#,#' + escape(shoppingList[x][1]) + '#,#' + shoppingList[x][2];
		}
	}
	return str;
}