$(function(){

	var unit = ' руб.';
	var price = $('.totalPrice').text().slice(0,$('.totalPrice').text().indexOf(unit));
	
	$.fn.culcprice = function(){
		var cp = 1*price;
		$('.product_option').each(function(){
			if ($(this).is('input[type="radio"]'))
				cp += 1*$(this).attr('rel');
			if ($(this).is('select'))
				cp += 1*$(this).find(':selected').attr('rel');
			if ($(this).is('input[type="hidden"]'))
				cp += 1*$(this).attr('rel');
		});
		$('.totalPrice').text(cp+unit);
	}
	
	$('.add_param_radio tr').each(function(){
		$(this).find('.radio2_total_price').text((1*price+1*$(this).find('input[type=radio]').attr('rel')) + unit);
	})
	
	$('.add_param_radio').click(function(el){
		var e = $(el.target);
		if (e.is('input[type="radio"]')) {
			//alert(e.attr('rel'));
			//$('.add_param_radio .radio2_total_price').text('');
			//e.closest('tr').find('.radio2_total_price').text((1*price+1*e.attr('rel')) + unit);
			$('.add_param_radio input[type="radio"]').each(function(){
				if ($(this).is(':checked')) 
					$(this).addClass('product_option');
				else
					$(this).removeClass('product_option');
			})
			$(this).culcprice();
		}
	})
	$('.add_param_checkbox').click(function(el){
		var e = $(el.target);
		if (e.is('input[type="checkbox"]')) {
			$('.add_param_checkbox input[type="checkbox"]').each(function(){
					var e = $(this).closest('tr').find('select');
					var sur = e.find(':selected').attr('rel');
				if ($(this).is(':checked')) {
					e.addClass('product_option');
					e.closest('tr').find('td:last').text('+ '+sur+' руб.');
				} else {
					$(this).closest('tr').find('select').removeClass('product_option');
					//e.closest('tr').find('td:last').text('');
				}
			})
			$(this).culcprice();
		}
	})
	
	function showCheckboxPrice() {
		var sur = 0;
		$('.add_param_checkbox tr').not('.add_param_checkbox thead tr').each(function(){
			sur = $(this).find(':selected').attr('rel');
			$(this).find('td:last').text('+ '+sur+' руб.');
		})
	}
	showCheckboxPrice()
	
	$('.add_param_checkbox select').change(function(){
		var sur = $(this).find(':selected').attr('rel');
		$(this).closest('tr').find('td:last').text('+ '+sur+' руб.');
	});
	
	
	$('.cpt_product_params_selectable').find('select').change(function(){
		$(this).culcprice();
	})
	
	$('.color_select img').css('width','60px');
	
	
	$('.color_select img').click(function(){
		var el = $(this).closest('div.color_select');
		el.find('.selcolor').text($(this).closest('li').find('div').text()); 
		el.find('img').css('outline','none')
		//.css('width','60px').css('position','static').css('z-index','1')
		.next().removeClass('product_option');
		$(this).css('outline','red solid 1px')
		//.css('width','140px').css('position','relative').css('z-index','100')
		.next().addClass('product_option');
		$(this).culcprice();
	})

	$('.color_select img').hover(function(){
		var el = $(this).closest('div.color_select');
		el.find('.selcolor').text($(this).closest('li').find('div').text());
		$(this).css('width','140px').css('position','relative').css('z-index','100');
	},function(){
		var el = $(this).closest('div.color_select'),
			s = el.closest('.color_select').find('.product_option').closest('li').find('div').text();
		el.find('.selcolor').text(s);
		$(this).css('width','60px').css('position','static').css('z-index','1');
	})
	
	$('.totalPrice').culcprice();
	
	$('#right').css('min-height',$('#left').height())
})
