$(document).ready(function(){
	toggleKWLProductNames();
	initProductItems();
	initTabMenu();
	initCheckboxMenu();
});

function filterByCategory( rel ){
	$('.product-matrix-item a').each( function(){		
		var cat = ( $(this ).attr('rel') ).split(',');
		if( jQuery.inArray( rel, cat ) != -1  || rel == "" ){
			$(this).parent().css('display','block');
		} else {
			$(this).parent().css('display','none');
		}
	});
}

function updateTabMenu( activeItem ){
	$('#tab tr td a').each( function(){
		if( $(this).attr('rel') == activeItem.attr('rel') ){
			$(this).parent().addClass('active');
		} else {
			$(this).parent().removeClass('active');
		}
	});
}

function toggleAttributeFilter( rel ){
	resetCheckBoxes();
	if( rel == '' ) {
		$('#product-filter form').show();
	} else {
		$('#product-filter form').hide();
	}
}

function toggleKWLProductNames(){	 
	if( $.cookie( 'showKWLProductNames' ) == 'true' ) {
		$('.product-matrix-item h1 span.kwl-name').show();
		$('.product-matrix-item h1 span.kwl-name').css('display', 'block');
		$('.product-matrix-item h1 span.new-name').hide();
		$('#kwl-product-names input[type=checkbox]').attr('checked', true);
	} else {
		$('.product-matrix-item h1 span.new-name').show();
		$('.product-matrix-item h1 span.new-name').css('display', 'block');
		$('.product-matrix-item h1 span.kwl-name').hide();
	}
	$('#kwl-product-names input').click(
		function(){
			if( $('.product-matrix-item h1 span.kwl-name').css('display') == "none" ){
				$('.product-matrix-item h1 span.kwl-name').show();
				$('.product-matrix-item h1 span.kwl-name').css('display', 'block');
				$('.product-matrix-item h1 span.new-name').hide();
				$('#kwl-product-names input[type=checkbox]').attr('checked', true);
				$.cookie( 'showKWLProductNames', 'true' );
			}else{
				$('.product-matrix-item h1 span.new-name').show();
				$('.product-matrix-item h1 span.new-name').css('display', 'block');
				$('.product-matrix-item h1 span.kwl-name').hide();
				$.cookie( 'showKWLProductNames', 'false' );
			}		
		}
	);			
}

function filterByAttribute() {
	// collect checkbox attributes
	var rel = new Array();
	$('#product-filter input[type=checkbox]').each( function() {
		if( $(this).attr('checked') == true ){
			rel.push($(this).attr('value'));
		}
	});

	if( rel.length == 0 ) {
		$('.product-matrix-item').show();
		return;
	}
	
	// hide all product items
	$('.product-matrix-item').hide();
	
	// compare attributes and show matching product items
	$('.product-matrix-item').each( function(){
		var cat = ( $( 'a', this ).attr('rel') ).split(',');
		if( isSubset( rel, cat ) ) $(this).show();
	});
	
}

function resetAttributeFilter() {
	resetCheckBoxes();
	$('.product-matrix-item').show();
}

function resetCheckBoxes() {
	$('#product-filter input[type=checkbox]').each( function() {
		$(this).attr('checked', '');
	});
}

function initTabMenu(){
	$('#tab tr td a').each( function(){
		$(this).click( function(){
			var rel = $(this).attr('rel');
			filterByCategory( rel );
			toggleAttributeFilter( rel );
			updateTabMenu( $(this) );
			$('#product-matrix').jScrollPane();
			$.cookie( 'fenoProductCat', rel );
			return false;
		});
	});
	if( $.cookie( 'fenoProductCat' ) != '' ) {
		var cat = $.cookie( 'fenoProductCat' );
		$('#tab tr td a[rel='+cat+']').click();
	}
}

function initProductItems(){
	$('.product-matrix-item').each( function(){
		$(this).click( function(){
			var url = $( "a", this).attr('href');
			window.location.href = '/'+url;
			return false;
		});
	});
}

function initCheckboxMenu(){
	$('#product-filter input[type=checkbox]').each( function() {
		$(this).click( function(){
   			filterByAttribute();
			$('#product-matrix').jScrollPane();
		});
	});
	$('#product-filter .reset').click( function(){
  		resetAttributeFilter();
		$('#product-matrix').jScrollPane();
   		return false;
	});
}

function isSubset( array1, array2 ){
	if( array1.length == 0 ) return false;
	var b = true;
	for( i = 0; i<array1.length; i++ ){
		if( jQuery.inArray( array1[i], array2 ) == -1 ) b = false;
	}
	return b
}

