$(document).ready(function() {

    $(".item-selecao select").change(function(){
        var orderitem = $(this).parents("div.item-selecao").index(),
            valor = $(this).val();
        $(".item-hidden").each(function(){
            var indexCorreto = ($(this).index() - 7);
            if (orderitem == indexCorreto){
		//var url = 'www.arquivocontemporaneo.com.br/selection/setDim';
		var url = 'selection/setDim';
		var pid = $(this).children('.sets').val();
		$.post(url, {id: pid, dim: valor});
               $(this).children(".dimensao").val(valor);
	    }
        });

    });

     $(".item-selecao-qtd").keyup(function(){
         var orderitem = $(this).parents("div.item-selecao").index(),
             valor = $(this).val();
         $(".item-hidden").each(function(){
             var indexCorreto = ($(this).index() - 7);
             if (orderitem == indexCorreto){
		 //var url = 'http://www.arquivocontemporaneo.com.br/selection/setQtd/';
		 var url = 'selection/setQtd';
		 var pid = $(this).children('.sets').val();
		 $.post(url, {id: pid, qtd: valor});
                 $(this).children(".qtd").val(valor);
	     }
         });
     });

     //Locacao
     $(".item-selecao-qtd-locacao").keyup(function(){
         var orderitem = $(this).parents("div.item-selecao-locacao").index(),
             valor = $(this).val();
             //alert("Valor: "+valor);
         $(".item-hidden").each(function(){
	     //alert ('orderitem: '+orderitem+' index: '+$(this).index());
             var indexCorreto = ($(this).index() - 11);
             if (orderitem == indexCorreto)
                 $(this).children(".qtd").val(valor);
         });
     });
     $(".item-selecao-locacao .item-selecao-dimensao").change(function(){
        var orderitem = $(this).parents("div.item-selecao-locacao").index(),
            valor = $(this).val();
        $(".item-hidden").each(function(){
	    //alert ('orderitem: '+orderitem+' index: '+$(this).index());
            var indexCorreto = ($(this).index() - 11);
            if (orderitem == indexCorreto)
               $(this).children(".dimensao").val(valor);
        });

    });
    $(".item-selecao-locacao .item-selecao-acabamento").change(function(){
        var orderitem = $(this).parents("div.item-selecao-locacao").index(),
            valor = $(this).val();
        $(".item-hidden").each(function(){
	    //alert ('orderitem: '+orderitem+' index: '+$(this).index());
            var indexCorreto = ($(this).index() - 11);
            if (orderitem == indexCorreto)
               $(this).children(".acabamento").val(valor);
        });

    });


    $("#printSelection").click(function(){
        window.print();
    });

    $(".submit").click(function(){
        var id = $(this).attr("id");
        var ser = $("#form_"+id).serialize();  //produced above response
        if ($(this).hasClass('select')){
            $("#"+id).addClass("loading").removeClass("select");
            $.post($("#form_"+id).attr("action"), ser, function(){
                $("#cartqtd").html(($("#cartqtd").html()*1)+1);
                $("#"+id).addClass("active").removeClass("loading");
            });
            return false;
        } else {
            $("#"+id).addClass("loading").removeClass("active");
            $.post($("#form_"+id).attr("action"), ser,
            function() {
                $("#cartqtd").html(($("#cartqtd").html()*1)-1);
                $("#"+id).addClass("select").removeClass("loading");
            });
            return false;
        }
    });

    $(".submit3").click(function(){
        var id = $(this).attr("id");
        var ser = $("#form_"+id).serialize();  //produced above response
        if ($(this).hasClass('select3')){
            $("#"+id).addClass("loading3").removeClass("select3");
            $.post($("#form_"+id).attr("action"), ser, function(){
                $("#cartqtd").html(($("#cartqtd").html()*1)+1);
                $("#"+id).addClass("active3").removeClass("loading3");
            });
            return false;
        } else {
            $("#"+id).addClass("loading3").removeClass("active3");
            $.post($("#form_"+id).attr("action3"), ser,
            function() {
                $("#cartqtd").html(($("#cartqtd").html()*1)-1);
                $("#"+id).addClass("select3").removeClass("loading3");
            });
            return false;
        }
    });

    $(".eliminar-produto-selecao").click(function(){
       var id =  $(this).attr("id");
       var ser = $("#form_"+id).serialize();  //produced above response
       $.post($("#form_"+id).attr("action"), ser, function(){
            $("#cartqtd").html(($("#cartqtd").html()*1)-1);
            $("#li_"+id).parents("div.item-selecao").hide('fast', function(){
                $(this).remove(); // Remove a div do produto! *-* So nice
                $("#hidden_"+id).remove();
            });
        });
        return false;
    });

    $(".eliminar-produto-selecao-locacao").click(function(){
       var id =  $(this).attr("id");
       var ser = $("#form_"+id).serialize();  //produced above response
       $.post($("#form_"+id).attr("action"), ser, function(){
            $("#cartqtd").html(($("#cartqtd").html()*1)-1);
            $("#li_"+id).parents("div.item-selecao-locacao").hide('fast', function(){
                $(this).remove(); // Remove a div do produto! *-* So nice
                $("#hidden_"+id).remove();
            });
        });
        return false;
    });
});
