WidgetBrowser=Class.create();
WidgetBrowser.prototype={

baseURI:'http://search.eserver.org/advanced/',

categories:null,
widgets:null,
categoryColumn:null,
widgetColumn:null,
infoColumn:null,
currentCategory:null,
currentWidget:null,

initialize:function(baseURI,indexFile,browserPanel){

this.baseURI=baseURI;

this.categories=[];
this.widgets={};

new Ajax.Request(baseURI+indexFile,{
method:'get',
onSuccess:this.showCategories.bind(this)
});

var panel=$(browserPanel);

var columns=panel.getElementsByTagName('div');

this.categoryColumn=columns[0];
this.widgetColumn=columns[1];
this.infoColumn=columns[2];
},

showCategories:function(response){

var categoryNodes=response.responseXML.getElementsByTagName('category');
var categoryList=document.createElement('ul');
this.categoryColumn.parentNode.replaceChild(categoryList,this.categoryColumn);
this.categoryColumn=categoryList;
for(var i=0;i<categoryNodes.length;i++){
var categoryNode=categoryNodes[i];
var title=categoryNode.getElementsByTagName('title')[0].firstChild.nodeValue;
var xmlFile=categoryNode.getElementsByTagName('xml_file')[0].firstChild.nodeValue;
var recordCount=parseInt(categoryNode.getElementsByTagName('records')[0].firstChild.nodeValue);
var pubDate=new Date(categoryNode.getElementsByTagName('pub_date')[0].firstChild.nodeValue);
var category=new WidgetCategory(title,xmlFile,recordCount,pubDate);
this.categories.push(category);
var categorySelect=function(category){

return(function(){
if(this.currentCategory==category){
return;
}

this.closeCurrentCategory();

if(!category.hasWidgets()){

var widgetsSource=this.baseURI+'/categories/'+category.xmlFile;

new Ajax.Request(widgetsSource,{
method:'get',
onSuccess:function(response){
category.populateWidgets(response,this);
this.showWidgets(category);
}.bind(this)});

}else{
this.showWidgets(category);
}

});
};

Event.observe(category.node,'click',categorySelect(category).bind(this));
Event.observe(category.node,'mouseover',category.mouseOver.bind(category));
Event.observe(category.node,'mouseout',category.mouseOut.bind(category));

this.categoryColumn.appendChild(category.node);

}

if(this.widgetColumn.getElementsByTagName('p').length==0){
var placeholder=document.createElement('p');
Element.addClassName(placeholder,'placeholder');
placeholder.appendChild(document.createTextNode('Select A Category'));
this.widgetColumn.appendChild(placeholder);
}
},

showWidgets:function(category){
this.currentCategory=category;
var widgetList=document.createElement('ul');
this.widgetColumn.parentNode.replaceChild(widgetList,this.widgetColumn);
this.widgetColumn=widgetList;
this.currentCategory.open(this.widgetColumn);

if(this.infoColumn.getElementsByTagName('p').length==0){
var placeholder=document.createElement('p');
Element.addClassName(placeholder,'placeholder');
placeholder.appendChild(document.createTextNode('Select A Database'));
this.infoColumn.appendChild(placeholder);
}

},

showWidget:function(widget){

this.closeCurrentWidget();

this.currentWidget=widget;
this.currentWidget.open();
this.infoColumn.parentNode.replaceChild(this.currentWidget.infoNode,this.infoColumn);
this.infoColumn=this.currentWidget.infoNode;

document.forms[0].search_query.focus()
},

closeCurrentCategory:function(){

this.closeCurrentWidget();

if(this.currentCategory!=null){
this.currentCategory.close();
this.currentCategory=null;
}
},

closeCurrentWidget:function(){
if(this.currentWidget!=null){
this.currentWidget.close();
this.currentWidget=null;

var blank=document.createElement('div');
this.infoColumn.parentNode.replaceChild(blank,this.infoColumn);
this.infoColumn=blank;

document.forms[0].search_query.focus()
}
}

};

