$(document).ready(function() {
    /* Zoom */
    initZoom();

    /* Ajouter au panier */
    $("#addCart").click(function(){
        var t=$(this);
        if($("#input_complement").size()==0){
            $.ajax({type: "POST",
                       url: AJAX_PATH + "addproduct.ajax.php",
                       data: {type: "cart", id: $("#product_id").text()},
                        success:function(){
                            top.location = URL_PATH + 'panier/';
                        }
            });
        } else {
            if($("#input_complement").val()==""){
               $("#balloon").attr("style", "display:block;");
            }else{
                $.ajax({type: "POST",
                       url: AJAX_PATH + "addproduct.ajax.php",
                       data: {type: "cart", id: $("#product_id").text(), id_complement:$("#input_complement").val()},
                        success:function(){
                            top.location = URL_PATH + 'panier/';
                        }
                });
            }
        }
    });

    /* Ajouter ŕ la wishlist */
    $("#addWl").click(function() {
        var t = $(this);
        var product_id = $("#product_id").text();
        var user_id = session['user']['id'];
        var url = AJAX_PATH + "addproduct.ajax.php";
        var data = {type: "wishlist", id: product_id};
        if(!user_id) {
            tb_init_login("ajax", url, data);
        } else {
            $.ajax({type: "POST",
               url: url,
               data: data,
                success:function(){
                    t.find("img").attr("src", IMG_PATH + "icones/croix_ok.png");
                    t.find("span").html("Ajouté ŕ ma wishlist");
                }
            });
        }
    });

    /* Compléments */
    $(".valide").click(function () {
        var t = $(this);
        var tab = t.attr("id").split("_");
        var id_complement = tab[1];
        $.ajax({type: "POST",
               url: AJAX_PATH+"productcomplement.ajax.php",
               data:"id="+id_complement,
               success:function(data){
                    $("#taille").html(data);
                    $("input[name='complement']").val(id_complement);
               }
        });
        $("#balloon").attr("style", "display:none;");
        $(".complement").removeClass("blue");
        t.addClass("blue");
    });

    /* Commentaires */
    $("#post").click(function(){
        $("#error-com").slideUp("fast");
        var comment = trim($(".add_comment").val());
        var nbcomments = parseInt($("#nbcomments").html());
        var id_user = session['user']['id'];
        var url = AJAX_PATH + "productcomment.ajax.php";
        var data = {action: "addcomment", id: $("#product_id").text(), id_user: id_user, comment: comment};

        if(comment != "") {
            if(!id_user) {
                tb_init_login("ajax", url, data);
            } else {
                $.ajax({type: "POST",
                       url: url,
                       data: data,
                       success: function(data){
                           $(".add_comment").val("");
                           $("#nbcomments").html(nbcomments+1);
                           $('#listing_comments').prepend(data);
                           $('#listing_comments').find(".comment:first").slideDown("fast");
                           $(".comment_delete").click(function(){
                                deleteComment($(this));
                           });
                       }
                });
            }
        } else {
            $("#error-com").html("Votre commentaire est vide");
            $("#error-com").slideDown("fast");
        }
    });

    $(".comment_delete").click(function(){
        deleteComment($(this));
    });

    $(".pager a").attr("href", "#poster");
    $(".pager a").each(function() {
    var t = $(this);
	t.click(function() {

    var page=$(this).text( );
	$.ajax({ type: "POST",
                 url: AJAX_PATH+"productcomment.ajax.php",
                 data:"action=pager&page="+page+"&id="+$("#product_id").text(),
                 beforeSend:function(){
                     $('#listing_comments').fadeOut("fast");
                      $('#listing_comments').html("<img src='"+IMG_PATH+"preloader.gif' alt='Chargement...' />")
                 }, success:function(data){
                     $('.pageron').removeClass("pageron");
                     t.parent().addClass("pageron");
                     $('#listing_comments').html(data, function(){});
                     $('#listing_comments').fadeIn("slow");
                     $(".comment_delete").click(function(){
                        deleteComment($(this));
                     });
                 }});

        });
    });

    /* Description d'un produit */
    $(".onglets div").click(function() {
        name = $(this).text().toLowerCase();
        $(".box-produit").hide();
        $("#box-" + name).slideDown("fast");
        $(".onglets div").attr("class", "off")
        $(this).attr("class", "on");
    });
});

/* Chargement des images */
function changePict(id) {
    $.ajax({type: "POST",
       url: AJAX_PATH + "photo.ajax.php",
       data: {id: id},
        success:function(data){
            $("#big").html(data);
            initZoom();
        }
    });
}

/* Zoom */
function initZoom() {
    var options = {
            zoomWidth: 200,
            zoomHeight: 200,
            xOffset: 20,
            title: false,
            lens:false,
            showEffect:'show',
            hideEffect:'fadeout',
            fadeoutSpeed: 'slow'
        };
        $('.jqzoom').jqzoom(options);
}

function deleteComment(t) {
     var nbcomments = parseInt($("#nbcomments").html());
        var tab_id_commentaire = t.parents("div").attr("id").split("_");
        id_commentaire = tab_id_commentaire[1];
        $.ajax({type: "POST",
                   url: AJAX_PATH+"productcomment.ajax.php",
                   data:"action=delcomment&id="+$("#product_id").text()+"&id_commentaire="+id_commentaire,
                   success:function(){
                       $("#nbcomments").html(nbcomments-1);
                       t.parents(".comment").slideUp("fast");
                   }
            });
}

function trim (myString){
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}