function showBlock(sel)
{
	$(sel).show();
}

function toggleBlock(sel)
{
	$(sel).toggle();
}


$(document).ready(function() {
	
	/* sustainability link on nav - launch new browser */
	$('#sustainability').attr('target','_blank');
	
	
	function openProductSubcategories(elem){
		var $el       = $(elem),
		    url       = $el.attr('data-url'),
		    subcatNum = $el.attr('data-subcat-num'),
		    callback  = arguments[1] || function(){};
		
		if( $el.hasClass('no_subcat') )
		{
			goTo(url);
		}
		else
		{
			$el.addClass('open');
			$el.css('background-position','bottom');
			$el.next().slideToggle("swing", callback);
			$('.acc_content   li:last-child').css('border-bottom','#86badc solid 1px');
			$('.acc_content   li:first-child').addClass('first');
			return false;
		}
	};
	
	
	function closeProductSubcategories(elem){
		var $el       = $(elem),
		    callback  = arguments[1] || function(){};
		if( ! $el.hasClass('no_subcat') )
		{
			$el.removeClass('open');
			$el.css('background-position','top');
			$el.next().slideToggle("swing", callback);
			$('.acc_content   li:first-child').addClass('first');
			return false;
		}
	};
	
	
	
	$('a[data-link-code*=]').live('click',function(ev){
		
		ev.preventDefault();
		
		var expandCollapseTitles = function(codes) {
			codes = codes.split(',')
			$('.accordion .acc_title').each(function(ev){
				var $el = $(this),
				    hasSubcat     = ! $el.hasClass('no_subcat'),
				    isOpen        = $el.hasClass('open'),
				    catId         = $el.attr('data-cat-id'),
				    needToExpand  = ( $.inArray(catId, codes) != -1 );
				
				if( hasSubcat )
				{
					if( ! isOpen && needToExpand )
					{
						openProductSubcategories($el, function(){
							goTo('Products.html#browse');
						});
					}
					else if( isOpen && needToExpand )
					{
						goTo('Products.html#browse');
					}
					else if( isOpen && ! needToExpand )
					{
						closeProductSubcategories($el, function(){
							
						});
					}
				}
			});
		};
		
		var removeHighlights = function() {
			$('*[data-cat-id*=]').removeClass('highlight');
		};
		
		var highlightByCodes = function(codes)
		{
			codes = codes.split(',');
			for (var i=0; i < codes.length; i++) {
				var code = codes[i];
				$('*[data-cat-id='+code+']').addClass('highlight');
			};
		};
		
		removeHighlights();
		expandCollapseTitles($(this).attr('data-link-code'));
		highlightByCodes($(this).attr('data-link-code'));
	});
	
	
	/* --- ACCORDION ---*/
	$('.accordion .acc_title').toggle(function() {
		openProductSubcategories($(this));
	}, function() {
		closeProductSubcategories($(this));
	});
	
	/*
	 * PAGINATION
	 */
	$('.dynamic-paginator').each(function(){
		var $el        = $(this),
		    $paginator = $el.find('.paginator')
		    pagesCount = parseInt( $paginator.attr('pagesCount') ),
		    currPage   = $paginator.attr('currPage'),
		    baseHref   = $paginator.attr('baseHref'),
		    lastIndex  = (pagesCount - 1) * 10,
		    firstInx   = 0;

		for (i=0; i<pagesCount; i++) 
		{
			$paginator.find('.pattern').clone()
			.removeClass('pattern')
			.css('display', 'inline')
			.attr({'startNum': i * 10})
			.appendTo( $paginator )
			.find('a').text(i+1).attr('href', baseHref + '&start=' + (i*10));
		}

		$paginator.find('li a').each(function() {
			if ($(this).text() == currPage) 
			{
				$(this).css('font-weight', 'bold');
			}
		});

		$paginator.find('li a').click(function() {
			if ($(this).text() == currPage) return false;
		});
		
		$el.find('.pagination_first').each(function(){
			$(this).attr('href', baseHref + '&start=0' );
		});
		
		$el.find('.pagination_last').each(function(){
			$(this).attr('href', baseHref + '&start=' + ( (pagesCount - 1) * 10) );
		});
		
		$el.find('.pagination_prev').each(function(){
			var prevIndex = currPage - 1,
			    index     = 0;
			if( prevIndex > 0 )
			{
				index = (prevIndex - 1) * 10;
			}
			$(this).attr('href', baseHref + '&start=' + index );
		});
		
		$el.find('.pagination_next').each(function(){
			var nextIndex = currPage,
			    index     = (pagesCount - 1) * 10;
			if( nextIndex < pagesCount )
			{
				index = nextIndex * 10;
			}
			$(this).attr('href', baseHref + '&start=' + index );
		});
		
	});

	
});



