$(function(){	
	//$('#price_range').change(function(){window.location='catalog.php?cid='+$('#hidden_category_id').val()+'&price_range='+$(this).val()});
	//$('#category_id').change(function(){window.location='catalog.php?cid='+$(this).val();});
	//$('#gender').change(function(){window.location='catalog.php?cid='+$(this).val();});
	$("#tabs").tabs();
	$("#banner-tabs").tabs();
	$('#tacori-style-number').watermark('Search By Tacori Style #')
	$('.filter').change(function()
	{
		var filter_string = '';
		var category_text = '';		
		if(new RegExp('^/catalog/(create-a-ring|tacori/create-a-ring|verragio/create-a-ring|tacori|verragio)/?').test(location.pathname)) //URL is rewritten, get category text
		{
			link_prefix = location.pathname.replace(/^(\/catalog\/(create-a-ring|tacori\/create-a-ring|verragio\/create-a-ring)\/?)(.+)/,'$1');
			if(! new RegExp('/$').test(link_prefix))
				link_prefix += "/";
			$(".filter").each(function(){									   
				if($(this).val() != '')
				{
					filter_string += $(this).attr('id')+'/'+$(this).val()+"/"					
				}
			});		
				
			filter_string = rtrim(filter_string,'/');
			
			if(filter_string != '')
				window.location=link_prefix+filter_string;
			else	
				window.location=link_prefix;
		}
		else if(new RegExp('^/catalog/[^/]+?/[0-9]+?').test(location.pathname)) //URL is rewritten, get category text
		{		
			$(".filter").each(function(){									   
				if($(this).val() != '')
				{
					if($(this).attr('id') != 'cid')				
						filter_string += $(this).attr('id')+'/'+$(this).val()+"/"
					else
					{
						filter_string += sanatize_for_mod_rewrite($('#'+$(this).attr('id')+' option:selected').text()) +'/'+ $(this).val() +'/';
					}
				}
			});		
				
			filter_string = rtrim(filter_string,'/');
			
			if(filter_string != '')
				window.location='/catalog/'+filter_string;
			else	
				window.location='/catalog.php';
		}
		else
		{
			$(".filter").each(function(){
				if($(this).val() != '')
					filter_string += $(this).attr('id')+'='+$(this).val()+"&"
			});		
			if($('#create_a_ring').val() == 1)
				filter_string += 'create-a-ring=1&';
			filter_string = rtrim(filter_string,'&');
			
			if(filter_string != '')
				window.location='/catalog.php?'+filter_string;
			else	
				window.location='/catalog.php';
		}
	});	
});
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function sanatize_for_mod_rewrite(str)
{	
	str = trim(str,'\\s');
	var new_string = '';
	var prev_char = '';	
	for (i=0;i<str.length;i++)
	{		
		if(new RegExp(/^[a-z0-9]$/i).test(str.charAt(i)))
		{
			new_string += str.charAt(i);
			prev_char = str.charAt(i);
		}
		else if(new RegExp(/^\s$/i).test(str.charAt(i)) && !new RegExp(/^\s$/i).test(prev_char))
		{
			new_string = new_string + str.charAt(i);		
			prev_char = str.charAt(i);
		}
	}	
	new_string = new_string.replace(/\s/gi,'-').toLowerCase();
	return new_string;
}
