var const_cbPhoto = 5;

function Cart()
{
}

Cart.add = function(ixPhoto, fBlackAndWhite)
{
  if (fBlackAndWhite === undefined)
    fBlackAndWhite = false;
  var rgb = decode64(Cart.getCookie("cartPhotos"));
  rgb += Cart.serializePhoto(ixPhoto);
  rgb += String.fromCharCode((fBlackAndWhite) ? 1 : 0);
  Cart.setCookie("cartPhotos", encode64(rgb), 300);
}

Cart.update = function(ixPhoto, fBlackAndWhite)
{
  var rgb = decode64(Cart.getCookie("cartPhotos"));
  for (i = 0; i < rgb.length; i += const_cbPhoto)
  {
    if (ixPhoto == Cart.deserializePhoto(rgb.substring(i, i + const_cbPhoto)))
    {
      rgb = rgb.substring(0, i + const_cbPhoto - 1) +
        String.fromCharCode((fBlackAndWhite) ? 1 : 0) +
        rgb.substring(i + const_cbPhoto);
      Cart.setCookie("cartPhotos", encode64(rgb), 300);
      return;
    }
  }
}

Cart.remove = function(ixPhoto)
{
  var rgb = decode64(Cart.getCookie("cartPhotos"));
  var i = 0;
  while (i < rgb.length)
  {
    if (ixPhoto == Cart.deserializePhoto(rgb.substring(i, i + const_cbPhoto)))
    {
      rgb = rgb.substring(0, i) + rgb.substring(i + const_cbPhoto);
      continue;
    }
    i += const_cbPhoto;
  }
  Cart.setCookie("cartPhotos", encode64(rgb), 300);
}

Cart.contains = function(ixPhoto)
{
  var rgb = decode64(Cart.getCookie("cartPhotos"));

  for (i = 0; i < rgb.length; i += const_cbPhoto)
  {
    if (ixPhoto == Cart.deserializePhoto(rgb.substring(i, i + const_cbPhoto)))
      return true;
  }
  return false;
}

Cart.isBlackAndWhite = function(ixPhoto)
{
  var rgb = decode64(Cart.getCookie("cartPhotos"));
  for (i = 0; i < rgb.length; i += const_cbPhoto)
  {
    if (ixPhoto == Cart.deserializePhoto(rgb.substring(i, i + const_cbPhoto)))
    {
      return rgb.charCodeAt(i + const_cbPhoto - 1) == 1;
    }
  }
  return false;
}

Cart.getCount = function()
{
  return decode64(Cart.getCookie("cartPhotos")).length / const_cbPhoto;
}

Cart.serializePhoto = function(ixPhoto)
{
  var rgb = String.fromCharCode(ixPhoto & 0xff);
  rgb += String.fromCharCode((ixPhoto >> 8) & 0xff);
  rgb += String.fromCharCode((ixPhoto >> 16) & 0xff);
  rgb += String.fromCharCode((ixPhoto >> 24) & 0xff);
  return rgb;
}

Cart.deserializePhoto = function(rgb)
{
  return rgb.charCodeAt(0) + (rgb.charCodeAt(1) << 8) +
    (rgb.charCodeAt(2) << 16) + (rgb.charCodeAt(3) << 24);
}

Cart.setCookie = function(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=/";
}

Cart.getCookie = function(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 "";
}
