var Profile = {

	queue: [],
	maxExtUrls: 10,

	Apply: function(profile)
	{
		this.lang = Locale['_Profile'];
		this.preload = ce('div', document.body, {id: 'preload', innerHTML: '<div id="shroud"></div><table><tbody><tr><td align="center"><div><span><img src="/img/blogo.jpg" width="315" height="315" title="Krozilo"/><br/><br/><img src="/img/actions/loading.gif" width="16px" height="16px" title="" align="absmiddle"/>'+ this.lang['Loading'] +'</span></div></td></tr></tbody></table>'});
		this.ApplyProfileInProgress = true;
		var AlreadyCreated = {}
		// Проверяем наличие открытых сайтов
		var URLPresent = false;
		for(var TabID in profile.containers.cMain['tabs'])
		{
			if(profile.containers.cMain['tabs'][TabID].url)
				URLPresent = true;
		}

		if (URLPresent)
		{
		  Desktop.URLPresent = true;
/*
			if(confirm(this.lang['OpenPrevSites']))
				Desktop.URLPresent = true;
			else
				Desktop.URLPresent = false;
*/
		}
	
		if(DataUpdater.sections.length)
		{
			if(window.confirm('Появились обновления для Ваших профилей. Загрузить?'))
			{
				Update.request('GProfile', 'gprofiles', 'updateProfiles', {}, true);
			}
		}

		var i = 0;
		for(cName in profile.containers)
		{
			var item = profile.containers[cName];
			if(item.size)
				Desktop.SetPanelSize(cName, item.size);
			if(!profile.containers[cName]['tabs'] || !profile.containers[cName]['tabs'].zip)
			{
				for(TabID in profile.containers[cName]['tabs'])
				{
					item = profile.containers[cName]['tabs'][TabID];
					if(cName == 'cMain' && item.url)
					{
						if(++i <= this.maxExtUrls && Desktop.URLPresent)
							Desktop.NewTab(cName, item.name, item.icon, parseInt(TabID), item.url, item.update, null, true);
					}
					else
					{
						Desktop.NewTab(cName, item.name, item.icon, parseInt(TabID), item.url, item.update);
					}

					var order = item.order;
					if(order)
					{
						order = $(order).uniq();
						for(var j=0; j!=order.length;j++)
						{
							var ModuleID = order[j];
							if(profile.modules[ModuleID] && !AlreadyCreated[ModuleID])
								ModManager.Create(profile.modules[ModuleID].type, profile.modules[ModuleID].container, profile.modules[ModuleID].tab, ModuleID, profile.modules[ModuleID].color, profile.modules[ModuleID].ShowOptions, profile.modules[ModuleID].ShowContent);
							AlreadyCreated[ModuleID] = true;
						}
					}
				}
			}

		}

		if(profile.modules)
		{
			for (ModuleID in profile.modules)
			{
				if(!AlreadyCreated[ModuleID])
					ModManager.Create(profile.modules[ModuleID].type, profile.modules[ModuleID].container, profile.modules[ModuleID].tab, ModuleID, profile.modules[ModuleID].color, profile.modules[ModuleID].ShowOptions, profile.modules[ModuleID].ShowContent);
			}
		}

		for(cName in profile.containers)
		{
			if(cName != 'cUp')
			{
				var current = profile.containers[cName].current ? profile.containers[cName].current : null;
				Desktop.SetActiveByID(cName, current);
				if(profile.containers[cName].toggle)
					Desktop.TogglePanel(cName);
			}
		}


		this.ApplyProfileInProgress = false;
		Update.request(null, null, null, null, null,true);
		this.timer = setInterval(function() {this.update(null, null, null, null, true);}.bind(this), 2000);

		if(Cache.News || Cache.Hello)
		{
			for(var i in SProfile.containers.cMain.tabs)
			{
				if(!SProfile.containers.cMain.tabs[i].url)
				{
					var newsBlock = '';
					var helloText = '';
					if(Cache.News)
					   newsBlock = '<div id="newsBlock" style="border: 1px solid #635F57; background-color: #FFFCC8; padding: 10px; margin-bottom: 10px;"><h3 style="margin-bottom: 8px; font-size: 14px;"><img src="/img/icons/excl_mark.gif" align="absmiddle" style="margin-right: 5px;"/>'+ Locale['News'] +'</h3>'+ Cache.News +'</div>';
					if(Cache.Hello)
					{
						helloText = Cache.Hello;
						//насыщаем текст функциональностью (при наличии кодов %S)
						var tImg = '<img src="/img/s_vpravo.gif" width="16" height="16"/>';
						helloText = helloText.replace(/%S1/g, tImg +'&nbsp;<img src="/img/tpl/'+Skin+'/toolbar/register.gif" title="'+ Locale['Registration'] +'" onclick="Startup.DrawRegisterForm()" class="href"/>');
						helloText = helloText.replace(/%S2/g, tImg +'&nbsp;<img src="/img/tpl/'+Skin+'/toolbar/profiles.gif" title="'+ this.lang['ProfilesGallery'] +'" onclick="GProfiles.show()" class="href"/>');
						helloText = helloText.replace(/%S3/g, tImg +'&nbsp;<img src="/img/tpl/'+Skin+'/toolbar/settings.gif" title="'+ Locale['Options'] +'" onclick="Settings.Show()" class="href"/>');
						helloText = helloText.replace(/%S4/g, tImg +'&nbsp;<img src="/img/tpl/'+Skin+'/toolbar/feedback.gif" title="'+ this.lang['Feedback'] +'" onclick="Startup.DrawFeedbackForm()" class="href"/>');
					}
					document.getElementById('Content.cMain.'+i).innerHTML = '<li class="Welcome"><div class="showFrame">'+ newsBlock + helloText +'</div></li>';
				}
			}
		}

		Desktop.URLPresent = true;
		if(document.body.getWidth()<1024)
			Desktop.TogglePanel('cRight');
		this.s = document.body.getElementsByTagName('select');
		setTimeout(function() {
			de(this.preload);
		}.bind(this), 2000);
	},

	update: function(source, action, item, variables, type)
	{
		if (this.ApplyProfileInProgress) return;
		if (source && action && item)
		{
			var record = {
				event: {
					source: source,
					action: action,
					item: item
				},
				variables: variables
			}
			this.queue.push(record);
			var id = item.id || null;
		}
		else
		{
//			debugger;
		}

		if(type && this.queue.length>0)
		{
			new Ajax.Request('/profile.php', {
				postBody: 'data='+Object.toJSON(this.queue),
				method: 'post'
			});
			this.queue = [];
		}

	}

}
