// checking if IE: this variable will be understood by IE: isIE = !false
isIE = /*@cc_on!@*/false;

Control.Slider.prototype.setDisabled = function()
{
	this.disabled = true;
	
	if (!isIE)
	{
		this.track.parentNode.className = this.track.parentNode.className + ' disabled';
	}
};

function layered_navigation_hide_products()
{
	var items = $('narrow-by-list').select('a', 'input');
	n = items.length;
	for (i=0; i<n; ++i){
		items[i].addClassName('layered-navigation-disabled');
	}
	
	if (typeof(layerednavigation_slider) != 'undefined')
		layerednavigation_slider.setDisabled();
	
	var divs = $$('div.layered-navigation-progress');
	for (var i=0; i<divs.length; ++i)
		divs[i].show();
}

function layered_navigation_show_products(transport)
{
	var resp = {} ;
	if (transport && transport.responseText){
		try {
			resp = eval('(' + transport.responseText + ')');
		}
		catch (e) {
			resp = {};
		}
	}
	
	if (resp.products){
		var el = $('layered-navigation-container');
		var ajaxUrl = $('layered-navigation-ajax').value;
		
		el.update(resp.products.gsub(ajaxUrl, $('layered-navigation-url').value));
		layered_navigation_toolbar_init(); // reinit listeners

		$('layered-navigation-navigation').update(resp.layer.gsub(ajaxUrl, $('layered-navigation-url').value));
		
		$('layered-navigation-ajax').value = ajaxUrl;  
	}
	
	var items = $('narrow-by-list').select('a','input');
	n = items.length;
	for (i=0; i<n; ++i){
		items[i].removeClassName('layered-navigation-disabled');
	}
	if (typeof(layerednavigation_slider) != 'undefined')
		layerednavigation_slider.setEnabled();
}

function layered_navigation_add_params(k, v, isSingleVal)
{
	var el = $('layered-navigation-params');
	var params = el.value.parseQuery();
	
	var strVal = params[k];
	if (typeof strVal == 'undefined' || !strVal.length){
		params[k] = v;
	}
	else if('clear' == v ){
		params[k] = 'clear';
	}
	else {
		if (k == 'price')
			var values = strVal.split(',');
		else
			var values = strVal.split('-');
		
//		var values = strVal.split('-');
		if (-1 == values.indexOf(v)){
			if (isSingleVal)
				values = [v];
			else 
				values.push(v);
		} 
		else {
			values = values.without(v);
		}
				
		params[k] = values.join('-');
	} 
	
   el.value = Object.toQueryString(params).gsub('%2B', '+');
}

function layered_navigation_make_request()
{
	layered_navigation_hide_products();

	var params = $('layered-navigation-params').value.parseQuery();	
	
	if (!params['order']) // Respect Sort By settings!
	{
		select = null;
		$$('select').each(function(el) {
			if (el.onchange)
			{
				if (el.onchange.toString().match(/layered_navigation_toolbar_make_request/))
				{
					select = el; 
				} // if (el.onchange.toString().match(/layered_navigation_toolbar_make_request/))
			} // if (el.onchange)
		});
		
		if (select)
		{
			var selectParams = select.value.parseQuery();
			
			if (selectParams && selectParams['order'])
			{
				params['order'] = selectParams['order'];
			} // if (selectParams && selectParams['order']) 
		}	
	}
	
	if (!params['dir'])
	{
		params['dir'] = 'desc';
	}
	
	$('layered-navigation-params').value = Object.toQueryString(params);
	
	// tmp aitoc	
	new Ajax.Request($('layered-navigation-ajax').value + '?' + $('layered-navigation-params').value, 
		{method: 'get', onSuccess: layered_navigation_show_products}
	);
}


function layerednavigation_update_links(evt, className, isSingleVal)
{
	var link = Event.findElement(evt, 'A'),
		sel = className + '-selected';
	
	if (link.hasClassName(sel))
		link.removeClassName(sel);	
	else
		link.addClassName(sel);
	
	//only one  price-range can be selected
	if (isSingleVal){
		var items = $('narrow-by-list').getElementsByClassName(className);
		var i, n = items.length;
		for (i=0; i<n; ++i){
			if (items[i].hasClassName(sel) && items[i].id != link.id)
				items[i].removeClassName(sel);
		}
	}

	layered_navigation_add_params(link.id.split('-')[0], link.id.split('-')[1], isSingleVal);
	
	layered_navigation_make_request();
	
	Event.stop(evt);
}


function layered_navigation_attribute_listener(evt)
{
	layered_navigation_add_params('p', 'clear', 1);
	layerednavigation_update_links(evt, 'layered-navigation-attribute', 0);
}

function layered_navigation_icon_listener(evt)
{
	layered_navigation_add_params('p', 'clear', 1);
	layerednavigation_update_links(evt, 'layered-navigation-icon', 0);
}

function layered_navigation_price_listener(evt)
{
	layered_navigation_add_params('p', 'clear', 1);
	layerednavigation_update_links(evt, 'layered-navigation-price', 1);
}

function layered_navigation_clear_listener(evt)
{
	var link = Event.findElement(evt, 'A'),
		varName = link.id.split('-')[0];
	
	layered_navigation_add_params('p', 'clear', 1);
	layered_navigation_add_params(varName, 'clear', 1);
	
	if ('price' == varName){
		var from =  $('layered-navigation-price-from'),
			to   = $('layered-navigation-price-to');
		
		if (Object.isElement(from)){
			from.value = from.name;
			to.value   = to.name;
		}
	}
	
	layered_navigation_make_request();	
	
	Event.stop(evt);  
}


