if(!String.prototype.trim)
{
	String.prototype.trim = function()
	{
		return this.replace(/^\s+|\s+$/g, '');
	};
}

function hasInSpaceSeparated(key, val)
{
	val = ' ' + val + ' ';
	return (val.indexOf(key) > -1);
}

function forEach(object, callback)
{
	var i, l = object.length, x;
	if(l === undefined)
	{
		for(x in object)
		{
			if(callback.call(object[x], x, object[x]) === false)
			{
				break;
			}
		}
	}
	else
	{
		for(i = 0, x = object[0]; i < l && callback.call(x, i, x) !== false; x = object[++i])
		{
		}
	}
} 

function eraseAll()
{
  var x = document.getElementById('iStoreFilters');
  forEach(x.getElementsByTagName('select'), function()
	{
      var y = this.id;
      var val = document.getElementById('js-val-' + y);
      forEach(document.getElementById(y).getElementsByTagName('option'), function()
  		{
  			this.selected = false;
  		});
      
      forEach(document.getElementById('js-tip-' + y).getElementsByTagName('input'), function()
		  {
        this.checked = false;
		  })
      
      val.title = '';
		  val.innerHTML = '- wybierz -';
		  val.style.color = '';
	});
   document.getElementById('form-body-filters-price-min').value = '- od -';
   document.getElementById('form-body-filters-price-max').value = '- do -';
   document.getElementById('new').checked = false
   document.getElementById('promotion').checked = false
  	
}
 
