/*
$(function(){

	 $('.check').bind('click', function(){
	 	alert('test');
	 });

});
*/

var r;
$(function(){
		$('#browseoptions input[type=checkbox]').bind('click', function(){						
			setTimeout(function(){
			filter();
			},200);
		});
		
		$('#browseoptions .check_all').click(function(){
		
			var t = $(this);
			var whichone = t.val();			
			
			
			var el = $('#browse'+whichone+' .jquery-safari-checkbox');
			if(t.is(':checked')){
				el.removeClass('jquery-safari-checkbox-checked');
				$('#browse'+whichone+' .check').attr('checked', false);
			} else {
				el.addClass('jquery-safari-checkbox-checked');
				$('#browse'+whichone+' .check').attr('checked', true);				
			}
			
						
			filter();
			
		
		});
});



function filter() {

	// Lets start with a results array - by copying the products array
	r = products;
	// we're going to manipulate this array in unkind ways
	
	// begin filtering
		
		var colors = new Array;
	
		// #colors
		 $('#browsecolors .check:checked').each(function(){
			
			// add each color option to the list
			colors [colors.length] = $(this).val();
		});
		
//		alert( $('#browsecolors .check:checked').size()+'total'+$('#browsecolors .check').size());
		
		var sizes = new Array;
	
		// #sizes
		 $('#browsesizes .check:checked').each(function(){
			
			// add each size option to the list
			sizes [sizes.length] = $(this).val();
			
		});
		
		var weights = new Array;
	
		// #weights
		 $('#browseweights .check:checked').each(function(){
			
			// add each weight option to the list
			weights [weights.length] = $(this).val();
			
		});
		
		var grips = new Array;
		
		// #grips
		$('#browsegrips .check:checked').each(function(){
		
			// add in each grip option to the list
			grips [grips.length] = $(this).val();
		});
	
		
		check({
			
			weights : weights,
			sizes : sizes,
			colors : colors
			
		});
		
		for (var k in r){
		
			var remove = true;

			for (var g in r[k]['grips']){
				
				for (var g1 in grips){
					
					if(grips[g1] == r[k]['grips'][g]) {
						remove = false;
					}
									
				}
				
			}
			
			
			if(remove == true){
			
				r=removeKey(r, k);
			
			}
			
		}
		
		
		
		
		for (var idp in products){
			var el = $('#product-'+idp);
						
			if(typeof(r[idp]) != 'undefined') { // is a result
			
				el.fadeIn();
							
			} else {
							
				el.fadeOut();
				
			}
			
		}

}
// #check
function check (options) {
	
	var options = $.extend({
		
		colors : null,
		sizes : null,
		weights : null
		
	}, options);
	
	checkthis({
		check : 'color',
		values : options.colors
	});
	
	checkthis({
		check : 'size',
		values : options.sizes
	});
	
	checkthis({
		check : 'weight',
		values : options.weights
	});
	
	
	

}
// # checkthis
function checkthis (options) {
	
	var options = $.extend({

		check : null,
		values : null

	},options);



	for (var id in r){

		var remove = true;

		var o = r[id]['options'];

		for (var oid in o){
			var opt = o[oid];
			if(isArray(options.values)){
				for (var v in options.values){
					if(options.values[v] == opt[options.check]){
						remove = false;
					}
				}
			} else {
				if(options.values == opt[options.check]){
					remove = false;
				}					
			}
	}
	
	
	if(remove==true){
		r = removeKey(r, id);		
	}
	
	}
}
	

// #removeKey
function removeKey(arrayName,key){
	var tmpArray = new Array();
	for(var x in arrayName)
	{
	if(x!=key) { tmpArray[x] = arrayName[x]; }
	}
	return tmpArray;
} // close removeKey


// #isArray
function isArray(obj) {
   if(typeof(obj) != 'undefined'){
   if (obj.constructor.toString().indexOf("Array") == -1){
      return false;
  } else {
      return true;
      
   }
   } else {
   	return false;
   }
}

// #count
function count(ar){
	var c = 0;
	for (var i in ar){
		c += 1;
	}
	return c*1;
}
