$(document).ready(function(){
  
  $('#similer_products ul').jcarousel({ scroll: 3,wrap: 'last' });
  $('#new_items ul').jcarousel({ scroll: 3,wrap: 'last' });

  $(".discount_popup a").click(function(){
    $.modal.close();
  });
  $(".cart_popup a").click(function(){
    $.modal.close();
  });

  if ($(".scroll_anchor").html())
  {
    $("html").animate({scrollTop: $("#item_scroll_"+$(".scroll_anchor").html()).offset().top});
    $.modal($(".cart_popup"), {autoPosition: true, overlayClose: false})
  }

  if ($(".added_anchor").html())
  {
    $.modal($(".cart_popup"), {autoPosition: true, overlayClose: false})
  }

  $(".discount").click(function(){
    $.modal($(".discount_popup"), {autoPosition: true, overlayClose: true});
  });

  // CART

  $("#cart_recalculate").click(function(){
    $.post('/cart', {
      action: 'recalculate',
      items: $(".items_count").serializeArray(),
      fordelete: $(".for_delete").serializeArray()
    }, function(data){
      if(data=='empty')
      {
        //alert('Cart is empty.');
      };
      document.location.href = document.location.href;
      //document.location = '';
    });
  });

  $("#print_invoice").click(function(){
    newwindow = window.open('/cart/review/?print=true','print invoice', 'width=810px, menubar=no, scrollbars=yes');
    if (window.focus) {newwindow.focus()}
  });
  // end CART

  // BILL
  $(".bill_currency").click(function(){
    $.post('/plugin_ba', {pl: 'bill', act: 'setcur', cur: $(this).attr('name')}, function(data){
      document.location.href = document.location.href;
    });
  });

  $("#bn_checkout_cancel").click(function(){
    document.location='/cart/view';
  });

  $("#btn_submit_order").click(function(){
    $(this).attr('disabled', 'true');
    $.post('/cart/finish', {}, function(data){
      $("#oid").val(data);
      $("#form_review_payment").submit();
    });
  });


  $(function(){
    $("#image_gallery div a").lightBox({
      imageLoading: '/js/jquery-lightbox-0.5/images/loading.gif',
      imageBtnClose: '/js/jquery-lightbox-0.5/images/close.gif',
      imageBtnPrev: '/js/jquery-lightbox-0.5/images/prev.gif',
      imageBtnNext: '/js/jquery-lightbox-0.5/images/next.gif'
    });
  });
  $(document).bind('keyup', function(e) {
    if(e.keyCode == 27) {
      $('#lightbox-secNav-btnClose').click();
    }
  });

})

