function removeCalcOrder(){
  $('#operation_container_bottom').removeClass('calculator order');
  $('#calculator, #online_order').removeClass('act');
  $('.calculator_container, .order_container, .close-bt').hide();
  $('.operation_container').show();

  return false;
}

var refreshTime = false;


var refresh_fn = function(){
			
		  cmsAjax(
				'/modules/xcur/includes/check_new.php', // backend
			{ 
				'time': refreshTime
			},
			function(res) {
				
				if(res.refresh){
					refreshTime = res.refresh;
				}
				
				if (res.reload) {
			    window.location.reload();

				}
			
			});

	}



$(document).ready(function(){

/* REFRESH */

//  refresh_fn();
//	var refresh = setInterval('refresh_fn()', 60*10*1000);


/* table with prices */

$('#dropdown_prices span').hover(function(){
	$('#operation_container_bottom').toggleClass('slidedown');
});

  $('#dropdown_prices').click(function(){
      
    if($('#other_currency').is(':visible')){
      $('#other_currency').slideUp();
      $('#dropdown_prices span').text('Все валюты');
    } else {

      cmsAjax(
        '/modules/xcur/all_courses.php',
        {},
        function(res) {
          $('#other_currency').html(res.test+res.html).slideDown();
          $('#dropdown_prices span').text('Свернуть');
      });

    }
      return false;
  });

  
  $('#calculator').click(function(e){
    e.preventDefault();

    if(!$(this).hasClass('act')){
      removeCalcOrder();
      $(this).addClass('act');
      $('#operation_container_bottom').addClass('calculator');
      $('.operation_container').hide();
      $('#calculator_container, .close-bt').show();
      if(!$('#calculator_container .newListSelected').length){
        $('#calculator_container select').sSelect();
      }

    } else {
      removeCalcOrder();

    }



  });

  $('#online_order').click(function(e){
    e.preventDefault();
    if(!$(this).hasClass('act')){
      removeCalcOrder();
      $(this).addClass('act');
      $('#operation_container_bottom').addClass('order');
      $('.operation_container').hide();
      $('#order_container, .close-bt').show();
      if(!$('#order_container .newListSelected').length){
        $('#order_container select').sSelect();
      }


    } else {
      removeCalcOrder();
    }

  });

  $('#calcsubmit').click(function(){

    if($('#form_calc_order_1_amount').val() != '' && parseInt($('#form_calc_order_1_amount').val()) > 0){

      cmsAjax(
        '/modules/xcur/calculator.php', 
        {
            'sell' : $('#form_calc_order_1_currency_sell').val(),
            'buy' : $('#form_calc_order_1_currency_buy').val(),
            'amount'		: $('#form_calc_order_1_amount').val()
        },
        function(res) {

          $('.calchtml').hide();
          $('.calcresult .result').html(res.html + ' ' + $('#form_calc_order_1_currency_buy option[selected]').text());
          $('#form_calc_order_1_operation').val(res.operation);
          $('#form_calc_order_1_currency').val(res.currency);

          $('.calcresult').show();

      });
    }
    return false;

  });

  $('#calcrecount').click(function(){
    $('.calchtml').show();
    $('.calcresult').hide();
    return false;
  });


  $('.close-bt').click(removeCalcOrder);


  $('#form_calc_order_1_currency_sell, #form_calc_order_1_currency_buy').change(function(){
    var i;
    var options = ''; // восстанавливаем список

    if($(this).val() == 0 || $(this).val() == 1){

      /** calc_valutes defined in html by php */
      for (i in calc_valutes){
        options += '<option value="'+i+'">'+ calc_valutes[i] +'</option>';
      }

      var select = $('.calc_select select').not('#'+$(this).attr('id'));
      if(select.hasClass('changed')){
        select.removeClass('changed').html(options);
        select.resetSS();
      }

    } else {

      var other = $('.calc_select select').not('#'+$(this).attr('id'));
      var val = other.val(); // что было выбрано (для восстановления)

      for (i in calc_valutes){
        if(i == 0 || i == 1)
          options += '<option value="'+i+'">'+ calc_valutes[i] +'</option>';
      }

      if(!other.hasClass('changed')){
        other.addClass('changed').html(options).val(val);
        other.resetSS();
      }

    }

    if($('#form_calc_order_1_currency_sell').val() == $('#form_calc_order_1_currency_buy').val()){
      $('#calcsubmit').addClass('disabled').attr('disabled', 'disabled');
    } else {
      $('#calcsubmit').removeClass('disabled').removeAttr('disabled');
    }
  });

  $('#form_order_1_currency_sell, #form_order_1_currency_buy').change(function(){
    var i;
    var options = ''; // восстанавливаем список

    if($(this).val() == 0 || $(this).val() == 1){

      for (i in calc_valutes){
          options += '<option value="'+i+'">'+ calc_valutes[i] +'</option>';
      }

      var select = $('.order_select select').not('#'+$(this).attr('id'));
      if(select.hasClass('changed')){
        select.removeClass('changed').html(options);
        select.resetSS();
      }


    } else {

      var other = $('.order_select select').not('#'+$(this).attr('id'));
      var val = other.val(); // что было выбрано (для восстановления)

      for (i in calc_valutes){
        if(i == 0 || i == 1)
          options += '<option value="'+i+'">'+ calc_valutes[i] +'</option>';
      }

      if(!other.hasClass('changed')){
        other.addClass('changed').html(options).val(val);
        other.resetSS();
      }

    }

    if($('#form_order_1_currency_sell').val() == $('#form_order_1_currency_buy').val()){
      $('#form_order_1_submit').addClass('disabled').attr('disabled', 'disabled');
    }	else {
      $('#form_order_1_submit').removeClass('disabled').removeAttr('disabled');
    }

  });


});
