/* ГАДЖЕТ СТИКЕРОВ (ЛИПКИХ ЗАПИСОК) */
function __Sticker(id)
{
	this.lang = Locale.Gadgets.Sticker;
	this.Core = new __StdModuleCore(this, id, 'notebook', this.lang.title, this.lang.desc);
	this.numCounters = 0;
	this.colors = ['#000000', '#F70505', '#0402A3', '#3C5F13', '#664016', '#FFFFFF'];
	this.bgcolors = ['#B7BDCD', '#E4C8D7', '#C5E0F3', '#C7E4D2', '#EDE3BF', '#E2E2E2'];
	this.fonts = {'Tahoma': 'Tahoma', 'Arial': 'Arial', 'Arial Black': 'Arial Black', 'Verdana': 'Verdana', 'Comic Sans': 'Comic Sans MS', 'Times New Roman': 'Times New Roman', 'Courier New': 'Courier New', 'Georgia': 'Georgia'};
	this.fontSizes = [8,9,10,11,12,13,14,15,16,17,18];
	this.oData = {settings: {}};
	this.titleAutoLength = 22;

	this.finput = {};
	this.fpanel = {};
	this.constants = {titleMaxLen: 90, noteMaxLen: 255, noteAreaMaxHeight: 300, noteAreaMinHeight: 30};
	this.createContentDistrict();

	this.fpanel.content = ce('UL', this.Content);	//контейнер для городов с часами

	Update.request('Sticker', 'module', 'startup', { id: this.id });
}

__Sticker.prototype.Relay = function(data, part)
{
	switch(data.event.action)
	{
		case 'startup':
			this.oData.settings = data.variables.data;
			this.createPanelSettings();

			if(!this.oData.settings.font)	//для старых профилей
				this.oData.settings.font = 'Arial';
			if(!this.oData.settings.fontSize)
				this.oData.settings.fontSize = 13;
			var k = -1;
			for(var i in this.fonts)
			{
				k++;
				if(this.oData.settings.font == this.fonts[i])
					this.finput.font.options[k].selected = true;
			}
			for(var k=0; k<this.fontSizes.length; k++)
			{
				if(this.oData.settings.fontSize == this.fontSizes[k])
					this.finput.fontSize.options[k].selected = true;
			}
			this.change();
			break;

		default:
			break;
	}
}

//Панель управления гаджетом
__Sticker.prototype.createPanelSettings = function()
{
	var self = this;

	this.fpanel.formSettings = ce('FORM', null);

	ce('SPAN', this.fpanel.formSettings, {innerHTML: this.lang['Header'] +':'});
	this.finput.noteTitle = ce('INPUT', this.fpanel.formSettings, {type: 'text', maxLength: this.constants.titleMaxLen}, {marginLeft: '5px'});

//	this.createColorPanel('bg');
	ce('BR', this.fpanel.formSettings);
	this.createColorPanel('text');
	ce('BR', this.fpanel.formSettings);

	var oTr = ce('TR', ce('TBODY', ce('TABLE', this.fpanel.formSettings)));
	ce('TD', oTr, {innerHTML: this.lang['Font'] +':'});
	this.finput.font = ce('SELECT', ce('TD', oTr), null, {marginLeft: '5px'});
	var t = null;
	for(var i in this.fonts)
	{
		ce('OPTION', this.finput.font, {value: i, innerHTML: i}, {fontFamily: this.fonts[i]});
	}
	this.finput.font.onchange = function()
	{
		self.changeFont(self.fonts[this.value]);
	}

	this.finput.fontSize = ce('SELECT', ce('TD', oTr), null, {marginLeft: '5px'});
	for(var i=0; i<this.fontSizes.length; i++)
	{
		ce('OPTION', this.finput.fontSize, {value: this.fontSizes[i], innerHTML: this.fontSizes[i]});
	}
	this.finput.fontSize.onchange = function()
	{
		self.changeFontSize(this.value);
	}

	ce('BR', this.fpanel.formSettings);
	ce('INPUT', this.fpanel.formSettings, {type: 'submit', className: 'std_button', value: Locale['ToSave']});

	this.Options.appendChild(this.fpanel.formSettings);

	this.fpanel.formSettings.onsubmit = function(e)
	{
		var settings = {};
		if(self.finput.text.flag.value && self.finput.text.color.value!='')
		{	//манипуляции с цветами
			settings.color = self.finput.text.color.value;
		}
/*
		if(self.finput.bg.flag.value && self.finput.bg.color.value!='')
		{	//манипуляции с цветами
			settings.bgcolor = self.finput.bg.color.value;
		}
*/
		settings.font = self.fonts[self.finput.font.value];
		settings.fontSize = self.finput.fontSize.value;

		if(self.finput.noteTitle.value != '')
		{	//манипуляции с заголовком
			self.finput.noteTitle.value = self.finput.noteTitle.value.substr(0, self.constants.noteMaxLen);
			settings.owntitle = true;
		}
		else
		{
			self.makeNotePreview(self.note);
		}
		self.tryShowTitle();
		settings.title = self.finput.noteTitle.value;
		settings.note = self.finput.note.value;
		Update.request('Sticker', 'module', 'change', {id: self.id}, settings);
		self.Core.ToggleOptions(e)
		return false;
	}
}

