

var boxWidth  = 300;
var boxHeight = 300;
var openBoxLinkId = "OPEN_BOX";

var visibleItemsInBasketPreview  = 5;
var isFold = true;

function folding(event){
    event.preventDefault();

    if($(".cart .products_incart ol li:hidden").length > 0){
       $(".cart .products_incart ol li:hidden").show();
       isFold = false;
       $(this).html("Zwiń");
    }else{
        $(".cart .products_incart ol li:gt("+(visibleItemsInBasketPreview-1)+")").hide();
        isFold = true;
        $(this).html("Rozwiń");
    }
}

function folding2(event){
    event.preventDefault();

    if(window.top.$(".cart .products_incart ol li:hidden").length > 0){
       window.top.$(".cart .products_incart ol li:hidden").show();
       isFold = false;
       $(this).html("Zwiń");
    }else{
        window.top.$(".cart .products_incart ol li:gt("+(visibleItemsInBasketPreview-1)+")").hide();
        isFold = true;
        $(this).html("Rozwiń");
    }
}

function activateAddForms(){
    $("form.add_to_basket").submit(function(event){
        event.preventDefault();
        
        var product  = $("input#product").val();
        if($(this).children("input[@name = hasFeatures]").val() == 0){
            addToBasket(product);
        }else{            
            var quantity = $(this).find("#quantity").val();                        
            if($(this).children("#"+openBoxLinkId).length > 0){
               $(this).children("#"+openBoxLinkId).remove();
            }
            
            var url = $(this).attr("action");
            if(quantity != "" && quantity > 0)
                url += "/quantity/" + quantity;

            if($('input[name = "inbox"]').length > 0){
                if($('input[name = "inbox"]:checked').val() == 1){
                    url += "/inbox/1/";
                }else{
                    url += "/inbox/0/";
                }
            }
            console.log(url);
            var aTag = $("<a></a>");
            aTag.attr("href",url)
                .addClass("fancybox")
                .addClass("iframe")
                .addClass("add_to_cart")
                .attr('id',openBoxLinkId)
                .css("display","none");
            $(this).append(aTag);

            initAddToBasketLink();

            $(this).children("#"+openBoxLinkId).click();
        }
        
    });
}



function closeAddBox(){
    parent.$.fancybox.close();
}

function addItemToPreviewBasket(product_name,product_id,quantity,price){
    if(window.top.$(".cart .products_incart ol").length  == 0){
        window.top.$(".cart .products_incart").append("<ol></ol>");
        var html =
            "<div class='actions'>" +
                "<a href='#'>Rozwiń</a>" +
                "<form action='" + siteUrl + "basket/show/' >" +
                "<p><input type='submit' value='Zamawiam' /></p>" +
                "</form>" +
            "</div>";
        window.top.$(".cart .products_incart").after(html);
    }

    var item = $("<li></li>");
    var link = $("<a></a>")
               .attr("href",siteUrl + "product/card/id/" + product_id)
               .html(product_name);
    item.append(link);
    window.top.$(".cart .products_incart ol").append(item);

    // dodanie linka Rozwijania jesli jest to koniecznie
    if(window.top.$(".cart .products_incart ol li").length > visibleItemsInBasketPreview
       && window.top.$(".cart .actions a").length == 0){
       
       var a_tag = $("<a></a>")
                   .attr("href","#") 
                   .html("Rozwiń")                   
                   .click(folding2);
       window.top.$(".cart .actions").prepend(a_tag);
    }

    if(isFold)
        window.top.$(".cart .products_incart ol li:gt("+(visibleItemsInBasketPreview-1)+")").hide();

    window.top.$(".cart > ul li:last span").html(price);
    window.top.$(".cart > ul li:first span").html(quantity);
}

function addToBasket(product_id){
    var url = siteUrl + "basket/add/";
    $.post(url, {'product':product_id,'quantity':1}, function(response){        
        if(response.status == 0){
            product_name = response.product_name;
            price = response.price;
            quantity = response.quantity;

            if($(".cart .products_incart ol").length  == 0){
                $(".cart .products_incart").append("<ol></ol>");
                var html =
                    "<div class='actions'>" +
                        "<a href='#'>Rozwiń</a>" +
                        "<form action='" + siteUrl + "basket/show/' >" +
                        "<p><input type='submit' value='Zamawiam' /></p>" +
                        "</form>" +
                    "</div>";
                $(".cart .products_incart").after(html);
            }

            var item = $("<li></li>");
            item.html(product_name);
            $(".cart .products_incart ol").append(item);
            if(isFold)
                $(".cart .products_incart ol li:gt("+(visibleItemsInBasketPreview-1)+")").hide();

            $(".cart > ul li:last span").html(price);
            $(".cart > ul li:first span").html(quantity);

            displayMessage(response.msg);
        }else{            
            displayMessage(response.msg);
        }
    }, 'json');
}


$(document).ready(function(){
    activateAddForms();

    $(".cart .actions a").click(folding);



});
