$(document).bind('ready', function() {
  cart_func_history();
  page_func_history();
});
function page_func_history()
{
  $('#orderNumber').bind('keyup', function() {
    if (/^ *0*[1-9]+[01-9]* *$/.test($(this).attr('value'))) {
      $('#dv_orderCheck a.primary').removeClass('disabled').find('span').removeClass('disabled');
    } else {
      $('#dv_orderCheck a.primary').addClass('disabled').find('span').addClass('disabled');
    }
  }).bind('keydown', function(event) {
    var key = event.charCode ? event.charCode : event.keyCode;
    if (key==13) {
      $('#dv_orderCheck a.primary').trigger('click');
    }
  });
  $('#checkOrder a.secondary').bind('click', function() {
    $(this).addClass('hide');
    $('#checkOrder tr.active').addClass('hide').prev().removeClass('hide');
    return false;
  })
  $('#dv_orderCheck a.primary').bind('click', function() {
    if (!$(this).is('.disabled')) {
      $('#dv_orderedItemsSmall').addClass('hide');
      $('#checkOrder tr.active').load('/history.php?modal_order=' + encodeURIComponent($('#orderNumber').attr('value')), function() {
        set_win_title();
        $(this).removeClass('hide').prev().addClass('hide');
        $('#checkOrder a.secondary').removeClass('hide');
        $('#history .historyTable tr').removeClass('active');
        if (!/не найден/.test($(this).html())) {
          $('#dv_orderedItemsSmall').load('/cart_pre.php?exorder=1&id_order=' + encodeURIComponent($('#orderNumber').attr('value')), function() {
            $(this).removeClass('hide');
            $('#dv_orderedItemsSmall .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
            cart_func_history();
          })
        }
      })
    }
    return false;
  });
  $('#history .historyTable td').css({cursor : 'pointer'}).bind('click', function() {
    $('#history .historyTable tr').removeClass('active');
    $(this).parent().addClass('active');
    $('#checkOrder a.secondary').addClass('hide');
    $('#checkOrder tr.active').addClass('hide').prev().removeClass('hide');
    $('#dv_orderedItemsSmall').load('/cart_pre.php?exorder=1&id_order=' + encodeURIComponent($(this).parent().attr('rel')), function() {
      $(this).removeClass('hide');
      $('#dv_orderedItemsSmall .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      cart_func_history();
    })
  });
  $('div.container:last .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
}
carttm_history = new Object();
function cart_func_history()
{
  if ($('div.selectedItem[@rel=h] a.delete').length == 1) {
    $('div.selectedItem[@rel=h] a.delete').addClass('disabled');
  } else {
    $('div.selectedItem[@rel=h] a.delete').removeClass('disabled');
  }
  $('#dv_orderedItemsSmall .forPayment .secondary').bind('click', function() {
    if ($('#dv_cart').length > 0) {
      $('#invis').load(modal_link('/history/?double_order=' + $(this).attr('rel')), function() {
        set_win_title();
        $(this).html('');
        page_close();
        reload_cart();
      });    	
    } else {
      document.location.href = '/history/?double_order=' + $(this).attr('rel');
    }
    
    return false;
  });
  $('#dv_orderedItemsSmall .forPayment .negative').bind('click', function() {
    $('div.container:last').load(modal_link('/history/?delete_order=' + $(this).attr('rel')), function() {
      set_win_title();
      page_func();
      cart_func_history();
      page_func_history();
    });
    return false;
  });
  $('#dv_orderedItemsSmall a.info').bind('click', function() {
     $('div.selectedItem[@rel=h]').hide();
     $(document.body).append($('#infoh' + $(this).attr('rel')).show().css({left:($(this).offset().left - 5) + 'px', top:($(this).offset().top - 50) + 'px'}));
    return false;
  });
	$('#dv_orderedItemsSmall a.delete').bind('click', function() {
      if (!$(this).is('.disabled')) {
        if (carttm_history[$(this).attr('rel')]) window.clearTimeout(carttm_history[$(this).attr('rel')]);
        $('div#dv_orderedItemsSmall').load('/cart_pre.php?exorder=1&&delete_product=' + $(this).attr('rel') + '&rand=' + Math.random(), function() {
          $('#orderedList').each(function() {fleXenv.fleXcrollMain(this)});
          cart_func_history();
        });
        $('div.selectedItem[@rel=h]').remove();
      }
      return false;
		});
  $('#dv_orderedItemsSmall a.dec').bind('click', function() {
			if($(this).next('input').attr('value') > 1){
        if (carttm_history[$(this).attr('rel')]) window.clearTimeout(carttm_history[$(this).attr('rel')]);
        $('#dv_orderedItemsSmall').add('div.selectedItem[@rel=h]').find('input.text[@rel=' + $(this).attr('rel') + ']').attr('value', $(this).next('input').attr('value')/1 - 1);
        updatetm_history($(this).attr('rel'));
			} else {
        /*
        $('div#dv_orderedItemsSmall').load('/cart_pre.php?exorder=1&&delete_product=' + $(this).attr('rel') + '&rand=' + Math.random(), function() {
          $('#cartTable').each(function() {fleXenv.fleXcrollMain(this)});
          cart_func_history();
        });
        $('div.selectedItem[@rel=h]').remove();
        */
      }
      return false;
		});		
		
 $('#dv_orderedItemsSmall a.inc').bind('click', function() {
	    if($(this).prev('input').attr('value') < 100){
        if (carttm_history[$(this).attr('rel')]) window.clearTimeout(carttm_history[$(this).attr('rel')]);
        $('#dv_orderedItemsSmall').add('div.selectedItem[@rel=h]').find('input.text[@rel=' + $(this).attr('rel') + ']').attr('value', $(this).prev('input').attr('value')/1 + 1);
        updatetm_history($(this).attr('rel')); 
      }
      return false;
		});

 $('#dv_orderedItemsSmall input.text[@rel]').bind('keyup', function() {
    if (carttm_history[$(this).attr('rel')]) window.clearTimeout(carttm_history[$(this).attr('rel')]);
    if (!/^[1-9][01-9]*$/.test($(this).attr('value'))) {
      if (/[1-9][01-9]*$/.test($(this).attr('value'))) $(this).attr('value', $(this).attr('value').replace(/[^01-9]+$/, ''));
      else $(this).attr('value', 1);
    }
    if ($(this).attr('value') > 100) $(this).attr('value', 100);
    //$(this).prev('input').attr('value', $(this).prev('input').attr('value')/1 + 1);
    $('#dv_orderedItemsSmall').add('div.selectedItem[@rel=h]').find('input.text[@rel=' + $(this).attr('rel') + ']').attr('value', $(this).attr('value'));
    updatetm_history($(this).attr('rel'));
    return false;
  }).bind('focus', function() { this.select(); }).bind('keydown', function(event) {
    var key = event.charCode ? event.charCode : event.keyCode;
    if (key == 38) {
      $(this).next('a').trigger('click');
    } else if (key == 40) {
      $(this).prev('a').trigger('click');
    }
  });
  $('#dv_orderedItemsSmall .order a').bind('click', function() {
    if (!$(this).is('.disabled')) {
      //document.regform.submit();
    }
    return false;
  })
  $('#dv_orderedItemsSmall a.shipinfo').bind('click', function() {
    var last_href = modal_link($(this).attr('href'));
    $('div.selectedItem[@rel=h]').hide();
    $('div.container:last').hide().each(function() {
      var el = document.createElement('div');
      fpar = 'fr=4';
      $(el).attr('id', 'container').attr('class', 'container').insertAfter($(this)).load(add_params(last_href, fpar), function() {
        set_win_title();
        page_func();
        $('div.container:last .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      });
    });   
    set_href('/shipment/');  
    return false;
  });
  $('#dv_orderedItemsSmall a.det').bind('click', function() {
    var last_href = modal_link($(this).attr('href'));
    $('div.selectedItem[@rel=h]').hide();
    $('div.container:last').hide().each(function() {
      var el = document.createElement('div');
      fpar = 'fr=4';
      fpar2 = 'fr2=4';
      $(el).attr('id', 'container').attr('class', 'container').insertAfter($(this)).load(add_params(last_href, fpar+'&' + fpar2), function() {
        set_win_title();
        item_func();
        $('#dv_detailsSide .flexcroll').each(function() {fleXenv.fleXcrollMain(this)});
      });
    });    
    set_href($(this).attr('href'));
    return false;
  });
}
function updatetm_history (id)
{
  recalccart_history();
  window.waitamoment++;
  window.setTimeout('window.waitamoment--', 2000);
  carttm_history[id] = window.setTimeout('_updatetm_history(' + id + ')', 2000);
}
function _updatetm_history(id)
{
    $('#invis').load('/cart_pre.php?exorder=1&&update_product=' + id + '&qty=' + $('#dv_orderedItemsSmall').add('div.selectedItem[@rel=h]').find('input.text[@rel=' + id + ']').attr('value') + '&rand=' + Math.random(), function() {
      recalccart_history();
      $(this).html('');
    });
}
function recalccart_history()
{
  if ($('#dv_orderedItemsSmall .forPayment strong').length == 0) return;
  $('#dv_orderedItemsSmall').add('div.selectedItem[@rel=h]').find('a.dec').each(function() {
    if ($(this).next('input').attr('value') == 1) {
      $(this).addClass('disabled');
    } else {
      $(this).removeClass('disabled');
    }
  });
  $('#dv_orderedItemsSmall').add('div.selectedItem[@rel=h]').find('a.inc').each(function() {
    if ($(this).prev('input').attr('value') == 100) {
      $(this).addClass('disabled');
    } else {
      $(this).removeClass('disabled');
    }
  });
  if ($('div.selectedItem[@rel=h] a.delete').length == 1) {
    $('div.selectedItem[@rel=h] a.delete').addClass('disabled');
  } else {
    $('div.selectedItem[@rel=h] a.delete').removeClass('disabled');
  }
  var total = 0;
  var this_total = 0;
  var this_qty = 0;
  $('#dv_orderedItemsSmall input[@type=hidden][@rel]').each(function() {
    this_qty = $('#dv_orderedItemsSmall').add('div.selectedItem[@rel=h]').find('input.text[@rel=' + $(this).attr('rel') + ']').attr('value');
    this_total = parseInt($(this).attr('value')*this_qty);
    total += this_total;
    $('#dv_orderedItemsSmall').add('div.selectedItem[@rel=h]').find('.price[@rel='+$(this).attr('rel')+']').each(function(){
      $(this).html($(this).html().replace(/[1-9][01-9]*/, this_total))
    });
    if (this_qty > 1) {
      $('#dv_orderedItemsSmall').add('div.selectedItem[@rel=h]').find('.quantity2[@rel='+$(this).attr('rel')+']').each(function(){
        $(this).html($(this).html().replace(/[01-9]+/, this_qty)).removeClass('hide');
      });
    } else {
      $('#dv_orderedItemsSmall').add('div.selectedItem[@rel=h]').find('.quantity2[@rel='+$(this).attr('rel')+']').each(function(){
        $(this).html($(this).html().replace(/[01-9]+/, this_qty)).addClass('hide');
      });
    }
  });
  if ($('#dv_orderedItemsSmall .forPayment em.tt').length > 0) {
    total += Math.round($('#dv_orderedItemsSmall input[@name=total_ship]').attr('value')/1);
    $('#dv_orderedItemsSmall .forPayment em.tt').html($('#dv_orderedItemsSmall .forPayment em.tt').html().replace(/[1-9][01-9]*/, total));
    $('#dv_orderedItemsSmall .forPayment strong').html($('#dv_orderedItemsSmall .forPayment strong').html().replace(/[1-9][01-9]*/, parseInt(total*$('#dv_orderedItemsSmall input[@name=exchange]').attr('value'))));
  } else {
    total += Math.round($('#dv_orderedItemsSmall input[@name=total_ship]').attr('value')*$('#dv_orderedItemsSmall input[@name=exchange]').attr('value'));
    $('#dv_orderedItemsSmall .forPayment strong').html($('#dv_orderedItemsSmall .forPayment strong').html().replace(/[1-9][01-9]*/, parseInt(total)));
  }
}
