COMP.LoadStylesheet('rws_center');
COMP.LoadStylesheet('rws_simple');
COMP.LoadStylesheet('rws_advanced');

var listIcons = {};

function InitSearch(type,e)
{
	var text=Ext.getCmp('simpleSearchBar').GetSearchText().trim();
	if (text.length < 2 && !SearchContainsStudyProgramInfo())
	{
		alert(L(25774)); // Each search term must be at least 2 characters long.
		return;
	}
		
	if (!type) type="Keyword";
	var o={ SearchType0:type, SearchField0:text, Interface:'simple' };
	
	RWS.Search.SendRequest(o,RWS.Search.PreFilters,null,e);
}

function Browse(rowId)
{
	var row = Ext.getCmp(rowId);
	row.GetBrowseDialog();
	
	var term = row.browseText;
	var type = row.category;
	
	RWS.Browse.Show(term,type,function(text)
		{
			row.SetSearchText(text);
		});
}

function InitAdvancedSearch(e)
{
	var o={Interface:'advanced'};
	var ok=false;
	var idx=1;
	for (var i=1; i<=4; i++)
	{
		var c=Ext.getCmp('advRow'+i);
		var text=c.GetSearchText().trim();
		{
			if ((text.length < 2 && text.length > 0) && !SearchContainsStudyProgramInfo())
			{
				alert(L(25774)); //"Each search term must be at least 2 characters long."
				return;
			}
			
			ok=true;
			o['SearchField' +(idx-1).toString()] = text;
			o['SearchType'  +(idx-1).toString()] = c.GetSearchType();
			if(idx > 1){
				var c=Ext.getCmp('advRow'+(idx));
				o['OperatorType'+(idx-2).toString()] = c.GetSearchOper();
			}
			idx++;
		}
	}
	
	if (!ok) return;
	
	COMP.LoadLibrariesSerial(['rws_search'], function(){RWS.Search.SendRequest(o,null,null,e)});
}


function toggleSearch(){
	var cs = RWS.Layout.ComboSearch;
	var as = RWS.Layout.AdvancedSearch;
	var ss = RWS.Layout.SimpleSearch;
	if(as.isVisible()){   
		as.getEl().fadeOut();
		as.hide();
		ss.show();
		ss.getEl().fadeIn();
	}
	else{
		as.show();
		as.getEl().fadeIn();     
		ss.getEl().fadeOut();
		ss.hide();
	}
}


function resetFunction(){
	RWS.Explore.Reset();
	RWS.Layout.Bread.hide();
	if(RWS.Layout.Results)
		RWS.Layout.Results.hide();
	if(RWS.Layout.NetTrekker.el)
		RWS.Layout.NetTrekker.hide();
	if(RWS.Layout.Bulletins.el)
		RWS.Layout.Bulletins.hide();
	RWS.Layout.NorthBar.reset();
	RWS.Layout.ComboSearch.show();
	RWS.Layout.AddonSearch.show();
	RWS.Layout.SearchPostFilters.hide();
	RWS.Layout.SimpleSearch.doLayout();
	
}

function toggleFilters(){
	var cs = RWS.Layout.ComboSearch;
	cs.body.setStyle('z-index', '1');
	var spf = RWS.Layout.SearchPreFilters;    
	var opt = {
	    duration: .25,
	    scope: spf
	};
	if(spf.isVisible()){                    
		opt.callback = function(){this.hide()};
		spf.el.setHeight(0, opt);
		Ext.getCmp('toggleText').setIconClass('filterArrow_right')
	}
	else{
		spf.show();
		spf.el.setHeight(275, opt);
		spf.doLayout();
		spf.doLayout();
		Ext.getCmp('toggleText').setIconClass('filterArrow_down')
	}
};

function clearAll(){
	RWS.Layout.PreFilters.library.clear();
	RWS.Layout.PreFilters.medium.clear();
	RWS.Layout.PreFilters.awards.clear();
	RWS.Layout.PreFilters.pubyear.clear();
	RWS.Layout.PreFilters.lexile.clear();
	RWS.Layout.PreFilters.studyprogram.clear();
};

function resetSearch(){
	clearAll();
	Ext.getCmp('simpleSearchBar_textfield').clear();
	Ext.getCmp('advRow1_textfield').setValue('');
	Ext.getCmp('advRow2_textfield').setValue('');
	Ext.getCmp('advRow3_textfield').setValue('');
	Ext.getCmp('advRow4_textfield').setValue('');
}

function searchAllClicked(){
	if(COMP.Prefs.searchALL){
		var lang = (COMP.Lang.id == '25856') ? 'english' : 'spanish';
		var len = COMP.Prefs.searchALL.indexOf(".com");
		COMP.Prefs.searchAllHost = COMP.Prefs.searchALL.substring(0, len + 4);
		var path = COMP.Prefs.searchALL.substring(len + 4) + '&w=vauth';
		if(COMP.Prefs.searchALLUn && COMP.Prefs.searchALLPw)
			path += '&uid=' + COMP.Prefs.searchALLUn + '&pwd=' + unencryptPw(COMP.Prefs.searchALLPw);
		path += '&defaultlang=' + lang;
		path = path.replace('login.asp', 'verifyuser.asp');
		// while(path.indexOf('&') != -1){
		// 	path = path.replace('&', escape('&'));
		// }
		COMP.Prefs.searchAllHost = COMP.Prefs.searchAllHost.replace('http://', '');
		COMP.SendAjaxCommand('rws_request_url_response', {host:COMP.Prefs.searchAllHost, path:path}, function(o){
				COMP.PopOpen('http://' + COMP.Prefs.searchAllHost + '/' + o.response);
			});
	}
}

