function productList(cookieName) {
	this.products = {};
	// SkuID used as index, value is an object storing 'name' and 'href'

	this.length = 0;
	this.hidden = true;
	this.cookieName = cookieName || 'productList';
}


productList.prototype.refresh = function () {
    // Refreshes the product list to ensure it's up to date with the current list

    var list, i, ogText, me = this;

    if (jQuery === undefined) {
        return false;
    } else {

        // Automatically hide later items if more than 5 total
        // Add hide/show button to toggle their visibility
        if (!$('#product-list-pod a.toggle').length) { // First refresh
            $('#product-list-pod ul').after('<a class="toggle closed" href="javascript:;"><span>Show all products in my list</span></a>');
            if (this.hidden == true) {
                $('#product-list-pod ul').children('li:gt(4)').hide();
            }
            ogText = $('#product-list-pod a.toggle span').text();
            $('#product-list-pod a.toggle').toggle(function () {
                $('#product-list-pod ul').children('li:gt(4)').slideDown(200);
                me.hidden = false;
                $(this).children('span').text('Collapse my product list');
                $(this).removeClass('closed');
                $(this).addClass('open');
            },
			function () {
			    $('#product-list-pod ul').children('li:gt(4)').slideUp(200);
			    me.hidden = true;
			    $(this).children('span').text(ogText);
			    $(this).removeClass('open');
			    $(this).addClass('closed');
			});
        }

        if (this.length) {
            list = '';

            for (i in this.products) {
                list += '<li data-skuid="' + i + '"><a href="' + this.products[i]['href'] + '">' + this.products[i]['name'] + '</a><input class="remove" /></li>';
            }

            // 'Remove' buttons
            $('#product-list-pod ul').html(list).find('input.remove').click(function () {
                me.remove($(this).parent().attr('data-skuid'));
                me.refresh();
            });

        } else {
            $('#product-list-pod ul').html('<li class="noproductlist">Want to know more about a product or check out availability? Add them to this list and send it through to us!</li>');
        }

        if (this.length > 5) {
            $('#product-list-pod a.toggle').show();
            if (this.hidden) {
                $('#product-list-pod ul').children('li:gt(4)').hide();
            }
        } else {
            $('#product-list-pod a.toggle').hide();
        }


        // 'Contact us' link
        list = '';
        for (i in this.products) {
            list += i + '.';
        }
        // Update 'skus' query string parameter
        $('#product-list-pod .inner > a.btn-180-small').attr('href', $('#product-list-pod .inner > a.btn-180-small').attr('href').replace(/(&|\?)skus=[^&]*/, '$1skus=' + list));

        return true;
    }
}

productList.prototype.save = function (cookieName) {
	// Saves the product list to a cookie

	var productString = '', i;
	this.length = 0;
	for (i in this.products) {
		productString += '+SkuID=' + i + '&SkuName=' + this.products[i]['name'] + '&href=' + this.products[i]['href'];
		this.length++;
	}
	createCookie(cookieName || this.cookieName, escape(productString.substring(1)), 30);
	return true;
}

productList.prototype.load = function (cookieName) {
	// Loads the product list from a cookie and applies it to the object

	var products, i, iMax;

	this.products = {};
	this.length = 0;

	if (readCookie(cookieName || this.cookieName)) {
		products = unescape(readCookie(cookieName || this.cookieName)).split('+');
		for (i = 0, iMax = products.length; i < iMax; i++) {
			products[i] = products[i].split('&');
			this.add(products[i][0].match(/[^=]+$/).toString(), products[i][1].match(/[^=]+$/).toString(), products[i][2].match(/[^=]+$/).toString());
		}
		return true;
	} else {
		return false;
	}
}

productList.prototype.add = function (SkuID, SkuName, href) {
	// Adds a new product to the list of products

	if (SkuID in this.products) {
		return false;
	} else {
		this.products[SkuID] = {
			'name': SkuName || '',
			'href': href || '#'};
		this.save();
		return true;
	}
}

productList.prototype.remove = function (SkuID) {
	// Removes a product from the list of products

	if (SkuID in this.products) {
		delete this.products[SkuID];
		this.save();
		return true;
	} else {
		return false;
	}
}


/*
// Thanks to Peter-Paul Koch for these cookie functions (http://www.quirksmode.org/js/cookies.html)
*/

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	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(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
