var productListFilter;
Event.observe(window, 'load', function() 
	{
		if ($('productListFilter'))
		{
			productListFilter = new ProductListFilter();
			productListFilter.init();
		}
		
	}
);
var ProductListFilter = Class.create (
{
	moving:false,
	visible:false,
	
	init:function()
	{
		
		this.getySelectList = [];
		$$('div.filterSelect').each(function(element)
		{
			getySelect = new GetySelect();
			getySelect.id = element.identify();
			getySelect.className = element.className
			getySelect.init();
			getySelect.refresh();
			this.getySelectList.push(getySelect); 
		}.bind(this));
		
		$('productListFilter').select('input[type=text]').each(function(element)
		{
			Event.observe(element, 'blur', this.checkTextField.bind(this));	
			this.checkTextField(element);
		}.bind(this));
		
		$('productSpecialFlags').select('input[type=checkbox]').each (function(element)
	    {
			Event.observe(element, 'click', this.checkSpecialTag.bind(this));
			this.checkSpecialTag(element);
		}.bind(this));
		
		if ($('singleSelectList'))
		{
			$('singleSelectList').select('select').each (function(element)
			{
				Event.observe(element,'change', this.checkSingleSelect.bind(this));
				this.checkSingleSelect(element);
			}.bind(this));
		}
		 if ($('filterStatus').innerHTML != '')
		{
		 	$('productListFilter').addClassName('active');
		 }
		
		
		Event.observe($('productListFilterHandle'),'click', this.toogle.bind(this));
		
		

	},
	toogle:function()
	{
		if (!this.moving && !this.visible)
		{
			this.show();
		}
		if (!this.moving && this.visible)
		{
			this.hide();
		}
	
	},
	hide:function()
	{
		this.moving = true;;
		Effect.SlideUp('productListFilterContent',{ duration: 0.3, afterFinish: function() {
					
					  this.visible = false;
					  this.moving = false;
					  if ($('filterStatus').innerHTML != '')
					  {
					  	$('productListFilter').addClassName('active');
					  }
					  $('showFilter').show();
					  $('hideFilter').hide();
					  }.bind(this)});
	},
	show:function()
	{
	
		if ($('productListFilter').hasClassName('active'))
		{
				$('productListFilter').removeClassName('active');
		}
		
		this.moving = true;;
		Effect.SlideDown('productListFilterContent',{ duration: 0.3, afterFinish: function() {
				   $('showFilter').hide();
				   $('hideFilter').show();
				  this.visible = true;
				  this.moving = false;
				  }.bind(this)});
	},
	clear:function()
	{
		 $('productListFilter').select('input[type=checkbox]').each (function(element)
		{
				if (element.checked)
				{
					element.click();
				}
		});
		
		 $('productListFilter').select('select').each (function(element)
		{
				element.selectedIndex = 0;
				this.checkSingleSelect(element);
				
		}.bind(this));
		 
		 
		 $('productListFilter').select('input[type=text]').each (function(element)
		{
				element.value = '';
				this.checkTextField(element);
			
		}.bind(this)); 
	},
	
	refereshFilterStatus:function()
	{
		var filterStatus = '';
		filterStatus += this.getSpecialTagStatus();
		var manufacturerStatus =this.getManufacturerStatus();		
		if (manufacturerStatus.length > 0)
		{
			filterStatus += (filterStatus.length > 0) ? ' (' + manufacturerStatus + ')' : manufacturerStatus;
		}
		filterStatus += (filterStatus.length > 0 ) ? ' ' + this.getPriceStatus() : this.getPriceStatus();
		var customFilterStatus = this.getCustomFilterStatus();
		if (customFilterStatus.length > 0)
		{
			filterStatus += (filterStatus.length > 0 ) ? ', ' + customFilterStatus : customFilterStatus;
		}
		if (filterStatus != '')
		{
			
			$('filterStatusEmpty').hide();
		}
		else
		{
			$('filterStatusEmpty').show();
		}
		$('filterStatus').update(filterStatus);
	},
		
	checkSingleSelect:function(obj)
	{
		var checkbox;
		if (obj.element)
		{ 
		  element = obj.element();
		}
		else
		{
			element = obj;
		}	
		
		if (element.selectedIndex != 0)
		{
			if (!element.hasClassName('selected'))
			{
				element.addClassName('selected');
			}
		}
		else
		{
			if (element.hasClassName('selected'))
			{
				element.removeClassName('selected');
			}
		}
		this.refereshFilterStatus();
	},
	
	checkTextField:function(obj)
	{
		var element;
		if (obj.element)
		{ 
		  element = obj.element();
		}
		else
		{
			element = obj;
		}		
		if (element.value != '') {
			if (!element.hasClassName('selected'))
				{
					element.addClassName('selected');
				}
		}
		else
		{
			if (element.hasClassName('selected'))
				{
					element.removeClassName('selected');
				}
		}
		this.refereshFilterStatus();
	},
	
	checkSpecialTag:function(obj)
	{
		var checkbox;
		if (obj.element)
		{ 
		  checkbox = obj.element();
		}
		else
		{
			checkbox = obj;
		}	
		var label = $$('label[for='+checkbox.identify()+']').first();
		if (checkbox.checked)
		{
			if (!label.hasClassName('selected'))
			{
				label.addClassName('selected');
			}
		}
		else
		{
			if (label.hasClassName('selected'))
			{
				label.removeClassName('selected');
			}
		}
		this.refereshFilterStatus();
	},
	
	getSpecialTagStatus:function()
	{
		var status = '';
		$('productSpecialFlags').select('input[type=checkbox]').each (function(element)
	    {
			if (element.checked)
			{
				var label = $$('label[for='+element.identify()+']').first();				
				status += (status.length > 0) ? ', ' : '';
				status += label.innerHTML;
			}
		}.bind(this));
		return status;
	
	},
	getManufacturerStatus:function()
	{
		var status = '';
		if (!$('manufacturer')) return status;
		$('manufacturer').select('input[type=checkbox]').each (function(element)
	    {
			if (element.checked)
			{
				var label = $$('label[for='+element.identify()+']').first();				
				status += (status.length > 0) ? ', ' : '';
				status += label.innerHTML;	
			}
		}.bind(this));
		return status;
	},
	getPriceStatus:function()
	{
		var status = '';
		var priceFrom = $('priceFromInput').value;
		var priceTo = $('priceToInput').value;
		status += (priceFrom.length > 0) ? 'od ' + priceFrom + ' &euro;' : '';
		status += (status.length > 0) ? ' ' : '';
		status += (priceTo.length > 0) ? 'do ' + priceTo + ' &euro;' : '';
		return status;
	},
	getCustomFilterStatus:function()
	{
	  var status = '';
	  $('customFilterSection').select('input[type=checkbox]').each (function(element)
																			 {
			if (element.checked)
			{
				var label = $$('label[for='+element.identify()+']').first();				
				status += (status.length > 0) ? ', ' : '';
				status += label.innerHTML;	
			}
		}.bind(this));
	  $('customFilterSection').select('select').each (function(element)
																			 {
			if (element.selectedIndex != 0)
			{
						
				var chosenOption = element.options[element.selectedIndex];
				status += (status.length > 0) ? ', ' : '';
				status += chosenOption.text;	
			}
		}.bind(this));
	  
	  
		return status;
	}
	

});

