COMP.LoadStylesheet('rws_explore');

RWS.Explore=
{
	RequestExplore : function(id)
	{
		// Make sure we're back to simple/advanced search w/explore instead of results.
		var sr = Ext.getCmp('searchResults');
		if (sr && sr.isVisible())
			resetFunction();
		
		var o;
		if (id)
			o = {explorePaneID:id};
		COMP.SendAjaxCommand('rws_request_explore',o,
			function(o)
			{
				if (o.found)
					RWS.Explore.HandleExplore(o);
				else{
					Ext.getCmp('exploreSearch').hide();
					
				}
			});
	}, 
	
	Reset : function(){
		if(this.firstPane){
			RWS.Layout.ExploreSearch.show();
			this.HandleExplore(this.firstPane);
		}
	},

	HandleExplore : function(o,addCrumb)
	{
		
		var sr = Ext.getCmp('searchResults');
		if (sr && sr.isVisible())
			sr.hide();
			
		RWS.Layout.ExploreSearch.show();
		
		// Remember the first one, no matter what.
		if (this.firstPane===null) this.firstPane=o;
		
		// Create a bread crumb to get back to the current pane.
		if (addCrumb===undefined) addCrumb=true;
		if (addCrumb && o.request.length>0)
			COMP.LoadLibraries(['rws_search', 'rws_bread'], function(){
				RWS.Bread.AddCrumb(L(7744) + ": " + o.name, function() {RWS.Explore.HandleExplore(o,false)}); // Explore
			});
		
		RWS.Layout.ExploreSearch.removeAll();
		RWS.Layout.ExploreSearch.add({xtype:'panelHeader', title:(o.name == "v6_explore_start" || o.name == "v6 explore start") ? 'Explore' : o.name});
		for (var x = 0; x < o.buttons[0].length; x ++){
			var rowItems = new Array();
			for (var y=0; y<o.buttons.length; y++){
				rowItems.add(new Ext.ux.exploreButton(Ext.apply(o.buttons[y][x], {cls:'exploreButtonFirst'})));
			}
			RWS.Layout.ExploreSearch.add(new Ext.Container({layout:'hbox', height: 110, items: rowItems}));		
		}
		RWS.Explore.SetExploreHeight(o.buttons);
	},
	
	SetExploreHeight : function(buttons)
	{
		var h=0, r=[false,false,false];
		for (var x=0; x<buttons.length; x++)
			for (var y=0; y<buttons[x].length; y++)
				if (RWS.Explore.ButtonIsValid(buttons[x][y]))
					r[y]=true;
		if (r[2]){
			Ext.getCmp('exploreSearch').addClass('threeRows');
			Ext.getCmp('exploreSearch').removeClass('oneRow');
			Ext.getCmp('exploreSearch').removeClass('twoRows');
			h = 385;
		}
		else if (r[1]){
			Ext.getCmp('exploreSearch').addClass('twoRows');
			Ext.getCmp('exploreSearch').removeClass('oneRow');
			Ext.getCmp('exploreSearch').removeClass('threeRows');
			h = 275;
		} 
		else if (r[0]){
			Ext.getCmp('exploreSearch').addClass('oneRow');
			Ext.getCmp('exploreSearch').removeClass('threeRows');
			Ext.getCmp('exploreSearch').removeClass('twoRows');
			h = 165;  
		} 
		RWS.Layout.ExploreSearch.setHeight(h);
		RWS.Layout.ExploreSearch.doLayout();
	},
	
	ButtonIsValid : function(b)
	{
		return b.labelText.length > 0 && !b.image.endsWith('blank.gif');
	},

	ApplyExploreButton : function(x,y,o)
	{
		var panelId = 'explore_x' + x + '_y' + y;
		var c = Ext.getCmp(panelId);
		c.labelText = o.labelText;
		var o2={};
		if (COMP.V.collectioninfo)
			o2 = {collectioninfo:COMP.V.collectioninfo};
		c.image = COMP.AssembleURL(o.image,o2,false);
		c['buttonAction'] = o.buttonAction;
		c['buttonData1'] = o.buttonData1;
		c['buttonData2'] = o.buttonData2;
		c['curButton'] = o.curButton;
		c['paneID'] = o.paneID;
		c.Update();
	},
	
	firstPane : null
}


Ext.ns('RWS.Explore.xVars');
RWS.Explore.xVars.exploreNone = 0;
RWS.Explore.xVars.exploreMessage = 1;
RWS.Explore.xVars.exploreSearch = 2;
RWS.Explore.xVars.explorePane = 3;
RWS.Explore.xVars.exploreLink = 4;
RWS.Explore.xVars.exploreLaunch = 5;
RWS.Explore.xVars.exploreHelp = 6;  // Outdated if USING_ONLINE_HELP.  Did not remove, due to the fact that the lower consts have specific id's and I don't want to modify these, if something assums an unchanging id
RWS.Explore.xVars.exploreResource = 7;
RWS.Explore.xVars.exploreBulletinPane = 8;
RWS.Explore.xVars.exploreDownloadSet = 4999;
RWS.Explore.xVars.exploreDelete = 5000;	// used when editing button

if (window.COMP) COMP.Broadcast('scriptLoaded_rws_explore');