$(document).ready(function() {
	$('#colorSwatches li a').click(function() {
		var selected_color = $('#colorSwatches li a.selected').attr('class').split(' ').shift();

		var color = $(this).attr('class');
		var hex = $(this).css('background-color');
		var collection = window.location.href.split('/').clean().pop();
		
		$('#colorSwatches li a').removeClass('selected');									
		$(this).addClass('selected');
		
		$('.currentDisplay span').html(color.ucwords());
		$('.currentDisplay span').css('color',hex);
		
		$('#productListing li a img').each(function () {
			var src = $(this).attr('src');
			var path = src.split('/');	
			var img = path.pop().replace(collection + '_','').replace(selected_color + '_','').split('_');
			img.unshift(color);		
			
			img.unshift(collection)
			var img = img.join('_');
			
			path.push(img);
			var src = path.join('/');

			$(this).attr('src',src);
		});
		
		$('#productListing li a').attr('href',function () {
			var url = this.href.split('?');
			return url[0] + '?color=' + color;												
		});
	});
	
	$("#color_sel").change(function () {
		$url = window.location.href.split('?color=');
		href($url[0] + '?color=' + $("#color_sel option:selected").val());
	});
});

