
$(document).ready(
    function() {
        setListener();
        setListenerProducts();
        //$(document).pngFix();
        //
        //getProductsList(sFilterBrand, aFilterAllergens, sFilterVariety, sFilterCategory);
    });

    var bNextLabel = true;
    var sCurrentLabelOpen = "";
    var sFilterVariety = "";
    var sFilterCategory = "";
    var sFilterBrand = "";
    var aFilterAllergens = new Array();
    var nMinHeigth = 315; // Altezza minima

    function setListener() {
        $('.radio_filter_brand').bind('click', function() {
            var aBrand = $('.radio_filter_brand');
            for (i = 0; i < aBrand.length; i++) {
                myRadio = aBrand[i];
                if ($(myRadio).attr('checked') == true) {
                    var sId = $(myRadio).attr('id');
                    sFilterBrand = sId.replace('brand_', '');
                }
            }
            //
            $('#productDetail').html('');
            $('html, body').animate({ scrollTop: 0 }, 'slow');  
            //
            getProductsList(sFilterBrand, aFilterAllergens, sFilterVariety, sFilterCategory);
        });

        $('.checkbox_filter_allergens').bind('click', function() {
            var aAllergens = $('.checkbox_filter_allergens');
            aFilterAllergens = new Array();
            iFilter = 0;
            for (i = 0; i < aAllergens.length; i++) {
                myCheckbox = aAllergens[i];
                if ($(myCheckbox).attr('checked') == true) {
                    var sId = $(myCheckbox).attr('id');
                    sId = sId.replace('allerg_', '');
                    aFilterAllergens[iFilter] = sId;
                    iFilter++;                  
                }
            }
            //
            $('#productDetail').html('');
            $('html, body').animate({ scrollTop: 0 }, 'slow');  
            //
            getProductsList(sFilterBrand, aFilterAllergens, sFilterVariety, sFilterCategory);
        });

        $('.variety').bind('click', function() {

            $('#variety_' + sFilterVariety).parent().attr("class", "unsel");
            sId = $(this).attr('id');
            sContent = $(this).parent();
            $(sContent).attr("class", "sel");
            sId = sId.replace('variety_', '');
            sFilterVariety = sId;
            //
            $('#productDetail').html('');
            $('html, body').animate({ scrollTop: 0 }, 'slow');
            //
            getProductsList(sFilterBrand, aFilterAllergens, sFilterVariety, sFilterCategory);
        });

        $('.category').bind('click', function() {

            $('#category_' + sFilterCategory).parent().attr("class", "unsel");
            sId = $(this).attr('id');
            sContent = $(this).parent();
            $(sContent).attr("class", "sel");
            sId = sId.replace('category_', '');
            sFilterCategory = sId;
            //
            $('#productDetail').html('');
            $('html, body').animate({ scrollTop: 0 }, 'slow');
            //
            getProductsList(sFilterBrand, aFilterAllergens, sFilterVariety, sFilterCategory);
        });
        
    }

    function getProductsList(_FilterBrand, _aFilterAllergens, _FilterVariety, _FilterCategory) {
        href = "ajax/getProductsList.aspx";
        if (href) {

            var post_string = "";
            post_string += "id_variety=" + _FilterVariety;
            post_string += "&id_category=" + _FilterCategory;
            post_string += "&id_brand=" + _FilterBrand;
            post_string += "&allergens=";
            for (i = 0; i < _aFilterAllergens.length; i++) {
                post_string += _aFilterAllergens[i];
                if (i < _aFilterAllergens.length - 1)
                    post_string += ",";
            }

            $('#productList').load(href, post_string, function() {
                setListenerProducts();
            });                    
        }
    }

    function setListenerProducts() {
        $('.thumb_products').bind('click', function() {
            sTitle = $(this).attr('title');
            $(this).attr('href', "javascript:;");
            sId = $(this).attr('id');
            sId = sId.replace('thumb_products_', '');
            //
            getProductsDetails(sId, sTitle);
        });
    }

    function setListenerLabel() {
        
        var nHeight_Main = document.getElementById('detail_main').offsetHeight;
        var nHeight_Ingredients = document.getElementById('detail_ingredients').offsetHeight;
        var nHeight_Nutrizionali = document.getElementById('detail_nutrizionali').offsetHeight;
        var nHeight_Gda = document.getElementById('detail_gda').offsetHeight;

        if (nHeight_Main < nMinHeigth)
            nHeight_Main = nMinHeigth;

        if (nHeight_Ingredients < nMinHeigth)
            nHeight_Ingredients = nMinHeigth;

        if (nHeight_Nutrizionali < nMinHeigth)
            nHeight_Nutrizionali = nMinHeigth;

        if (nHeight_Gda < nMinHeigth)
            nHeight_Gda = nMinHeigth;
        
        $('#detail_ingredients').css({ height: '0px' });
        $('#detail_nutrizionali').css({ height: '0px' });
        $('#detail_gda').css({ height: '0px' });

        setOtherLabel();

        $('#detail_main').css({ height: nHeight_Main + 'px' });        
        $('#labelSel_main').css({ display: ('block') });

        $('.labelClick').bind('click', function() {
            if (bNextLabel) {
                setbNextLabel(false);
                var nHeight = 0;
                sId = $(this).attr('id');
                sId = sId.replace('labelClick_', '');

                //
                if (sId == 'main')
                    nHeight = nHeight_Main;
                else if (sId == 'ingredients')
                    nHeight = nHeight_Ingredients;
                else if (sId == 'nutrizionali')
                    nHeight = nHeight_Nutrizionali;
                else if (sId == 'gda')
                    nHeight = nHeight_Gda;

                $('#detail_' + sCurrentLabelOpen).animate({ height: ('0px') }, 600, function() {
                    $('#labelSel_' + sCurrentLabelOpen).css({ display: ('none') });
                });
                $('#labelSel_' + sId).css({ display: ('block') });
                $('#detail_' + sId).animate({ height: (nHeight + 'px') }, 600, function() {
                    sCurrentLabelOpen = sId;
                    setbNextLabel(true);
                });
            }
        });
    }
    
    function getProductsDetails(_id, _title) {
            href = "ajax/getProductDetail.aspx";
            if (href) {

                var post_string = "";
                post_string += "id=" + _id;

                $('#productDetail').load(href, post_string, function() {
                    sCurrentLabelOpen = "main";
                    setListenerLabel();
                    //
                    $('#briciole').html('<h2><a href="prodotti.aspx">Prodotti</a><span class="now"> / ' + _title + '</span></h2>');                    
                    $('html, body').animate({scrollTop:0}, 'slow');
                });
            }
        }

        function setbNextLabel(bValue) {
            bNextLabel = bValue;
        }
								      		        			         