/**
 * file Setting.js with Setting class
 * @author Janusz Rygał
 * @date 18.09.2008 
 */

/**
 * @class Settings 
 * Settings class use to get and set user settings also create preferences window
 */
Settings = function(){
	/**screen settings panel*/
	this.screenPanel;
	/**screen form*/
	this.screenForm;
	/**country form*/
	this.countryForm;
	/**country settings panel*/
	this.countryPanel;
	/**tab panel*/
	this.tabPanel;
	/**settings window*/
	this.window;
	/**allow save or not*/
	var disabled = false;
	
	/**
	 * get desktop id by desktop name
	 */
	 this.getDesktopIdByName = function(name_){
	 	for(i=0; i<context.user.preferences.desktopsStore.getCount(); ++i){
	 		if( context.user.preferences.desktopsStore.getAt(i).get('name') == name_ )
	 			return context.user.preferences.desktopsStore.getAt(i).get('id');
	 	}
	 	return 0;
	 }
	 
	/**
	 * get desktop name by desktop id
	 */
	 this.getDesktopNameById = function(id_){
	 	for(i=0; i<context.user.preferences.desktopsStore.getCount(); ++i){
	 		if( context.user.preferences.desktopsStore.getAt(i).get('id') == id_ )
	 			return context.user.preferences.desktopsStore.getAt(i).get('name');
	 	}
	 	return 0;
	 }
	
	/**
	 * method to apply changes after saving preferences
	 */
	this.apply = function(){
		context.user.preferences.preferencesLoader.load({
							callback: context.user.preferences.apply
						});
	}
	
	/**
	 * method to create country settings form
	 */
	this.createCountryForm = function(){
		 this.countryForm  = new Ext.FormPanel({
											labelWidth: 250,
											url:project_url + 'SaveCountrySettings',
											id: 'countrySettingsForm',
											name:'countrySettingsForm', 
											frame:true,
											disabled: false,
											draggable: false,
											style:'margin: 0 auto;',
											bodyStyle:'padding:5px 5px 0;',
											width: 580,
											autoHeight: true,
											defaults: {width: 230},
											defaultType: 'textfield',
											buttons: [{
														text: 'Save',
														disabled:disabled,
														handler: function(){
																	if (Ext.ComponentMgr.get('countrySettingsForm').getForm().isValid()) {
																		Ext.ComponentMgr.get('countrySettingsForm').getForm().submit({
																			url:project_url + 'SaveCountrySettings', 
																			waitMsg:'Saving Data...',														        	
																			success: function(form, action) {	
																					 	context.settings.apply();
																					 },
										                                	failure: function(form, action) {		
																					Ext.Msg.alert('OMS',"Error occured while changing settings !");
										                                	}							        	
																		});																
																	}
																}
													 },
													 {
													   text: 'Apply',
													   disabled: false,
													   handler: function(){
													   		var country  =  context.settings.countryForm.getComponent('countries_select').getValue();
													   		var language =  context.settings.countryForm.getComponent('languages_select').getValue();
													   		context.user.preferences.preferencesLoader.getAt(0).set('language', language);
													   		context.user.preferences.preferencesLoader.getAt(0).set('country', country);
													   		context.user.preferences.apply();
													   }	
													 }]
							});
							  					
            countryCombo =  new Ext.form.ComboBox({
						     fieldLabel: context.translator.translate('country'),
						     hiddenName:'country',
						     store: context.user.preferences.countriesStore,
						     valueField:'id',
						     name: 'countries_select',
						     id: 'countries_select',
						     displayField:'name',
						     typeAhead: true,
						     mode: 'remote',
						     triggerAction: 'all',
						     emptyText:'Select country...',
						     selectOnFocus:true,
						     allowBlank:false
			           });
			 
			countryCombo.setValue(context.user.preferences.getCountry());          
			           
			languageCombo =  new Ext.form.ComboBox({
						     fieldLabel: context.translator.translate('language'),
						     hiddenName:'language',
						     store: context.user.preferences.langStore,
						     valueField:'id',
						     name: 'languages_select',
						     id: 'languages_select',
						     displayField:'name',
						     typeAhead: true,
						     mode: 'remote',
						     triggerAction: 'all',
						     emptyText:'Select language...',
						     selectOnFocus:true,
						     allowBlank:false
			           }); 
			           
			 languageCombo.setValue(context.user.preferences.getLanguage());     
			 
			 this.countryForm.add(countryCombo);
			 this.countryForm.add(languageCombo);          
	} 
	
	/**
	 * method to create screen form
	 */
	this.createScreenForm = function(){
			this.screenForm  = new Ext.FormPanel({
											labelWidth: 250,
											url:project_url + 'SaveScreenSettings',
											id: 'screenSettingsForm',
											name:'screenSettingsForm', 
											frame:true,
											disabled: false,
											draggable: false,
											style:'margin: 0 auto;',
											bodyStyle:'padding:5px 5px 0;',
											width: 580,
											autoHeight: true,
											defaults: {width: 230},
											defaultType: 'textfield',
											buttons: [{
														text: 'Save',
														disabled:disabled,
														handler: function(){
																	if (Ext.ComponentMgr.get('screenSettingsForm').getForm().isValid()) {
																		Ext.ComponentMgr.get('screenSettingsForm').getForm().submit({
																			url:project_url + 'SaveScreenSettings', 
																			waitMsg:'Saving Data...',														        	
																			success: function(form, action) {	
																					 	context.settings.apply();
																					 },
										                                	failure: function(form, action) {		
																					Ext.Msg.alert('OMS',"Error occured while changing settings !");
										                                	}							        	
																		});																
																	}
																}
													 },
													 {
													   text: 'Apply',
													   disabled: false,
													   handler: function(){
													   		
													   		var news_show  = 0;
													   		var clock_show =  0;
													   		if(context.settings.screenForm.getComponent('showNewsChecker').getValue())
													   			news_show = 1;
													   		if(context.settings.screenForm.getComponent('showClockChecker').getValue())	
													   			clock_show = 1;
													   		var desktop_image =  context.user.preferences.getDesktopNameById( context.settings.screenForm.getComponent('desktops_select').getValue() ); 
													   		
													   		context.user.preferences.preferencesLoader.getAt(0).set('show_news', news_show);
													   		context.user.preferences.preferencesLoader.getAt(0).set('show_clock', clock_show);
													   		context.user.preferences.preferencesLoader.getAt(0).set('desktop_image', desktop_image);
													   		context.user.preferences.apply();
													   }
													 }]
							});
							  					
            imageCombo =  new Ext.form.ComboBox({
						     fieldLabel: context.translator.translate('background_image'),
						     hiddenName:'background_image',
						     store: context.user.preferences.desktopsStore,
						     valueField:'id',
						     name: 'desktops_select',
						     id: 'desktops_select',
						     displayField:'short_desc',
						     typeAhead: true,
						     mode: 'remote',
						     triggerAction: 'all',
						     emptyText:'Select a desktop image...',
						     selectOnFocus:true,
						     allowBlank:false
			           });
              this.screenForm.add( imageCombo ); 

              this.screenForm.add(
					new Ext.form.Label({
									style: "margin-top: 10px;",
									html:  "<img id='deskThumbView' src='/../../imc/images/omsImages/main/desktops/"+context.user.preferences.getDesktopImage().replace('.jpg','.small.jpg')+"'></img>" 
								})
							  );
              
              imageCombo.on('select', function(combo, item){
              		document.getElementById('deskThumbView').src = '/../../imc/images/omsImages/main/desktops/'+context.settings.getDesktopNameById( combo.getValue() ).replace('.jpg','.small.jpg');
              }, imageCombo);
              
              imageCombo.setValue( this.getDesktopIdByName( context.user.preferences.getDesktopImage() ) ); 
              
              
              var grLevelsCombo =  new Ext.form.ComboBox({
						     fieldLabel: context.translator.translate('graphic_level'),
						     hiddenName:'graphic_levels',
						     store: context.user.preferences.graphicLevelsStore,
						     valueField:'id',
						     name: 'graphic_levels_select',
						     id: 'graphic_levels_select',
						     displayField:'symbol',
						     typeAhead: true,
						     mode: 'local',
						     triggerAction: 'all',
						     emptyText:'Select graphic level...',
						     selectOnFocus:true,
						     allowBlank:false
			           });
              	grLevelsCombo.setValue(context.user.preferences.getGraphicLevel());
              var showClockChecker = new Ext.form.Checkbox({
	          							id: 'showClockChecker',
	          							name: 'show_clock',
	          							fieldLabel: context.translator.translate('show_clock'),
	          							width: 20,
	          						    height: 20,
	          						    checked: context.user.preferences.isClockEnabled() 
	          						});   
	          
	          var showNewsChecker = new Ext.form.Checkbox({
	          							id: 'showNewsChecker',
	          							name: 'show_news',
	          							fieldLabel: context.translator.translate('show_news'),
	          							width: 20,
	          						    height: 20,
	          						    checked: context.user.preferences.isNewsEnabled() 
	          						});

              var showReminderChecker = new Ext.form.Checkbox({
	          							id: 'showReminderChecker',
	          							name: 'show_reminder',
	          							fieldLabel: context.translator.translate('show_reminder'),
	          							width: 20,
	          						    height: 20,
	          						    checked: context.user.preferences.isReminderEnable()
	          						});

               var showTipsChecker = new Ext.form.Checkbox({
	          							id: 'showTipsChecker',
	          							name: 'show_tips',
	          							fieldLabel: context.translator.translate('show_tips'),
	          							width: 20,
	          						    height: 20,
	          						    checked: context.user.preferences.isTipEnable()
	          						});
	         // showClockChecker.setValue(context.user.preferences.isClockEnabled());	
	         
	         this.screenForm.add(grLevelsCombo); 						
	         this.screenForm.add(showClockChecker); 
	         this.screenForm.add(showNewsChecker);
             this.screenForm.add(showReminderChecker);
             this.screenForm.add(showTipsChecker);
	}
	
	/**
	 * method to initialize
	 */
	this.init = function(){
		 if(context.user.getUserSystemPosition() == 3)
		    disabled  = true;
		
		this.createScreenForm();
		this.createCountryForm();
		
		this.countryPanel = new Ext.Panel({
						                    region:'center',
											title: context.translator.translate('country_settings'),
						                    deferredRender:false,
						                    layout: 'fit',
						                    style: 'background: #FFFFFF;',
						                    id: 'country_settings',
						                    activeTab:0,
						                    autoScroll:true,
						                    autoHeight: true,
						                    items: [this.countryForm]  
						               });
		
		this.screenPanel = new Ext.Panel({
						                    region:'center',
											title: context.translator.translate('screen'),
						                    deferredRender:false,
						                    layout: 'fit',
						                    style: 'background: #FFFFFF;',
						                    id: 'screen_settings',
						                    activeTab:0,
						                    autoScroll:true,
						                    autoHeight: true,
						                    items: [this.screenForm]
						               });
	
		this.tabPanel = new Ext.TabPanel({
		                    region:'center',
		                    deferredRender:false,
		                    id: 'main_tab',
		                    activeTab:0,
		                    autoHeight: true,
		                    autoScroll:true,
		                    items: [this.screenPanel,
		                    	    this.countryPanel]
		                });
	
		this.window = new Ext.Window({
							id: 'settingsWindow',
						  	title: context.translator.translate('settings'),
							autoHeight: true,
							//height: 600,
							autoScroll: true,
							resizable: false,
							closable: true,
							manager: null,
							listeners: null,
							modal: true,
							width:     550,
							iconCls: 'settings',
							items: [this.tabPanel],
							shim:true,
							animCollapse:false,
							constrainHeader:true					  			
						  });
	}
	
	/**
	 * method to show settings window
	 */
	this.showSettingsWindow = function(){
		this.window.show();
	} 
	
	/**
	 * starting method
	 */
	this.start = function(){
        context.settings.init();
		context.settings.showSettingsWindow();
	} 
	 	
}