var ceneoFilters = {
	init: function()
	{
    var x = document.getElementById('iStoreFilters');
		if(x)
		{
			forEach(x.getElementsByTagName('input'), function()
			{
				if(this.type == 'text')
				{
					if(hasInSpaceSeparated('number', this.className))
					{
						this.onfocus = function()
						{
							ceneoFilters.toogle('none');
							if((hasInSpaceSeparated('half-1', this.className) && (this.value == '- od -')) || (hasInSpaceSeparated('half-2', this.className) && (this.value == '- do -')))
							{
								this.value = '';
							}
							this.className = this.className + ' text-active';
						};
						this.onblur = function()
						{
							if(this.value === '')
							{
								this.value = hasInSpaceSeparated('half-1', this.className) ? '- od -' : '- do -';
								this.className = this.className.replace(' text-choosen', '').replace(' text-error', '');
							}
							else
							{
								this.className = /^[\d]*((,|\.)[\d]+)?$/.test(this.value) ? this.className.replace(' text-error', '') + ' text-choosen' : this.className.replace(' text-choosen', '') + ' text-error';
							}
							this.className = this.className.replace(' text-active', '');
						};
					}
					else if(hasInSpaceSeparated('string', this.className))
					{
						this.onfocus = function()
						{
							ceneoFilters.toogle('none');
							if(this.value == '- wpisz -')
							{
								this.value = '';
							}
							//this.style.color = '#000';
							//this.style.textAlign = 'left';
						};
						this.onblur = function()
						{
							if(this.value === '')
							{
								this.value = '- wpisz -';
								//this.style.color = '';
								//this.style.textAlign = '';
							}
							else
							{
								//this.style.color = /^[a-zA-Z0-9_ ]*$/.test(this.value) ? '#000' : 'red';
							}
						};
					}
					this.className = this.className + ' text-js';
				}
			});
			forEach(x.getElementsByTagName('div'), function()
			{
				if(hasInSpaceSeparated('filter', this.className))
				{
					forEach(this.getElementsByTagName('a'), function()
					{
						if(this.parentNode && this.parentNode.tagName.toLowerCase() == 'label')
						{
							this.onclick = function()
							{
								this.blur();
								this.parentNode.parentNode.getElementsByTagName('input')[0].checked = true;
								this.parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('button')[0].onclick();
								return false;
							};
						}
					});
				}
			});
			forEach(x.getElementsByTagName('a'), function()
			{
				if(this.parentNode && this.parentNode.tagName.toLowerCase() == 'p')
				{
					this.style.display = 'block';
				}
			});
			forEach(x.getElementsByTagName('select'), function()
			{
				if(hasInSpaceSeparated('multiple', this.className))
				{
					this.multiple = true;
				}
				this.style.display = 'none';
				this.parentNode.getElementsByTagName('label')[0].onclick = function()
				{
					ceneoFilters.toogle(this.parentNode.getElementsByTagName('select')[0].id);
					return false;
				};
			});
		}
		if(document.getElementById('filters-choosen'))
		{
			ceneoBoxes.hide('filters');
			forEach(document.getElementById('close-filters').getElementsByTagName('a'), function()
			{
				this.onclick = function()
				{
					ceneoBoxes.toogle(this.href.split('#box-')[1]);
					document.getElementById('filters-choosen').style.display = 'none';
					this.blur();
					return false;
				};
			});
		}
	},
	onKeyCheck: function(e)
	{
		e = e || window.event;
		if(e.keyCode == 27)
		{
			ceneoFilters.toogle('none');
		}
	},
	toogle: function(x)
	{
		document.onkeydown = null;
		forEach(document.getElementById('iStoreFilters').getElementsByTagName('div'), function()
		{
			if(hasInSpaceSeparated('filterTip', this.className))
			{
				var id = this.id.replace(/js\-tip\-/, ''), y = document.getElementById(this.id), z = 0;
				if(id == x && y.style.display != 'block')
				{
					
					forEach(document.getElementById(id).getElementsByTagName('option'), function()
					{
						if(this.id)
						{
							//document.getElementById('js-' + this.id).checked = this.selected;  
						}
					});
					z = 1;
					document.onkeydown = ceneoFilters.onKeyCheck;
				}
				document.getElementById('js-val-' + id).className = z ? 'filter-active' : (document.getElementById('js-val-' + id).innerHTML == '- wybierz -' ? 'filter' : 'filter-choosen');
				y.style.display = z ? 'block' : 'none';
				//alert ( 'klikamy w: '+ this.id+ ' szukamy: '+$(this).getParent() );
				if (z) {
					//alert('dodaje hover');
					$(this).getParent().addClass('hover');
				} else {
					$(this).getParent().removeClass('hover');
					//alert('zdejmuje hover');
				};
			}
		});
		return false;
	},
	apply: function(x)
	{
		var txt = [], val = '', y = document.getElementById('js-val-' + x);
		forEach(document.getElementById(x).getElementsByTagName('option'), function()
		{
			//this.selected = false;
		});
		forEach(document.getElementById('js-tip-' + x).getElementsByTagName('input'), function()
		{
			if(this.id && this.checked)
			{
				var z = document.getElementById(this.id.replace(/js\-/, ''));
				txt.push(z.text.trim());
				z.selected = true;
			}
		});
		if(txt.length === 0)
		{
			y.title = '';
			y.innerHTML = '- wybierz -';
			y.style.color = '';
		}
		else
		{
			forEach(txt, function()
			{
				if(val.length < 6)
				{
					if(val.length !== 0)
					{
						val += ', ';
					}
					val += this;
				}
			});
			if(val.length > 10)
			{
				val = val.substring(0, 8).trim() + '...';
			}
			else if(val.length != txt.join(', ').length)
			{
				if(val.length > 8)
				{
					val = val.substring(0, 8).trim();
				}
				val += '...';
			}
			y.title = txt.join(', ');
			y.innerHTML = val;
			//y.style.color = '#000';
		}
		return ceneoFilters.toogle(x);
	},
	erase: function(x)
	{
    var y = document.getElementById('js-val-' + x);
    forEach(document.getElementById(x).getElementsByTagName('option'), function()
		{
			this.selected = false;
		});
		
    forEach(document.getElementById('js-tip-' + x).getElementsByTagName('input'), function()
		{
      this.checked = false;
		});
		
		y.title = '';
		y.innerHTML = '- wybierz -';
		y.style.color = '';
		return ceneoFilters.toogle(x);
	}
};

ceneoFilters.init();
