var qweqwe;
var qwe;

function recalcCost(item_id)
{
	var item_total = $('#item-total-' + item_id);
	var item_options = $('#item-options-' + item_id);  
	var type = item_options.attr('type');
  var inp_radio = $('input:radio:checked.inp_price', item_options);
  var inp_days = $('.inp_count_days-' + item_id);
  var inp_adult = $('.inp_count_adult-' + item_id);
  var inp_child = $('.inp_count_child-' + item_id);
  var countDays = 1;
  var countMan = 1;
  var total_part = $('#cart-total-' + type);
  var value = 0;
  
  if (parseInt(inp_days.val()) > 0)
    countDays = parseInt(inp_days.val());
  
  countAdult = parseInt(inp_adult.val());
  if (!(countAdult > 0))
  	countAdult = 1;

  countChild = parseInt(inp_child.val()); 	
  if (!(countChild > 0))
  	countChild = 0;
	
  countMan = countAdult + countChild;
  
  // подставляем значение детей в нужное поле 
  if (inp_radio.length > 0)
  {
  	var key = inp_radio.attr('key');
  	
  	$('.inp_child', item_options).val('');
  	$('.inp_child-key-' + key, item_options).val(countChild);
  }
  
	if (inp_radio.length > 0)
	{
	  if ($('.inp_adult', item_options).length > 0)
	  {
  	  $('.inp_adult', item_options).val('');
  	  $('.inp_adult-key-' + key).val(countAdult);
  	}
	  
  	value = parseFloat(inp_radio.attr('price')) * countAdult;

    if (type == 'hotel')
    {
      var inp_child = $('.inp_child-key-' + key, item_options);
    	if (parseInt(inp_child.val()) > 0)
      	value += (parseInt(inp_child.val()) * parseFloat(inp_child.attr('price')));
    } 
    
    if (type == 'ticket')
    {
    	value = parseFloat(inp_radio.attr('price'));
    	//value *= countMan;
    } 
    
    if (type == 'excursion')
    {
      var inp_child = $('.inp_child-key-' + key, item_options);
      var inp_man = $('.inp_man-key-' + key, item_options);
    	if (parseInt(inp_child.val()) > 0)
      	value += parseInt(inp_child.val()) * parseFloat(inp_child.attr('price'));
    	if (parseInt(inp_man.val()) > 0)
      	value += parseInt(inp_man.val()) * parseFloat(inp_man.attr('price'));
    } 

    value *= countDays;
    
  	$('.inp_additional:checked', item_options).each(
      function(){
      	type_id = parseInt($(this).attr('type_id'));
      	
      	if (type_id == 0)
      	{
      		value += (parseFloat($(this).attr('price')) * countMan);
        }
      	if (type_id == 1)
      	{
      		value += parseFloat($(this).attr('price'));
        }
      	if (type_id == 2)
      	{
      		value += (parseFloat($(this).attr('price')) * countMan * countDays);
        }
      	if (type_id == 3)
      	{
      		value += parseFloat($(this).attr('price')) * countDays;
        }
      }
    );    	

//      item_options.attr('price', value);
//
//      var total_value_hotel = 0;
//      var total_value_excursion = 0;
//       
//      $('.item-options').each(
//        function(){
//          if ($(this).attr('type') == 'hotel')
//            total_value_hotel += parseInt($(this).attr('price'));
//          if ($(this).attr('type') == 'excursion')
//            total_value_excursion += parseInt($(this).attr('price'));
//        }
//      );
//      if (type == 'hotel')
//        total_part.text(total_value_hotel);
//      if (type == 'excursion')
//        total_part.text(total_value_excursion);      
  }		
	
	// Если подсчитываем авиабилеты
//	if (type == 'ticket')
//	{
//		value = parseFloat($('.inp_price-ticket', item_options).val());
//		
//		value *= countMan;
//	}
	
	// Если подсчитываем авиабилеты
	if (type == 'inner_ticket')
	{
		//value = parseFloat($('.inp_price-inner_ticket', item_options).val());
		value = value = parseFloat(inp_radio.attr('price'));
		
		value *= countMan;
		
  	$('.inp_additional:checked', item_options).each(
        function(){
        	value += parseFloat($(this).attr('price')) * countMan;
        }
    );    					
	}
	
	// Если подсчитываем гиды
	if (type == 'gid')
	{
		value = parseFloat($('.inp_price-gid', item_options).val());
		
		value *= countDays;
		
		$('.inp_additional:checked', item_options).each(
				function(){
	      	type_id = parseInt($(this).attr('type_id'));
	      	
	      	if (type_id == 0)
	      		value += parseFloat($(this).attr('price')) * countDays;
	      	
	      	if (type_id == 1)
	      		value += parseFloat($(this).attr('price'));
				}
		);    					
	}
	
	// Если подсчитываем машины
	if (type == 'auto')
	{
		value = parseFloat($('.inp_price-auto', item_options).val());
		
  	$('.inp_additional:checked', item_options).each(
        function(){
        	value += parseFloat($(this).attr('price'));
        }
    );    			
	}
	
	// Если подсчитываем симпл пакеты
	if (type == 'simplepacket')
	{
		if (inp_radio.attr('variant'))
			$('.variant', item_options).val(inp_radio.attr('variant'));
		
		value = 0;
		if (inp_radio)
		{
			value = parseFloat(inp_radio.attr('price')) * countAdult;
			value += $('.inp_child-key-' + key, item_options).attr('price') * countChild;
		}
	}
	
	if (parseFloat(value) > 0)
	{
		value = value.toFixed(2);
		item_total.html('$' + value);
	}
	else
	{
		item_total.html('уточните параметры');
	}
	
	updateTotalFields();
}

function updateSubTotalFields()
{
  $('.cart-total-category').each(
    function(){
    	var parent = $('.cart-group-' + $(this).attr('type'));
    	var cart_total_value = 0;
    	
      $('.cart-item-total', parent).each(
        function(){
          if (parseFloat($(this).text().replace('$','')) > 0)
        	  cart_total_value += parseFloat($(this).text().replace('$',''));
        }
      ); 

      $(this).text(cart_total_value.toFixed(2));
    }
  ); 
}

function updateTotalFields()
{
	updateSubTotalFields();
	
  var lb_cart_total = $('#cart-total');
  var inp_cart_total = $('#inp_cart-total');
  var cart_total_value = 0;
  
  $('.cart-total-category').each(
    function(){
      cart_total_value += parseFloat($(this).text());
    }
  ); 
  lb_cart_total.text(cart_total_value.toFixed(2));
  inp_cart_total.val(cart_total_value.toFixed(2));		
}

function doRecalcCost(sender)
{
  var item_id;
  

  if (parseInt(sender.attr('item_id')) > 0)
    item_id = parseInt(sender.attr('item_id'));
  else if (parseInt(sender.parent().attr('item_id')) > 0)
    item_id = parseInt(sender.parent().attr('item_id'));
  else if (parseInt(sender.parent().parent().attr('item_id')) > 0)
    item_id = parseInt(sender.parent().parent().attr('item_id'));
  
  recalcCost(item_id);	
}

$(document).ready(
		function(){
			
			$('.price-detail-button').click(
					function(){
						if ($('.price-detail').is('.hidden'))
						{
							$('.price-detail').fadeIn('fast').removeClass('hidden'); 
						}
						else
						{
							$('.price-detail').fadeOut('fast').addClass('hidden');
						}
					}
			);
			
			$('.hidden-images-show').click(
					function()
					{
						$('img','.hidden-images-container').fadeIn('fast');
						$(this).hide();
						return false;
					}
			);						
		}
);
