function Pedidor(asGlobals)
{
	self = this;
	this.consts = asGlobals.consts;
	this.consts.hash_sep = '-';
	this.consts.default_page = 'orders';
	this.consts.title = 'Pedidor';
	
	this.init = function()
	{
		//Variables & constants from php
		self.vars('tmp', 'object');
		self.vars('page', 'string');
		self.vars('template', 'boolean');
		self.updateVars(asGlobals.vars);
		self.vars('fb_count', 'integer');
		
		//page elem
		self.elem = {};
		self.elem.container = $('#container');
		
		//on window resize
		$(window).resize(self.onResize).resize();
		
		//Hash management
		self.resetTmpFunctions();
		$(window)
			.bind('hashchange', self.onHashChange)
			.trigger('hashchange');
	};
	
	this.updateVars = function(asVars)
	{
		$.each(asVars, function(sKey, oValue){self.vars(sKey, oValue)});
	};
	
	/* Scrollbar */
	
	/*this.scrollbar = function(sPos)
	{
		var $Cv = $('#main');
		if(!self.vars('mobile'))
		{
			if(typeof self.vars('scrollbar') === 'undefined')
			{
				$Cv.tinyscrollbar({axis:'y', thumbSizeMin:'20', thumbSize:'20'});
				self.vars('scrollbar', $Cv.data("plugin_tinyscrollbar"));
			}
			else self.vars('scrollbar').update(sPos);
		}
		else $Cv.unbind("tinyscrollbar");
	}*/
	
	/* Menu */
	
	this.initMenu = function()
	{
		
	};
	
	/* Events */
	
	this.onResize = function()
	{
		self.vars('mobile', $('body').css('min-width')=='120px');
		//self.scrollbar();
	};
	
	this.onHashChange = function()
	{
		var asHash = self.getHash();
		var sDefaultPage = self.vars('log_in')?self.consts.default_page:'logon';
		if(asHash.hash !='' && asHash.page != '') self.switchPage(asHash); //page switching
		else if(self.vars('page')=='') self.setHash(sDefaultPage); //first page
	};
	
	this.resetTmpFunctions = function()
	{
		self.pageInit = function(asHash){console.log('no init for the page: '+asHash.page)};
		self.onSamePageMove = function(asHash){return false};
		self.onQuitPage = function(){return true};
		self.onFeedback = function(sType, sMsg){self.globalFeedback(sType, sMsg);};
	};
	
	this.globalFeedback = function(sType, sMsg)
	{
		sMsg = sMsg || '';
		if(sType=='error' && sMsg=='') sMsg = 'Oops! An unknown error occured';
		$('<span>', {'class':'global feedback '+sType})
			.css('top', (self.vars('fb_count') * 4.1)+'em')
			.append($('<i>', {'class':'fa fa-icon-left '+sType}))
			.append(addPunctuation(sMsg))
			.appendTo('body')
			.slideDown('fast')
			.delay(5000)
			.slideUp('fast', function(){
				$(this).remove();
				self.vars('fb_count', self.vars('fb_count') - 1);
				//$('.global.feedback').each(function(iKey, oElem){$(this).css('top', '-=4.1em');});
			});
		
		self.vars('fb_count', self.vars('fb_count') + 1);
	}

	/* Hash Handling */
	
	this.getHash = function()
	{
		var sHash = self.hash();
		var asHash = sHash.split(self.consts.hash_sep);
		var sHashPage = asHash.shift() || '';
		var sPage = self.consts.hash_to_page[sHashPage];
		return {hash:sHash, page:sPage, items:asHash};
	};
	
	this.setHash = function(sPage, asItems, bReboot)
	{
		sPage = sPage || '';
		asItems = asItems || [];
		bReboot = bReboot || false;
		var sHash = self.consts.page_to_hash[sPage];
		
		if(typeof asItems == 'string') asItems = [asItems];
		if(sHash != '')
		{
			var sItems = (asItems.length > 0)?self.consts.hash_sep+asItems.join(self.consts.hash_sep):'';
			self.hash(sHash+sItems, bReboot);
		}
	};
	
	this.hash = function(hash, bReboot)
	{
		bReboot = bReboot || false;
		if(!hash) return window.location.hash.slice(1);
		else window.location.hash = '#'+hash;
		
		if(bReboot) location.reload();
	};
	
	this.getVars = function(fOnSuccess)
	{
		fOnSuccess = fOnSuccess || function(){};
		getInfo
		(
			'vars', 
			function(asData)
			{
				self.updateVars(asData.vars);
				fOnSuccess();
			},
			{}
		);
	};
	
	/* Page Switching */
	
	this.getActionLink = function(sAction, oVars)
	{
		if(!oVars) oVars = {};
		var sVars = '';
		for(i in oVars)
		{
			sVars += '&'+i+'='+oVars[i];
		}
		return self.consts.process_page+'?a='+sAction+sVars;
	};
	
	this.switchPage = function(asHash)
	{
		var sPageName = asHash.page;
		var bSamePage = self.vars('page')==sPageName;
		if(self.onQuitPage(bSamePage) && !bSamePage || self.onSamePageMove(asHash))
		{
			//Delete tmp variables
			self.vars('tmp', {});
			
			//disable tmp functions
			self.resetTmpFunctions();
			
			//Officially a new page
			var bFirstPage = self.vars('page')=='';
			var sOldPage = self.vars('page');
			self.vars('page', sPageName);
			
			//Update Page Title
			var sDetail = asHash.items[0] || '';
			document.title = self.consts.title+' - '+sPageName+' '+sDetail;
			
			//Replacing DOM
			var $Dom = $(self.consts.pages[sPageName]);
			if(bFirstPage)
			{
				self.elem.container.html($(self.consts.pages['template']));
				self.elem.main = self.elem.container.find('#main');
				self.splash($Dom, asHash, bFirstPage); //first page
			}
			else
			{
				self.elem.main.stop().fadeTo('fast', 0, function(){self.splash($Dom, asHash, bFirstPage);}); //Switching page
			}
			
			if(bFirstPage && asHash.page!='logon' || !bFirstPage && sOldPage=='logon') self.loadTemplate();
		}
	};
	
	this.splash = function($Dom, asHash, bFirstPage)
	{
		//Switch main content
		self.elem.main.empty().html($Dom);

		//Page Bootstrap
		self.pageInit(asHash, bFirstPage);
		
		//Show main
		var $FadeInElem = bFirstPage?self.elem.container:self.elem.main;
		$FadeInElem.hide().fadeTo('slow', 1);
	};
	
	this.loadTemplate = function()
	{
		$('#header').addButton('exit', 'Salir', function(){document.cookie = "checksum=; expires=Thu, 01 Jan 1970 00:00:00 UTC"; self.setHash('logon', [], true);}, 'no_bg no_text', 'exit');
	};
	
	/* Variables Handling */
	
	this.vars = function(sVarName, oValue)
	{
		var asTypes = {boolean:false, string:'', integer:0, array:[], object:{}};
		var asVarName = (typeof sVarName == 'object')?sVarName:[sVarName];
		var sFirstVarName = asVarName[0];
		
		//Get, only name parameter
		if(typeof oValue == 'undefined')
		{
			return getElem(self.vars, asVarName);
		}
		//Init, name & type / default value
		else if(typeof oValue !== 'undefined' && typeof self.vars[sFirstVarName] === 'undefined')
		{
			self.vars[sFirstVarName] = (typeof asTypes[oValue] !== 'undefined')?asTypes[oValue]:oValue;
			return self.vars[sFirstVarName];
		}
		//Set, name & value
		else if(typeof oValue !== 'undefined' && typeof self.vars[sFirstVarName] !== 'undefined')
		{
			setElem(self.vars, asVarName, oValue);
			return getElem(self.vars, asVarName);
		}
		return null;
	};
	
	this.tmp = function(sVarName, oValue)
	{
		var asVarName = (typeof sVarName == 'object')?sVarName:[sVarName];
		asVarName.unshift('tmp');
		return self.vars(asVarName, oValue);
	};
}