function unencryptPw(pw){
	if (!pw.length) return '';
	pw = pw.split(',');
	var result = "";
	var key = COMP.Prefs.searchALLUn;
	if (!key.length)return '';
		
	while(key.length < pw.length){
		key += key;
	}
	for(i = 0; i < pw.length; i++) {
		result+=String.fromCharCode(parseInt(pw[i]) ^ key.charCodeAt(i));
	}
	return result;
}

Ext.ns('RWS.North');

RWS.North.TriggerAction = function(buttonId){
	switch(buttonId){
	case 'north_home':
		if (COMP.V.homepage.length)
			window.location = COMP.V.homepage;
		else
			COMP.Reload();
		break;
	case 'north_login':
		COMP.LoadLibrariesSerial(['rws_patron', 'rws_login'], function(){
			if(RWS.Patron.current.loggedIn)
				COMP.Login.HandleLogout();
			else
				COMP.Login.handler(buttonId);
		});
		break;
	case 'north_patron':
		RWS.Patron.Show();
		break;
	case 'north_library':
		RWS.Library.Show();
		break;
	}
	
	
};

RWS.Library=
{
	data:{}
	
	// libCode is for future use. For now, the server ignores it.
	,Request : function(collectionID)
	{
		COMP.SendAjaxCommand('all_request_library_info',{collectionID:collectionID},
			RWS.Library.Show, RWS.Library);
	}
	
	,Show : function(data)
	{
		if (data === undefined)
		{
			RWS.Library.Request(COMP.Config.GetDefault('localCollection'));
			return;
		}
		
		var me=RWS.Library;
		me.data = data;
		
		me.dialog = new Ext.Window({
			title:L(26279)
			,modal: true
			,width:600
			,height:380
			,closeAction: 'close'
			,cls:'dialog600'
			,border:false
			,resizable:false
			,defaults:{border:false}
			,items:[{
				xtype:'panel'
				,width:'100%'
				,bodyStyle:'text-align:center;'
				,items:[{
					xtype:'label'
					,cls:'titleText'
					,text:data.libraryName
				}]
			},{
				xtype:'panel'
				,layout:'column'
				,width:'100%'
				,defaults:{border:false}
				,items:[{
					columnWidth:.5
					,xtype:'panel'
					,defaultType:'label'
					,items:[
						{cls:'heading', html:L(25449) + " " + L(4125)+'</br>'}	// Address
						,{cls:'info', html:data.address1+ (data.address2.length > 0 ? '</br>' + data.address2 : '') + '</br>'}
						,{cls:'info', html:data.city + ", " + data.state + '&nbsp;&nbsp;' + data.postal + '</br>'}
						,{cls:'info', html:data.country}
						,{xtype:'linebreak'}
						,{cls:'heading', html:L(5606)+'</br>'}	//Telephone
						,{cls:'info', html:data.phone.length > 0 ? data.phone + '</br>' : ''}
						,{cls:'info', html:(data.fax.length > 0 ? data.fax + ' (' + L(4132) + ')' : '')} // fax
						,{xtype:'linebreak'}
						,{cls:'heading', html:L(11606)+'</br>'}	 // Contact Information*
						,{cls:'info', html:L(4087)+': '+data.librarianTitle+'</br>'}
						,{cls:'info', html:L(4198)+': '+data.librarianName}
						,{xtype:'linebreak'}
						,{cls:'heading', html:L(25449) + " " + L(11909)+'</br>'}	//Email
						,{cls:'info', html:data.email}
					]
				},{
					columnWidth:.5
					,xtype:'panel'
					,defaultType:'label'
					,items:[
						{cls:'heading', html:L(26653)+'</br>'}	//Site Hours
						,{
							xtype:'panel'
							,bodyStyle:'padding-left:10px;font-size:10pt;'
							,border:false
							,items:[{xtype:'label', html:data.libraryHours}]
						}
						,{xtype:'linebreak'}
						,{cls:'heading', html:L(5604)+'</br>'}	//School
						,{cls:'info', html:L(26599)+': '+data.startGrade+'</br>'}	//Starting Grade
						,{cls:'info', html:L(26600)+': '+data.endGrade+'</br>'}	//Ending Grade
					]
				}]
			}]
			,bbar:['->',{
				xtype:'button'
				,text:L(17141) //'OK' 
				,handler:function() { me.dialog.close(); }
			}]
			,listeners:{
				'beforeclose':{fn:function(){me.dialog = null; ResetFocus();}}
			}
		});
		me.dialog.show('nP_center_text');
	}
};
if (window.COMP) COMP.Broadcast('scriptLoaded_rws_center');