var _http_path = 'http://www.mkds.lt';
function cart_sum() {
  $.ajax({
    async: "false",
    type: "GET",
    url: _http_path + "/index.php?ajax=1&sum=1&delivery=" + $("input[name=delivery]").attr("value"),
    success: function(msg) {
      if( document.getElementById("suma") )
        document.getElementById("suma").innerHTML = msg;
    }
  });
}

function calculate(item_id, act) {
  plus_minus('quantity'+item_id, act);

  var source_id = $("#source"+item_id).val();
  var quantity = $("#quantity"+item_id).val();

  $.ajax({
    async: "false",
    type: "POST",
    url: _http_path + "/index.php?ajax=1&change_price=1",
    data: "module=shop_cart&act=add&cart_add=1&product_count="+quantity+"&source="+source_id+"&product_id="+item_id,
    success: function(msg) {
      $('#total'+item_id).html(msg);
      
      cart_sum();
      top_recalc();
    }
  });
}

function del_item(item_id) {
  if( confirm("Ar tikrai norite pašalinti prekę ?") == false )
    return;

  var quantity = $("#quantity"+item_id).val();

  $.ajax({
    async: "false",
    type: "POST",
    url: _http_path + "/index.php?ajax=1&change_price=1",
    data: "module=shop_cart&act=remove&product_id="+item_id+"&product_count="+quantity+"&cart_add=1&deliver=1&product_color=",
    success: function(msg) {
      cart_sum();
      top_recalc();
      
      $("#item"+item_id).remove();
    }
  });
}

function top_recalc() {
  $.ajax({
    async: "false",
    type: "GET",
    url: _http_path + "/index.php?ajax=1&top=1",
    success: function(msg) {
      $("#block_shopping").html(msg);
    }
  });
}

$(function() {
  $(".add_prod").submit( function() {
    $("input[name=product_count]").val( ($("input[name=product_count_dummy]").val()*1) + ($("input[name=product_count]").val()*1) );
    
    $.ajax({
      async: "false",
      cache: "false",
      type: "POST",
      url: $(this).attr("action"),
      data: $(this).serialize(),
      success: function(msg) {
        top_recalc();
        $("input[name=product_count_dummy]").val(1);
        $(".added").css("visibility", "visible");
        setTimeout( function(){ $(".added").css('visibility', 'hidden') }, 3000 );
        //setTimeout( function(){ window.location.href=_http_path+'/prekiu-krepselis' }, 1000 );
      }
    });

    $.ajax({
      async: "false",
      cache: "false",
      type: "GET",
      url: _http_path + '/index.php?ajax=1&prodcount=1&prodid=' + $("input[name=product_id]").val() + '&color=' + $("input[name=product_color]").val(),
      success: function(msg) {
        $("input[name=product_count]").val(msg*1);
        //window.location.href=_http_path+'/prekiu-krepselis';
      }
    });
  });
});

function calculate2(obj, act) {
  var item_id = obj.parent().children("input[name=product_id]").val();
  var item_color = obj.parent().children("input[name=product_color]").val();
  var source_id = obj.parent().children("input[name=source]").val();
  var delivery = obj.parent().children("input[name=delivery]").val();
  
  plus_minus('quantity'+item_id+item_color, act);
  
  var quantity = obj.parent().children("input[name=quantity]").val();
  $.ajax({
    async: "false",
    type: "POST",
    url: _http_path + "/index.php?ajax=1&change_price=1",
    data: "module=shop_cart&act=add&cart_add=1&product_count="+quantity+"&source="+source_id+"&product_id="+item_id+"&product_color="+item_color,
    success: function(msg) {
      $('#total'+item_id+item_color).html(msg);
      cart_sum();
      top_recalc();
    }
  });
}

function calculate3(obj) {
  var item_id = obj.parent().children("input[name=product_id]").val();
  var item_color = obj.parent().children("input[name=product_color]").val();
  var source_id = obj.parent().children("input[name=source]").val();
  
  //plus_minus('quantity'+item_id+item_color, act);
  
  var quantity = obj.parent().children("input[name=quantity]").val();
  $.ajax({
    async: "false",
    type: "POST",
    url: _http_path + "/index.php?ajax=1&change_price=1",
    data: "module=shop_cart&act=add&cart_add=1&product_count="+quantity+"&source="+source_id+"&product_id="+item_id+"&product_color="+item_color,
    success: function(msg) {
      $('#total'+item_id+item_color).html(msg);
      cart_sum();
      top_recalc();
    }
  });
}

