/**************************************************************

	Script	: Sitebrowser for Stefan Vogelzang
	Version	: 1.0
	Authors	: Erik Huisman
	
**************************************************************/

var Sitebrowser = new Class({
							
	Implements: [Options,Events],
	
	options: {
		transition: 'circ:in:out',
		duration: 	500		
	},
	
	initialize: function(pages, options){
		
		this.setOptions(options);	

		this.pages 			= pages;
		this.currentPage 	= 0;
		this.bodywidth 		=  $(document.body).getSize().x; if(Browser.Engine.webkit) { this.bodywidth -= 15; }
			
	},	
	
	resize: function(){
	
		this.bodywidth = $(document.body).getSize().x; if(Browser.Engine.webkit) { this.bodywidth -= 15; }
		this.setPages(this.currentPage);
	
	}, 
	
	openbyhash: function(hash){
		this.pages.each(function(item){		
			if(item.slug=='/'+hash){ 
				this.setPages(item.id);
			}	
		}.bind(this));		
	},
	
	setPages: function(id){
	
		this.totalwidth = 0;
		this.pages.each(function(item){
			item.el.setStyles({ 'width': this.bodywidth, 'left': this.totalwidth });									 
			this.totalwidth += this.bodywidth; 
		}.bind(this));		
		$('pages').setStyle('width',this.totalwidth);
		this.gotoPage(id);
	
	}, 
	
	gotoPage: function(id){
	
		if(id!=this.currentPage) { this.fireEvent('loaded',this.pages[id]); }
	
		this.currentPage = id;
	
		$('pagecontainer').setStyle('height',this.pages[id].el.getSize().y);
			
		$('pages').set('morph', { duration: this.options.duration, transition:  this.options.transition });
		$('pages').morph({ 'left':  (((id)*this.bodywidth)*-1) });
						
	}, 
	
	next: function(){
		if(this.currentPage < (this.pages.length-1)) { this.gotoPage(this.currentPage+1); }	
	}, 
	
	prev: function(){
		if(this.currentPage > 0) { this.gotoPage(this.currentPage-1); }	
	},			  
		
	thePage: function(){
		return this.pages[this.currentPage].name;
	}				  
			
});
