DY = window.DY || {};

DY.buildOptions = function (json) {
    html = '<option value="0">...</option>';
    for (x = 0; x < json.data.length; x++) {
        html += '<option value="' + json.data[x].id + '">' + json.data[x].name + '</option>';
    }
    return html;
};
DY.boxProdDimension = function () {
    $('#box-prodotti').css({
        'float':'left',
        'width':'70%'
    });
    $('#box-dx').css({
        'width':'27%'
    });
};
DY.redimensionProdDXMain = function () {
    //rimuovo il min-height utilizzato
    $('#box-prodotti,#box-dx').css('min-height', '300px');

    //calcoli sulle altezze degli elementi

    main = $('#box-prodotti').outerHeight(true);
    dx = $('#box-dx').outerHeight(true);

    if (main - dx > 0) {
        $('#box-dx').css('min-height', main + 'px');
    } else {
        $('#box-prodotti').css('min-height', dx + 'px');
    }

};

DY.toggleProductDetails = function (ele) {
    $('.' + $(ele).attr('rel')).slideToggle();
};

DY.closeFancy = function () {
    $.fancybox.close();
};

$(document).ready(function () {

    $('.cssMenu').addClass('sf-menu');
    $('.cssMenu *').attr('style', '');
    $('.cssMenu *').attr('class', '');

    $('.cssMenu ul').each(function () {
        if ($(this).find('li').length == 0) {
            $(this).remove();
        }
    });

    $("ul.sf-menu").supersubs({
        minWidth:9.5, // minimum width of sub-menus in em units
        maxWidth:27, // maximum width of sub-menus in em units
        extraWidth:1     // extra width can ensure lines don't sometimes turn over
        // due to slight rounding differences and font-family
    }).superfish();

    $('#sceltaLingua select').change(function () {
        $('#sceltaLingua').submit();
    });


    $('#fissi a').first().css('border', 'none');

    if ($.ui) {
        $('.tabsProd>#tabs').tabs({
            show:DY.redimensionProdDXMain
        });

        //disabilit i tabs che sono vuoti
        DY.arrDisabl = [];
        $('.tabsProd>#tabs .ui-tabs-nav a').each(function (index) {

            if ($(this.hash).length == 0 || $(this.hash).children().length == 0) {
                DY.arrDisabl.push(index);
            }
        });
        $('.tabsProd>#tabs').tabs("option", "disabled", DY.arrDisabl);
    }

    //posizionamento dell'immagine che segue'
    if ($('.imageProd').length > 0) {
        position = $('.imageProd').position();
        altezzaTolgo = $('.tabsProd').height() - 45;
        $('.imageProd').css({
            top:(position.top - altezzaTolgo) + 'px',
            left:position.left + 'px',
            position:'fixed',
            width:$('.imageProd').width()
        });
    }

    //selezione stato e ricerca di regioni
    $('#registrati select[name*="stato"],#registrati select[name*="statoazienda"]').change(function () {
        $('#elencoRegioni').slideUp();
        $('#elencoRegioni select').html('');
        val = this.options[this.selectedIndex].value;
        $.ajax({
            dataType:'json',
            data:{
                id:val
            },
            type:'POST',
            success:function (data) {
                if (data.success) {
                    $('#elencoRegioni select').html(DY.buildOptions(data));
                    $('#elencoRegioni').slideDown();
                }
            },
            url:'elencoRegioni.php'
        });
    });

    //gestione tabella dettagli

    $('.boxProdDett td,#tabdettagli td').attr('width', '');
    $('.boxProdDett tr,#tabdettagli tr').each(function () {
        $(this).find('td').first().addClass('nomeDettaglio');
        $(this).find('td').last().addClass('valoreDettaglio');
    });
    //registrazione
    $('.formLoginRow:nth-child(2n+1)').css({
        'border':'none'
    });

    $('.registrazione tr').each(function () {
        $(this).find('td:first-child').addClass('labelForms').attr('width', '40%');
        $(this).find('td').last().attr('width', '60%').css({
            'textAlign':'right'
        });
        $(this).find('input,select').addClass('inputColor').attr('size', '').css({
            'width':'100%',
            'float':'left',
            'fontSize':'130%'
        });

    });
    $('.registrazione input[name="code"]').css({
        'width':'100px',
        'float':'left',
        'marginTop':'10px'
    });

    //apertura blocchi dei prodotti
    $('.btnOpen').click(function (e) {
        DY.toggleProductDetails(this);
    });

    //gestione immagine del prodotto
    $('.imageProd a').each(function () {
        $(this).attr('href', $(this).children('img').attr('src'));
    });

    $('#reloadChapta').click(function () {
        var img = $(this).siblings('img'), now = new Date();
        src = img.attr('src').replace(/&dc=[0-9]+$/, '');
        img.attr('src', src + '&dc=' + now.getTime())
    });

    //cancellazione dal footer di alcuni elementi
    $('.elencoFooter>li>a[href*="idp=29"],.elencoFooter>li>a[href*="idp=116"]').parent().remove();


    if ($.fancybox) {
        $("a[rel='lightbox']").fancybox();
        $('.fancyboxProds').fancybox();
    }

    //bocco tasto destro sulle immagini
    $('img').live('contextmenu mousedown', function (e) {
        e.preventDefault();
    });


    //popup per home
    if ($('#boxPopUpHome').length > 0) {
        if ($('#boxPopUpHome').html() != '' && !document.location.toString().match(/home\.php/)) {
            $.fancybox(
                $('#boxPopUpHome').html(),
                {
                    'autoDimensions':false,
                    'width':'auto',
                    'height':'auto',
                    'transitionIn':'none',
                    'transitionOut':'none',
                    'onComplete':function () {
                        var timeoutID = window.setTimeout(DY.closeFancy, 5000);
                    }
                }
            );
        }
    }

    DY.stateOpenDet = true;
    $('#showAllDett').click(function () {
        $('.btnOpen').each(function () {
            if (DY.stateOpenDet) {
                $('.' + $(this).attr('rel')).slideDown();
            } else {
                $('.' + $(this).attr('rel')).slideUp();
            }
        });
        DY.stateOpenDet = !DY.stateOpenDet;
    });

    $('#printProductList').click(function () {
        window.print();
    });

    $('#topLogin a').click(function () {
        $('#topLogin .formblk').slideToggle('slow');
    });


    //settaggio pagina di destinazione per OEM
    var server = location.hostname;
    var rubinc = 143;
    var rubvalves = 136;
    var href = '';
    if (server.match(/rubinc/)) {
        href = 'pagina.php?idp=' + rubinc;
    }
    if (server.match(/rubvalves/)) {
        href = 'pagina.php?idp=' + rubvalves;
    }
    if (href != '') {
        $('a[href="OEM"]').attr('href', href);
    }
});


