﻿Type.registerNamespace('BIT.WebControls');


BIT.WebControls.ToolBar = function(element) 
{
	BIT.WebControls.ToolBar.initializeBase(this,[element]);

	this.addProperty('selectedItemCssClass', null);
	this.addProperty('checkedItemCssClass', null);
	this.addProperty('disabledItemCssClass', null);
	this.addProperty('hoverItemCssClass', null);
	this.addProperty('itemCssClass', null);
	
	this.addProperty('leftImageCssClass', null);
	this.addProperty('rightImageCssClass', null);
	
	this.addProperty('orientation', 0);
	
	this.addProperty('leftImageUrl', null);
	this.addProperty('rightImageUrl', null);

	this.addProperty('leftHoverImageUrl', null);
	this.addProperty('rightHoverImageUrl', null);
	
	this.addProperty('leftCheckedImageUrl', null);
	this.addProperty('rightCheckedImageUrl', null);
	
	this.addProperty('leftSelectedImageUrl', null);
	this.addProperty('rightSelectedImageUrl', null);
	
	this.addProperty('leftDisabledImageUrl', null);
	this.addProperty('rightDisabledImageUrl', null);
	
	this.addProperty('uniqueID', null);
	
	this.addProperty('items', []);
	
	this.addProperty('clientStateField', null);

	this.addProperty('autoPostBack', false);
	
	
	this.addProperty('baseResUrl', null);
	this.addProperty('baseImgUrl', null);

	
	this.addEvent('init');
	this.addEvent('dispose');

	this._prevCell = null;
	
	this._postbackPending = false;
	this._postbackArg = null;
	this._pageRequestManager = null;
	
	this._checkGroups = [];
	this._updateInit = false;
}

