/************************************************************/
/* Previous Product Note Made By Thomas (dec 2009) 			*/
/* How does it works?										*/
/* 1- You have to create a ul tag with a specific ID		*/
/* 2- Create a Cookies_manager var called cookies_manager	*/
/*   #1st argument is the number of items saved				*/
/*	 #2nb argument is your ul id							*/
/************************************************************/

var img_dash = '/assets/templates/layout8/images/frames/dash.png';
var img_delete = '/assets/templates/layout8/images/frames/delete.png';
//var img_dash = '../images/frames/dash.png';
//var img_delete = '../images/frames/delete.png';
var img_enable = '/assets/templates/layout8/images/frames/enable.png';
var img_disable = '/assets/templates/layout8/images/frames/disable.png';

function create_cookie(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 Visited_product(name, href)
{
	this.tr = null;
	this.name = name;
	this.href = href;
}

Visited_product.prototype = 
{
	create_product : function()
	{
		if (this.name == 'null' && this.href == 'null')
			return null;
		var this_ = this;
		this.tr = document.createElement('tr');
		var img = document.createElement('img');
		img.src = img_dash;
		img.title = 'Delete';
		img.onclick = function(){ this_.delete_product(); };

		var a = document.createElement('a');
		a.innerHTML = this.name;
		a.href = this.href;

		if (navigator.appName == 'Microsoft Internet Explorer')
		{
			this.tr.onmouseenter = function(){ img.src = img_delete;}
			this.tr.onmouseleave = function(){ img.src = img_dash; }
		}
		else
		{
			this.tr.onmouseover = function(){ img.src = img_delete; }
			this.tr.onmouseout = function(){ img.src = img_dash; }
		}

		var td1 = document.createElement('td');
		td1.style.verticalAlign = 'middle';
		td1.appendChild(img);
		
		var td2 = document.createElement('td');
		td2.style.width = '200px';
		td2.appendChild(a);
		
		this.tr.appendChild(td1);
		this.tr.appendChild(td2);
		return (this.tr);
	},
	
	delete_product : function()
	{
		if (this.tr)
			cookies_manager.ul.removeChild(this.tr);
		cookies_manager.delete_product(this.tr);
	}
}

function Cookies_manager(size, id)
{
	this.ul = document.getElementById(id);
	this.last = -1;
	this.show = true;
	this.enable = true;
	this.size = size; 	// Size = number of products in the list
	this.cookies_tab = new Array();
}

Cookies_manager.prototype = 
{
	create_cookie : 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=/";
	},

	read_cookie : 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 null;
	},

	remove_cookie : function(name)
	{
		this.create_cookie(name, "", -1);
	},

	create_cookies_tab : function()
	{
		var cookies = document.cookie.split('; ');
		for (var i = 0; (i < cookies.length) && (cookies[i] != ''); ++i)
		{
			var cookie_name = new String(cookies[i].split('=')[0]);
			/* Just for test! */
			if (cookie_name == 'tester')
			{
//				document.getElementById('tester').style.display = 'block';
//				document.getElementById('tester2').style.display = 'block';
			}
			if (cookie_name == 'last')
				this.last = parseInt(cookies[i].split('=')[1]);
			else if ((cookie_name == 'display') && (cookies[i].split('=')[1] == 'false'))
			{
				this.display();
			}
			else if ((cookie_name == 'enable') && (cookies[i].split('=')[1] == 'false'))
			{
				this.enable = false;
				var img = document.getElementById('enable_visited_product');				
				img.src = img_disable;
				img.title = 'Enable';
			}
			else if (cookie_name.substr(0, 7) == 'product')
			{
				for (j = 0; (j < cookies[i].length) && (cookies[i].charAt(j) != '='); ++j);
				var cookie_values = cookies[i].substring(j + 1, cookies[i].length).split('*=');
				this.cookies_tab.push(new Visited_product(cookie_values[0], cookie_values[1]));
			}
		}
		if (this.cookies_tab.length > this.size)
		{
			var i = 0;
			var len = this.cookies_tab.length;
			var tmp_array = this.cookies_tab.slice(0, len);
			for (i = 0; i < this.size; ++i)
			{
				this.cookies_tab[this.size - i - 1] = tmp_array[(this.last - i + len) % len];
				var actual_cookie = this.cookies_tab[this.size - i - 1];
				this.create_cookie('product_' + (this.size - i - 1), actual_cookie.name + '*=' + actual_cookie.href, 7);
			}
			this.last = this.size - 1;
			this.create_cookie('last', this.last, 7);
			for (i = 0; i < (len - this.size); ++i)
			{
				this.cookies_tab.pop();
				this.remove_cookie('product_' + (i + this.size));
			}
		}
		this.fill_div();
	},

	no_repeat : function(name, href)
	{
		if (this.cookies_tab[this.last] && this.cookies_tab[this.last].name == name)
			return true;
		var next_place = (this.last + 1) % this.cookies_tab.length;
		for (var i = 0; (i < this.cookies_tab.length) && this.cookies_tab[i].name != name; ++i);
		if (i >= this.cookies_tab.length)
			return false;
		var prod = this.cookies_tab[i];
				
		for (j = i + this.cookies_tab.length; (j % this.cookies_tab.length) != next_place; --j)
		{
			this.cookies_tab[j % this.cookies_tab.length] = this.cookies_tab[(j - 1) % this.cookies_tab.length];
			var name = this.cookies_tab[j % this.cookies_tab.length].name;
			var href = this.cookies_tab[j % this.cookies_tab.length].href;
			this.create_cookie('product_' + (j % this.cookies_tab.length), name + '*=' + href, 7);
		}
		if (this.cookies_tab.length < this.size)
		{
			for(var j = i; j < this.cookies_tab.length - 1; ++j)
			{
				this.cookies_tab[j] = this.cookies_tab[j + 1];
				var name = this.cookies_tab[j].name;
				var href = this.cookies_tab[j].href;				
				this.create_cookie('product_' + j, name + '*=' + href, 7);				
			}
			this.cookies_tab[this.last] = new Visited_product(null, null);
			--this.last;
		}
		prod.delete_product();
		return false;
	},

	add_product : function(name, href)
	{
		if (!this.enable)
			return;
		if (this.no_repeat(name, href))
			return;
		this.last = (this.last + 1) % this.size;
		this.create_cookie('product_' + this.last, name + '*=' + href, 7);
		this.create_cookie('last', this.last, 7);
		var new_product = new Visited_product(name, href);
		if (!this.cookies_tab[this.last])
			this.cookies_tab.push(new_product);
		else
			this.cookies_tab[this.last] = new_product;
		this.add_div(new_product);
	},
	
	delete_product : function(elt)
	{
		for (var i = 0; (i < this.cookies_tab.length) && (this.cookies_tab[i].tr != elt); ++i);
		if (i <= this.last)
		{
			for (; i < this.last; ++i)
			{
				this.cookies_tab[i] = this.cookies_tab[i + 1];
				this.create_cookie('product_' + i, this.cookies_tab[i].name + '*=' + this.cookies_tab[i].href, 7);
			}
			--this.last;
			if (this.last < 0)
			{
				for (var j = this.cookies_tab.length - 1; (j >= 0) && 
					((i == j) || (this.cookies_tab[j].name == null || this.cookies_tab[j].name == 'null')); --j);
				this.last = j;
			}
			this.create_cookie('last', this.last, 7);
		}
		if (i < this.cookies_tab.length)
		{
			this.cookies_tab[i] = new Visited_product(null, null);		
			this.create_cookie('product_' + i, null + '*=' + null, 7);
		}
	},
	
	add_div : function(new_product)
	{
		this.ul.insertBefore(new_product.create_product(), this.ul.firstChild);
		if (this.ul.childNodes.length > (this.size + 1))
			this.ul.removeChild(this.ul.childNodes[this.size + 1]);
	},

	fill_div : function()
	{
		for (var i = 0; i < this.cookies_tab.length; ++i)
			if (elt = this.cookies_tab[(this.last - i + this.cookies_tab.length) % this.cookies_tab.length].create_product())
				this.ul.appendChild(elt);
	},
	
	delete_all : function()
	{
		while(this.last != -1)
			this.cookies_tab[this.last].delete_product();
	},

	display : function()
	{
		if (this.show)
			this.ul.style.display = 'none';
		else
			this.ul.style.display = 'block';
		this.show = !this.show;
		this.create_cookie('display', this.show, 7);	
	},
	
	change_enable : function()
	{
		var img = document.getElementById('enable_visited_product');
		if (!this.enable)
		{
			this.enable = true;
			this.create_cookie('enable', 'true', 7);			
			img.src = img_enable;
			img.title = 'Disable';
		}
		else
		{
			var confirm_str = "If you disable this feature, you will loose all your favourites.\n";
			var confirm_ = confirm(confirm_str + "Are you sure you want to continue?");
			if (confirm_)
			{
				this.enable = false;
				this.create_cookie('enable', 'false', 7);
				this.delete_all();
				img.src = img_disable;
				img.title = 'Enable';
			}
		}
	},
	
	print_ : function(str)
	{
		var aString = new String(str + '\nlast = ' + this.last + '\n');
		for (var i = 0; i < this.cookies_tab.length; ++i)
			aString += 'tab[' + i + '] = ' + this.cookies_tab[i].name + '\n';
		alert(aString);
	}
}