function __Settings()
{
	var self = this;
	this.lang = Locale['_Settings'];
	this.Pages = {};

	this.Container = ce('div', document.body, { className: 'Settings' }, { width: '700px', height: '500px' });
	ce('img', this.Container, { className: 'CloseIcon', src: '/img/actions/close.gif', title: Locale['ToClose'], onclick: function() { self.Hide() }});
	var Header = ce('h1', this.Container, { innerHTML: Locale['Options']});

	var Menu = ce('div', this.Container, { className: 'Menu' });
	this.Content = ce('div', this.Container, { className: 'Content' });

	this.Menu = ce('ul', Menu);
	ce('li', this.Menu, { innerHTML: '<img src="/img/icons/trash.gif"/>'+ this.lang['ToAddModule'], onclick: function() { self.runMenuItem(this, 'menuModules') } } );
	ce('li', this.Menu, { innerHTML: '<img src="/img/icons/trash.gif"/>'+ this.lang['Backup'], onclick: function() { self.runMenuItem(this, 'menuBackup') } } );
	ce('li', this.Menu, { innerHTML: '<img src="/img/icons/trash.gif"/>'+ this.lang['LanguageSelect'], onclick: function() { self.runMenuItem(this, 'menuLang') } } );
	ce('li', this.Menu, { innerHTML: '<img src="/img/icons/trash.gif"/>'+ this.lang['TemplateSelect'], onclick: function() { self.runMenuItem(this, 'menuSkins') } } );
	ce('li', this.Menu, { innerHTML: '<img src="/img/icons/trash.gif"/>'+ this.lang['SpecialProjects'], onclick: function() { self.runMenuItem(this, 'menuSpecialProjects') } } );

	if (SProfile.user.name)
	{
		this.Password = ce('li', this.Menu, { innerHTML: '<img src="/img/icons/trash.gif">'+ this.lang['PasswordChange'], onclick: function() { self.runMenuItem(this, 'ChangePassword') } } );
	}

	for(var i=0; i!=this.Menu.childNodes.length; i++)
	{
		var Item = this.Menu.childNodes[i];
		Item.onmouseover = function() { this.className = 'Selected' };
		Item.onmouseout = function() { this.className = '' };
	}
}

__Settings.prototype.Relay = function(r)
{
	if(r.variables.modules)
		Cache.Modules = r.variables.modules;
	switch (r.event.action)
	{
		case 'GetModulesList':
			this.Pages.NewModule.innerHTML = '';
			if(this.Pages.NewModule.previousSibling && this.Pages.NewModule.previousSibling.src)
				de(this.Pages.NewModule.previousSibling);
			this.menuModulesEngine(r.variables.modules);
			break;

		case 'GetModulesArchive':
			this.modulesArchiveRefresh(r.variables);
			break;

		case 'RecoverModules':
			if(r.variables.status == 'ok' && !isEmpty(r.variables.modules))
			{
				this.recoverModules(r.variables.modules);
			}
			break;

		case 'changeSkin':
			if(r.variables.status)
				document.location.href = '/';
			break;

		case 'GetSpecialProjectsList':
			this.Pages.SProjects.innerHTML = '';
			if(this.Pages.SProjects.previousSibling && this.Pages.SProjects.previousSibling.src)
				de(this.Pages.SProjects.previousSibling);
			this.menuSpecialProjectsEngine(r.variables);
			break;
		
		case 'addSpecialProject':
			if(r.variables.status)
				document.location.href = '/';
			break;
	}
}

__Settings.prototype.runMenuItem = function(MenuItem, Action)
{
	if (this.PrevSelected)
	{
		if(this.PrevSelected == MenuItem) return;
		this.PrevSelected.onmouseover = function() { this.className = 'Selected' };
		this.PrevSelected.onmouseout = function() { this.className = '' };
		this.PrevSelected.className = '';
	}
	this.PrevSelected = MenuItem;
	MenuItem.onmouseover = null;
	MenuItem.onmouseout = null;
	MenuItem.className = 'Selected';
	var FirstChild = this.Content.firstChild;
	if(FirstChild)
		this.Content.removeChild(FirstChild);
	if(Action)
		eval('this.'+Action+'()');
}