var GetySelect = Class.create (
{
	moving:false,
	visible:false,
	identify:function()
	{
		return this.id;
	},
	init:function()
	{
		Event.observe($(this.id).select('.selectedTitle').first(), 'click', this.toogleOptions.bind(this));
		//attaching event for checkbox
		$(this.id).select('input[type=checkbox]').each (function(checkbox)
		{
			Event.observe(checkbox, 'click', this.refresh.bind(this));		
		}.bind(this));
		
	},
	
	refresh:function()
	{
		
		var selectedText = '';
		
		$$('input[class="'+this.id+'"]').each(function(element)
		{
			var cbLabel = $$('label[for="' + element.id +'"]').first();
			if (element.checked) 
			{
				selectedText += (selectedText.length > 0) ? ', ' : ' ';
				selectedText += cbLabel.innerHTML;
				
				if (!cbLabel.hasClassName('selected'))
				{
					cbLabel.addClassName('selected');
				}
				
			}
			else
			{
				if (cbLabel.hasClassName('selected'))
				{
					cbLabel.removeClassName('selected');
				}
			}
		});
	

		if (selectedText.length == 0) 
		{
			
			$(this.id+'None').show();
			$(this.id+'Selected').innerHTML = '';
			if ($(this.id).hasClassName('active'))
			{
				$(this.id).removeClassName('active');
			}
			
		}
		else
		{
			
			$(this.id+'None').hide();
			$(this.id+'Selected').innerHTML = selectedText;
			if (!$(this.id).hasClassName('active'))
			{
					$(this.id).addClassName('active');
			}
		}		productListFilter.refereshFilterStatus();
	},
	
	showOptions:function()
	{
		this.visible = true;
		this.moving = true;
		Effect.SlideDown(this.id + 'Options',{ duration: 0.2, afterFinish: function() {
				  this.moving = false;
				  }.bind(this)});
	
	},
	hideOption:function()
	{
		this.moving = true;
		Effect.SlideUp(this.id + 'Options',{ duration: 0.2, afterFinish: function() {
					
					  this.visible = false;
					  this.moving = false;
					  }.bind(this)});
	},
	toogleOptions:function()
	{
		if (!this.moving && !this.visible)
		{
			this.showOptions();
		}
		if (!this.moving && this.visible)
		{
			this.hideOption();
		}
	}
	

});