/*
	Wishlist object - implements image wishlist.

	Cookies: the wishlist saves the following cookies:
			wishlist.lastOriginalArtwork
			wishlist.lastShownArtwork
			wishlist.zoom			
		
	HTML requirements:
		Required is existence of: div id 'main', img id 'mainImage'.
		Optinal is existence of: div id 'controlPanel', div id 'wishlistTrigger'.
		
	Page parameter requirements: page must accept these parameters:
			artwork: artwork id, a number. This is used when wishlist must reload
				the page with a different artwork.
			wishlist: value='on' means start wishlist immediately on page load
			
	Callback requirement:
		The page must call wishlist.onResize() whenever window size has changed.
		Example: window.onresize = function onResize(){ myWishlist.onResize(); };

	Copyright (c) 2009 Taitl Design, LLC. All rights reserved.
*/

/*
TODO:
*/

// Wishlist constructor
function Wishlist(objectName, pageName)
{
	// Constants
	this.COOKIE_NAME = 'wishlist.artworks';

	// Members
	this.objectName = objectName; // Name of current object, for eval() and setTimeout()
	this.pageName = pageName; // Name of current page, for redirects
	this.artworks = new Array(); // Array of artwork IDs
	
	// Messages
	this.lang = new Array(); // Array of message strings
	this.lang['Artwork added'] = 'The artwork has been added to your collection.';
	this.lang['Artwork already in collection'] = 'This artwork is already in your favorites collection.';
	this.lang['Artwork removed'] = 'Artwork has been removed.';
	this.lang['Failed to remove artwork'] = 'Failed to remove artwork.';
	
	// Other
	this.today = new Date();
	this.tomorrow = new Date(this.today.getTime() + 3600 * 24 * 1000);
	this.sixmonth = new Date(this.today.getTime() + 3600 * 24 * 1000 * 180);
	//this.sixmonth = new Date(this.today.getTime() + 3600 * 1000); // Temp for debug

	// Load artworks in wishlist 
	this.load();
}

// Return array of artwork IDs in wishlist
Wishlist.prototype.getArtworks = function WishlistGetArtworks()
{
	return this.artworks;
}

// Return array of artwork IDs in wishlist
Wishlist.prototype.getArtworkIdList = function WishlistGetArtworkIdList()
{
	return this.artworks.join(",");
}

// Add artwork to wishlist
Wishlist.prototype.addArtwork = function WishlistAddArtwork(artworkID)
{
	if (artworkID)
	{
		var index = this.indexOf(artworkID);
		if (index == -1)
		{
			this.artworks.push(artworkID);
			this.save();
			if (this.lang['Artwork added'])
				alert(this.lang['Artwork added']);
		}
		else
		{
			if (this.lang['Artwork already in collection'])
				alert(this.lang['Artwork already in collection']);
		}
	}
}

// Remove artwork from wishlist
Wishlist.prototype.removeArtwork = function WishlistRemoveArtwork(artworkID)
{
	if (artworkID)
	{
		var index = this.indexOf(artworkID);
		if (index != -1)
		{
			this.artworks.splice(index, 1);
			this.save();
			if (this.lang['Artwork removed'])
				alert(this.lang['Artwork removed']);
		}
		else
		{
			if (this.lang['Failed to remove artwork'])
				alert(this.lang['Failed to remove artwork']);			
		}
	}
}

// Get index of an artwork in artwork array
Wishlist.prototype.indexOf = function WishlistIndexOf(artworkID)
{
	var index = -1;

	if (artworkID)
	{
		for (var i = 0; i < this.artworks.length; i++)
		{
			if (this.artworks[i] == artworkID)
			{
				index = i;
				break;
			}
		}
	}

	return index;
}

// Save wishlist to persistent storage
Wishlist.prototype.save = function WishlistSave()
{
	// Save artwork ids in a cookie as a comma-separated list 
	var artworkIDs = this.artworks.join(",");
	if (artworkIDs)
		this.setCookie(this.COOKIE_NAME, artworkIDs, this.sixmonth);
	else
		this.deleteCookie(this.COOKIE_NAME);
}

// Load wishlist from persistent storage
Wishlist.prototype.load = function WishlistLoad()
{
	// Save artwork ids in a cookie as a comma-separated list 
	var artworkIDs = this.getCookie(this.COOKIE_NAME);
	if (artworkIDs)
		this.artworks = artworkIDs.split(",");
	else
		this.artworks = new Array();
}

/* Wishlist helper methods, for private use */

// Find HTML element by its ID 
Wishlist.prototype.findItem = function WishlistFindItem(item) 
{
	if (document.all) return(document.all[item]);
	if (document.getElementById) return(document.getElementById(item));
	return(false);
}

// Set a cookie
Wishlist.prototype.setCookie = function WishlistSetCookie(cookieName, cookieValue, expires, path, domain, secure)
{
  if (!expires)
	{
		if (cookieDefaultExpiration)		
			expires = cookieDefaultExpiration; 
		else
			expires = this.cookieDefaultExpiration; 
	}

  document.cookie = 
    escape(cookieName) + '=' + escape(cookieValue) 
    + (expires ? '; EXPIRES=' + expires.toGMTString() : '')
    + (path ? '; PATH=' + path : '')
    + (domain ? '; DOMAIN=' + domain : '')
    + (secure ? '; SECURE' : '');
}

// Get a cookie
Wishlist.prototype.getCookie = function WishlistGetCookie(cookieName) 
{
  var cookieValue = null;
  var posName = document.cookie.indexOf(escape(cookieName) + '=');
  if (posName != -1)
  {
	var posValue = posName + (escape(cookieName) + '=').length;
	var endPos = document.cookie.indexOf(';', posValue);
	if (endPos != -1)
	  cookieValue = unescape(document.cookie.substring(posValue, endPos));
	else
	  cookieValue = unescape(document.cookie.substring(posValue));
  }
  return cookieValue;
}

// Delete a cookie
Wishlist.prototype.deleteCookie = function WishlistDeleteCookie(name,path,domain)
{
	if (this.getCookie(name))
	{
		var d = new Date();
		document.cookie = name + "=1;expires=" + d.toGMTString();
/*
		document.cookie = name + '=' + 'xxx' +
			+ ';expires=' + d.toGMTString() + 
//			';expires=Monday, 19-Aug-1996 05:00:00 GMT' + 
//		';expires=Thu, 01-Jan-70 00:00:01 GMT' +
		((path) ? ';path=' + path :'') +
		((domain) ? ';domain=' + domain :'')
*/
	}
}