ISHAM.namespace('ISHAM.view');

ISHAM.view.Archive = new Class({
	Implements: [Options, Events],
	options: {
		classes: {
			activePageClass: 'page-active',
			post: 'post',
			postContainer: 'post-container',
			postFrame: 'column-post-frame'
			
		},
		ids: {
			linkContainer: 'post-navigation'			
		}
	},
	
	initialize: function(options) {
		
		this.setOptions(options);
		
		window.addEvent('panelSetupComplete', this._setInitial.bindWithEvent(this));
		
		this._setInitial();
		
	},
	
	
		//----------------------------------------------------------------------
		// PROPERTIES
		//----------------------------------------------------------------------
		
	
	activePanel: null,	
	buttons: [],
	panels: [],
	panelFrame: null,
	previousPanel: null,
	selectedPanel: null,
	
	
		//----------------------------------------------------------------------
		// PRIVATE METHODS
		//----------------------------------------------------------------------
		
		
	_attachListeners: function() {
		
		this.buttons.addEvent('click', this.handleClick.bindWithEvent(this));
		
	},
		
	_findElements: function() {
		
		this.buttons = $$('.' + this.options.classes.activePageClass + ' ul.archive-nav a');
		this.panels = $$('.' + this.options.classes.activePageClass + ' div.' + this.options.classes.post);
		
		
	},
	
	_panelScroll: function(el) {
		
		var previous = this.previousPanel;
		
		el.removeClass('hidden');
		
		new Fx.Scroll(this.panelFrame, {
			duration: 500,
			transition: Fx.Transitions.Quart.easeInOut,
			onComplete: function() {
				previous.addClass('hidden');
			}
		}).start(650,0);
		
	},
	
	_setInitial: function() {

		if($$('div.' + this.options.classes.postContainer)) {
			
			this._findElements();
			this._attachListeners();
			
			this.previousPanel = null;
			this.selectedPanel = null;
			
			if(this.panels[0]) { 
				this.activePanel = this.panels[0];
				this.panelFrame = this.activePanel.getParent('div.'+this.options.classes.postFrame);
			}
			
		}
		
	},
	

		//----------------------------------------------------------------------
		// EVENT HANDLERS
		//----------------------------------------------------------------------
		
	
	handleClick: function(e) {
		
		e.preventDefault();
		
		this.selectedPanel = this.panels[this.buttons.indexOf(e.target)];
		
		if(this.previousPanel) {
			
			this.previousPanel.dispose();
			//this.previousPanel.inject(this.options.ids.postContainer);
			new Fx.Scroll(this.panelFrame).set(0,0);
		
		}
		
		if(this.selectedPanel != this.activePanel) {
		
			this.selectedPanel.inject(this.activePanel, 'after');
			
			this.previousPanel = this.activePanel;
			this.activePanel = this.selectedPanel;
					
			this._panelScroll(this.activePanel);
			
		}
		
	}	

	
});