BIT.WebControls.ToolBar.prototype =
{
    add_init: function(handler) {
        this.get_events().addHandler('init', handler);
        this._updateInit = true;
    },

    get_items: function() {
        if (!this.items)
            this.items = [];

        return this.items;
    },

    _updateItems: function(elm, addData) {
        var blocks = elm.rows;

        elm._object = addData ? this : null;

        if (!this.orientation)
            blocks = blocks[0].cells;

        var items = this.get_items();
        var bl = blocks.length;


        this._checkGroups = [];

        for (var i = 0, cnt = 0; i < bl; i++) {
            var cell = blocks[i];

            if (this.orientation)
                cell = cell.cells[0];

            var index = Sys.UI.DomElement.getAttribute(cell, 'ditem');

            if (typeof (index) != 'undefined') {
                if (addData) {
                    var item = null;

                    do {
                        if (items.length <= cnt)
                            throw Error.invalidOperation();

                        item = items[cnt++];
                    }
                    while (item.skipped)

                    cell.item = item;
                    item.cell = cell;
                    cell.table = cell.childNodes[0];

                    if (item.checkMode)
                        Array.add(this._checkGroups['_' + item.checkGroup] || (this._checkGroups['_' + item.checkGroup] = []), cell);

                    if (this._updateInit)
                        this._updateCellLook(cell, false, false);
                }
                else {
                    cell.table = null;
                    cell.item = null;
                    cell.affectedCtl = null;
                    cell.textCtl = null;
                }

            }
        }

        this._updateInit = false;
    },


    _itemOver: function(cell) {
        var item = cell.item;
        var res = true;

        if (item && item.onover)
            res = new Function('item', 'cell', item.onover).call(this, item, (cell.table || cell.childNodes[0]));

        if ((this.hoverItemCssClass || this.leftHoverImageUrl || this.rightHoverImageUrl) && (typeof (res) == 'undefined' || res)) {
            this._updateCellLook(cell, true, false);
            this._prevCell = cell;
        }
    },

    _itemOut: function(cell) {
        var item = cell.item;
        var res = true;

        if (item && item.onout)
            res = new Function('item', 'cell', item.onout).call(this, item, (cell.table || cell.childNodes[0]));

        if ((this.hoverItemCssClass || this.leftHoverImageUrl || this.rightHoverImageUrl) && (typeof (res) == 'undefined' || res)) {
            this._updateCellLook(cell, false, false);
            this._prevCell = null;
        }
    },

    _updateCellLook: function(cell, hover, click) {
        var item = cell.item;

        if (item) {
            var table = cell.table;

            var className = (this.itemCssClass || '');

            var leftUrl = this.leftImageUrl;
            var rightUrl = this.rightImageUrl;

            if (!isUndefinedOrTrue(item.enabled)) {
                className += ' ' + this.disabledItemCssClass;

                if (this.leftDisabledImageUrl)
                    leftUrl = this.leftDisabledImageUrl;

                if (this.rightDisabledImageUrl)
                    rightUrl = this.rightDisabledImageUrl;
            }

            if (item.selected) {
                className += ' ' + this.selectedItemCssClass;

                if (this.leftSelectedImageUrl)
                    leftUrl = this.leftSelectedImageUrl;

                if (this.rightSelectedImageUrl)
                    rightUrl = this.rightSelectedImageUrl;
            }

            if (item.checked) {
                className += ' ' + this.checkedItemCssClass;

                if (this.leftCheckedImageUrl)
                    leftUrl = this.leftCheckedImageUrl;

                if (this.rightCheckedImageUrl)
                    rightUrl = this.rightCheckedImageUrl;
            }

            if (click) {
                var recalc = false;

                if (item.affectedCtlBaseID || item.affectedCtlID) {
                    if (!cell.affectedCtl)
                        cell.affectedCtl = $get((item.affectedCtlBaseID || '') + item.affectedCtlID);

                    if (cell.affectedCtl) {
                        var disp = item.checked ? '' : 'none';

                        if (cell.affectedCtl.style.display != disp) {
                            cell.affectedCtl.style.display = disp;

                            if (item.checked && document.recalc)
                                document.recalc();
                        }
                    }
                }

                if (item.textCtlBaseID && item.checked) {
                    if (!cell.textCtl)
                        cell.textCtl = $get((item.textCtlBaseID || '') + item.textCtlID);

                    if (cell.textCtl) {
                        try {
                            var html = htmlEncode(item.text);
                            if (cell.textCtl.innerHTML != html)
                                cell.textCtl.innerHTML = html;
                        }
                        catch (e) { }
                    }
                }
            }

            if (isUndefinedOrTrue(item.enabled) && hover) {
                className += ' ' + this.hoverItemCssClass;

                if (this.leftHoverImageUrl)
                    leftUrl = this.leftHoverImageUrl;

                if (this.rightHoverImageUrl)
                    rightUrl = this.rightHoverImageUrl;
            }

            var row = table.rows[0];

            this._updateCellImageLook(row, leftUrl, this.leftImageCssClass, true);
            this._updateCellImageLook(row, rightUrl, this.rightImageCssClass, false);

            if (table.className != className)
                table.className = className;
        }
    },

    getItemById: function(id) {
        var items = this.get_items();

        for (var i = 0; i < items.length; i++)
            if (items[i].id == id)
            return items[i];

        return null;
    },

    changeTab: function(id) {
        var item = this.getItemById(id);
        this._itemClick(item.cell);
    },

    _updateCellImageLook: function(row, url, className, first) {
        var cells = row.cells;
        var cell = cells[first ? 0 : cells.length - 1];
        var prevSrc = Sys.UI.DomElement.getAttribute(cell, 'prevSrc');

        var flag = (typeof (prevSrc) != 'undefined');

        if (url) {
            url = resolveImageUrl(url, this.baseResUrl, this.baseImgUrl);

            if (!flag) {
                cell = row.insertCell(first ? 0 : -1);
                cell.appendChild(new Image());
                prevSrc = null;
            }

            if (cell.className != className)
                cell.className = className;

            if (prevSrc != url) {
                Sys.UI.DomElement.setAttribute(cell, 'prevSrc', url);
                with (cell.childNodes[0]) { src = url; alt = ''; };
            }
        }
        else if (flag) {
            row.removeChild(cell);
        }
    },

    _itemClick: function(cell) {
        var item = cell.item;

        if (item && isUndefinedOrTrue(item.enabled)) {
            if (item.checkMode) {
                var uncheck = null;

                if (item.checked) {
                    if (item.checkMode == 3)
                        uncheck = false;
                }
                else {
                    if (item.checkMode == 1)
                        uncheck = true;
                }


                if (uncheck !== null) {
                    item.checked = uncheck;

                    if (this._prevCell && this._prevCell != cell)
                        this._updateCellLook(this._prevCell, false, true);

                    this._updateCellLook(cell, true, true);
                }
                else {

                    var cells = this._checkGroups['_' + item.checkGroup];

                    if (this._prevCell && this._prevCell != cell)
                        this._updateCellLook(this._prevCell, false, true);

                    var showCell = null;

                    for (var i = 0, cl = cells.length; i < cl; i++) {
                        var ccell = cells[i];
                        citem = ccell.item;

                        if (!!citem.checked != (citem == item)) {
                            citem.checked = !citem.checked;

                            if (!citem.checked)
                                this._updateCellLook(ccell, false, true);
                        }
                    }

                    this._updateCellLook(cell, true, true);
                }
            }

            this.saveClientState();

            var res = true;

            if (item.onclick)
                res = new Function('item', 'cell', item.onclick).call(this, item, cell.table);

            if (typeof (res) == 'undefined' || res) {
                if (item.navigateUrl)
                    window.location.href = resolveUrl(item.navigateUrl, this.baseResUrl);
                else if (this.autoPostBack || item.autoPostBack)
                    this._raisePostBack(Array.indexOf(this.get_items(), item));
            }

        }
    },

    _doPostBack: function() {
        this.saveClientState();
        __doPostBack(this.get_uniqueID(), (this._postbackArg === null) ? '' : this._postbackArg + '');
        this._postbackArg = null;
    },

    _raisePostBack: function(arg) {
        this._postbackArg = arg;

        if ((this._pageRequestManager === null) || (!this._pageRequestManager.get_isInAsyncPostBack())) {
            this._doPostBack();
            this._postbackPending = false;
        }
        else {
            this._postbackPending = true;
        }
    },

    _handleEndRequest: function() {
        if ((this._postbackPending === true) && (this._pageRequestManager != null) && (this._pageRequestManager.get_isInAsyncPostBack() === false)) {
            this._postbackPending = false;
            this._doPostBack();
        }
    },

    saveClientState: function() {
        if (this.get_clientStateField()) {
            var sb = new Sys.StringBuilder();

            if (this.items) {
                sb.append('{items:[');
                for (var i = 0; i < this.items.length; i++) {
                    if (i > 0)
                        sb.append(',');

                    sb.append('{selected:' + (!!this.items[i].selected).toString() + ',checked:' + (!!this.items[i].checked).toString() + '}');
                }
                sb.append(']}');
            }
            else {
                sb.append('null');
            }
            this.get_clientStateField().value = sb.toString();
        }
    },

    initialize: function() {
        BIT.WebControls.ToolBar.callBaseMethod(this, "initialize");

        window[this.get_id()] = this;


        this.raiseEvent('init');

        this._handleEndRequestDelegate = Function.createDelegate(this, this._handleEndRequest);

        if (Sys.WebForms && Sys.WebForms.PageRequestManager)
            this._pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();

        if (this._pageRequestManager != null)
            this._pageRequestManager.add_endRequest(this._handleEndRequestDelegate);


        this._updateItems(this.get_element(), true);

        this.saveClientState();
    },

    dispose: function() {

        this._updateItems(this.get_element(), false);

        if (this._pageRequestManager != null)
            this._pageRequestManager.remove_endRequest(this._handleEndRequestDelegate);

        this.raiseEvent('dispose');

        window[this.get_id()] = null;

        BIT.WebControls.ToolBar.callBaseMethod(this, "dispose");
    }
}

BIT.WebControls.ToolBar.registerClass('BIT.WebControls.ToolBar', Sys.UI.Control);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();