__Settings.prototype.menuModules = function()
{
	if(!this.Pages.NewModule)
	{
		ce('IMG', this.Content, {src: '/img/actions/loading.gif', title: Locale['Loading']});
		this.Pages.NewModule = ce('ul', this.Content, { className: 'SelectNewModule'});
		Update.request('Startup', 'settings', 'GetModulesList', null, null, true);
	}
	this.Content.appendChild(this.Pages.NewModule);
}

__Settings.prototype.menuModulesEngine = function(modules)
{
	var self = this;
	for(var i in modules)
	{
		var Module = ce('LI', this.Pages.NewModule, {id: i, onclick: add, onmouseover: over, onmouseout: out});
		ce('IMG', Module, { src: '/img/icons/'+ modules[i].icon +'.gif' });
		ce('SPAN', Module, { innerHTML: '<b>'+ modules[i].title +'</b><br><small>'+ modules[i].description +'</small>' });
		ce('A', ce('div', Module), {id: modules[i].title, innerHTML : Locale['ToAdd']});
	}

	function over()
	{
		this.className = 'Selected';
	}
	function out()
	{
		this.className = '';
	}
	function add()
	{
		self.addModule(this.id);
	}
}

__Settings.prototype.menuBackup = function()
{
	var self = this;
	if(!this.Pages.Backup)
	{
		this.Pages.Backup = ce('ul', this.Content, {innerHTML: '<li style="margin: 15px 0px 25px 0px;">'+ this.lang['YouCanGetProfileInBackup']+'</li><li style="margin: 15px 0px;">'+ this.lang['RestoreProfileFromBackup'] +'</li><li id="ModulesArchiveSection" style="margin: 15px 0px;"/><li id="ProfileToFriendSection" style="margin: 15px 0px;"/>'}, {margin: '15px'});
		ce('INPUT', this.Pages.Backup.childNodes[0], {type: 'button', value: this.lang['GetProfileBackup'], onclick: function(){document.location.href = '/backup.php?a=get';}});
		this.Pages.Backup.childNodes[1].innerHTML = '<form method="post" enctype="multipart/form-data" action="/backup.php?a=restore"><input type="file" name="profile"/><input type="submit" value="'+ Locale['ToRestore'] +'"/></form>';
		this.Pages.Backup.childNodes[1].firstChild.onsubmit = function()
			{
				var items = this.getElementsByTagName('input');
				if(!items[1].value)
				{
					window.alert(self.lang['ChooseFileToBackup']);
					return false;
				}
			}
	}
	this.Content.appendChild(this.Pages.Backup);
	this.modulesArchiveInit();
}


__Settings.prototype.menuLang = function()
{
	if(!this.Pages.Lang)
	{
		this.Pages.Lang = ce('FORM', this.Content, {method: 'post', action: '/', innerHTML: '<h4>'+ this.lang['LanguageSelect'] +'</h4>'}, {padding: '10px'});
		var oUl = ce('UL', this.Pages.Lang);
		var tmp = '';
		for(var i in AvailableLangs)
		{
			if(i == 'IT')
				continue;
			if(i == Lang)
				tmp = ' checked="true" disabled="true"';   //текущий язык
			else
				tmp = '';
			ce('LI', oUl, {innerHTML: '<input type="radio" name="lang" align="absmiddle" value="'+ i +'"'+ tmp +'/> <img src="/img/lang_'+ i +'.gif" align="absmiddle" style="margin: 0px 5px"/>' + AvailableLangs[i]}, {margin: '5px 0px'});
		}
		ce('INPUT', this.Pages.Lang, {type: 'submit', value: 'OK', className: 'std_button'});

		this.Pages.Lang.onsubmit = function()
		{
			return true;
		}
	}
	this.Content.appendChild(this.Pages.Lang);
}