__Sticker.prototype.createColorPanel = function(type)
{
	var self = this;
	this.finput[type] = {};
	switch(type)
	{
		case 'bg':
			var colors = this.bgcolors;
			ce('SPAN', this.fpanel.formSettings, {innerHTML: '<br/>'+ this.lang['BG'] +': '});
			break;
		case 'text':
			var colors = this.colors;
			ce('SPAN', this.fpanel.formSettings, {innerHTML: Locale['Text'] +': '});
			break;
	}

	this.finput[type].color = ce('INPUT', this.fpanel.formSettings, {type: 'hidden'});
	this.finput[type].flag = ce('INPUT', this.fpanel.formSettings, {type: 'hidden', value: false});
	for(var i=0; i<colors.length; i++)
	{
		var boxColor = ce('A', this.fpanel.formSettings, {type: 'button', className: 'color_box', href: '#', userValue: colors[i]}, {backgroundColor: colors[i]});
		boxColor.onclick = boxColorOnClick;
	}
	this.finput[type].userColor = ce('INPUT', ce('DIV', this.fpanel.formSettings, {innerHTML: '#&nbsp;'}), {type: 'text', size: 6, maxLength: 6});
	this.finput[type].userColor.onblur = userColorOnBlur;

	function boxColorOnClick()
	{
		if(type=='bg')
			t = self.changeBgColor(this.userValue);
		else
			t = self.changeColor(this.userValue);
		if(t)
		{
			self.finput[type].flag.value = true;
			self.finput[type].color.value = this.userValue;
		}
		else
			self.finput[type].flag.value = false;
		return false;
	}

	function userColorOnBlur()
	{
		if(/[a-f0-9]{6}/i.test(this.value))
		{
			if(type=='bg')
				t = self.changeBgColor(this.value);
			else
				t = self.changeColor(this.value);
			if(t)
			{
				document.getElementById(flagColorId).value = true;
				document.getElementById(inputColorId).value = this.value;
			}
			else
				document.getElementById(flagColorId).value = false;
		}
		else
		{
			alert(self.lang.msg['RGBIncorrect']);
		}
		return false;
	}
}

__Sticker.prototype.changeColor = function(color)
{
	if(this.Header.style.color = color)
	{
		this.finput.note.style.color = color;
		this.Content.style.color = color;
		return true;
	}
	else return false
}

__Sticker.prototype.changeBgColor = function(bgcolor)
{
	this.finput.note.style.backgroundColor = bgcolor;
	return true;
/*
	if(this.Header.style.backgroundColor == bgcolor)
	{
		this.Content.style.backgroundColor = bgcolor;
		this.finput.note.style.backgroundColor = bgcolor;
	}
	else return false
*/
}

__Sticker.prototype.changeFont = function(font)
{
	this.Content.style.fontFamily = font;
	this.finput.note.style.fontFamily = font;
	return true;
}

__Sticker.prototype.changeFontSize = function(fontSize)
{
	this.Content.style.fontSize = fontSize+'px';
	this.finput.note.style.fontSize = fontSize+'px';
	return true;
}

__Sticker.prototype.createContentDistrict = function()
{
	var self = this;
	this.fpanel.formNote = ce('FORM', this.Content, {onsubmit: null});
	this.finput.note = ce('TEXTAREA', this.fpanel.formNote, null, {width: '100%', height: this.constants.noteAreaMinHeight+'px', background: 'none', border: 'none', fontFamily: 'comic sans ms', fontSize: '14px'});
	this.finput.note.onchange = function()
	{
		if(!self.oData.settings.owntitle) self.makeNotePreview();
		Update.request('Sticker', 'module', 'change', {id: self.id}, {title: self.finput.noteTitle.value, note: self.finput.note.value, owntitle: self.oData.settings.owntitle}, false);
		resizeBlockByContent(self.finput.note, self.constants.noteAreaMinHeight, self.constants.noteAreaMaxHeight);	//Автоматический ресайзинг textarea с текстом записки
	}
	this.finput.note.onfocus = function()
	{
		this.style.border = '1px solid #FF0000';
		return false;
	}
	this.finput.note.onblur = function()
	{
		this.style.border = 'none';
		return false;
	}
}

__Sticker.prototype.makeNotePreview = function()
{
	this.finput.noteTitle.value = this.finput.note.value.substr(0, this.titleAutoLength) +'...';
	this.oData.settings.owntitle = false;
}

__Sticker.prototype.tryShowTitle = function()
{
	if(this.oData.settings.owntitle || (this.Content.style.display == 'none' || this.Content.style.display != 'none'))
	{
		this.Core.SetHeader(this.finput.noteTitle.value);
	}
	else
	{
		this.Core.SetHeader('');
	}
}

__Sticker.prototype.change = function()
{
	this.tryShowTitle();
	this.changeColor(this.oData.settings.color);
//	this.changeBgColor(this.oData.settings.bgcolor);
	this.changeFont(this.oData.settings.font);
	this.changeFontSize(this.oData.settings.fontSize);
	if(!this.oData.settings.owntitle)
		this.makeNotePreview();
	else
		this.finput.noteTitle.value = this.oData.settings.title;
	this.finput.note.value = this.oData.settings.note;
	this.Core.SetHeader(this.oData.settings.title);
	resizeBlockByContent(this.finput.note, this.constants.noteAreaMinHeight, this.constants.noteAreaMaxHeight);
}
