﻿/*!
 * Ext JS Library 3.0.0
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
var MX_SEARCH_THE = Ext.extend(Ext.util.Observable, {
    constructor: function(config){
        this.addEvents('finished'); // specify the events we're going to fire
        MX_SEARCH_THE.constructor.call(this, config);
    },
    
    create: function(pid){
		var me = this;
		 
		this.json = Ext.util.JSON.decode('{"id":"77"}');
		this.panel = new Ext.Panel({});
		this.pid = pid;
		
		var connUrl='/fileadmin/ressources_mob/mt_search/mx/mx_search_the/form_functions.php';
		var conn = new Ext.data.Connection();
		var responseObject = new Object();
		conn.request({
			url: connUrl,
			method: 'POST',
			params:{
				actionMobi: 'feImport',
				pid: this.pid
			},
			success: function(responseObject){	
				me.json = Ext.util.JSON.decode(responseObject.responseText);
				
				me.panel = new Ext.Panel({ 
					title: me.json.labels[0].title,
					id: 'mt_search_mx_the',
					layout:'form',
					width:'auto',
					mobiClass: 'MX_SEARCH_THE'
				});
				
				switch (me.json.id){
					case '2':	//case_a_cocher_fourchette
						var i = 0;
						while (i < me.json.blocks.length){
							me.panel.add({	
								xtype: 'checkbox', 
								id: 'mt_search_mx_the_check-'+i,
								name: me.json.blocks[i][0].value,
								hideLabel: true
							},{
								xtype:'label',
								text: mobiHtmlDecode(me.json.blocks[i][0].title),
								cls: 'x-form-item-label '+(me.json.blocks[i][0].title.length > mobiFieldLength ? 'mobi-large-label':'mobi-normal-label'),
								labelSeparator: ''
							});
							i++;
						}
						break;
					case '3':	//bouton_radio_fourchette
						var i = 0;
						while (i < me.json.blocks.length){
							me.panel.add({xtype: 'radio', 
								id: 'mt_search_mx_the_radio-'+i,
								name: 'radio', 
								inputValue: me.json.blocks[i][0].value,
								hideLabel: true
							},{
								xtype:'label',
								text: mobiHtmlDecode(me.json.blocks[i][0].title),
								cls: 'x-form-item-label '+(me.json.blocks[i][0].title.length > mobiFieldLength ? 'mobi-large-label':'mobi-normal-label'),
								labelSeparator: ''
							});
							i++;
						}
						break;
					case '4':	//liste_deroulante
						// create a Record constructor:
						var rt = Ext.data.Record.create([
						    {name: 'title'},
						    {name: 'id'}
						]);
						var comboStore = new Ext.data.Store({
						    // explicitly create reader
						    reader: new Ext.data.ArrayReader(
						        {
						            idIndex: 0  // id for each record will be the first element
						        },
						        rt // recordType
						    )
						});
						
						var i = 0;
						while (i < me.json.blocks.length){
							var recData = {
							    title: me.json.blocks[i][0].title,
							    id: me.json.blocks[i][0].value
							};
							var r = new comboStore.recordType(recData, i); // create new record
							comboStore.insert(0, r); // insert a new record into the store (also see add)
							i++;
						}
			
						var combo= new Ext.form.ComboBox({
							id: 'mt_search_mx_the_combo',
							store: comboStore,
							fieldLabel: me.json.labels[1].title,
							name: 'title',
							displayField:'title',
							valueField:'id',
							mode: 'local',
							triggerAction: 'all',
							hideLabel: me.json.labels[1].title.length > 0?false:true
						});
						me.panel.add(combo);
						
						break;	
					default: 
						break;
				}
				
				me.fireEvent('finished', me.panel);
			},
			failure:function(){ 
				return '{}';
			}
		});	
    },
	
	get: function(){
		var r = '';
		switch (this.json.id){
			case '2':	//case_a_cocher_fourchette
				var list = '';
				var i = 0;
				while (i < this.json.blocks.length){
					if (Ext.getCmp('mt_search_mx_the_check-'+i).getValue() == true){
						if(list.length > 0){
							list += ',';
						}
						list += Ext.getCmp('mt_search_mx_the_check-'+i).getName();
					}
					i++;
				}
				r = list;
				break;
			case '3':	//bouton_radio_fourchette
				r = Ext.getCmp('mt_search_mx_the_radio-0').getGroupValue();
				break;
			case '4':	//liste_deroulante
				r = Ext.getCmp('mt_search_mx_the_combo').getValue();
				break;	
			default: 
				break;
		}
		r = 't='+escape(r)+'-'+this.json.andCheck;
		return r;
	}
	
});

Ext.ux.SliderTip = Ext.extend(Ext.Tip, {
    minWidth: 10,
    offsets : [0, -10],
    init : function(slider){
        slider.on('dragstart', this.onSlide, this);
        slider.on('drag', this.onSlide, this);
        slider.on('dragend', this.hide, this);
        slider.on('destroy', this.destroy, this);
    },

    onSlide : function(slider){
        this.show();
        this.body.update(this.getText(slider));
        this.doAutoWidth();
        this.el.alignTo(slider.thumb, 'b-t?', this.offsets);
    },

    getText : function(slider){
        return String(slider.getValue());
    }
});