//allineamento blocchi contenuti e di sx
$(window).load(function () {

    if ($('#box-main').length > 0) {
        main = $('#box-main').outerHeight();
        dx = $('#box-dx').outerHeight();
        if (main - dx > 0) {
            $('#box-dx').css('min-height', main + 'px');
        } else {
            $('#box-main').css('min-height', dx + 'px');
        }
    }
    if ($('#box-prodotti').length > 0) {
        DY.redimensionProdDXMain();
    }
    if ($('#homeVideo').length > 0) {
        main = $('#homeVideo').outerHeight();
        dx = $('#homeforms').outerHeight();
        if (main - dx > 0) {
            $('#homeforms').css('min-height', main + 'px');
        } else {
            $('#homeVideo').css('min-height', dx + 'px');
        }
    }
    //dimensione in home cat e search
    if ($('#box-fasciaProdSearch').length > 0) {
        height = $('#box-fasciaProdSearch').height();
        $('.ricercaProdotti').height(height);
        $('#box-homeprod').height(height - 20);

        main = $('#homeVideo').outerHeight();
        dx = $('#homeforms').outerHeight();
        if (main - dx > 0) {
            $('#homeforms').css('min-height', main + 'px');
        } else {
            $('#homeVideo').css('min-height', dx + 'px');
        }
    }

});


