﻿/*
 * Ext JS Library 1.1.1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://www.extjs.com/license
 */

var ProductSearchTab = {
    init : function(){
        this.IsProductIndexLoaded = false;
        this.tabs = new Ext.TabPanel('ProductSearchTab');
     
        this.SearchFormTab = this.tabs.addTab('ProductSearch', Entegris.Translations.get('lblProductSearchTab', "Product Search"),'', false);
        
        this.AZIndexTab = this.tabs.addTab('AZProductIndex', Entegris.Translations.get('lblAZIndexTab', "A-Z Product Index"),'',false);
        this.AZIndexTab.setHidden(true);
        
        Ext.DomHelper.append(this.tabs.stripWrap.select('tr').elements[0], {tag: 'td', style: 'width: 100%'})
        Ext.get('EmailThisPageEmailDiv').appendTo(Ext.DomHelper.append(this.tabs.stripWrap.select('tr').elements[0], {tag: 'td'}, true));
        Ext.get('EmailThisPagePrintDiv').appendTo(Ext.DomHelper.append(this.tabs.stripWrap.select('tr').elements[0], {tag: 'td'}, true));
        
        Ext.get('EmailThisPage').on('click', this.onEmailThisPageClick, this);
        
        this.EmailThisPageDialog = new Entegris.ProductCatalog.EmailThisPageDialog();
        
        this.tabs.Sleep = function ZZzzzZZzzzzzzZZZz(naptime){
          naptime = naptime * 1000;
          var sleeping = true;
          var now = new Date();
          var alarm;
          var startingMSeconds = now.getTime();
          while(sleeping){
             alarm = new Date();
             alarmMSeconds = alarm.getTime();
             if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
          }      
       }
       
        for (var i =0; i< this.tabs.getCount();i++)
        {
            var activeTab = this.tabs.getTab(i);
            if( activeTab.id == "AZProductIndex")
            {
                activeTab.tabheight = 220;
            }
            else
            {
                activeTab.tabheight = activeTab.bodyEl.getHeight(true) + 20;
            }
        }
        
        this.SearchFormTab.isExpanded = false;
        //this.AZIndexTab.isExpanded = false;
       
        this.SearchFormTab.on('activate', this.onActivate_Search  );
        //this.AZIndexTab.on('activate', this.onActivate_AZIndex  );
        
        //this.SearchFormTab.on('beforeclose', this.onBeforeClose  );
        //this.AZIndexTab.on('beforeclose', this.onBeforeClose  );
        
        
       this.tabs.activate('ProductSearch');
        
       var scope = {
            basictab : this.tabs.el
        };
    },
    
    onEmailThisPageClick: function () {
        this.EmailThisPageDialog.show(Ext.get('EmailThisPage'));
    }, 
    
	onActivate_Search: function(tabs, panel){
	    //ProductSearchTab.AZIndexTab.inner.dom.lastChild.attributes["class"].value= "inactive-close-icon";
	    
		//panel.inner.dom.lastChild.attributes["class"].value= "close-icon";
		//var closeNode = Ext.get(panel.inner.dom.lastChild);
		//closeNode.addClassOnOver("inactive-close-over");
		
		//ProductSearchTab.expandPanel(panel, ProductSearchTab.AZIndexTab.bodyEl.getHeight());
		//ProductSearchTab.expandPanel(panel);
    },
    
    
	onActivate_AZIndex: function(tabs, panel){
		
	    ProductSearchTab.SearchFormTab.inner.dom.lastChild.attributes["class"].value= "inactive-close-icon";
	    
		panel.inner.dom.lastChild.attributes["class"].value= "close-icon";
		var closeNode = Ext.get(panel.inner.dom.lastChild);
		closeNode.addClassOnOver("inactive-close-over");
		
		if( ProductSearchTab.IsProductIndexLoaded == false)
		{
		    ProductSearchTab.LoadAZIndex();
		}
		ProductSearchTab.expandPanel(ProductSearchTab.AZIndexTab, undefined, Ext.get('AZIndexTable').getHeight() + 20);
    },
    
    
    LoadAZIndex: function() {
	    // Generate Product Index Characters
        this.activelinkTemplate = new Ext.Template('<a href="#" class = "activeLink" onclick="return false;" id="lnk_{IndexChar}">','{label}','</a>&nbsp;&nbsp;&nbsp;&nbsp;');
        this.inactivelinkTemplate = new Ext.Template('<a href="#" class = "inactiveLink" onclick="return false;" id="lnk_{IndexChar}">','{label}','</a>&nbsp;&nbsp;&nbsp;&nbsp;');
    
        this.indexLinkDiv = Ext.select('.' + "ProductIndexLinks").elements;
        var activeLinks = Entegris.API.ProductCatalog.ProductCatalog.GetProductIndexCharacters(68).value.split(",");
        for (var i = 65; i <= 90 ; i++) 
        {
            var isActive = false;
            for( var j = 0 ; j < activeLinks.length; j++)
            {
                var iChar = activeLinks[j];
                if ( iChar.charCodeAt(0) == i)
                {
                    this.activelinkTemplate.append(this.indexLinkDiv[0], {IndexChar: i, label: String.fromCharCode(i)});
                    var l = Ext.get('lnk_'+ i);
                    l.on('click',ProductSearchTab.onProductIndexLinkClick.createDelegate(this,[i]), this);
                    if( ProductSearchTab.IsProductIndexLoaded == false)
                    {
                        ProductSearchTab.onProductIndexLinkClick(i);
                    }
                    ProductSearchTab.IsProductIndexLoaded = true;
                    isActive=true;
                    break;
                }
             };
             if( isActive== false){
                 this.inactivelinkTemplate.append(this.indexLinkDiv[0], {IndexChar: i, label: String.fromCharCode(i)});
             }
		}
		
    },
    
    
    
    
    

    onBeforeClose: function(panel, e){
        if ( panel.isExpanded )    
		{
		    ProductSearchTab.collapsePanel(panel);
		}
		else{
		    ProductSearchTab.expandPanel(panel);
		}
		e.cancel = true;
    },
    
    
    expandPanel: function (panel, startHeight, endHeight, doFade) {
        if (typeof(doFade) == 'undefined') {
            doFade = true;
        }
	    //panel.inner.dom.lastChild.attributes["class"].value= "close-icon";
	    panel.bodyEl.setVisible(!doFade, false);
	    if (startHeight) {
	        panel.bodyEl.setHeight(startHeight, false);
	    }
	    if (!endHeight) {
	        endHeight = panel.tabheight;
	    }
	    var anim = {};
	    if (doFade) {
	        anim.callback = function() {
	                            panel.isExpanded = true;
	                            panel.bodyEl.setVisible(true,{duration: .2});
	                        }
	    }
    	panel.bodyEl.setHeight(endHeight,anim);
	    ProductSearchTab.tabs.activate(panel.id);
    },
    
    collapsePanel: function (panel) {
	    //panel.inner.dom.lastChild.attributes["class"].value= "close-icon-expand";
        panel.bodyEl.setVisible(true, false);
        panel.bodyEl.setVisible(false,{duration: .2, callback: function() {
                                                                panel.isExpanded = false;
                                                                panel.bodyEl.setHeight(0,true);}});
    },
    
    
        
    onProductIndexLinkClick:function(indexChar){
           var response = Entegris.API.ProductCatalog.ProductCatalog.GetProductIndex(68,String.fromCharCode(indexChar));
           var el = Ext.get('ProductIndexResults');
           el.update(response.value);
           
		   ProductSearchTab.expandPanel(ProductSearchTab.AZIndexTab, undefined, Ext.get('AZIndexTable').getHeight() + 20, false);
		
           return false;
    }
} 
Ext.EventManager.onDocumentReady(ProductSearchTab.init, ProductSearchTab, true);