WidgetCategory=Class.create();
WidgetCategory.prototype={

title:'',
xmlFile:'',
recordCount:0,
pubDate:null,
node:null,
widgets:null,

initialize:function(title,xmlFile,recordCount,pubDate){
this.title=title;
this.xmlFile=xmlFile;
this.recordCount=recordCount;
this.pubDate=pubDate;

this.node=document.createElement('li');
this.node.appendChild(document.createTextNode(
this.title+' ('+this.recordCount+')'));

this.widgets=[];
},

hasWidgets:function(){
return(this.widgets.length>0);
},

open:function(widgetColumn){
Element.addClassName(this.node,'active');
for(var i=0;i<this.widgets.length;i++){
widgetColumn.appendChild(this.widgets[i].node)
}
},

close:function(){
Element.removeClassName(this.node,'active');
},

mouseOver:function(){
Element.addClassName(this.node,'hover');
},

mouseOut:function(){
Element.removeClassName(this.node,'hover');
},

populateWidgets:function(response,browser){
var widgetNodes=response.responseXML.getElementsByTagName('download');

for(var i=0;i<widgetNodes.length;i++){

var widgetNode=widgetNodes[i];

var idNumber=widgetNode.getAttribute('id');
var name=widgetNode.getAttribute('name');

var widget=new Widget(idNumber,name);

var selectWidget=function(widget){
return(function(){

if(this.currentWidget==widget){
return;
}

widget.open();

if(typeof(this.widgets[widget.idNumber])=='undefined'){

var widgetSource=this.baseURI+widget.xmlFile;

new Ajax.Request(widgetSource,{
method:'get',
onSuccess:function(response){
widget.populate(response);
this.widgets[widget.idNumber]=widget;
this.showWidget(widget);
}.bind(this)});
}else{
this.showWidget(widget);
}
});
};

Event.observe(widget.node,'click',selectWidget(widget).bind(browser));
Event.observe(widget.node,'mouseover',widget.mouseOver.bind(widget));
Event.observe(widget.node,'mouseout',widget.mouseOut.bind(widget));

this.widgets.push(widget);
}
}
};

Widget=Class.create();
Widget.prototype={

idNumber:null,
title:'',
xmlFile:'',

version:'',
pubDate:null,
fileName:'',
briefDescription:'',
downloadLink:'',
license:'',

imageHeight:0,
imageWidth:0,
image:'',

node:null,
infoNode:null,

initialize:function(idNumber,title){
this.idNumber=idNumber;

this.xmlFile='details/'+idNumber+'.xml';
this.title=title;

this.node=document.createElement('li');
this.node.appendChild(document.createTextNode(this.title));
},

open:function(){
Element.addClassName(this.node,'active');
},

close:function(){
Element.removeClassName(this.node,'active');
},

mouseOver:function(){
Element.addClassName(this.node,'hover');
},

mouseOut:function(){
Element.removeClassName(this.node,'hover');
},

populate:function(response){

var widgetInfo=response.responseXML.getElementsByTagName('download')[0];

this.version=widgetInfo.getElementsByTagName('name')[0].firstChild.nodeValue;
this.pubDate=new Date(widgetInfo.getElementsByTagName('pub_date')[0].firstChild.nodeValue);
this.fileName=widgetInfo.getElementsByTagName('file_name')[0].firstChild.nodeValue;
this.briefDescription=widgetInfo.getElementsByTagName('brief_description')[0].firstChild.nodeValue;
this.downloadLink=widgetInfo.getElementsByTagName('download_link')[0].firstChild.nodeValue;
this.license=widgetInfo.getElementsByTagName('license')[0].firstChild.nodeValue;
this.image=widgetInfo.getElementsByTagName('image')[0].firstChild.nodeValue;
this.imageWidth=widgetInfo.getElementsByTagName('image')[0].getAttribute('width');
this.imageHeight=widgetInfo.getElementsByTagName('image')[0].getAttribute('height');

this.imageWidth=220;
this.imageHeight=48;

this.infoNode=document.createElement('div');

var image=document.createElement('img');
image.setAttribute('src',this.image);
image.setAttribute('width',this.imageWidth);
image.setAttribute('height',this.imageHeight);
image.setAttribute('alt',this.title);
this.infoNode.appendChild(image);

var title=document.createElement('h4');
title.appendChild(document.createTextNode(this.title));
this.infoNode.appendChild(title);

var license=document.createElement('p');
Element.addClassName(license,'license');
license.appendChild(document.createTextNode(this.license));
this.infoNode.appendChild(license);

var download=document.createElement('input');
download.setAttribute('type','hidden');
download.setAttribute('name','dbase');
download.setAttribute('value',this.downloadLink);
this.infoNode.appendChild(download);

var description=document.createElement('p');
description.appendChild(document.createTextNode(this.briefDescription));
this.infoNode.appendChild(description);
var moreInfo=document.createElement('a');
moreInfo.setAttribute('href',this.fileName);
Element.addClassName(moreInfo,'more-info');
moreInfo.appendChild(document.createTextNode('Go to Site >>'));
this.infoNode.appendChild(moreInfo)

}
};