/* *********************************************************  
    QGridPanel,QDataControl
    v1.0 Qdos Computer Consultants Ltd http://www.qdoscc.com
   ********************************************************* */

function QGridPanel(ChildCtl){
    var panel = {};
    if ($(ChildCtl).attr('class').indexOf('QGridPanel') > -1){
        panel = ChildCtl;
    }else{
        panel = FindParentFromClass(ChildCtl,'QGridPanel');
    }
    //if (panel) eval('panel.Settings = ' + $(panel).find('.SCCDataControl_Settings').val() + ';');
    return panel;
}

function QGridPanelRow(ChildCtl){
    if(ChildCtl){
        var $RowElement = $($(ChildCtl).parents('.QGridPanelRow').get(0));
        eval('var Settings='+$RowElement.children('.QDataControl_Settings').val());
        this.ClientID = $RowElement.attr('id');
        this.UniqueID = $RowElement.children('.QDataControl_Settings').attr('name');
        this.$Row = $RowElement;
        this.element = $RowElement.get(0);
        this.$SecondaryRow = $RowElement.children('.QGridPanel_SecondaryRow');
        this.RowIndex = Settings.RowIndex;
        this.PopulateOnDemand = Settings.PopulateOnDemand;
        this._chkExpanded = this.$Row.children('.QGridPanelExpanded').children('input').get(0);
        this.Expanded = function(b){
            if(typeof(b) != 'undefined'){
              this._chkExpanded.checked = b;
              return b;
            }else{
                var checked = this._chkExpanded.checked;
                if(typeof(checked)=='undefined'){checked = false;}
                return checked;
            }
        }
        this.NextRow = function(){
            if (1==1){
                var i = 20;
                i = 30;
            }
        }
        this.PostBack = function(){__doPostBack(this.UniqueID+'$Primary','onPopulate');}
        this.ShowSecondary = function(b){
            if(typeof(b) != 'undefined'){
                if(b){
                    this.$Row.addClass('QGridPanelRow_Expanded');
                    this.$SecondaryRow.show(300);                
                }else{
                    this.$Row.removeClass('QGridPanelRow_Expanded');
                    this.$SecondaryRow.hide(300);
                }
            }else{
                return this.Expanded();
            }
        }
        this.ToggleShowSecondary = function() {
            this.Expanded(!this.Expanded());
            this.ShowSecondary(this.Expanded());
        }
    }
}

function QGridPanel_SetFocusAndBlur(expression){
    $(expression)
        .focus(QGP.onInputFocus)
        .blur(QGP.onInputBlur);       
}

function QGridPanel_SetKeyPress(expression){
    $(expression).keypress(QGP.onInputKeyPress);
}


function QGridPanel_Expand(ctl) {
    var Row = new QGridPanelRow(ctl);
    QGP.onItemExpand(Row);
    Row.ToggleShowSecondary();
    if (Row.PopulateOnDemand && Row.Expanded()){
        if(!Row.element.Populated) Row.PostBack();
    }else if (Row.PopulateOnDemand){
        Row.element.Populated = true;
    }
    return false;
}

function QGPObject(){
    this.ItemExpand = new QEvent("QGP_ItemExpand");
    
    // Methods
    this.onItemExpand = function(GridRow){
        QGP.ItemExpand.Raise({Row:GridRow});
    }
    this.onInputKeyPress = function(e){
        // this is an input element
        if (e.which==KEY_ENTER){
            $(this).blur();
            return false;
        }
    }
    this.onInputFocus = function(e){
        // this is an input element
        $(this).parents('.GridCell').addClass('QGridCell_Focus');
    }
    this.onInputBlur = function(e){
        // this is an input element
        $(this).parents('.GridCell').removeClass('QGridCell_Focus');
    }
    
}

var QGP = new QGPObject();

function QGridPanel_Init(ev,args) {   
    QGridPanel_SetKeyPress('.QGridPanelRow .GridCell .TextBox');
    QGridPanel_SetKeyPress('.QGridPanelRow .GridCell .NumberBox');
    QGridPanel_SetKeyPress('.QGridPanelRow .GridCell .DropDown');
    QGridPanel_SetFocusAndBlur('.QGridPanelRow .GridCell .TextBox');
    QGridPanel_SetFocusAndBlur('.QGridPanelRow .GridCell .NumberBox');
    QGridPanel_SetFocusAndBlur('.QGridPanelRow .GridCell .DropDown');
}

QCore.Init.AddHandler(QGridPanel_Init);