function del_item2(obj) {
  if( confirm("Ar tikrai norite pašalinti prekę ?") == false )
    return;
  
  var item_id = obj.closest("tr").children(".cart_quantity_td").children("input[name=product_id]").val()
  var item_color = obj.closest("tr").children(".cart_quantity_td").children("input[name=product_color]").val();
  var quantity = obj.closest("tr").children(".cart_quantity_td").children("input[name=quantity]").val();

  $.ajax({
    async: "false",
    type: "POST",
    url: _http_path + "/index.php?ajax=1&change_price=1",
    data: "module=shop_cart&act=remove&product_id="+item_id+"&product_count="+quantity+"&cart_add=1&deliver=1&product_color="+item_color,
    success: function(msg) {
      cart_sum();
      top_recalc();
      $("#item"+item_id+item_color).remove();
    }
  });
}

//var sel_color_id = '';
$(document).ready(function() {
  $(".__plus").click(function() { calculate2($(this), 1); });
  $(".__minus").click(function() { calculate2($(this), -1); });
  $(".cart_del_prod").click(function() { del_item2($(this)); });
  cart_sum();

  var border_sel_ = 'sel';
  
  $(".color_block_ li div").mouseover( function() {
    if( $(this).parent().attr("id") != sel_color_id ) {
      $(this).addClass(border_sel_);
    }
  });
  $(".color_block_ li div").mouseout( function() {
    if( $(this).parent().attr("id") != sel_color_id ) {
      $(this).removeClass(border_sel_);
    }
  });
  
  $(".color_block_ li").click( function() {
    var sel_color_id_temp = $(this).attr("id");
    if( sel_color_id.length > 1 && sel_color_id_temp != sel_color_id ) {
      var idd = "#" + sel_color_id + " > div";
      $(idd).removeClass(border_sel_);
    }
    
    sel_color_id = $(this).attr("id");
    $(".add_prod input[name=product_color]").val(sel_color_id);
    
    $.ajax({
      async: "false",
      type: "GET",
      url: _http_path + '/index.php?ajax=1&prodcount=1&all=1&prodid=' + $("input[name=product_id]").val() + '&color=' + $("input[name=product_color]").val(),
      success: function(msg) {
        $("input[name=product_count]").val(msg*1);
      }
    });
  });

  function autoSelColor() {
    var idd = ".color_block_ li #" + sel_color_id + " > div";
    var idd2 = "#" + sel_color_id + " > div";
    $(idd2).addClass(border_sel_);
    $(".add_prod input[name=product_color]").val(sel_color_id);
    
    $.ajax({
      async: "false",
      type: "GET",
      url: _http_path + '/index.php?ajax=1&prodcount=1&all=1&prodid=' + $(".add_prod input[name=product_id]").val() + '&color=' + $(".add_prod input[name=product_color]").val(),
      success: function(msg) {
        $(".add_prod input[name=product_count]").val(msg*1);
      }
    });
  }
  

  $('input[name=quantity]').keypress(function(e) {
    code = (e.keyCode ? e.keyCode : e.which);
    
    //alert($(this).parent().children("input[name=product_color]").val());
    
    if( (code > 57  || code < 48) && code != 8 && code != 37 && code != 39 && code != 35 && code != 36 && code != 46 && code != 13) {
      e.preventDefault();
    }
    else if( code == 13 ) {
       calculate3($(this));
    }
    
  });
  $('input[name=product_count_dummy]').keypress(function(e) {
    code = (e.keyCode ? e.keyCode : e.which);
    if( (code > 57  || code < 48) && code != 8 && code != 37 && code != 39 && code != 35 && code != 36 && code != 46 && code != 13)
      e.preventDefault();
  });
  
  if( $('.color_block_').is('ul') )
    autoSelColor();

  $("input[name=deliveryPrice]").change(function() {
    var value = $(this).attr("id")=="RadioGroup1_2" ? 0 : $(this).attr("value");
    $("input[name=delivery]").attr("value", value);
    cart_sum();
  });
});
