var QueryFilter = Class.create({
	facets:false,
	blockSize:3,
	btnRight:"btnMoreFiltersRigth",
	btnLeft:"btnMoreFiltersLeft",
	facetCSSMatch:"div.queryFilterFacet",
	position:0,
	hiddenFacetsCodes:Array(),
	topBarHeight:0,
	
	initialize:function(opts){
		this.initObjects();
		this.initFacets();
		this.initEvents();
		this.toggleButtons();
		this.adjustHeight();
	},
	
	initObjects:function(){
		this.btnRight = $(this.btnRight);
		this.btnLeft = $(this.btnLeft);
		this.topBarHeight = $('queryFilterTopBar').getHeight();
		
		/*Show hidden facet extenders. We hide them at render time because we need to watit to window load to intialize the component*/
		$$('.btnFilterQueryShowHidden').each(function(s){s.show()});
		
	},
	
	initFacets:function(){
		this.facets = $$(this.facetCSSMatch);
	},
	
	adjustHeight:function(){
		tmpHeights = new Array();
		pos = this.position+this.blockSize;
		//alert(pos);
		for(i=0;i<this.facets.length;i++){
				
			if(i<pos){
				tmpHeights.push(this.facets[i].getHeight());
			}
			
		}
		
		//We refreshh topbar height
		this.topBarHeight = $('queryFilterTopBar').getHeight();
		
		newHeight = Math.max.apply(Math,tmpHeights);
		
		$('queryFilterHolder').setStyle({height:String(newHeight + this.topBarHeight)+"px"});
		$('queryFilterHolder2').setStyle({height:String(newHeight + this.topBarHeight)+"px"});
		
		$('query_filter_iframe').setStyle({height:String(newHeight + this.topBarHeight)+"px"});
		$('queryFilterTopHolder').setStyle({height:String(newHeight + this.topBarHeight)+"px"});
		
		
	},
	
	moveRight:function(){
		if(this.canMoveRight()){
			//this.showOnIndex(this.position+this.blockSize,this.blockSize);
			//this.showOnIndex(this.position+this.blockSize,this.blockSize);
			this.showAll();
			this.expandContainer();
			
			this.position = this.position+this.blockSize;
			this.toggleButtons();
			this.hideAllHidden();
			this.adjustHeight();
		}
	},
	moveLeft:function(){
		if(this.canMoveLeft()){
			this.collapseContainer();
			
			this.showOnIndex(this.position-this.blockSize,this.blockSize);
			this.position = this.position-this.blockSize;
			this.toggleButtons();
			this.hideAllHidden();
			this.adjustHeight();
		}
	},
	
	canMoveLeft:function(){
		if((this.position-this.blockSize)<0){
			return false;
		}else{
			return true;
		}
	},
	canMoveRight:function(){
		if((this.position+this.blockSize)>=this.facets.length){
			return false;
		}else{
			return true;
		}
	},
	
	showHidden:function(id){
		//hideOthers
		this.hideAllHidden();
		$('filterQueryHiddenElems_'+id).show();
		//$('filterQueryVisibleElems_'+id).hide();
		
		$('btnFilterQueryHideHidden_'+id).show();
		$('btnFilterQueryShowHidden_'+id).hide();
		
		$('queryFilterFacet_'+id).addClassName('facet_showing_hidden');
		
	},
	
	hideHidden:function(id){
		$('filterQueryHiddenElems_'+id).hide();
		//$('filterQueryVisibleElems_'+id).show();
		$('btnFilterQueryHideHidden_'+id).hide();
		$('btnFilterQueryShowHidden_'+id).show();
		
		$('queryFilterFacet_'+id).removeClassName('facet_showing_hidden');
	},
	
	hideAllHidden:function(){
		for(i=0;i<this.hiddenFacetsCodes.length;i++){
			tmp = this.hiddenFacetsCodes[i];
			this.hideHidden(tmp);
		}
	},
	
	toggleButtons:function(){

		this.btnRight.show();
		this.btnLeft.show();
		if(!this.canMoveRight()){
			//this.btnRight.addClassName('disabled');
			this.btnRight.hide();
		}
		if(!this.canMoveLeft()){
			//this.btnLeft.addClassName('disabled');
			this.btnLeft.hide();
		}
//&& 		
		if(!this.canMoveLeft() && !this.canMoveRight() ){
			//If no navigation is shown, we hide the entire row, it takes up valuable space
			if(!($('queryFilterFacetsTopBar')==undefined)){
				$('queryFilterFacetsTopBar').hide();
			}
			
		}else{
			if(!($('queryFilterFacetsTopBar')==undefined)){
				$('queryFilterFacetsTopBar').show();
			}
		}
		
	},
	
	showOnIndex:function(start, len){
		
		for(i=0; i< this.facets.length; i++){
			this.facets[i].hide();	
		}
		for(i=start; i<start+len; i++){
			if(typeof this.facets[i] == 'undefined'){
			}else{
				this.facets[i].show();
			}
		}
	},
	
	expandContainer:function(){
		newWidth = this.facets.length * 156 + 2;
		//$('queryFilterHolder').addClassName('qFilterExpandido');
		$('queryFilterHolder2').addClassName('qFilterExpandido');
		$('queryFilterHolder').setStyle({width: String(newWidth)+"px"});
		$('queryFilterHolder2').setStyle({width: String(newWidth)+"px"});
		$('query_filter_iframe').setStyle({width: String(newWidth)+"px"});
	},
	
	collapseContainer:function(){
		newWidth = this.facets.length * 156 + 2;
		
		//$('queryFilterHolder').removeClassName('qFilterExpandido');
		$('queryFilterHolder2').removeClassName('qFilterExpandido');
		
		$('queryFilterHolder').setStyle({width:"auto"});
		$('queryFilterHolder2').setStyle({width:"485px"});
		
		$('query_filter_iframe').setStyle({width:"auto"});
	},

	showAll:function(){
		for(i=0; i< this.facets.length; i++){
			this.facets[i].show();	
		}
	},
	
	initEvents:function(){
		Event.observe(this.btnRight,'click',function(ev){
			this.moveRight();	
		}.bind(this));
		
		Event.observe(this.btnLeft,'click',function(ev){
			this.moveLeft();	
		}.bind(this));
		
				 
		arr = $$('.btnFilterQueryShowHidden');
		
		for(i=0;i<arr.length;i++){
		
			Event.observe((arr[i].id),"click",function(ev){
				bits = ev.target.id.split("_");
				this.showHidden(bits[1]);
			}.bind(this));
			
			tmp = arr[i].id.split("_");
			this.hiddenFacetsCodes.push(tmp[1]);
		}
		
		
		arr = $$('.btnFilterQueryHideHidden');
		for(i=0;i<arr.length;i++){
			Event.observe((arr[i].id),"click",function(ev){
				bits = ev.target.id.split("_");
				this.hideHidden(bits[1]);
			}.bind(this));
		}
		
		/*arr =  $$('.queryFilterFacetElement');
		for(i=0;i<arr.length;i++){
			Event.observe(arr[i],"mouseover",function(ev){
				newT = ev.target.title;
				oldT = ev.target.innerHTML;
				ev.target.update(newT);
				ev.target.title = oldT;
			});
			Event.observe(arr[i],"mouseout",function(ev){
				newT = ev.target.title;
				oldT = ev.target.innerHTML;
				ev.target.update(newT);
				ev.target.title = oldT;
			});
		}*/
		
	}
	
});

//Event.observe(window, 'load', function() {
	//myQueryFilter = new QueryFilter();
//});
