;(function(){

var _ = require("underscore");
var $ = require('jquery');
var BackBone = require("backbone");

var ViewStack = module.exports = BackBone.View.extend({

	_stack: null,
	_stackReplace: null,
	_current: null,

	initialize: function initialize(options){
		if ( typeof this.setShared !== "undefined" )
			this.setShared( options.shared );
		this._stack   = [];
		this._stackReplace = [];
		this._current = null;
		this.$el.empty();
	},

	render: function render(){
		return this;
	},

	getView: function getView(){
		return this._stack.length > 0 ? this._stack[ this._stack.length - 1 ] : null;
	},

	getViewActive: function getViewActive(){
		return this._current;
	},

	// pushView
	// Aggiunge una view all'applicazione con z-Index più alto
	pushView: function pushView( newView, options ){

		// Controllo se è la stessa view che sto cercando di pushare
		if ( this._current === newView ) return this;


		if ( options && options.replace == true ){

			if ( this._current ){

				var popped = this._stack.pop();

				this._stackReplace.push( popped );

				popped.$el.detach();

				// Calcolo il nuovo z-Index che è incrementale
				var zIndex = (this._stack.length*100) + 100;
				// Setto lo z-index
				if ( newView.setZindex )
					newView.setZindex( zIndex );
				else
					newView.$el.css("z-index", zIndex);

				this._stack.push( newView );

				this.$el.append( newView.el );

				// setto la view Corrente
				this._current = newView;
				// 
				newView.render();


			}

		}else{

			// Calcolo il nuovo z-Index che è incrementale
			var zIndex = (this._stack.length*100) + 100;
			// Setto lo z-index
			if ( newView.setZindex )
				newView.setZindex( zIndex );
			else
				newView.$el.css("z-index", zIndex);
			// aggiungo allo stack
			this._stack.push( newView );
			// Appendo alla view dell'applicazione
			this.$el.append( newView.el );
			// setto la view Corrente
			this._current = newView;
			// 
			newView.render();

			if ( this._current && this._current.onActivate ) 
				this._current.onActivate();

		}

		// Scateno l'evento active per dire alla view che è attiva
		if ( this._current )
        	this._current.trigger("active");

		this
			.refreshUrl()
			.trigger('pushed', newView);
		return this;
	},

	
	// popView 
	// Rimuove l'ultima view dello stack
	popView: function popView(){

		var self 	   = this,
			poppedView = self._current;

		if ( !poppedView || !poppedView.onDeactivate )
			return self._popView();

		poppedView.onDeactivate(function(){
			self._popView();
		});
		return poppedView;

	},

	// refresh
	// Aggiorna l'url se la view corrette ce l'ha settata
	refreshUrl: function refreshUrl(){
		var shared = this.getShared ? this.getShared() : null;
		if ( typeof shared !== "undefined" && shared && this._current && this._current.url ){
			shared.app.navigate(_.result(this._current, 'url'));
		}	
		return this;
	},

	// clearStack 
	// Pulisce tutto lo stack dalle view
	clearStack: function clearStack(){
		
		while( this._stack.length > 0 ){
			this.popView();
		}

		return this;
	},


	//
	// Private
	// 

	_popView: function _popView(){
		var popped = this._stack.pop();
		if (popped) {
            popped.destroy();
            popped.trigger("deactive");
        }

        // Assegno la nuova view corrente
        this._current = this._stack[ this._stack.length-1 ];

        if ( this._stackReplace.length > 0 ){
        	this.pushView( this._stackReplace.pop() );
        }

        this
			.refreshUrl()
			.trigger('popped', popped);

		return popped;
	}

});


ViewStack.middleware = function middleware(options){
	return function(shared, next){
		var viewStack = new ViewStack(options);
		viewStack.clearStack(); // Pulisco dalle eventuali view appese
		if ( viewStack.setShared )
			viewStack.setShared( shared ); // setto lo shared object
		viewStack.render(); // Renderizzo la view base application
		shared.viewstack = viewStack;
		next();
	}

}

})();
@@ -2325,16 +2325,22 @@
 popView(
+popped
 )%7B%0A%0A%09%09va
@@ -2463,16 +2463,22 @@
 popView(
+popped
 );%0A%0A%09%09po
@@ -2524,24 +2524,30 @@
 lf._popView(
+popped
 );%0A%09%09%7D);%0A%09%09r
@@ -3123,25 +3123,198 @@
 popView(
-)%7B%0A%09%09var 
+popped)%7B%0A%0A%09%09if ( typeof popped !== %22undefined%22 )%7B%0A%09%09%09var index = _.indexOf(this._stack, popped);%0A%09%09%09delete this._stack%5B index %5D;%0A%09%09%09this._stack = _.compact(this._stack);%0A%09%09%7Delse%7B%0A%09%09%09
 popped =
@@ -3337,20 +3337,27 @@
 ();%0A
+%09%09%7D%0A%0A
 %09%09if (
+ 
 popped
+ 
 ) %7B%0A

Delete a specific view from the array of views