
(function($) { // hide the namespace
var cookie_name = "favorite_fund_list";

function FavoriteFundList() {
	this._list = new Array();
}

$.extend(FavoriteFundList.prototype, {
	_add: function(fcode) {
		for ( i = 0; i < this._list.length; i++ ) {
			if ( this._list[i] == fcode ) {
				this._update();
				return;
			}
		}
		this._list.push(fcode);
		this._update();
	},
	_remove: function(fcode) {
		for ( i = 0; i < this._list.length; i++ ) {
			if ( this._list[i] == fcode ) {
				this._list.splice(i, 1);
				this._update();
				return;
			}
		}
	},
	_getAll: function() {
		return this._list;
	},
	_update: function() {
		$.cookie(cookie_name, this._list.join(','), { expires: 365, path: '/'});
	},
	_loadFromCookie: function() {
		var favorites = $.cookie(cookie_name);
		if ( favorites ) {
			this._list = favorites.split(',');
		}
	},
	_clear: function() {
		this._list = new Array();
		this._update();
	}
});

$.fn.addToFavoriteFundList = function() {
	return this.each(function() {
		$.favoriteFundList._add($(this).val());
	});
};

$.fn.removeFromFavoriteFundList = function(fcode) {
	return this.each(function() {
		$.favoriteFundList._remove($(this).val());
	});
};

$.fn.getFavoriteFundList = function() {
	return $.favoriteFundList._getAll();
};

$.fn.clearFavoriteFundList = function() {
	$.favoriteFundList._clear();
};

$(document).ready(function() {
	// singleton
	$.favoriteFundList = new FavoriteFundList();
	$.favoriteFundList._loadFromCookie();
//	console.log("loaded: favorite fund list=", $.favoriteFundList._getAll());
});

})(jQuery);
function addToFavorite(fcode) {
	$.favoriteFundList._add(fcode);
}
function deleteFromFavoriteList(fcodeList) {
	var list = fcodeList.split(',');
	for ( var i = 0; i < list.length; i++ ) {
		$.favoriteFundList._remove(list[i]);
	}
}
