﻿

var swicthFields = Class.create();
swicthFields.prototype = {

    cur_text: null,
    cur_radio: null,

    initialize: function(arr){

        for(var i = 0; i < arr.length; i++){
            var el_text = $(arr[i].text);
            var el_radio = $(arr[i].radio);

            el_text.addClassName('disabled'); // аля отключаем
            el_text.value = el_text.title;

            el_radio.el_text = el_text; // ссылка на зависимый

            el_radio.onclick = Delegate.create(this, this.changed, el_radio); // действие на изменение
            el_text.onfocus = Delegate.create(this, this.ala_label, el_radio); // ставим флаг при клике по полю
            el_text.onblur = Delegate.create(this, this.killfocus, el_radio); // восстанавливаем значение по умолчанию
        }

    },


    changed: function(event, radio){
        if( !radio ) radio = event; // IE
        var text = radio.el_text;

        if(radio.checked){
            if( this.cur_text ) this.cur_text.addClassName('disabled');
            text.removeClassName( 'disabled');
            this.cur_text = text;
            this.cur_radio = radio;
            //
            // очищаем поле.
            if( radio.el_text.value == radio.el_text.title ) radio.el_text.value = '';
            /* // или не очищаем, а выделяем текст как в ос
            if ( text.selectionStart != 'undefined' ) text.setSelectionRange(0, text.value.length);
            else if( text.createTextRange ) text.createTextRange().select();
            */
            text.focus(); // курсор в поле
       }

    },


    ala_label: function(event, radio){
        if( !radio ) radio = event; // IE
        radio.checked = true;
        this.changed(null, radio);
    },


    killfocus: function(event, radio){
        if( !radio ) radio = event; // IE
        var text = radio.el_text;

        if( text.value.blank() ) text.value = text.title;
        /*else
        if ( text.selectionStart != 'undefined' ) text.setSelectionRange(0, 0);*/
    }

}// end class
