﻿Ext.ns("Ext.ux.renderer");

Ext.ux.renderer.ComboRenderer = function(options) {
    var value = options.value;
    var combo = options.combo;

    var returnValue = value;
    var valueField = combo.valueField;
        
    var idx = combo.store.findBy(function(record) {
        if(record.get(valueField) == value) {
            returnValue = record.get(combo.displayField);
            return true;
        }
    });
    
    // This is our application specific and might need to be removed for your apps
    if(idx < 0 && value == 0) {
        returnValue = '';
    }
    
    return returnValue;
};

Ext.ux.renderer.Combo = function(combo) {
    return function(value, meta, record) {
        return Ext.ux.renderer.ComboRenderer({value: value, meta: meta, record: record, combo: combo});
    };
}

Ext.override(Ext.tree.TreeNode, {
    destroy : function(){
        if(this.childNodes){
            for(var i = 0,l = this.childNodes.length; i < l; i++){
                this.childNodes[i].destroy();
            }
            this.childNodes = null;
        }
        if (this.getOwnerTree()) {
            this.getOwnerTree().unregisterNode(this);
        }
        if(this.ui.destroy){
            this.ui.destroy();
        }
    }
});

Ext.form.TriggerField.override({
    afterRender : function(){
        Ext.form.TriggerField.superclass.afterRender.call(this);
        var y;
        if(Ext.isIE && !this.hideTrigger && this.el.getY() != (y = this.trigger.getY())){
            this.el.position();
            this.el.setY(y);
        }
    }
});


function SetAutoCompleteBaseParams( queryEvent  )
{
    
	var combo = queryEvent.combo;
	var baseParams = new Object();	
	baseParams["keyName"] = combo.getEl().id;
	baseParams["filterColumn"] = combo.filterColumn;
	combo.store.baseParams = baseParams;
	combo.store.triggerObj = combo;
	return true;
}

function DeselectOption(ds, records, o)
{
	ds.triggerObj.selectedIndex = -2;
}

function DeselectComboOption(combo)
{
	combo.selectedIndex = -2;
}

function CollapseComboOption(combo)
{
    combo.collapse(); 
    combo.store.removeAll();
}

function createAutoComplete(txtField, column, autoStore)
{
    // Custom rendering Template
    var resultTpl = new Ext.XTemplate(
        '<tpl for="."><div class="none">',
            '{Result}',
        '</div></tpl>'
    );
    
	var combo = new Ext.form.ComboBox({
            store: autoStore,
            displayField:'Result',
            typeAhead: false,
            minChars:1,
            loadingText: 'Searching...',
            hideTrigger:true,
            tpl: resultTpl,
	        applyTo: txtField,
            filterColumn : column,
	        itemSelector: 'div.none',
	        listeners:{beforequery :{fn: SetAutoCompleteBaseParams}, expand:{fn: DeselectComboOption}, blur:{fn: CollapseComboOption}}
    });
	
	return combo;
}

function GetGridHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( parent.window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = parent.window.innerWidth;
    myHeight = parent.window.innerHeight;
  } else if( parent.document.documentElement && ( parent.document.documentElement.clientWidth || parent.document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = parent.document.documentElement.clientWidth;
    myHeight = parent.document.documentElement.clientHeight;
  } else if( parent.document.body && ( parent.document.body.clientWidth || parent.document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = parent.document.body.clientWidth;
    myHeight = parent.document.body.clientHeight;
  }
  if(myHeight>90) {
    return myHeight-70;
  }
  
  return 50;
}

function SetGridHeight(grid) {
    
    var gridEl = grid.getEl()
    
    if (grid && gridEl ) {
		var windowHeight = 600;
        if(window.frameElement)
        {
	        windowHeight = Ext.get(window.frameElement).getHeight();
        }else{
			if (window.innerHeight) {
				windowHeight= window.innerHeight;
			}
			else if (document.documentElement && document.documentElement.clientHeight) {
				windowHeight = document.documentElement.clientHeight;
			}
			else if (document.body) {
				windowHeight = document.body.clientHeight;
			}
        }
        var renderElementTop = gridEl.getTop(); 
        grid.setHeight((windowHeight - renderElementTop)); 
    }
    
}

function SetGridWidth(grid) {

    var gridEl = grid.getEl()
    
    if (grid && gridEl ) {

      var containerWidth =400;
      
      if(window.frameElement)
        {
	        containerWidth = Ext.get(window.frameElement).getWidth();
        }else{
			if (window.innerHeight) {
				containerWidth= window.innerWidth;
			}
			else if (document.documentElement && document.documentElement.clientWidth) {
				containerWidth = document.documentElement.clientWidth;
			}
			else if (document.body) {
				containerWidth = document.body.clientWidth;
			}
        }
      
      grid.setWidth(containerWidth);
    }
}

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( parent.window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = parent.window.innerWidth;
    myHeight = parent.window.innerHeight;
  } else if( parent.document.documentElement && ( parent.document.documentElement.clientWidth || parent.document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = parent.document.documentElement.clientWidth;
    myHeight = parent.document.documentElement.clientHeight;
  } else if( parent.document.body && ( parent.document.body.clientWidth || parent.document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = parent.document.body.clientWidth;
    myHeight = parent.document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

function winResize(win,height,width){  
    			
            var windowItems =win.items;
            var i=0;
            
            for (i=0; i <= windowItems.getCount() -1; i++) {
                
                var panel = windowItems.get(i);
                var panelItem = windowItems.get(i).items;
                var j;
                for (j=0; j <= panelItem.getCount() - 1; j++) {
                       
                       if(panel.getActiveTab)
                       {
						   if (panel.getActiveTab().getId() ==  panelItem.get(j).getId() ) {
	                       
									if ( panelItem.get(j).getFrame() ) {
	            	                    
										var contentWindow = panelItem.get(j).getFrame().getWindow();
										if (contentWindow.onWindowResize) { contentWindow.onWindowResize();  } 
								   }                        
						   }
                       }                   
                }			            
            
          }
}

function winLookupResize(win,height,width){  
    			
            var windowItems =win.items;
            var i=0;
            
            for (i=0; i <= windowItems.getCount() -1; i++) {
                    
                    if ( windowItems.get(i).getFrameWindow() ) {
	                    
                        var contentWindow = windowItems.get(i).getFrameWindow();
                        if (contentWindow.onWindowResize) { contentWindow.onWindowResize();  } 
                   }                        
            
          }
}

function GetBaseParams(grid)
{
    var filterObj = new Object();
    for(var i=0;i<filterFields.length;i++)
    {
		if(filterFields[i].list)
			filterObj[filterFields[i].getName()] = filterFields[i].getValue();
		else if (filterFields[i].isXType('checkbox'))
		    filterObj[filterFields[i].getName()] = filterFields[i].getValue();
		else
			filterObj[filterFields[i].getName()] = filterFields[i].getRawValue();
    }
    return filterObj;
//    return grid.store.baseParams;
}

function OpenWindowUrl(moduleName,url)
{
    QoDesk.on('ready',function(){
    
       var module = QoDesk.getModule(moduleName);
       
       if (module)
       {
           module.createWindowUrl(url);           
       }                 
    });
   
}