function layered_navigation_round(num){
	num = parseFloat(num);
	if (isNaN(num))
		num = 0;
		
	return Math.round(num);
}

function layered_navigation_price_input_listener(evt){
	if (evt.type == 'keypress' && 13 != evt.keyCode)
		return;
		
	if (evt.type == 'keypress')
	{
		var inpObj = Event.findElement(evt, 'INPUT');
	}
	else 
	{
		var inpObj = Event.findElement(evt, 'BUTTON');
	}

	var sKey = inpObj.id.split('---')[1];

	var numFrom = layered_navigation_round($('layered-navigation-price-from---' + sKey).value),
		numTo   = layered_navigation_round($('layered-navigation-price-to---' + sKey).value);
 
	if ((numFrom<0.01 && numTo<0.01) || numFrom<0 || numTo<0)   
		return;

	layered_navigation_add_params('p', 'clear', 1);
//	layered_navigation_add_params('price', numFrom + ',' + numTo, true);
	layered_navigation_add_params(sKey, numFrom + ',' + numTo, true);
	layered_navigation_make_request(); 		
}

function layered_navigation_category_listener(evt){
	var link = Event.findElement(evt, 'A');
	var catId = link.id.split('-')[1];
	
	var reg = /cat-/;
	if (reg.test(link.id)){ //is search
		layered_navigation_add_params('cat', catId, 1);
		layered_navigation_add_params('p', 'clear', 1);
		layered_navigation_make_request(); 
		Event.stop(evt);  
	}
	//do not stop event
}

function layered_navigation_toolbar_listener(evt){
	layered_navigation_toolbar_make_request(Event.findElement(evt, 'A').href);
	Event.stop(evt); 
}



function layered_navigation_toolbar_init()
{
//	var items = $('layered-navigation-container').select('.pages a', '.view-by a');
	var items = $('layered-navigation-container').select('.pages a', '.view-mode a', '.sort-by a');
	var i, n = items.length;
	for (i=0; i<n; ++i){
		Event.observe(items[i], 'click', layered_navigation_toolbar_listener);
	}
}

function layered_navigation_dt_listener(evt){
	var e = Event.findElement(evt, 'DT');
	e.nextSiblings()[0].toggle();
	e.toggleClassName('layered-navigation-dt-selected');
}

function layered_navigation_clearall_listener(evt)
{
	var params = $('layered-navigation-params').value.parseQuery();
	$('layered-navigation-params').value = 'lnclear=true';
	if (params['q'])
	{
		$('layered-navigation-params').value += '&q=' + params['q'];
	}
	layered_navigation_make_request();
	Event.stop(evt); 
}

function layered_navigation_init()
{
	var items, i, j, n, 
		classes = ['category', 'attribute', 'icon', 'price', 'clear', 'dt', 'clearall'];
	
	for (j=0; j<classes.length; ++j){
		items = $('narrow-by-list').select('.layered-navigation-' + classes[j]);
		n = items.length;
		for (i=0; i<n; ++i){
			Event.observe(items[i], 'click', eval('layered_navigation_' + classes[j] + '_listener'));
		}
	}

// start new fix code
	items = $('narrow-by-list').select('.layered-navigation-price-input-id');
	
	n = items.length;
	
	var btn = $('layered-navigation-price-go');
	
	for (i=0; i<n; ++i)
	{
		btn = $('layered-navigation-price-go---' + items[i].value);
		if (Object.isElement(btn)){
			Event.observe(btn, 'click', layered_navigation_price_input_listener);
			Event.observe($('layered-navigation-price-from---' + items[i].value), 'keypress', layered_navigation_price_input_listener);
			Event.observe($('layered-navigation-price-to---' + items[i].value), 'keypress', layered_navigation_price_input_listener);
		}
	}
// finish new fix code	
}

function layered_navigation_create_slider(width, from, to, min_price, max_price, sKey) 
{
	var price_slider = $('layered-navigation-price-slider' + sKey);

	return new Control.Slider(price_slider.select('.handle'), price_slider, {
		range: $R(0, width),
		sliderValue: [from, to],
		restricted: true,
		
		onChange: function (values){
		var f = layered_navigation_calculate(width, from, to, min_price, max_price, values[0]),
			t = layered_navigation_calculate(width, from, to, min_price, max_price, values[1]);

		layered_navigation_add_params(sKey, f + ',' + t, true);
		
		// we can change values without sliding  
		$('layered-navigation-range-from' + sKey).update(f); 
		$('layered-navigation-range-to' + sKey).update(t);
			
		layered_navigation_make_request(); 
	},
	onSlide: function(values) { 
		$('layered-navigation-range-from' + sKey).update(layered_navigation_calculate(width, from, to, min_price, max_price, values[0]));
		$('layered-navigation-range-to' + sKey).update(layered_navigation_calculate(width, from, to, min_price, max_price, values[1]));
	}
	});
}

function layered_navigation_calculate(width, from, to, min_price, max_price, value)
{
	var calculated = layered_navigation_round(((max_price-min_price)*value/width) + min_price);
	
	return calculated;
}

