var GProfiles = {

AvailableLangsC: {'RU': AvailableLangs['RU'], 'EN': AvailableLangs['EN'], 'FR': AvailableLangs['FR'], 'DE': AvailableLangs['DE'], 'IT': 'Italiano', 'ES': AvailableLangs['ES']},

show: function()
{
//	Update.request('GProfile', 'gprofiles', 'updateGProfilesByDB', { }, { });
	var self = this;
	if(!document.getElementById('GProfilesForm'))
	{
		var obj = new __Dialog(800, null, {BlockAll: true});
		this.oForm = ce('FORM', ce('DIV', ce('DIV', obj.Container, {innerHTML: '<h1>'+ Locale['_Profile']['ProfilesGallery'] +'</h1>'}), {className: 'content'}, {padding: '5px'}), {id: 'GProfilesForm'});
	}
	this.oForm.innerHTML = '';
	if(!this.profiles)
	{
		this.oForm.innerHTML = '<div><img src="/img/actions/loading.gif" width="16px" height="16px" title="" align="top">'+ Locale['_Profile']['Loading'] +'</div>';
		Update.request('Startup', 'gprofiles', 'GetProfiles', null, true);
		return;
	}

	var oTbodyProfiles = ce('TBODY', ce('TABLE', this.oForm));
	var oTr = ce('TR', oTbodyProfiles);
	var col = 1;
	for(var i in this.profiles)
	{
		if(col>3)
		{
			col = 1;
			oTr = ce('TR', oTbodyProfiles);
		}
		col++;
		var pname = this.profiles[i].name || i;
		var oTd = ce('TD', oTr, null, {width: '226px'});

		var checked = false;
		if(SProfile.gprofiles)
		{
			if(typeof(arraySearch(SProfile.gprofiles.curset, i)) == 'number')
			{
				checked = true;
			}
		}
		var oInput = new __Checkbox(oTd, i, checked, checked);
		oInput.setOnChange(true, deleteGProfile, true);
		oInput.setOnChange(false, cancelDeleteGProfile, true);

		ce('DIV', oTd, {innerHTML: pname, tipInfo: this.profiles[i].description, onmouseover: showTip, onmouseout: hideTip}, {fontWeight: 'bold', background: 'url(\'/img/ticons/'+ this.profiles[i].icon +'.gif\') no-repeat', paddingLeft: '22px', marginLeft: '8px', width: '200px', 'float': 'left', 'cssFloat': 'left'});
	}
	ce('HR', this.oForm, null, {margin: '10px 0px'});
	ce('H4', this.oForm, {innerHTML: Locale['_Profile']['LangPrefers']});
	oTbodyLangs = ce('TBODY', ce('TABLE', this.oForm, null, {margin: '3px 0px 6px'}));
	var num_cols = 3;
	var j = 0;
	var j_col = 1;
	var n = sizeof(this.AvailableLangsC);
	var j_threshold = n%num_cols;
	var on_col = Math.floor(n/num_cols);
	var w = Math.floor(100/num_cols);
	var oTr = ce('TR', oTbodyLangs);
	var oUl = ce('UL', ce('TD', oTr, {width: w+'%'}, {border: 0}));
	var defLang = false;
	if(!SProfile.gprofiles || isEmpty(SProfile.gprofiles.curset))
		defLang = true;
	for(i in this.AvailableLangsC)
	{
		j++;
		var oLi = ce('LI', oUl, {innerHTML: '<img src="/img/lang_'+ i +'.gif" title="'+ this.AvailableLangsC[i] +'" align="absmiddle" style="margin: 0px 7px"/>'+ this.AvailableLangsC[i]}, {paddingTop: '4px'});
		checked = false;
		var disabled = false;
		if(defLang && Lang == i)
			checked = true;
		else if(SProfile.gprofiles && SProfile.gprofiles.langs)
		{
			if(typeof(arraySearch(SProfile.gprofiles.langs, i)) == 'number')
			{
				checked = true;
				disabled = true;
			}
		}
		var oInput = new __Checkbox(null, i, checked, disabled);
		oInput.setOnChange(true, deleteLang, true);
		oInput.setOnChange(false, cancelDeleteLang, true);
		oLi.insertBefore(oInput.o, oLi.firstChild);
		if((j_col <= j_threshold && j == ((on_col+1)*j_col)) || (j_col > j_threshold && j == (on_col*j_col+j_threshold)))
		{
			if(j_col+1 <= num_cols)
			{
				j_col++;
				oUl = ce('UL', ce('TD', oTr, {width: w+'%'}, {border: 0}));
			}
		}
	}


	ce('INPUT', ce('DIV', this.oForm, null, {textAlign: 'right', paddingRight: '25px'}), {type: 'submit', className: 'std_button', value: Locale['ToApply']});

	this.oForm.onsubmit = function()
	{
		var obj = {};
		var ar = oTbodyProfiles.getElementsByTagName('IMG');
		var checkedAnyOne = false;
		for(var i=0; i<ar.length; i++)
		{	//профили
			if(!ar[i].type || ar[i].type != 'checkbox')
				continue;
			if(ar[i].dis)
			{	//удаление
				checkedAnyOne = true;
				if(!ar[i].checked)
				{
					if(!obj.del)
						obj.del = [];
					obj.del.push(ar[i].value);
				}
				continue;
			}
			if(ar[i].checked)
			{
				checkedAnyOne = true;
				if(!obj.add)
					obj.add = [];
				obj.add.push(ar[i].value);
			}
		}
		if(!checkedAnyOne)
			return false;
		checkedAnyOne = false;
		ar = oTbodyLangs.getElementsByTagName('IMG');
		for(i=0; i<ar.length; i++)
		{	//языки
			if(!ar[i].type || ar[i].type != 'checkbox')
				continue;
			if(ar[i].dis)
			{
				if(!ar[i].checked)
				{
					if(!obj.delLangs)
						obj.delLangs = [];
					obj.delLangs.push(ar[i].value);
				}
				checkedAnyOne = true;
				continue;
			}
			if(ar[i].checked)
			{
				checkedAnyOne = true;
				if(!obj.addLangs)
					obj.addLangs = [];
				obj.addLangs.push(ar[i].value);
			}
		}
		if(!checkedAnyOne)
			return false;
		Update.request('GProfile', 'gprofiles', 'applyGProfiles', {}, obj, true);
		return false;
	}

	function showTip(e)
	{
		if(!this.tipInfo) return;
		e = e || window.event;
		Tooltip.Show(this.tipInfo, Event.element(e), 250);
	}
	function hideTip()
	{
		if(!this.tipInfo) return;
		Tooltip.Hide();
	}

	function deleteGProfile(o)
	{
		if(window.confirm(Locale['_Profile'].msg['QAllResourcesByThemeWillBeDelContinue']))
		{
			window.alert(Locale['_Profile'].msg['ProfileWillBeDel']);
		}
		else
		{
			o.checked = !o.checked;
		}
	}
	function cancelDeleteGProfile(o)
	{
		if(window.confirm(Locale['_Profile'].msg['QCancelDel']))
		{
			window.alert(Locale['_Profile'].msg['CancelOk']);
		}
		else
		{
			o.checked = !o.checked;
		}
	}
	function deleteLang(o)
	{
		if(window.confirm(Locale['_Profile'].msg['QAllResourcesByLangWillBeDelContinue']))
		{
			window.alert(Locale['_Profile'].msg['RourcesByLangWillBeDel']);
		}
		else
		{
			o.checked = !o.checked;
		}
	}
	function cancelDeleteLang(o)
	{
		if(window.confirm(Locale['_Profile'].msg['QCancelDelByLang']))
		{
			window.alert(Locale['_Profile'].msg['CancelByLangOk']);
		}
		else
		{
			o.checked = !o.checked;
		}
	}
},

Relay: function(r)
{
	switch(r.event.action)
	{
		case 'GetProfiles':
			if(r.variables.profiles)
			{
				this.profiles = r.variables.profiles;
				this.show();
			}
			break;

		case 'applyGProfiles':
			document.location.href = '/';
			break;

		case 'applyProfileFromFriend':
			document.location.href = '/';
			break;

		case 'ProfileToFriend':
			if(r.variables.status == 'ok')
			{
				window.alert(Locale['_Settings'].msg['ProfileToFriendSendOk']);
			}
			else if(r.variables.status == 'error')
			{
				var t = Locale['_Settings'].msg['ProfileToFriendSendError'];
				switch(r.variables.errcode)
				{
					case 2:
						window.alert(t +': '+ Locale['_Settings'].msg['InputCorrectEmail']);
						break;
					default:
						window.alert(t);
						break;
				}
			}
			break;

		case 'updateProfiles':
			//document.location.href = '/';
			break;
	}
}

}