COMP.LoadStylesheet('rws_prefilters'); 
Ext.ns('RWS.Layout');

var spStore = new Ext.data.SimpleStore
	({
		fields: ['text','id'],
		data: 
		[
			[ L(6924), 1 ], // accelerated reader
			[ L(7716), 2 ], // reading counts
			[ L(25783), 3 ] // Other (Type it here)
		]
	});

var filterState = {};
var filterDefaults = {};


function changeState(component, value){
	if(filterDefaults[component] === undefined){
		setDefaultState(component, value);
	}
	
	filterState[component] = value;
	evaluateChanges();
}

function resetState(component){
	filterState[component] = filterDefaults[component];
	evaluateChanges();
}

function evaluateChanges(){
	var same = true;
	for(var key in filterState){
		if(filterState[key] != filterDefaults[key]){
			same = false;
		}
	}
	// This solution should work, but it doesn't work 100%, whereas the one above works in all cases
	// if(RWS.Search.PreFilters.prefLibrary == ''&& RWS.Search.PreFilters.prefMedium == ''&& RWS.Search.PreFilters.prefAwards == ''&& RWS.Search.PreFilters.prefPubYear == ''&& RWS.Search.PreFilters.prefLexile == ''&& RWS.Search.PreFilters.prefStudyProgramName == ''&& RWS.Search.PreFilters.prefInterestLevel == ''&& RWS.Search.PreFilters.prefReadingLevel == ''&& RWS.Search.PreFilters.prefPointCount == ''&& RWS.Search.PreFilters.prefHelpFindBook == (COMP.Prefs.showLexileHelp == true)){
	// 	same = true;
	// }
	if(same){
		 Ext.getCmp('clearFilter').hide();
	}
	else {
		 Ext.getCmp('clearFilter').show();
	}
	Ext.getCmp('searchFiltersControl').doLayout();
}

function setDefaultState(component, value){
	filterDefaults[component] = value;
	filterState[component] = value;
}

//The Pre Filter panels. Each filter has a panel defined here that will be used by the BubbleMenu to
//display the filter. Each filter panel must know how to save and load its own filter params to
//and from RWS.Search.  Each filter panel must also know how to clear itself.


/*
awards = new Array();
for(var i = 0; i < COMP.Awards.awards.length; i ++){
	var str = COMP.Awards.awards[i].ID;
	if(str.indexOf('A') == 0 || str.indexOf('THE') == 0 || str.indexOf('NORTH') == 0 || str.indexOf('NEW') == 0 || str.indexOf('SOUTH') == 0 )
		str = str.replace(' ','!@#$')
	var array = str.split(' ');
	array[0] = array[0].replace('!@#$',' ')
	array.remove('');
	awards.add(array);
}
newAwards = new Array();



for(var i = 0; i < awards.length; i ++){
	var award = awards[i];
	var same = true;
	var block = getStringsWithSharedRoot(awards.slice(i));
	i = i + block.length;
	blockWidth = block.width;
	newAwards.add(block); 
}

function getStringsWithSharedRoot(array){
	var numWords, ret = 0;
	for(var i = 0; i < array.length -1; i ++){
		if(i == 0){
			numWords = getNumWordsInCommon(array[i], array[i + 1]);
		}
		if(numWords != getNumWordsInCommon(array[i], array[i + 1])) break;
		else ret ++;
	}
	return ({'length':ret, 'width':numWords, 'word':array[0+]});
}

function getNumWordsInCommon(array1, array2){
	var length = (array1.length < array2.length) ? array1.length : array2.length;
	var i;
	for(i = 0; i < length && array1[i] == array2[i]; i ++);
	return i;
}*/

