﻿var typeFineInput = "fineInput";
function objGetFineInputInst(sId, sButtonLabel, fTextEnteredCallback, sBeforeInputLabel)
{
    
    var inst = new Object();
    inst.Type = typeFineInput;
    inst.Id = getObjUniqueId();
    Objects[inst.Id] = inst;
    
    inst._textEnteredCallback = fTextEnteredCallback
    
    inst.Parent = null;
    inst.SetParent = function(object)
    {
        this.Parent = object;
    }
    // STUB включить трим для всех строковых (лабельных) параметров
    //sId = sId.trim();
    //sButtonLabel = trim(sButtonLabel);
    
    if (sId != "")
    {
        sId = ' id="' + sId + '"';
    }
    // STUB отрефакторить. В .TextEntered this нихуя не нужен.
    var code = '<p class="key-word-plus-wrap"><input onfocus="if(this.value==\'ручной ввод ключевых слов\')this.value=\'\';this.select()" onblur="if(!this.value) this.value=\'ручной ввод ключевых слов\'" value="ручной ввод ключевых слов" type="text"' + sId + ' onkeypress="' +
                                'if (getKeyCode(event) == 13)' + 
                                '{' +
                                    ' getObjectById(\'' + inst.Id + '\').TextEntered(this); return false;' +
                                '}" class="key-word-input" /><b onclick="getObjectById(\'' + inst.Id + '\').TextEntered($(this).prev(\'input\'))" class="key-word-plus"></b><div style="clear: left"></div>';
    
    if (sButtonLabel != "")
    {
        
    }
    
    code += "</p>";
    
    
    
    // STUB переделать мож
    inst.TextEntered = function (elInput)
    {
        this._textEnteredCallback(this.GetValue());
        elInput.focus();
        elInput.select();
    }
    
    inst.jq = $(code);
    inst.jqInput = $('input', inst.jq);
    
    inst.SetValue = function(sValue)
    {
        // STUB сагрегировать $()
        this.jq.attr("value", sValue);
    }
    inst.GetValue = function()
    {
        // STUB сагрегировать $()
        return this.jqInput.attr("value");
    }
    
    return inst;
}



var typeSuperInput = "superInput";
function objGetSuperInputInst(bReadonly, textColor)
{
    // STUB EVENT onEditModeQuit
    
    if (bReadonly != true)
    {
	bReadonly = false;
    }
    if (textColor == null)
    {
	textColor = "";
    }
    else
    {
	textColor = "color: " + textColor;
    }
    
    var inst = new Object();
    inst.Id = getObjUniqueId();
    Objects[inst.Id] = inst; // STUB это везде сделать через прототип, фабрику или блаблабла

    inst._readonly = bReadonly;

    inst._styleBackup;
    inst.SetReadonly = function(bValue)
    {
	inst._readonly = bValue;
	if (inst._readonly == false)
	{
	    inst.jq.removeClass("inputReadonly");
	    $('span', inst.jq).attr("style", inst._styleBackup);
	}
	else
	{
	    inst.jq.addClass("inputReadonly");
	    inst._styleBackup = $('span', inst.jq).attr("style");
	    $('span', inst.jq).attr("style", "cursor: default");
	}
    }

    inst.Type = typeSuperInput;
    
    inst.fakeValue = false;
    
    inst.TextEnteredCallback = null;
    
    inst.Value = "Текст";
    inst.SetValue = function(sValue)
    {
        var oldValue = this.Value;
	
	this.Value = trim(sValue); /* .replace(new RegExp(" ", "gi"), "");*/
	if (this.Value.length > 0)
	{
	    this.fakeValue = false;
	    this.jqLabel.text(this.Value);
	    if (this.Value != oldValue) // STUB Может этот иф распространить на весь метод?
	    {
	        if (inst.TextEnteredCallback != null)
	        {
	            inst.TextEnteredCallback(trim(sValue));
	        }
	    }
	}
	else
	{
	    this.fakeValue = true;
	    this.jqLabel.text("Значение не введено");
	}
    }
    //<div class="editIcon ">Edit</div>
    var code = '<div class="input-wrap">'
             + '<input type="text" value="" class="comparator" />'
             + '<span class="editable-box" style="' + textColor + ';" onclick="getObjectById(\'' + inst.Id + '\').ToggleEditMode()">' + inst.Value + '</span>'
	     + '<em class="Ya-B-G Ya-B-G-top">'
	     + '<i class="vsego">27</i> — <i><b class="Yan">Я:</b> <b class="ya"></b></i>, <b class="Beg">B: <b class="be"></b></b>, <b class="Gug">G: <b class="gu"></b></b></em>'
             + '</div>';
    inst.jq = $(code);
    inst._styleBackup = $('span', inst.jq).attr("style");
    
    
    
    inst.jqInput = $(':text', inst.jq);
    inst.jqLabel = $('span', inst.jq);
    inst.jqInput.addClass("invisible");
    
    inst.ReinitNedded = true;
    inst.ReinitInputEventHandlers = function()
    {
	if (inst.ReinitNedded == true) // STUB должен быть способ пизже
	{
	    /*
	    inst.jqInput.mouseover(function(event)
				   {
					alert('mouseover');
				   }
				  );
   */
	    inst.jqLabel.mouseover(function(event)
				   {
					if (inst._readonly == false)
					{
					    $('.editable-box', inst.jq).addClass('penciled');
					}
				   }
				   );
	    inst.jqLabel.mouseout(function(event)
				   {
					if (inst._readonly == false)
					{
					    $('.editable-box', inst.jq).removeClass('penciled');
					}
				   }
				   );
    inst.jqInput.keypress(function (event)
                        {
                            if (event.which == 13)
                            {
                                inst.SetValue(inst.jqInput.attr('value'));
                                inst.ToggleEditMode();
                            }
                            else
                            {
                                if (event.which == 27)
                                {
                                    inst.ToggleEditMode();
                                }
                            }
                        }
                        )
    inst.jqInput.blur(function (event)
                      {
                          inst.SetValue(inst.jqInput.attr('value'));
                          inst.ToggleEditMode();
                      }
                     )
	    inst.ReinitNedded = false;
	}
    }
    
    inst.EditMode = false;    
    inst.SetEditMode = function(bValue)
    {
        this.EditMode = bValue;
        if (this.EditMode == true)
        {
	    // Включаем редактирование
            this.jqLabel.addClass('invisible');
	    //this.jqInput.parent().addClass('focused');
	    this.jqInput.parent().addClass('focused');
            this.jqInput.removeClass('invisible');
	    if (this.fakeValue == false)
	    {
		this.jqInput.attr('value', this.Value);
	    }
	    else
	    {
		this.jqInput.attr('value', "");
	    }
            this.jqInput.removeAttr("disabled");
            this.jqInput.focus(); this.jqInput.select();
        }
        else
        {
	    // Выключаем редактирование
            this.jqLabel.removeClass("invisible");
            this.jqInput.addClass("invisible");
            this.jqInput.attr("disabled", true);
	    this.jqInput.parent().removeClass('focused');
	    $(this).trigger("onEditModeQuit", this.Value);
        }
    }
    

    inst.ToggleEditMode = function ()
    {
	if (this._readonly == true)
	{
	    this.SetEditMode(false);
	}
	else
	{
	    this.SetEditMode(!this.EditMode);
	}
    }
    
    inst.ReinitInputEventHandlers();
    
    return inst;
}