iii.shareprice.apps.com.recent = new Class( {
	history: {},
	historykeys: [],
	limit: 5,
	cookie: '_recents',
	initialize: function ( superclass ) {
		var self = this;
		this.superclass = superclass;
		this.superclass.addEvent( 'layout-ready', function ( ) {
			self.load( );
			self.superclass.addEvent( 'stock-search', function ( stock ) {
				self.add( stock );
			} );
		} );
	},
	add: function ( stock ) {
		var id = stock.id;
		if( this.historykeys.contains( id ) ) {
			this.historykeys.erase( id ).unshift( id );
			this.history[ id ] = stock;
		} else {
			this.historykeys.unshift( id );
			this.history[ id ] = stock;
		}
		this.historykeys = this.historykeys.slice( 0, this.limit );
		return this.save();
	},
	save: function ( ) {
		var self = this;
		var save = [];
		this.historykeys.each( function ( key, index ) {
			save.push( [ key, self.history[ key ].descriptor.displayname, self.history[ key ].exchangecode ] );
		} );
		this.superclass.cookie.enc( this.cookie, save );
		return this.write();
	}, 
	load: function ( ) {
		var self = this;
		var load = this.superclass.cookie.dec( this.cookie ) || [];
		load.each( function ( data ) {
			self.historykeys.push( data[ 0 ] );
			self.history[ data[ 0 ] ] = {
				id: data[ 2 ],
				exchangecode: data[ 2 ],
				idnotation: data[ 0 ],
				descriptor: {
					displayname: data[ 1 ] || '',
					exchangecode:  data[ 2 ]			
				}
			};
			self.superclass.stocks.include( self.history[ data[ 0 ] ] );
		} );
		return this.write();
	},
	write: function ( ) {
		var self = this;
		var ul = new Element( 'ul', {
			'class': 'list'
		} );
		this.historykeys = this.historykeys.slice( 0, this.limit ); 
		this.historykeys.each( function( id, index ) {
			var t = new Element( 'li', {
				'class': 'list-item'
			} ).set( 'html', self.history[ id ].descriptor.displayname ).inject( ul ).addEvent( 'click', function ( ) {
				self.superclass.fireEvent( 'stock-before-add', [ self.history[ id ] ] );
			} );
		} );
		this.superclass.ly.recent( ul );
		return this;
	}
} );

iii.shareprice.apps.com.portfolios = new Class( {
	cookie: '_portfolios',
	portfolios: {},
	initialize: function ( superclass ) {
		this.superclass = superclass;
		var self = this;
		this.superclass.addEvent( 'portfolio-save', function ( name ) {
			
			if( name && name.clean() != "" && !self.activatePortfolio ) {
				self.add( name, self.superclass.active.get() );
			} else if ( self.activatedPortfolio ) {
				self.saveactivatePortfolio( self.superclass.active.get() );
			}
		} );
		this.superclass.addEvent( 'layout-ready', function () {
			self.load();
		} );
		
	},
	add: function ( name, stocks ) {
		if( this.portfolios[ name ] ) {
			this.portfolios[ name ] = stocks;
		} else {
			this.portfolios[ name ] = stocks;
		};
		this.save();
	},
	remove: function ( ) {
		
	},
	save: function ( ) {
		var data = {};
		for( var name in this.portfolios ) {
			data[ name ] = this.portfolios[ name ];
		}
		this.superclass.cookie.enc( this.cookie, data );
		this.render();
	},
	load: function ( ) {
		var data = this.superclass.cookie.dec( this.cookie ) || {};
		for( var key in data ) {
			this.portfolios[ key ] = data[ key ];
		}
		this.render();
	},
	render: function ( ) {
		var self = this;
		var ul = new Element( 'ul', {
			'class': 'list'
		} );
		for( var name in this.portfolios ) {
			( function ( data, name ) {
				new Element( 'li', {
					'class': 'list-item'
				} ).set( 'html', name ).inject( ul ).addEvent( 'click', function ( ) {
					if( $type( data ) == 'array' ) {
						self.activatePortfolio ( data, name );
					}
				} );
			} ) ( this.portfolios[ name ], name );
		}
		this.superclass.ly.portfolio( ul );
		return this;
	},
	activatePortfolio: function ( data, name ) {
		this.activatedPortfolio = name;
		this.superclass.ly.stock.empty.run( [ ], this.superclass.ly );
		this.superclass.fireEvent( 'stock-before-add', [ data ] );
		this.superclass.ly.els.stock.topbar.name.setValue( name );
		this.superclass.ly.els.stock.topbar.btn.setText( 'save portolio' );
	},
	saveactivatePortfolio: function ( name, ids ) {
		
	},
	deactivatePortfolio: function ( ) {
		this.activatePortfolio = false;
	}
} );
