var _ = require('lodash');
var querystring = require('querystring');
var keystone = require('keystone');


/**
	Initialises the standard view locals
*/

exports.initLocals = function(req, res, next) {

	var locals = res.locals;

	/*get nav language*/
	if(keystone.lang == undefined) keystone.lang = req.headers["accept-language"].split(',')[0].split("-")[0];

	locals.navLinks = [
		{ label: 'Home',			key: 'home',		href: '/' },
		{ label: 'About',			key: 'about',		href: '/about' },
		{ label: 'Members',			key: 'members',		href: '/members' },
		{ label: 'News',			key: 'blog',		href: '/blog' },
		{ label: 'Contact',			key: 'contact',		href: '/contact'},
		{ label: 'Tourism',			key: 'tourism',		href: '/tourism'},
		{ label: 'Living',			key: 'living',		href: '/living'},
		{ label: 'Culture',			key: 'culture',		href: '/culture'},
		{ label: 'Classifieds',		key: 'classifieds',	href: '/classifieds'}
	];

	locals.user = req.user;

	locals.basedir = keystone.get('basedir');

	locals.page = {
		title: 'Babylon',
		path: req.url.split("?")[0] // strip the query - handy for redirecting back to the page
	};

	locals.qs_set = qs_set(req, res);

	if (req.cookies.target && req.cookies.target == locals.page.path) res.clearCookie('target');

	var bowser = require('../lib/node-bowser').detect(req);

	locals.system = {
		mobile: bowser.mobile,
		ios: bowser.ios,
		iphone: bowser.iphone,
		ipad: bowser.ipad,
		android: bowser.android
	}

	next();

};



/**
	Inits the error handler functions into `req`
*/

exports.initErrorHandlers = function(req, res, next) {
	res.err = function(err, title, message) {
		res.status(500).render('errors/500', {
			err: err,
			errorTitle: title,
			errorMsg: message
		});
	}
	res.notfound = function(title, message) {
		res.status(404).render('errors/404', {
			errorTitle: title,
			errorMsg: message
		});
	}
	next();
};


/**
	Fetches and clears the flashMessages before a view is rendered
*/

exports.flashMessages = function(req, res, next) {
	var flashMessages = {
		info: req.flash('info'),
		success: req.flash('success'),
		warning: req.flash('warning'),
		error: req.flash('error')
	};
	res.locals.messages = _.any(flashMessages, function(msgs) { return msgs.length }) ? flashMessages : false;
	next();
};

/**
	Prevents people from accessing protected pages when they're not signed in
 */

exports.requireUser = function(req, res, next) {
	if (!req.user) {
		req.flash('error', 'Please sign in to access this page.');
		res.redirect('/signin');
	} else {
		next();
	}
}

/**
	Returns a closure that can be used within views to change a parameter in the query string
	while preserving the rest.
*/

var qs_set = exports.qs_set = function(req, res) {
	return function qs_set(obj) {
		var params = _.clone(req.query);
		for (var i in obj) {
			if (obj[i] === undefined || obj[i] === null) {
				delete params[i];
			} else if (obj.hasOwnProperty(i)) {
				params[i] = obj[i];
			}
		}
		var qs = querystring.stringify(params);
		return req.path + (qs ? '?' + qs : '');
	}
}

@@ -275,17 +275,20 @@
 defined)
- 
+%7B%0A%09%09
 keystone
@@ -354,16 +354,234 @@
 %22-%22)%5B0%5D;
+%09%09%0A%09%09if(keystone.lang != %22fr%22 && keystone.lang != %22en%22 && keystone.lang != %22pl%22 && keystone.lang != %22it%22 && keystone.lang != %22es%22 && keystone.lang != %22br%22)%7B%0A%09%09%09keystone.lang = %22en%22;%0A%09%09%7D%0A%09%7D %0A%09console.log(keystone.lang);
 %0A%0A%09local

Limit the language possible