WidgetBrowser = Class.create(); 
WidgetBrowser.prototype={

baseURI:'',
baseURISugar:'',

comunidades:null,
provincias:null,
poblaciones:null,

ComunidadColumn:null,
ProvinciaColumn:null,
PoblacionColumn:null,

currentComunidad:null,
currentProvincia:null,


initialize:function(baseURI,baseURISugar,indexFile,browserPanel){

this.baseURI=baseURI;

this.baseURISugar=baseURISugar;

this.comunidades=[];
this.provincias=[];
this.poblaciones=[];

new Ajax.Request(baseURI+indexFile,{
method:'get',
onSuccess:this.showcomunidades.bind(this)
});

var panel=$(browserPanel);

var columns=panel.getElementsByTagName('div');

this.ComunidadColumn=columns[0];
this.ProvinciaColumn=columns[1];
this.PoblacionColumn=columns[2];
},

showcomunidades:function(response){

var categoryNodes=response.responseXML.getElementsByTagName('category');


var categoryList=document.createElement('ul');

this.ComunidadColumn.parentNode.replaceChild(categoryList,this.ComunidadColumn);
this.ComunidadColumn=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.comunidades.push(category);




var categorySelect=function(category){

return(function(){

if(this.currentComunidad==category){

return;
}

this.closecurrentComunidad();


if(!category.hasprovincias()){

var provinciasSource=this.baseURI+category.xmlFile+'?timestamp='+new Date().getTime();

new Ajax.Request(provinciasSource,{
method:'get',
onSuccess:function(response){
	
  category.populateprovincias(response,this);
this.showprovincias(category);
}.bind(this)});

}else{
this.showprovincias(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.ComunidadColumn.appendChild(category.node);



}

if(this.ProvinciaColumn.getElementsByTagName('p').length==0){
var placeholder=document.createElement('p');
Element.addClassName(placeholder,'placeholder');
placeholder.appendChild(document.createTextNode('Seleccione la  Comunidad'));
this.ProvinciaColumn.appendChild(placeholder);



}
},

showprovincias:function(category){
	
this.currentComunidad=category;
var widgetList=document.createElement('ul');
this.ProvinciaColumn.parentNode.replaceChild(widgetList,this.ProvinciaColumn);
this.ProvinciaColumn=widgetList;
this.currentComunidad.open(this.ProvinciaColumn);

if(this.PoblacionColumn.getElementsByTagName('p').length==0){
var placeholder=document.createElement('p');
Element.addClassName(placeholder,'placeholder');
placeholder.appendChild(document.createTextNode('Seleccione la  Provincia'));
this.PoblacionColumn.appendChild(placeholder);
}

},

showpoblaciones:function(category){
	this.closecurrentPoblacion();
this.currentProvincia=category;
var widgetList=document.createElement('ul');
this.PoblacionColumn.parentNode.replaceChild(widgetList,this.PoblacionColumn);
this.PoblacionColumn=widgetList;
this.currentProvincia.openPoblacion(this.PoblacionColumn);
},

showWidget:function(widget){


this.closecurrentProvincia();

this.currentProvincia=widget;
this.currentProvincia.open();
this.PoblacionColumn.parentNode.replaceChild(this.currentProvincia.infoNode,this.PoblacionColumn);
this.PoblacionColumn=this.currentProvincia.infoNode;
},

closecurrentComunidad:function(){

this.closecurrentProvincia();
//alert("closecurrentProvincia " );
if(this.currentComunidad!=null){
this.currentComunidad.close();
this.currentComunidad=null;
}
},

closecurrentPoblacion:function(){

//this.closecurrentPoblacion();
//alert("close poblacion");
if(this.currentPoblacion!=null){
this.currentPoblacion.close();
this.currentPoblacion=null;
}
},

closecurrentProvincia:function(){
	this.closecurrentPoblacion();
if(this.currentProvincia!=null){
this.currentProvincia.close();
this.currentProvincia=null;


var blank=document.createElement('div');
this.PoblacionColumn.parentNode.replaceChild(blank,this.PoblacionColumn);
this.PoblacionColumn=blank;
}
}

};

WidgetCategory=Class.create();
WidgetCategory.prototype={

title:'',
xmlFile:'',
recordCount:0,
pubDate:null,

node:null,

provincias:null,
poblaciones: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.node.appendChild(document.createTextNode(this.title));

this.provincias=[];
this.poblaciones=[];
},

hasprovincias:function(){
return(this.provincias.length>0);
},

open:function(ProvinciaColumn){
	
Element.addClassName(this.node,'active');

for(var i=0;i<this.provincias.length;i++){

ProvinciaColumn.appendChild(this.provincias[i].node)
}

},

openPoblacion:function(PoblacionColumn){
	
Element.addClassName(this.node,'active');
for(var i=0;i<this.poblaciones.length;i++){
PoblacionColumn.appendChild(this.poblaciones[i].node)
}
},

close:function(){
Element.removeClassName(this.node,'active');
},

mouseOver:function(){
Element.addClassName(this.node,'hover');
},

mouseOut:function(){
Element.removeClassName(this.node,'hover');
},

populateprovincias:function(response,browser){

var categoryNodes=response.responseXML.getElementsByTagName('category');
for(var i=0;i<categoryNodes.length;i++){
var widgetNode=categoryNodes[i];
var title=widgetNode.getElementsByTagName('title')[0].firstChild.nodeValue;
var xmlFile=widgetNode.getElementsByTagName('xml_file')[0].firstChild.nodeValue;
//var idNumber=widgetNode.getAttribute('id');
//var name=widgetNode.getAttribute('name');
var provincia=new WidgetCategory(title,xmlFile,0,'');
this.provincias.push(provincia);
var selectWidget=function(provincia){
	return(function(){

		if(this.currentProvincia==provincia){

		return;
		}
		//if (this.currentProvincia!=null) alert("currentProvincia  "+this.currentProvincia.title);
		
		this.closecurrentPoblacion();
		
		
		
		if (this.currentProvincia!=null) this.currentProvincia.close();//11
		provincia.open();
	  this.currentProvincia=provincia;
		//var poblacionsource=browser.baseURISugar+provincia.xmlFile+'&timestamp='+new Date().getTime();
		//var poblacionsource=browser.baseURI+"../"+provincia.xmlFile+'&timestamp='+new Date().getTime();
		var poblacionsource=provincia.xmlFile+'&timestamp='+new Date().getTime();
		
		// linea ajax
		//var provinciasSource=this.baseURI+this.currentComunidad.xmlFile+'?timestamp='+new Date().getTime();
		
		new Ajax.Request(poblacionsource,{
					method:'get',
					
					onException: function(request, exception) {
    	

    		alert("Js Exception: " + exception.message);
    	
    },
					onSuccess:function(response){
						
							// en categorias category.populateprovincias(response,this);
							provincia.populatepoblaciones(response,browser);
							//poblacion.populate(response);
							this.provincias[i]=provincia;//widget.idNumber]=widget;
							this.showpoblaciones(provincia);
				  }.bind(this)});

});
};
Event.observe(provincia.node,'click',selectWidget(provincia).bind(browser));
Event.observe(provincia.node,'mouseover',provincia.mouseOver.bind(provincia));
Event.observe(provincia.node,'mouseout',provincia.mouseOut.bind(provincia));
this.provincias.push(provincia);
}

}

,

populatepoblaciones:function(response,browser){
	this.poblaciones=[];
	
	var categoryNodes=response.responseXML.getElementsByTagName('category');

for(var i=0;i<categoryNodes.length;i++){
var widgetNode=categoryNodes[i];
var title=widgetNode.getElementsByTagName('title')[0].firstChild.nodeValue;
var xmlFile=widgetNode.getElementsByTagName('xml_file')[0].firstChild.nodeValue;
//var idNumber=widgetNode.getAttribute('id');
//var name=widgetNode.getAttribute('name');
var poblacion=new WidgetCategory(title,xmlFile,0,'');

this.poblaciones.push(poblacion);
var selectWidget=function(poblacion){
	return(function(){

		if(this.currentPoblacion==poblacion){

		return;
		}
		//if (this.currentPoblacion!=null) alert("currentPoblacion  "+this.currentPoblacion.title);
		//alert("clic poblacion");
		
		if (this.currentPoblacion!=null) this.currentPoblacion.close();//11 
		poblacion.open(); 
		//document.location='viewARList.jsp?page=1&zone='+poblacion.title;//   browser.baseURISugar+'accountByCity.do?zone='+poblacion.title;
		
		document.location='RedAR_results.jsp?page=1&zone='+poblacion.title;//   browser.baseURISugar+'accountByCity.do?zone='+poblacion.title;
		
		/*this.currentPoblacion=poblacion; var 
		poblacionclic=browser.baseURISugar+provincia.xmlFile+'&timestamp='+new 
		Date().getTime();
	
		this.poblaciones[i]=poblacion;//widget.idNumber]=widget;
		
		this.showPoblacion(poblacion);*/
		// linea ajax
		/*new Ajax.Request(poblacionsource,{
					method:'get',
					onSuccess:function(response){
							alert("get poblaciones");
							// en categorias category.populateprovincias(response,this);
							poblacion.populate(response);
							//this.provincias[i]=provincia;//widget.idNumber]=widget;
							//this.showProvincia(provincia);
				  }.bind(this)});
*/
});
};

Event.observe(poblacion.node,'click',selectWidget(poblacion).bind(browser));
Event.observe(poblacion.node,'mouseover',poblacion.mouseOver.bind(poblacion));
Event.observe(poblacion.node,'mouseout',poblacion.mouseOut.bind(poblacion));
this.poblaciones.push(poblacion);
}
}


}

;

Widget=Class.create();
Widget.prototype={

idNumber:null,
title:'',
xmlFile:'',

version:'',
pubDate:null,
fileName:'',
briefDescription:'',
fileSize:0,
downloadLink:'',
license:'',

imageHeight:0,
imageWidth:0,
image:'',

node:null,
infoNode:null,

initialize:function(idNumber,title){
this.idNumber=idNumber;

this.xmlFile=idNumber.charAt(idNumber.length-1)+'/'+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.fileSize=widgetInfo.getElementsByTagName('file_size')[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');


if((this.imageHeight/2)>100){
this.imageWidth=(this.imageWidth*100)/this.imageHeight;
this.imageHeight=100;
}else{
this.imageHeight=this.imageHeight/2;
this.imageWidth=this.imageWidth/2;
}

this.infoNode=document.createElement('div');

var image=document.createElement('img');
image.setAttribute('src','http://images.apple.com'+this.image);
image.setAttribute('width',this.imageWidth);
image.setAttribute('height',this.imageHeight);
this.infoNode.appendChild(image);

var title=document.createElement('h4');
title.appendChild(document.createTextNode(this.title));
this.infoNode.appendChild(title);


var months=["January","February","March","April","May","June",
"July","August","September","October","November","December"];
var formattedDate=months[this.pubDate.getMonth()]+' '
+this.pubDate.getDate()+', '
+this.pubDate.getFullYear();

var date=document.createElement('p');
Element.addClassName(date,'date');
date.appendChild(document.createTextNode(formattedDate));
this.infoNode.appendChild(date);

var license=document.createElement('p');
Element.addClassName(license,'license');
license.appendChild(document.createTextNode(this.license));
this.infoNode.appendChild(license);

var download=document.createElement('p');
Element.addClassName(download,'download');
var link=document.createElement('a');
link.setAttribute('href',this.downloadLink);
Element.addClassName(link,'downloadbutton');
download.appendChild(link);
var b=document.createElement('b');
b.appendChild(document.createTextNode('Download'));
link.appendChild(b);
var i=document.createElement('i');
i.appendChild(document.createTextNode(this.fileSize));
link.appendChild(i);
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('More Info...'));
this.infoNode.appendChild(moreInfo)


}

};


