/**
 * product color selector
 * 
 * the following adds color selecting / product image switching
 * behaviour to specific select boxes.
 * additionally, it sets shopping cart links to reflect
 * the color selection.
 *
 * @author mglass at 3pc dot de
 */
//(function($) {
//$(function() {
var pcs = $('.product-color-switch');
if(pcs != null) {
	pcs.change(function() {
		
		if(this.value == '') return false;
		
		// get the id string from the switche's class
		var id = this.className.match(/for-(\S+)/)[1],
			value = $(this).val().match(/([^|]*)\|(.*)/);
		
		window.location.hash = '#'+this.selectedIndex;
		
		// switch the image src
		var img = $('#'+id),
			new_src = img.attr('src').replace(/([^/]*)$/, value[1]);
			
		img.attr('src', new_src);
		
		//$('#cart-button-'+id).attr('href', value[2]);
		var action = value[2].split('?');
		var form   = $('#rf_cart').attr('action', action[0]);
		
		if(action[1]) {
			var params = action[1].split('&');
			for(var i=0; i < params.length; i++) {
				var p = params[i].split('=');
				$('input[name='+p[0]+']', form).remove();
				$('<input type="hidden" />').attr('name', p[0]).attr('value', p[1]).appendTo(form);
			}
		}

	});
	
	// manage product selection via location.hash
	if(window.location.hash != '') {
		var hash = window.location.hash.substr(1, window.location.href.length);
		$('.product-color-switch option').each(function(index, item) {
			//console.log(arguments);
			if(index == hash) {
				this.selected = true;
				$('.product-color-switch').change();
			}
		});
		/*$('.product-color-switch option').each(function() {
			if(regexp.test(this.value)) {
				this.selected = true;
			}
		});*/
	}
}
//});
//})(jQuery);