__Settings.prototype.menuSkins = function()
{
	if(!this.Pages.Skins)
	{
		this.Pages.Skins = ce('FORM', this.Content, null, {padding: '10px'});
		var oTbody = ce('TBODY', ce('TABLE', this.Pages.Skins, {margin: '12px 0px'}), null);
		var oTr = ce('TR', oTbody);
		ce('H4', ce('TD', oTr), {innerHTML: this.lang['TemplateSelect']});
		ce('INPUT', ce('TD', oTr, {colSpan: 2, align: 'right'}, {marginTop: '12px'}), {type: 'submit', value: Locale['ToApply'], className: 'std_button2'}, {marginRight: '8px'});
		var j = 0;
		for(var tpl in Desktop.templates)
		{
			if((++j)%2)
				oTr = ce('TR', oTbody);
			if(tpl != Skin)
				var oTd = ce('TD', oTr, {checked: false, resource: null, tpl: tpl, onmouseover: fOn, onmouseout: fOut, onclick: fClick, className: 'href'}, {width: '204px', padding: '4px', border: '4px solid #FFFFFF', textAlign: 'center'});
			else
				var oTd = ce('TD', oTr, null, {width: '204px', padding: '4px', border: '4px solid #FFFFFF', backgroundColor: '#FFCE67', textAlign: 'center'});
			ce('IMG', oTd, {src: '/img/tpl/'+tpl+'/demo.jpg'}, {border: '2px solid #FFFFFF'});
			ce('STRONG', oTd, {innerHTML: this.lang.skins[tpl]});
		}
		if(j%2)
			ce('TD', oTr, null, {padding: '4px', border: '4px solid #FFFFFF'});
		ce('INPUT', ce('TD', ce('TR', oTbody), {colSpan: 2, align: 'right'}, {marginTop: '12px'}), {type: 'submit', value: Locale['ToApply'], className: 'std_button2'}, {marginRight: '8px'});
		this.Pages.Skins.onsubmit = fSubmit;
	}
	this.Content.appendChild(this.Pages.Skins);
	var self = this;

	function fSubmit()
	{
		var tmp = getChecked();
		if(tmp)
		{
			Update.request('Desktop', 'settings', 'changeSkin', null, {tpl: tmp.tpl}, true);
		}
		return false;
	}
	function fOn(e)
	{
		this.style.backgroundColor = '#F2FF88';
		e = e || event;
		var x = Event.pointerX(e);
		var y = Event.pointerY(e);
		var oTd = this;
		var timeout = (this.checked)?1000:300;
		this.resource = window.setTimeout(function () {showZoom(oTd, x, y)}, timeout);
	}
	function fOut()
	{
		if(!this.checked)
			this.style.backgroundColor = '';
		if(this.resource)
		{
			window.clearTimeout(this.resource);
			this.resource = null;
		}
	}
	function fClick(obj)
	{
		if(!obj || !obj.tpl)
			obj = this;
		var tmp = getChecked();
		if(tmp)
		{
			tmp.checked = false;
			tmp.style.backgroundColor = '';
		}
		obj.checked = true;
		obj.style.backgroundColor = '#F2FF88';
	}
	function getChecked()
	{
		var tmp = oTbody.getElementsByTagName('TD');
		for(var i=0; i<tmp.length; i++)
		{
			if(tmp[i].checked)
				return tmp[i];
		}
		return null;
	}
	function showZoom(oTd, x, y)
	{
		if(document.getElementById('skinTplZoom_'+oTd.tpl))
			return;
		ce('IMG', document.body, {id: 'skinTplZoom_'+oTd.tpl, src: '/img/tpl/'+oTd.tpl+'/demo_.jpg', width: 325, className: 'href', title: self.lang.msg['DoubleClickToApplySkin'],
			onmouseout: function() {
				de(this);
			},
			onclick: function() {
				var obj = this;
				fClick(oTd);
				window.setTimeout(function() {de(obj);}, 500);
			},
			ondblclick: function() {
				fClick(oTd);
				fSubmit();
			}
		}, {border: '10px solid #F2FF88', position: 'absolute', left: (Math.floor(x-152))+'px', top: (Math.floor(y-140))+'px', zIndex: 80000000});
	}
}