RWS.Layout.PreFilters = {
	library: {
		xtype:'container',
		width:215,
		height:65,
		items:[{
			layout:'hbox'
			,xtype:'container'
			,height:25
			,items:[{
				xtype: 'label'
				,text: L(26984).toString().replace("{0}", COMP.Prefs.institutionTerm ? COMP.Prefs.institutionTerm : "Site")
				,cls:'filter_title'
			},{
				xtype:'label'
				,text:L(5859)
				,cls:'textlink clearsmall'
				,listeners:{
					'render':{fn:function(c){
						c.getEl().on('click', function(){RWS.Layout.PreFilters.library.clear();});
					}}
				}
			}]
		},{
			id:'prefLibrary'
			,xtype:'comboMenu'
			,width: 170
			,showDefaultOption: false
			,displayField:'text'
			,valueField:'libID'
			,defaultID:COMP.Config.GetDefault('localCollection')
			,menuItems: COMP.Prefs.collectionsList[COMP.Config.GetDefault('searchAddressBook')]
			,listeners:{
				'select':{fn:function(box){
					value = box.getValue();
					RWS.Search.PreFilters.prefLibrary = value;
					changeState(this.id, value);
				}}
				,'render':{fn:function(){
					setDefaultState(this.id, this.getValue());
				}}
			}
		}]
		,listeners:{
			'render':{fn:function(fPanel){
				//If there is a filter param saved then reload it into the panel
				var val = RWS.Search.PreFilters.prefLibrary;
				if (typeof(val)=="string" && !val.length){
					val = COMP.Config.GetDefault('localCollection');
				}
				val = parseInt(val);
				RWS.Search.PreFilters.prefLibrary = val;
				var prefLibrary = fPanel.findById('prefLibrary');
				if(val != 0){
					prefLibrary.setValue(val);
				}
			}}
			,'beforedestroy':{fn:function(fPanel){fPanel.fireEvent('beforehide',fPanel);}}
		}
		,clear:function(){
			var t = RWS.Search.PreFilters.prefLibrary;
            RWS.Search.PreFilters.prefLibrary='';
			var value = COMP.Config.GetDefault('localCollection');
			if(!value) value = '';
			Ext.getCmp('prefLibrary').setValue(value);
			RWS.Search.PreFilters.prefLibrary = parseInt(value);
			resetState('prefLibrary');
		}
	}
	,medium: {
		xtype:'container'
		,width:215
		,height:65
		,items:[{
			layout:'hbox'
			,height:25
			,xtype:'container'
			,items:[{
				xtype: 'label'
				,text: L(4090)
				,cls:'filter_title'
			},{
				xtype:'label'
				,text:L(5859)
				,cls:'textlink clearsmall study'
				,listeners:{
					'render':{fn:function(c){
						c.getEl().on('click', function(){RWS.Layout.PreFilters.medium.clear();});
					}}
				}
			}]
		},{
			id:'prefMedium'
			,xtype:'comboMenu'
			,width:170
			,menuItems:COMP.Medium.mediums
			,displayField:'ID'
			,valueField:'ID'
			,defaultText:'None'
			,defaultID: 'None'
			,listeners:{
				'select':{fn:function(box){
					value = box.getValue();
					RWS.Search.PreFilters.prefMedium = value;
					changeState(this.id, value);
				}}
				,'render':{fn:function(){
					setDefaultState(this.id, this.getValue());
				}}
			}
		}]
		,listeners:{
			'render':{fn:function(fPanel){
				if(RWS.Search.PreFilters.prefMedium){
					Ext.getCmp('prefMedium').setValue(RWS.Search.PreFilters.prefMedium);
				}
			}}
		}
		,clear:function(){
			RWS.Search.PreFilters.prefMedium='';
			Ext.getCmp('prefMedium').setValue('');
			resetState('prefMedium');
		}
	}
	,awards: {
		xtype:'container'
		,width:215
		,height:65
		,items:[{
			layout:'hbox'
			,xtype:'container'
			,height: 25
			,items:[{
				xtype: 'label'
				,text: L(12500)
				,cls:'filter_title'
			},{
				xtype:'label'
				,text:L(5859)
				,cls:'textlink clearsmall study'
				,listeners:{
					'render':{fn:function(c){
						c.getEl().on('click', function(){RWS.Layout.PreFilters.awards.clear();});
					}}
				}
			}]
		},{
			xtype:'comboMenu'
			,menuItems:COMP.Awards.awards
			,displayField:'ID'
			,valueField:'ID'
			,defaultID: 'None'
			,id:'prefAwards'
			,width: 170
			,listeners:{
				'select':{fn:function(box){
					value = box.getValue();
					RWS.Search.PreFilters.prefAwards = value;
					changeState(this.id, value);
				}}
				,'render':{fn:function(){
					setDefaultState(this.id, this.getValue());
				}}
			}
		}]
		,listeners:{
			'render':{fn:function(fPanel){
				//If there is a filter param saved then reload it into the panel
				if(RWS.Search.PreFilters.prefAwards)
					Ext.getCmp('prefAwards').setValue(RWS.Search.PreFilters.prefAwards);
			}}
		}
		,clear:function(){
			RWS.Search.PreFilters.prefAwards='';
			Ext.getCmp('prefAwards').setValue('');
			resetState('prefAwards');
		}
	}
	,pubyear: {
		border:false
		,width:215
		,height:65
		,frame:false
		,filter:''
		,items:[{
			layout:'hbox'
			,xtype:'container'
			,height: 25
			,items:[{
				xtype: 'label'
				,text: L(4094)
				,cls:'filter_title'
			},{
				xtype:'label'
				,text:L(5859)
				,cls:'textlink clearsmall study'
				,listeners:{
					'render':{fn:function(c){
						c.getEl().on('click', function(){RWS.Layout.PreFilters.pubyear.clear();});
					}}
				}
			}]
		},{
			xtype:'textfield'
			,id:'prefPubYear'
			,width:170
			,listeners:{
				'change':{fn:function(field){
					value = field.getValue();
					RWS.Search.PreFilters.prefPubYear = value;
					changeState(this.id, value);
				}}
				,'render':{fn:function(){
					setDefaultState(this.id, this.getValue());
				}}
			}
		}]
		,listeners:{
			'render':{fn:function(fPanel){
				//If there is a filter param saved then reload it into the panel
				if(RWS.Search.PreFilters.prefPubYear)
					Ext.getCmp('prefPubYear').setValue(RWS.Search.PreFilters.prefPubYear);
			}}
		}
		,clear:function(){
			RWS.Search.PreFilters.prefPubYear='';
			Ext.getCmp('prefPubYear').setValue('');
			resetState('prefPubYear');
		}
	}
	,lexile: new Ext.Container({
		xtype:'container'
		,height:280
		,items:[{
			layout:'hbox'
			,height: 25
			,xtype:'container'
			,items:[{
				xtype:'label', text:L(17705), cls:'filter_title_lexile'
				},{
					xtype:'label'
					,text:L(5859)
					,cls:'textlink clearsmall study'
					,listeners:{
						'render':{fn:function(c){
							c.getEl().on('click', function(){RWS.Layout.PreFilters.lexile.clear();});
						}}
					}
				}]
		},{ 
			xtype:'container' 
			,items:[{
				xtype:'container'
				,layout:'hbox'
				,defaults:{width: 40}
				,items:[{
					xtype:'textfield'
					,id:'lexileMin'
					,listeners:{
						'change':function(cmp, newValue, oldValue){
							var max = parseInt(Ext.getCmp('lexileMax').getValue());
							 if(newValue < 0){
								cmp.setValue(0);
							}
							else if(parseInt(newValue) >= max){
								 cmp.setValue(max - 1);
							}
							var prefLexile = Ext.getCmp('prefLexile');
							prefLexile.setValue(0,parseInt(newValue));
							changeState(prefLexile.id + '0', parseInt(newValue));
						}
					}
				},{xtype:'spacer', width:100},{
					xtype:'textfield'
					,id:'lexileMax'
					,cls:'maxFilterLabel'
					,listeners:{
						'change':function(cmp, newValue, oldValue){
							var min = parseInt(Ext.getCmp('lexileMin').getValue());
							 if(newValue > 2000){
								cmp.setValue(2000);
							}
							else if(parseInt(newValue) <= min){
								 cmp.setValue(min + 1);
							}
							var prefLexile = Ext.getCmp('prefLexile');
							prefLexile.setValue(1,parseInt(newValue));
							changeState(prefLexile.id + '1', parseInt(newValue));
						}
					}
				}]
			},new Ext.slider.MultiSlider({
				id:'prefLexile'
				,width:180
				,height:30
				,minValue:0
				,maxValue:2000
				,cls:'rwsSlider'
				,sliderSnap:1
				,type:'horizontal'
				,range:true
				,values: (RWS.Search.PreFilters.prefHelpFindBook && RWS.Patron.current.loggedIn) ? RWS.Patron.current.data.lexile ? RWS.Patron.current.data.lexile : RWS.Search.PreFilters.prefLexile : [0, 2000]
				,listeners:{
					'afterrender':{fn:function(slider){					
						var checked = ((RWS.Search.PreFilters.prefHelpFindBook && RWS.Patron.current.loggedIn) == true);
						if(checked)
						{
   							Ext.getCmp('lexileMax').hide();
							var c = Ext.getCmp('check_helpFindBook');			
							if (c)
								c.setValue(checked);
						}
						if (checked && RWS.Patron.current.data.lexile){
							slider.setValue(RWS.Patron.current.data.lexile);
							Ext.getCmp('lexileMin').setValue(RWS.Patron.current.data.lexile);
						}
						else if (RWS.Search.PreFilters.prefLexile){
							slider.setValue(RWS.Search.PreFilters.prefLexile);
							Ext.getCmp('lexileMin').setValue(slider.getValues()[0]);
						}
						else{
							Ext.getCmp('lexileMin').setValue(slider.getValues()[0]);
							Ext.getCmp('lexileMax').setValue(slider.getValues()[1]);
						}
						setDefaultState(slider.id + '0', slider.getValues()[0]);
						setDefaultState(slider.id + '1', slider.getValues()[1]);
						var changeFn = function(slider){
							var value0 = slider.getValues()[0];
							Ext.getCmp('lexileMin').setValue(value0);
							changeState(slider.id + '0', value0);
							var value1 = slider.getValues()[1];
							Ext.getCmp('lexileMax').setValue(value1);
							changeState(slider.id + '1', value1);
							
							var prefLexile = Ext.getCmp('prefLexile');
							if(prefLexile.getValues()[0] == 0 && prefLexile.getValues()[1] == 2000)
								RWS.Search.PreFilters.prefLexile = "";
							else
								RWS.Search.PreFilters.prefLexile = prefLexile.getValues()[0] + ".." + prefLexile.getValues()[1];
							var check = Ext.getCmp('check_helpFindBook');
							RWS.Search.PreFilters.prefHelpFindBook = check ? check.getValue() : false;
						};
						slider.on('change', changeFn);
					}}
				}
				
			}),{
				xtype:'checkbox'
				,id:'check_helpFindBook'
	            // Pref can prevent the need for the checkbox.
				,hidden: COMP.Config.GetDefault('showLexileHelp') == 'hide'
				,checked: COMP.Config.GetDefault('showLexileHelp') && RWS.Patron.current.loggedIn
				,listeners:{
					'check':{fn:function(box, checked){
						this.check(checked);
					}}
					,'render':{fn:function(){
						this.check(this.checked);
						setDefaultState(this.id, this.getValue());
					}}
				}
				,check:function(checked){
					var prefLexile = this.ownerCt.findById('prefLexile');
					if(this.hidden) checked = false;
					if(checked){
						Ext.getCmp('lexileMax').hide();
						prefLexile.thumbs[1].el.hide();
						prefLexile.setValue(1, 2000);
						if(prefLexile.getValues()[0]){
							Ext.getCmp('lexileMin').setValue(prefLexile.getValues()[0]);
						}
						else{
							prefLexile.setValue(0, 1000);
							Ext.getCmp('lexileMin').setValue('1000');
						}
						if(prefLexile.getValues()[0] != 0 || prefLexile.getValues()[1] != 2000)
							RWS.Search.PreFilters.prefLexile = prefLexile.getValues()[0] + "";
					}
					else if(!checked){
						Ext.getCmp('lexileMax').show(); 
						prefLexile.setVisible(true);
						prefLexile.constrainThumbs = true;
						prefLexile.thumbs[1].el.show();
						prefLexile.setValue(0, 0);
						prefLexile.setValue(1, 2000);
						if(prefLexile.getValues()[0] != 0 || prefLexile.getValues()[1] != 2000)
							RWS.Search.PreFilters.prefLexile = prefLexile.getValues()[0] + ".." + prefLexile.getValues()[1];
					}
					if (checked && RWS.Patron.current.data.lexile){
						prefLexile.setValue(0, RWS.Patron.current.data.lexile);
						Ext.getCmp('lexileMin').setValue(RWS.Patron.current.data.lexile);
						RWS.Search.PreFilters.prefLexile = prefLexile.getValues()[0] + "";
					}
					changeState(this.id, checked);
				}
			},{
				xtype:'label'
				,id:'check_helpFindBook_label'
				,text: L(18507) //"Help me find a book"
				,hidden: COMP.Config.GetDefault('showLexileHelp') == 'hide'
				,style:'position: relative; top: -19; left: 16; font-size: 12;'
				,listeners:{
					'render':{fn:function(c){
						c.getEl().on('click', function(){
							Ext.getCmp('check_helpFindBook').check(!Ext.getCmp('check_helpFindBook').getValue());
							Ext.getCmp('check_helpFindBook').setValue(!Ext.getCmp('check_helpFindBook').getValue());
						});
					}}
				}
			}]
		}]
		,clear:function(){
			var prefLexile = this.ownerCt.findById('prefLexile');
			if(RWS.Search.PreFilters.prefHelpFindBook && RWS.Patron.current.loggedIn){
				prefLexile.thumbs[1].el.hide();
				Ext.getCmp('lexileMax').hide();
				prefLexile.setValue(0, RWS.Patron.current.data.lexile ? RWS.Patron.current.data.lexile : 1000);
			}
			else{
				prefLexile.thumbs[1].el.show() ;
				Ext.getCmp('lexileMax').show(); 
				prefLexile.setValue(0, 0);
			}
			prefLexile.setValue(1, 2000);
			var check = this.ownerCt.findById('check_helpFindBook');
			check.setValue(COMP.Config.GetDefault('showLexileHelp') && RWS.Patron.current.loggedIn);
			resetState('prefLexile0');
			resetState('prefLexile1');
			resetState('lexileMin');
			resetState('lexileMax');
			resetState('check_helpFindBook');
		}
		,resetDefaults:function(saveClicked){
			filterDefaults['check_helpFindBook'] = ((COMP.Config.GetDefault('showLexileHelp') && RWS.Patron.current.loggedIn) == true);
			if(!Ext.getCmp('searchPreFilters').isVisible()){
				Ext.getCmp('searchPreFilters').shouldResetOnShow = true;
				return;
			}
			var shouldShow = !(COMP.Config.GetDefault('showLexileHelp') == 'hide');
			var lexileCheck = Ext.getCmp('check_helpFindBook');
			lexileCheck.setValue(shouldShow);
			lexileCheck.check(shouldShow);
			// if(shouldShow){
			// 	Ext.getCmp('check_helpFindBook').show();
			// 	Ext.getCmp('check_helpFindBook_label').show();
			// 	Ext.getCmp('lexileMax').hide();
			// 	prefLexile.thumbs[1].el.hide();
			// 	prefLexile.setValue(1, 2000);
			// 	if(prefLexile.getValues()[0]){
			// 		Ext.getCmp('lexileMin').setValue(prefLexile.getValues()[0]);
			// 	}
			// 	else{
			// 		prefLexile.setValue(0, 1000);
			// 		Ext.getCmp('lexileMin').setValue('1000');
			// 	}
			// 	if(prefLexile.getValues()[0] != 0 || prefLexile.getValues()[1] != 2000)
			// 		RWS.Search.PreFilters.prefLexile = prefLexile.getValues()[0] + "";  
			// }
			// else {
			// 	Ext.getCmp('lexileMax').show(); 
			// 	prefLexile.setVisible(true);
			// 	prefLexile.constrainThumbs = true;
			// 	prefLexile.thumbs[1].el.show();
			// 	prefLexile.setValue(0, 0);
			// 	prefLexile.setValue(1, 2000);
			// 	if(prefLexile.getValues()[0] != 0 || prefLexile.getValues()[1] != 2000)
			// 		RWS.Search.PreFilters.prefLexile = prefLexile.getValues()[0] + ".." + prefLexile.getValues()[1];
			// 	Ext.getCmp('check_helpFindBook').hide();
			// 	Ext.getCmp('check_helpFindBook_label').hide();
			// }
			if(!saveClicked) this.clear();
		}	
	})
	,studyprogram: new Ext.Container({
		width:235
		,height:280
		,items:[{
			layout:'hbox'
			,height: 25
			,xtype:'container'
			,items:[{
				xtype:'label', text:L(7727), cls:'filter_title'
			},{xtype:'spacer', flex: 1},{
				xtype:'label'
				,text:L(5859)
				,cls:'textlink clearsmall'
				,listeners:{
					'render':{fn:function(c){
						c.getEl().on('click', function(){RWS.Layout.PreFilters.studyprogram.clear();});
					}}
				}
			}]
		},{
			id:'prefStudyProgramName'
			,xtype:'combo'
			,width:170
			,mode:'local'
			,store: spStore
			,triggerAction:'all'
			,displayField:'text'
			,emptyText:'None'
			,value:'None'
			,listeners:{
				'change':{fn:function(box){
					value = box.getValue();
					RWS.Search.PreFilters.prefStudyProgramName = value;
					changeState(this.id, value);
				}}
				,'render':{fn:function(){
					setDefaultState(this.id, this.getValue());
				}}
			}
		},{
			xtype:'label', text:'Interest Level', cls:'x-form-item', style:'padding-top:10px;'
		},{
			id:'prefInterestLevel'
			,xtype:'textfield' // Could use a browse button once that's working	
			,listeners:{
				'change':{fn:function(field){
					value = field.getValue();
					RWS.Search.PreFilters.prefInterestLevel = value;
					changeState(this.id, value);
				}}
				,'render':{fn:function(){
					setDefaultState(this.id, this.getValue());
				}}
			}
		},{
			xtype:'label'
			,text:L(6914)//'Reading Level'
			,cls:'x-form-item'
			,style:'padding-top:10px'
		},{
			xtype:'container'
			,layout:'hbox'
			,defaults:{width: 40}
			,items:[{
				xtype:'textfield'
				,id:'readingMin'
				,listeners:{
					'change':function(cmp, newValue, oldValue){
						var max = parseInt(Ext.getCmp('prefReadingLevel').getValues()[1]);
						 if(newValue < 0){
							cmp.setValue(0);
						}
						else if(parseFloat(newValue) >= max){
							 cmp.setValue(max - 1);
						}
						Ext.getCmp('prefReadingLevel').setValue(0, parseFloat(cmp.getValue()));
					}
				}
			},{xtype:'spacer', width:100},{
				xtype:'textfield'
				,id:'readingMax'
				,cls:'readingMax'
				,listeners:{
					'change':function(cmp, newValue, oldValue){
						var min = parseInt(Ext.getCmp('prefReadingLevel').getValues()[0]);
						 if(newValue > 12.8){
							cmp.setValue(12.8);
						}
						else if(parseFloat(newValue) <= min){
							 cmp.setValue(min + 1);
						}
						Ext.getCmp('prefReadingLevel').setValue(1, parseFloat(cmp.getValue()));
					}
				}
			}]
		},new Ext.slider.MultiSlider({
			id:'prefReadingLevel'
			,cls:'rwsSlider'
			,width:180
			,height:25
			,minValue:0.0
			,maxValue:12.8
			,decimalPrecision:1
			,increment:.1
			,values: RWS.Search.PreFilters.prefReadingLevel ? [parseFloat(RWS.Search.PreFilters.prefReadingLevel.split("..")[0]), parseFloat(RWS.Search.PreFilters.prefReadingLevel.split("..")[1])]: [0.0, 12.8]
			,listeners:{
				afterrender:function(slider){
					Ext.getCmp('readingMin').setValue(slider.getValues()[0]);
					Ext.getCmp('readingMax').setValue(slider.getValues()[1]);
					setDefaultState(slider.id + '0', slider.getValues()[0]);
					setDefaultState(slider.id + '1', slider.getValues()[1]);
					slider.on('change', function(slider){
						var values = slider.getValues();
						if(values[0] < 0.1) {
							values[0] = 0.0;
							slider.setValue(0, values[0]);
						}
						Ext.getCmp('readingMin').setValue(values[0]);
						Ext.getCmp('readingMax').setValue(values[1]);
						changeState(slider.id + "0", values[0]);
						changeState(slider.id + "1", values[1]);
						RWS.Search.PreFilters.prefReadingLevel = values[0] + ".." + values[1];
					});
				}
			}
		}),{
			xtype:'label'
			,text:L(6925)//'Point Count'
			,cls:'x-form-item'
			,style:'padding-top:10px;'
		},{
			xtype:'container'
			,layout:'hbox'
			,defaults:{width: 40}
			,items:[{
				xtype:'textfield'
				,id:'pointMin'
				,listeners:{
					'change':function(cmp, newValue, oldValue){
						var max = parseInt(Ext.getCmp('pointMax').getValue());
						 if(newValue < 0){
							cmp.setValue(0);
						}
						else if(parseInt(newValue) >= max){
							 cmp.setValue(max - 1);
						}
						Ext.getCmp('prefPointCount').setValue(0, parseInt(cmp.getValue()));
					}
				}
			},{xtype:'spacer', width:100},{
				xtype:'textfield'
				,id:'pointMax'
				,cls:'pointMax'
				,listeners:{
					'change':function(cmp, newValue, oldValue){
						var min = parseInt(Ext.getCmp('pointMin').getValue());
						 if(newValue > 100){
							cmp.setValue(100);
						}
						else if(parseInt(newValue) <= min){
							 cmp.setValue(min + 1);
						}
						Ext.getCmp('prefPointCount').setValue(1, parseInt(cmp.getValue()));
					}
				}
			}]
		},new Ext.slider.MultiSlider({
			id:'prefPointCount'
			,cls:'rwsSlider'
			,width:180
			,minValue:0
			,maxValue:100
			,values: RWS.Search.PreFilters.prefReadingLevel ? [parseInt(RWS.Search.PreFilters.prefPointCount.split("..")[0]), parseInt(RWS.Search.PreFilters.prefPointCount.split("..")[1])]: [0, 100]
			,listeners:{
				'afterrender':{fn:function(slider){
					Ext.getCmp('pointMin').setValue(slider.getValues()[0]);
					Ext.getCmp('pointMax').setValue(slider.getValues()[1]);
					setDefaultState(slider.id+"0", slider.getValues()[0]);
					setDefaultState(slider.id+"1", slider.getValues()[1]);
					slider.on('change', function(slider){
						var values = slider.getValues();
						Ext.getCmp('pointMin').setValue(values[0]);
						Ext.getCmp('pointMax').setValue(values[1]);
						changeState(slider.id+"0", values[0]);
						changeState(slider.id+"1", values[1]);
						RWS.Search.PreFilters.prefPointCount = values[0] + ".." + values[1];
					}); 
					Ext.getCmp('searchPreFilters').hide(); 
				}}
			}
		})]
		,clear:function(){
			RWS.Search.PreFilters.prefStudyProgramName='';
			RWS.Search.PreFilters.prefInterestLevel='';
			RWS.Search.PreFilters.prefReadingLevel='';
			RWS.Search.PreFilters.prefPointCount='';
			Ext.getCmp('prefStudyProgramName').setValue('');
			Ext.getCmp('prefInterestLevel').setValue('');
			Ext.getCmp('prefPointCount').setValue(0, Ext.getCmp('prefPointCount').minValue);
			Ext.getCmp('prefPointCount').setValue(1, Ext.getCmp('prefPointCount').maxValue);
			Ext.getCmp('prefReadingLevel').setValue(0, Ext.getCmp('prefReadingLevel').minValue);
			Ext.getCmp('prefReadingLevel').setValue(1, Ext.getCmp('prefReadingLevel').maxValue);
			resetState('prefStudyProgramName');
			resetState('prefInterestLevel');
			resetState('prefReadingLevel0');
			resetState('prefReadingLevel1');
			resetState('prefPointCount0');
			resetState('prefPointCount1');
		}
	})
};

function SearchContainsStudyProgramInfo()
{
	var x = ['prefLexile','prefStudyProgramName','prefInterestLevel','prefReadingLevel','prefPointCount'];
	for (var i=0; i<x.length; i++)
		if (RWS.Search.PreFilters[x[i]].length)
			return true;
	
	return false;
	
}
if (window.COMP) COMP.Broadcast('scriptLoaded_rws_prefilters');