﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Renins");

Renins.OsagoHistory = function(element) {
    Renins.OsagoHistory.initializeBase(this, [element]);
    this._controls = new Array();
    this._visibleRowCount = 0;
    this._paneIndex = 0;
    this._prefix = "driver_";
    this._osagoHistoryPattern = "<tr>"
                        + "<th>За {1}:</th>"
						+ "<td id='{0}'></td>"
						+ "</tr>";

    this._accordion = null;
    this._clickHandler = null;
}

Renins.OsagoHistory.prototype = {
    initialize: function() {
        Renins.OsagoHistory.callBaseMethod(this, 'initialize');

        // Add custom initialization here
    },
    dispose: function() {
        //Add custom dispose actions here
        Renins.OsagoHistory.callBaseMethod(this, 'dispose');
    },

    _getNumber: function(number) {
        var result = "первый";
        switch (number) {
            case 1:
                result = "первый";
                break;
            case 2:
                result = "второй";
                break;
            case 3:
                result = "третий";
                break;
            case 4:
                result = "четвертый";
                break;
            case 5:
                result = "пятый";
                break;
            case 6:
                result = "шестой";
                break;
            //AND020710    
            case 7:
                result = "седьмой";
                break;
        }
        return result;
    },

    _addRow: function() {
        var table = this.get_element();
        var crashId = table.id + "_crash_" + this._visibleRowCount;
        var tr = String.format(this._osagoHistoryPattern, crashId, this._getNumber(this._visibleRowCount));
        var index = 0;
        if (Sys.Browser.agent == Sys.Browser.InternetExplorer) {
            index = 0;
        }
        else {
            index = table.childNodes.length - 1;
        }
        var row = table.childNodes[index].insertRow(this._controls.length);
        var th = document.createElement("th");
        th.innerHTML = "за " + this._getNumber(this._visibleRowCount) + "&nbsp;&nbsp;" + "год" + ":";
        row.appendChild(th);
        var td = document.createElement("td");
        td.id = crashId;
        row.appendChild(td);

        var crash = new DIRadioGroup(crashId, driverCrashData, this._prefix + this._paneIndex + "_crash_" + this._visibleRowCount, null, null, 'selected', '');
        crash.selectByID("0");
        if (this._accordion != null) {
            crash.setAccordion(this._accordion);
        }
        else {
            if (this._clickHandler != null) crash.setClickHandler(this._clickHandler);
        }
        this._controls.push(crash);
    },

    _removeRow: function() {
        var table = this.get_element();
        var tbody = table.childNodes[table.childNodes.length - 1];
        var index = 0;
        if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
            index = tbody.childNodes.length - 1;
        else
            index = tbody.childNodes.length - 2;
        tbody.deleteRow(index);
        Array.remove(this._controls, this._controls[this._controls.length - 1]);
    },

    setRowCount: function(count) {
        var element = this.get_element();
        var i = 0;
        var sub = 0;
        if (this._visibleRowCount != count) {
            if (this._visibleRowCount < count) {
                if (this._visibleRowCount == 0) {
                    this._visibleRowCount = 1;
                    element.style.display = "";
                    this._caption.style.display = "";
                }
                if (count == 1) {
                    element.style.display = "";
                    this._caption.style.display = "";
                }
                else {
                    sub = count - this._visibleRowCount;
                    for (i = 0; i < sub; i++) {
                        this._visibleRowCount++;
                        this._addRow();
                    }
                }
            }
            else {
                if (count == 0) {
                    sub = this._visibleRowCount - 1;
                    for (i = 0; i < sub; i++) this._removeRow();
                    this._controls[0].selectByID(0);
                    element.style.display = "none";
                    this._caption.style.display = "none";
                }
                else {
                    sub = this._visibleRowCount - count;
                    for (i = 0; i < sub; i++) this._removeRow();
                }
            }
        }
        this._visibleRowCount = count;
    },

    setRowNumber: function(sender, value, text, tableId) {
        var c = $get(tableId).control;
        var count = parseInt(value);
        if (count != NaN) c.setRowCount(count - 1);
    },

    setPaneIndex: function(index) {
        this._paneIndex = index;
    },

    addControl: function(control) {
        this._controls.push(control);
    },

    setCaptionId: function(id) {
        this._caption = $get(id);
    },

    restoreState: function(state) {
        var i = 0;
        for (i = 0; i < state.length; i++) {
            //if (!(this._controls[i] == null)) //HACK
            //{
                if (state[i].item) {
                    this._controls[i].selectByID(state[i].item);
                }
                else {
                    this._controls[i].selectByID(state[i]);
                }
            //}
        }
    },

    setPrefix: function(prefix) {
        this._prefix = prefix;
    },

    getData: function() {
        var res = '';
        var i = 0;
        for (i = 0; i < this._controls.length; i++) {
            if (i > 0) {
                res += '|';
            }
            res += this._controls[i]._value.value;
        }
        return res;
    },

    setAccordion: function(accordion) {
        this._accordion = accordion;
        var i = 0;
        for (i = 0; i < this._controls.length; i++) {
            this._controls[i].setAccordion(this._accordion);
        }
    },

    setClickHandler: function(handler) {
        this._clickHandler = handler;
        var i = 0;
        for (i = 0; i < this._controls.length; i++) {
            this._controls[i].setClickHandler(this._clickHandler);
        }
    }
}

Renins.OsagoHistory.registerClass('Renins.OsagoHistory', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
