FavoritesClass = function(){

	var _favorites 	= {};
	var _debug		= false;
	var JQ			= jQuery;


	var _cookie = {
	
		write: 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: function(name, def) {
				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 def;
		}
		
		, erase: function(name) {
				_cookie.write(name,"",-1);
		}
	
	};	


	var _init = function() {

			// Load current favorites			
	 		var data = _cookie.read('favs');	
	 		if (data) _favorites = JQ.evalJSON(data);
				
			// Make sure we have favorites before contining
			if (JQ(_favorites).size() == 0) return;

			// Create header favorites list 
			_updateHeaderList(_favorites, false);

	};
	
	var _debug = function(msg) {
			if (_debug) console.log(msg);
	
	};
	
	
	var _updateCookie = function() {

			var data = JQ.toJSON(_favorites);
			_cookie.write('favs', data);
	
	};
	

	var _updateHeaderList = function(items, flash) {
	
			if (JQ(items).size() == 0) return;
	
			var list 	= JQ('#favorites').find('UL');

			JQ.each(items, function(k,item) {

				// Search site for fav buttons to activate
				if ((button = JQ('#favorites_button_'+item.id))) button.addClass('added');
			
				var image	= (item.image) ? ('<a class="title" href="'+item.path+'"><img src="'+item.image+'" width="60" height="60" /></a>')  : '';
			
				var html	= '<li id="favorites_row_'+item.id+'">'
							+ '<p class="img_wrapper_sml">' + image + '</p>'
							+ '<a class="title" href="'+item.path+'">'+item.title+'</a>'
							+ '<span>'+(item.desc ? item.desc : '')+'</span>'
							+ '<a onclick="FAVS.remove(\''+item.id+'\');return false;" class="remove" href="#">Remove</a>'
							+ '</li>';

				JQ(list).append(html);

			});
			
			_updateHeaderCount();
	
	};

	var _updateHeaderCount = function() {
	
			var count 			= JQ('#favorites').find('UL LI:not(.instructions)').size();
			var instructions 	= JQ('#favorites').find('.instructions');

			if (count > 0) {

				if ((instructions = JQ('#favorites').find('.instructions'))) {
					instructions.remove();
				}

				$('#favorites_count').html('('+count+')');

				JQ('#favorites_link').fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);			
			
			} else {

				if (!instructions.size()) JQ('#favorites UL').append('<li class="instructions">You do not have any favorites yet.</li>');
			
			}
	
	};



	/* Public Methods */

	this.erase = function() {
			_cookie.erase('favs');
			return true;
	};
	

	this.remove = function(id) {		
			
			// Remove active button state
			$('#favorites_button_'+id).removeClass('added');			
			
			var row = $('#favorites_row_'+id);
			
			// Remove from header list and update count
			row.fadeOut(200, function(){
	
				row.remove();
	
				var count 	= JQ('#favorites').find('UL LI').length;
				var html 	= (count > 0) ? ('('+count+')') : '';
				
				$('#favorites_count').html(html);			
	
			});			

			// Update cookie
			delete _favorites[id];
			_updateCookie();
			
			_updateHeaderCount();

	};

	
	this.add = function(data) {
	
			if (data.id == undefined) {
				_debug('No id.');
				return false;
			}
			
			if (data.id in _favorites) {
				_debug('Already exists.');
				return false;
			}

			_updateHeaderList({'new':data});

			_favorites[data.id] = data;
			_updateCookie();
			
			return true;
			
	};	
	

	this.toggle = function(data) {
	
			if (data.id == undefined) {
				_debug('No id.');
				return false;
			}
			
			if (_favorites && (data.id in _favorites)) {
				return this.remove(data.id);
			} else {
				return this.add(data);
			}
				
	};

	
	_init();
	
};

$(document).ready(function() {
	FAVS = new FavoritesClass();
});
