// -*- Mode: MooTools Javascript; tab-width: 4; -*-
var VirtualPages = new Class({
	
	Implements: Options, 
	
	options:{
		linkSelector:'a[href]:not([target]):not([href^=http]):not([href^=#])',
		
		getPages: function(){
			return $$('.page');
		},
		routeParser: function(url){
			
			return url.replace(/\W/g,'_');
		},
		getContent: function(link){
			if (!link) return $('page-content');
			return $$( link.get('data-href') )[0];
		},
		allowSetPage: true/*,
		allowSetPage: function(id){
			this === myVirtualPagesInstance;
			return true;
		}*/
	},
	
	currentPageID: null,
	pageElements: {},
	
	getID: function(url){
		return this.options.routeParser(url);
	},
	
	initialize: function(options){
		this.setOptions(options);
		
		// Set the start "page"
		var id = this.getID(document.location);
		this.setContent(id, this.options.getContent.call(this) );
		this.setPage(id);
		
		// Grab all internal links
		$(document.body).addEvent('click:relay('+ this.options.linkSelector +')', this.go.bind(this));
	},
	
	go: function(e, link){
		if (DEBUG){
			e.preventDefault();
		}
		
		link = link || e.target;
		
		var contentElement = this.options.getContent.call(this, link);
		if (!contentElement){
			return;
		}
		var id = contentElement.get('id') || this.getID(link.get('href'));
		
		this.setContent(id, contentElement, true);
		if (this.setPage(id) != null) e.preventDefault && e.preventDefault(); // preventDefault unless setPage returns null
	},
	
	getContent: function(id, defaultElement){
		return this.pageElements[$pick(id,this.currentPageID)] || $(defaultElement) || null;
	},
	
	setContent: function(id, contentElement){
		contentElement = this.getContent(id, contentElement);
		if (!contentElement){
			window.fireEvent('cancel',[id, contentElement]);
			return this;
		}
		this.pageElements[id] = contentElement;
		return this;
	},
	
	setPage: function(id){
		// Returns:
		//     true for success
		//     false for fail
		//     null for abort
//alert(id);
		if (!$pick( $lambda(this.options.allowSetPage).call(this,id), true )){
			//console.log('Aborted since this.options.allowSetPage returned a defined falsy value');
			return false;
		}
		
		var contentElement = this.getContent(id);
		if (!contentElement){
			return null;
		}
		document.fireEvent('virtual load leave ' + this.currentPageID);
		//alert(this.currentPageID);
		$(contentElement).fireEvent('activate');
		document.fireEvent('virtual load', [contentElement, id, this]);
		document.fireEvent('virtual load ' + id, [contentElement, id, this]); //Changed document.fireEvent('virtual load ' + id, [contentElement, id, this]);
		this.currentPageID = id;
		document.location.hash = '#/' + id;
		//alert(document.location.hash);
		return true;
	},
	
	tryAgain: function(){
		var retry = function(){
			this.setPage(this.currentPageID);
		}.bind(this);
		
		if (!Browser.loaded)
			window.addEvent('domready',retry);
		else
			retry.delay(10);
	},
	
	
	next: function(){
		
		//if(currSub == 'Earn_More')
			
		return this.gotoPage(1, true);
	},

	previous: function(){
		return this.gotoPage(-1, true);
	},

	gotoPage: function(index, relative){
		//var pagesArr = this.options.getPages.call(this);
		var pages = this.options.getPages.call(this);
		var currentIndex, id, page;
		
		// Get the closest page that is index distance away
		
		for(var i=0;i<pages.length;i++)
		{
			if(pages[i].get('id') == 'Welcome')
				pages[i].set('sno',0);
			else if(pages[i].get('id') == 'Earn_More')
				pages[i].set('sno',1);
			else if(pages[i].get('id') == 'Redeem_Easily')
				pages[i].set('sno',2);
			else if(pages[i].get('id') == 'In_Media')
				pages[i].set('sno',3);
			else if(pages[i].get('id') == 'Get_Benefits')
				pages[i].set('sno',4);
			else if(pages[i].get('id') == 'Great_Service')
				pages[i].set('sno',5);	
			else
				{
				}
		}
		
	
	for(var i=0;i<pages.length;i++)
	{
		for(var j=0;j<=i-1;j++)
		{
			if(pages[j].get('sno')>pages[j+1].get('sno'))
			{
				temp =pages[j+1];
				pages[j+1]=pages[j];
				pages[j]=temp;
			}
		}
	}
	
	
		
		if (relative){
			// Get current index
			for (currentIndex = 0; currentIndex < pages.length; currentIndex++)
			{
				
				if (pages[currentIndex].get('id') == this.currentPageID) 
				{
					break;
				}
			}
			page = pages[currentIndex + index]; // Relative to currentIndex
			
			
			if (!page){
				if (currentIndex + index >= pages.length)
				{
					
					page = pages[0]; // Min
					
				}
				else
				{
					page = pages[pages.length-1]; // Max
				
				}
			}
		}
		// Get the specific page by index
		else 
		{
			page = pages[index];
			
		}
		
		
		if (!page){
			return false;
		}
		//alert(page);
		id = page.get('id');
		//alert(id);
		this.setContent(id, this.options.getContent.call(this,page));
		//alert(id);
		return this.setPage(id);
	},

	0:0

});

// add pages to respond to events

document.definePage = function (id, page){
	// momoization, calls init only once
	function _load (){
		page.initialize && page.initialize.apply(page, arguments);
		page.load && page.load.apply(page, arguments);
		_load = page.load || function(){};
	}

	document.addEvent('virtual load ' + id, function (){
		_load.call(page);
	})

	if (page.leave) document.addEvent('virtual load leave ' + id, page.leave.bind(page))
	if (page.loadComplete) document.addEvent('virtual load complete ' + id, page.loadComplete.bind(page));
}


