$categories = jQuery.noConflict();
$categories(document).ready(function() {

var categories_productColor,
	categories_productType,
	categories_productMaterial,
	categories_ajaxRequestURL,
	categories_storeURL = 'http://'+window.location.host,
	categories_canAnimate = true,
	categories_showDropDown = true;

$categories(document).ready(function(){
	$categories('<div id="block-layered-nav-mask"></div>').appendTo('#layer-wrapper .block-layered-nav');
	$categories('#narrow-by-list .filter-item ul li').addClass('unactive');
	$categories('#narrow-by-list .filter-item').addClass('selectable');
	//$categories('#narrow-by-list .filter-item ul').hide();
	$categories('#narrow-by-list .filter-item ul li a').click(function(e) {
		e.preventDefault();
	})
	
	$categories('#narrow-by-list .filter-item.selectable')
		.live('hover', function() {
			if(categories_showDropDown) {
				$categories(this).find('ul').stop(true, true).slideToggle(300);
			}
		})
		
		
	//activate category
	$categories('#narrow-by-list .filter-item.selectable ul .unactive').live('click', function() {
	
		if(categories_canAnimate) {
			categories_canAnimate = false;
			categories_showDropDown = false;
		
			var categories_clicked = $categories(this),
				categories_currentCol = categories_clicked.parent(),
				categories_currentColWrapper = categories_currentCol.parent(),
				categories_clickedCategoryName = categories_clicked.find('a').html(),
				categories_clickedURL = categories_clicked.find('a').attr('href').split('?')[1],
				categories_clickedURLType = categories_clickedURL.split('=')[0]
				categories_ajaxRequestURL = categories_storeURL+'?';
				
			if(categories_clickedURLType == 'tag_product_type') {
				categories_productType = categories_clickedURL;
			} else if(categories_clickedURLType == 'tag_product_material') {
				categories_productMaterial = categories_clickedURL;
			} else if(categories_clickedURLType == 'tag_product_color') {
				categories_productColor = categories_clickedURL;
			}
			
			
			//creates ajax url
			if(categories_productColor){
				categories_ajaxRequestURL += categories_productColor;
				if(categories_productType) {
					categories_ajaxRequestURL += '&'+categories_productType;
					if(categories_productMaterial) {
					categories_ajaxRequestURL += '&'+categories_productMaterial;
					}
				} else if(categories_productMaterial) {
					categories_ajaxRequestURL += '&'+categories_productMaterial;
				}
			} else if(categories_productType) {
				categories_ajaxRequestURL += categories_productType;
				if(categories_productMaterial) {
				categories_ajaxRequestURL += '&'+categories_productMaterial;
				}
			} else if(categories_productMaterial) {
				categories_ajaxRequestURL += '&'+categories_productMaterial;
			} else {
				categories_ajaxRequestURL = categories_storeURL
			}
			
			$categories('#block-layered-nav-mask').css('display', 'block');
			categories_currentCol.find('.active').removeClass('active').addClass('unactive');
			categories_currentCol.slideUp(300, function(){
				$categories('<h2 class="selected"></h2>')
					.html(categories_clickedCategoryName)
					.appendTo(categories_currentColWrapper)
					.hide()
					.fadeIn(300);
				$categories('#ajaxLoading').fadeIn(300);
			});
			categories_currentCol.parent().removeClass('selectable');
			categories_clicked.removeClass('unactive').addClass('active');
			$categories('#productsList').slideUp(600, function() {
				$categories(this).remove();
			})
			$categories('#productsListWrapper').load(categories_ajaxRequestURL+' #productsList', function() {
				$categories('#productsList').hide()
				$categories('#ajaxLoading').fadeOut(300, function() {
					$categories('#productsList').slideDown(600, function() {
						tooltipFunctions();
						categories_canAnimate = true;
						categories_showDropDown = true;
						$categories('#block-layered-nav-mask').css('display', 'none');
					});
				});
				slides_slideIsOpen = false;
			});
			
		}	
		
		
	})
	
	//deactivate category
	$categories('#narrow-by-list .filter-item .selected').live('click', function() {
	
		if(categories_canAnimate) {
			categories_canAnimate = false;
			categories_showDropDown = false;
			
			var categories_clicked = $categories(this).parent().find('ul .active'),
				categories_currentCol = $categories(this).parent().find('ul'),
				categories_clickedURL = categories_clicked.find('a').attr('href').split('?')[1],
				categories_clickedURLType = categories_clickedURL.split('=')[0]
				categories_ajaxRequestURL = categories_storeURL+'?';
				
			if(categories_clickedURLType == 'tag_product_type') {
				categories_productType = '';
			} else if(categories_clickedURLType == 'tag_product_material') {
				categories_productMaterial = '';
			} else if(categories_clickedURLType == 'tag_product_color') {
				categories_productColor = '';
			}
			
			if(categories_productColor){
				categories_ajaxRequestURL += categories_productColor;
				if(categories_productType) {
					categories_ajaxRequestURL += '&'+categories_productType;
					if(categories_productMaterial) {
					categories_ajaxRequestURL += '&'+categories_productMaterial;
					}
				} else if(categories_productMaterial) {
					categories_ajaxRequestURL += '&'+categories_productMaterial;
				}
			} else if(categories_productType) {
				categories_ajaxRequestURL += categories_productType;
				if(categories_productMaterial) {
				categories_ajaxRequestURL += '&'+categories_productMaterial;
				}
			} else if(categories_productMaterial) {
				categories_ajaxRequestURL += '&'+categories_productMaterial;
			} else {
				categories_ajaxRequestURL = categories_storeURL
			}
			
			$categories('#block-layered-nav-mask').css('display', 'block');
			categories_currentCol.parent().find('h2.selected').fadeOut(300, function() {
				$categories(this).remove();
				categories_currentCol.parent().addClass('selectable');
			})
			categories_clicked.removeClass('active').addClass('unactive');
			$categories('#productsList').slideUp(600, function() {
				$categories(this).remove();
				$categories('#ajaxLoading').fadeIn(300)
			})
			$categories('#productsListWrapper').load(categories_ajaxRequestURL+' #productsList', function() {
				$categories('#productsList').hide()
				$categories('#ajaxLoading').fadeOut(300, function() {
					$categories('#productsList').slideDown(600, function() {
						tooltipFunctions();
						categories_canAnimate = true;
						categories_showDropDown = true;
						$categories('#block-layered-nav-mask').css('display', 'none');
					});			
				})
				slides_slideIsOpen = false;
			});
		}
	})
})
});