__Settings.prototype.menuSpecialProjects = function()
{
	if(!this.Pages.SProjects)
	{
		ce('IMG', this.Content, {src: '/img/actions/loading.gif', title: Locale['Loading']});
		this.Pages.SProjects = ce('UL', this.Content, {className: 'SelectNewModule'});
		Update.request('Startup', 'settings', 'GetSpecialProjectsList', {}, {}, true);
	}
	this.Content.appendChild(this.Pages.SProjects);
	var self = this;
}

__Settings.prototype.menuSpecialProjectsEngine = function(projects)
{
	var self = this;
	var oLi = null;
	var oDiv = null;
	for(var i=0; i<projects.length; i++)
	{
		oLi = ce('LI', this.Pages.SProjects, {onmouseover: over, onmouseout: out, project: projects[i].url, innerHTML: '<b>'+ projects[i].title +'</b> ('+ projects[i].url +')<br/><small>'+ projects[i].desc +'</small>'}, {cursor: 'default'});
		var oTr = ce('TR', ce('TBODY', ce('TABLE', oLi, null, {width: '100%', marginTop: '8px'})));
		ce('A', ce('SMALL', ce('TD', oTr, null, {textAlign: 'left'})), {href: 'http://'+projects[i].url+'/', target: '_blank', innerHTML: Locale['ToOpenInNewWindow'], project: projects[i].url});
		ce('BR', oDiv);
		ce('A', ce('SMALL', ce('TD', oTr, null, {textAlign: 'right'})), {innerHTML: Locale['ToAdd'], project: projects[i].url, onclick: add, className: 'href'});
	}

	function over()
	{
		this.className = 'Selected';
	}
	function out()
	{
		this.className = '';
	}
	function preview()
	{
		
	}
	function add()
	{
		Update.request('Desktop', 'settings', 'addSpecialProject', null, {k: this.project}, true);
		return false;
	}
}


__Settings.prototype.addModule = function(ModuleName)
{
	var current = Desktop.State.cLeft.current.id.split('.');
	ModManager.Create(ModuleName, 'cLeft', current[2]);
	this.Hide();
}

__Settings.prototype.recoverModules = function(data)
{
	for(var mid in data)
	{
		ModManager.Create(data[mid][0], 'cLeft', data[mid][1], mid, null, null, true, true);
	}
}

__Settings.prototype.modulesArchiveInit = function()
{
	var self = this;
	oLi = document.getElementById('ModulesArchiveSection');
	oLi.innerHTML = '<h4>'+ this.lang['ModulesArchive'] +'</h4>';
	var oForm = ce('FORM', oLi, null, {marginTop: '10px'});
	ce('IMG', oForm, {src: '/img/actions/loading.gif', className: 'href', align: 'top', ready: false, title: Locale['ToRefresh'], 
		onclick: function() {
			if(this.ready)
			{
				this.src = '/img/actions/loading.gif';
				this.ready = false;
				Update.request('Startup', 'settings', 'GetModulesArchive', { }, {}, true )
			}
		}
		});
	ce('SELECT', oForm, {id: 'ModulesArchiveSelectBox', multiple: true}, {margin: '0px 10px'});
	Update.request('Startup', 'settings', 'GetModulesArchive', {}, {}, true );
	ce('INPUT', oForm, {type: 'submit', value: Locale['ToRestore']});
	oForm.onsubmit = function()
	{
	   var oSelect = document.getElementById('ModulesArchiveSelectBox');
	   var data = [];
	   for(var i=0; i<oSelect.options.length; i++)
	   {
		 if(oSelect.options[i].selected)
			 data.push(oSelect.options[i].value);
	   }
	   Update.request('Desktop', 'settings', 'RecoverModules', {}, {modules: data}, true );
	   self.Hide();
	   return false;
	}
}

