/*jslint white:false plusplus:false browser:true nomen:false */
/*globals $, JbUI, window, POST */

var jawbone = jawbone || {};

jawbone.locale = {
	init: function() {
		this.setLocaleListeners();
	},
	setLocaleListeners: function() {
		var newSpan, $this = this, test = parseInt($.browser.version,0);
		$('#locale-settings').find('.locale-switcher').find('li').each(function(){
			if ($.browser.msie && parseInt($.browser.version,0) <= 7) {
				newSpan = $.element('span');
				$(this).append(newSpan);
			}
			$(this).click(function(){
				$this.updateLocale(this);
			});
		});
	},
	updateLocale: function(el) {
		var $this = this;
		POST(
			'/user/locale/update_locale',
			{
				locale: el.className
			},
			function(response) {
				$this.postLocaleUpdate(response);
			}
		);
	},
	postLocaleUpdate: function(response) {
		if (response.rc === 0) {
			var redirect_url = JbUI.Utility.cookie('locale_redirect');
			if (redirect_url) {
				window.location.href = redirect_url;
			} else {
				window.location.href = "/";
			}
		}
	},
	runTimeInit: function() {
	}
};

$(document).ready(function() {
	jawbone.locale.init();
});

(function(){
	jawbone.locale.runTimeInit();
}());

