﻿var LoginApp = Class.create();
LoginApp.prototype = 
{
    initialize: function(login, pass)
    {
        this.login = $(login);
        this.password = $(pass);
        this.startFieldEvents();
    },    
    startFieldEvents: function()
    {
        if(this.login != null && this.password != null)
        {
            if(this.login.value == "Логин")
                this.login.onclick = this.clearField.bindAsEventListener(this, this.login);
            if(this.password.value == "Пароль")
                this.password.onclick = this.clearField.bindAsEventListener(this, this.password); 
                       
            document.body.onclick = this.fullField.bindAsEventListener(this);
        }            
    },
    clearField: function(evt, field)
    {
        if(field.value == "" || field.value == "Логин" || field.value == "Пароль")
        {
            if(field == this.password)
            {
                var parent = this.password.parentNode;
                Element.remove(this.password);
                var input = document.createElement("input");
                input.type = "password";
                input.value = "";
                this.password = input;
                parent.appendChild(input);
                input.focus();
                input.focus();                
            }
            else
                field.value = "";            
        }           
    },
    fullField: function(evt)
    {
        if(this.login.value != "" && this.login.value != "Логин")
            this.login.onclick = this.startFieldEvents.bindAsEventListener(this);
        if(this.password.value != "" && this.password.value != "Пароль")
           this.password.onclick = this.startFieldEvents.bindAsEventListener(this);
            
        if(Event.element(evt) != this.login && Event.element(evt) != this.password)
        {
            if(this.login.value == "")
            {
                this.login.value = "Логин";
                this.login.onclick = this.clearField.bindAsEventListener(this, this.login);                
            }    
            if(this.password.value == "")
            {
                var parent = this.password.parentNode;
                Element.remove(this.password);
                var input = document.createElement("input");
                input.type = "text";
                input.value = "Пароль";
                this.password = input;
                parent.appendChild(input);                
                this.password.onclick = this.clearField.bindAsEventListener(this, this.password);                                
            }                
        }
    }
};    