__Settings.prototype.modulesArchiveRefresh = function(modules)
{
	var oSelect = null;
	if(oSelect = document.getElementById('ModulesArchiveSelectBox'))
	{
		oSelect.previousSibling.src = '/img/icons/mail/refresh.gif';	//да, это не слишком эстетично, зато удобно
		oSelect.previousSibling.ready = true;
		oSelect.innerHTML = '';
		var d = new Date();
		var tz_offset = d.getTimezoneOffset()*60000;
		for(var i=modules.length-1; i>=0; i--)
		{
			d = new Date(modules[i][1]*1000 - tz_offset);
			var ds = d.getDate() +'.'+ addZero(d.getMonth()+1) +'.'+ d.getFullYear() +' '+ addZero(d.getHours()) +':'+ addZero(d.getMinutes());
			ce('OPTION', oSelect, {value: i, innerHTML: Locale['Gadgets'][modules[i][0]]['title'] +' ('+ ds +')'});
		}
		if(oSelect.options.length > 0)
		{
			oSelect.style.height = '100px';
			oSelect.disabled = false;
		}
		else
		{
			oSelect.style.height = '40px';
			oSelect.disabled = true;
		}
	}
}

__Settings.prototype.ChangePassword = function()
{
	if(!this.Pages.ChangePassword)
	{
		this.Pages.ChangePassword = ce('ul', this.Content, { className: 'ChangePassword', innerHTML: this.lang['ChangingPassword'] +' "'+SProfile.user.name+'"<br><br><b>'+ this.lang['OldPassword'] +'</b><br><input class="std" type="password"><br><b>'+ this.lang['NewPassword'] +'</b><br><input class="std" type="password"><br><b>'+ this.lang['ConfirmPassword'] +'</b><br><input class="std" type="password"><br><br><input type="button" class="std_button" value="'+ this.lang['ToChangePassword'] +'">'}, { padding: '10px' });
		var fields = this.Pages.ChangePassword.getElementsByTagName('input');
		fields[3].onclick = function()
			{
				var fields = this.Pages.ChangePassword.getElementsByTagName('input');
				if($F(fields[1]) != $F(fields[2]))
				{
					alert(this.lang['PasswordMismatch']);
				}
				else
				{
					Update.request('Startup', 'startup', 'ChangePassword', { }, { OldPassword: $F(fields[0]), NewPassword: $F(fields[1]) }, true );
				}
			}.bind(this);
	}
	this.Content.appendChild(this.Pages.ChangePassword);
}

__Settings.prototype.FeedBack = function()
{
	if(!this.Pages.FeedBack)
	{
		var s = this.lang['KroziloCanBeBetter'].replace(/%S1/g, '<a href="#" onclick="Settings.Hide(); Startup.DrawFeedbackForm();">');
		s = s.replace(/%S2/g, '</a>');
		this.Pages.FeedBack = ce('ul', this.Content, { innerHTML: '<li>'+ s +'</li>'}, { padding: '10px' });
	}
	this.Content.appendChild(this.Pages.FeedBack);
}

__Settings.prototype.Agreement = function()
{
	if (!this.Pages.Agreement)
	{
		this.Pages.Agreement = ce('ul', this.Content, { innerHTML: '<div class="disc">'+ this.lang['UserAgreement'].join('') +'</div>'}, { padding: '10px' });
	}
	this.Content.appendChild(this.Pages.Agreement);
}

__Settings.prototype.PRelise = function()
{
	if(!this.Pages.PRelise)
	{
		var text = '<div class="disc">';
		text += this.lang['PressReleaseTxt'];
		text += '</div>';
		this.Pages.PRelise = ce('ul', this.Content, { innerHTML: text }, { padding: '10px' });
	}
	this.Content.appendChild(this.Pages.PRelise);
}

__Settings.prototype.Show = function()
{
	var self = this;
	window.onresize = function() {self.Reposition();}
	this.Container.style.display = 'block';
	this.Reposition();
	this.runMenuItem(this.Menu.firstChild, 'menuModules')
}

__Settings.prototype.Hide = function()
{
	window.onresize = null;
	$(this.Container).hide();
}

__Settings.prototype.Reposition = function()
{
	with(document.body)
	{
		this.Container.style.left = parseInt((offsetWidth - parseInt(this.Container.style.width))/2) + 'px';
		this.Container.style.top = parseInt((offsetHeight - parseInt(this.Container.style.height))/2) + 'px';
	}
}