$(document).ready(function(){
    $("#sizeList li").click(function() {
        $("#sizeList li").removeClass('selected');
        $(this).addClass('selected');
    });
    
    $(".otherPics img").mouseover(function() {
        $(".mainPic img").attr('src', $(this).attr('src').replace('thumb', 'detail'));
        $(".mainPic a").attr('href', $(this).attr('src').replace('thumb', 'zoom'));
        $('.cloud-zoom').CloudZoom(); // réinitialisation du plugin CloodZoom
    });
    
    $("#addToCartBt").click(function() {
        var pid = $("#pid").val();
        var tid = 0;
        var tnom;
        var type = $("#type").val();
        
        if ($("#istid").val() == "1") {
            $("#sizeList li").each(function() {
                if ($(this).hasClass("selected")) {
                    tid = $(this).attr("id").replace("t", "");
                    tnom = $(this).html();
                    return false;
                }
            });
                        
            if (!tid) { $(".addCartInfo").html("Veuillez sélectionner une taille !"); $(".addCartInfo").css("display", "block"); return false; }
            else      { $(".addCartInfo").css("display", "none"); }
        }
        
        var msg = $.ajax({ type: "POST",
                           url: "ajax/cart/check_stock_level.php",
                           async: false,
                           data: { pid: pid, tid: tid } }).responseText;
                
        if (msg.indexOf("OK") != -1) {
            $(".addCartInfo").css("display", "none");
        } else {
            if (tnom) {
                $(".addCartInfo").html("Stock insuffisant (ou avez ajouté la quantité max. pour la taille "+tnom+")");
            } else {
                $(".addCartInfo").html("Stock insuffisant (ou vous avez ajouté la quantité max.)");
            }
            $(".addCartInfo").css("display", "block");
            return false;
        }
        
        var info = $.ajax({ type: "POST",
                            url: "ajax/cart/add.php",
                            async: false,
                            data: { pid: pid, tid: tid, catnom: $(".details .category").html(), type: type } }).responseText;
                                    
        var nbart = parseInt($("#cartcount").html());
        nbart = nbart + 1;
        $("#cartcount").html(nbart); // maj count panier header
        
        popCartConfirm(info);
    });
});

function popCartConfirm(html)
{
    var offset = getScrollHeight() + 150;
		
	$("#cartpop .body").html(html);
	$("#cartpop").css('top', offset+'px');
	
	$("#overlay").css('height', document.body.offsetHeight+"px");
	
	//$("#cartpop, #overlay").css('display', 'block');
	
	if ($.browser.msie)
        $("#overlay").show();
    else
        $("#overlay").fadeIn();
        
    $("#cartpop").fadeIn();
}

function closeCartConfirm()
{
    $("#sizeList li").removeClass('selected');
    //$("#cartpop, #overlay").css('display', 'none');
    
    if ($.browser.msie)
        $("#overlay").hide();
    else
        $("#overlay").fadeOut();
        
    $("#cartpop").fadeOut();
}

function popSizeGuide(pid)
{
    if ($("#sizeguide").html().length > 0) {
        var guide = $("#sizeguide").html();
    } else {
        var guide = $.ajax({ type: "POST",
                             url: "ajax/article/size_guide.php",
                             async: false,
                             data: { pid: pid } }).responseText;
    }
                            
    var offset = getScrollHeight() + 150;
    
    $("#sizeguide").html(guide);
	$("#sizeguide").css('top', offset+'px');
	
	$("#overlay").css('height', document.body.offsetHeight+"px");
	$("#overlay").bind('click', function() { closeSizeGuide(); });
	
	$("#sizeguide, #overlay").css('display', 'block');
}

function closeSizeGuide()
{
    $("#overlay").bind('click'); 
    $("#sizeguide, #overlay").css('display', 'none');
}

function getScrollHeight()
{
	var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
	return h ? h : 0;
}
