// JavaScript Document
//Place ajax search
Ext.onReady(function() {
	    var ds = new Ext.data.Store({
			proxy: new Ext.data.ScriptTagProxy({
			   // url: document.location.href+'&type=1019'
            //method: 'POST',
            //params: {lang:Ext.get('tx_indexedsearch_lang').value},
				url: '/index.php?id=2&type=1019&lang=' + Ext.query('#tx_indexedsearch_lang')[0].value
			}),
         reader: new Ext.data.JsonReader({
				root: 'searchResult',
				totalProperty: 'totalCount',
				id: 'id'
			}, [
				{name: 'title', mapping: 'title'},
            {name: 'subHeader', mapping: 'subHeader'},
            {name: 'image', mapping: 'image'},
            {name: 'url', mapping: 'url'},
            {name: 'sectionName', mapping: 'sectionName'},
            {name: 'imgType', mapping: 'imgType'},
            {name: 'freeIndexUid', mapping: 'freeIndexUid'}
			])
		});
    
		// Custom rendering Template
      var lastSection = '';
		var resultTpl = new Ext.XTemplate(
         
			'<tpl for=\".\">',
               '<tpl if="this.isNewSection(sectionName) == true">',
                  '<div class=\"search-item-header\ {sectionName} fidx_{freeIndexUid}">',
                     '<h3>{sectionName}</h3>',
                  '</div>',
               '</tpl>',
               '<div class=\"search-item {imgType} fidx_{freeIndexUid}\">',
                  '{image}',
                  '<h3>{title}</h3>',
                  '<p>{subHeader}</p>',         
			      '</div>',
         '</tpl>',
         {
            // XTemplate configuration:
            compiled: true,
            disableFormats: true,
            // member functions:
            isNewSection: function(sectionName){
               if (sectionName != lastSection)
               {
                  lastSection = sectionName;
                  return true;
               }
               else 
               {
                  return false;
               }
            }
         }
		);
      
      
		var search = new Ext.form.ComboBox({
			store: ds,
			displayField:'title',
			typeAhead: false,
			loadingText: 'Söker...',
			width: 170,
			pageSize:0,
			hideTrigger:true,
			minChars:2,
         fieldClass:'quicksearch',
			tpl: resultTpl,
  	      listWidth: 300,
         listAlign: [ 'tr-br?', [25,6] ],
         applyTo: 'tx_indexedsearch-sword',
			itemSelector: 'div.search-item',
         maxHeight: 800,
			onSelect: function(record){ // override default onSelect to do redirect
            window.location = String.format('{0}', record.data.url);
			}
		});


});

// listAlign: 'tr-br?',

Ext.override(Ext.form.ComboBox, {
    restrictHeight : function(){
        this.innerList.dom.style.height = '';
        var inner = this.innerList.dom,
            pad = this.list.getFrameWidth('tb') + (this.resizable ? this.handleHeight : 0) + this.assetHeight,
            h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight),
            ha = this.getPosition()[1]-Ext.getBody().getScroll().top,
            hb = Ext.lib.Dom.getViewHeight()-ha-this.getSize().height,
            space = Math.max(ha, hb, this.minHeight || 0)-this.list.shadowOffset-pad-5;
        h = Math.min(h, space, this.maxHeight);
        this.innerList.setHeight(h);
        this.list.beginUpdate();
        this.list.setHeight(h+pad);
        this.list.alignTo.apply(this.list, [this.wrap].concat(this.listAlign));
        this.list.endUpdate();
    },
    expand : function(){
        if(this.isExpanded() || !this.hasFocus){
            return;
        }
        if(this.bufferSize){
            this.doResize(this.bufferSize);
            delete this.bufferSize;
        }
        this.list.alignTo.apply(this.list, [this.wrap].concat(this.listAlign));
        this.list.show();
        if(Ext.isGecko2){
            this.innerList.setOverflow('auto');
        }
        Ext.getDoc().on({
            scope: this,
            mousewheel: this.collapseIf,
            mousedown: this.collapseIf
        });
        this.fireEvent('expand', this);
    }
});
