﻿
// slideshow immagini

var slideshowCurrent = 0;
var slideshowTime = 1400;
var slideshowTimeout = 4000;

var startSlideshow = function (containerSelector) {

    var imgs = $(containerSelector).find('img');
    var imgsCount = imgs.length;

    // nascondo la corrente
    imgs.eq(slideshowCurrent).animate({ opacity: 0 }, slideshowTime);

    // aggiorno l'indice
    slideshowCurrent = (slideshowCurrent + 1) % imgsCount;

    // mostro la prossima
    imgs.eq(slideshowCurrent).animate({ opacity: 1 }, slideshowTime);

    // richiamo la funzione
    setTimeout(function () { startSlideshow(containerSelector); }, slideshowTimeout);

}

var topSlideshow = function (photoList, containerSelector) {

    var alreadyStarted = false;

    for (var i = 0; i < photoList.length; i++) {
        $('<img />').load(function () {
            $(containerSelector).append($(this));
            if (!alreadyStarted) {
                alreadyStarted = true;
                // richiamo la funzione solo la prima volta, le volte successive viene chiamata da se stessa
                setTimeout(function () { startSlideshow(containerSelector); }, slideshowTimeout);
            }
        }).attr('src', photoList[i]).css('opacity', 0);
    }

}

$(document).load(function () {

    // gestione messaggi nascosti
    $('.message-wait').delay(2500).fadeOut(800);

});

$(document).ready(function () {

    // gestione tabelle obbligazioni
    $('.table-obbligazioni tr:odd').addClass('odd');

    // gestione stelle votazioni
    $('#stars li').click(function () {
        var stars = $('#stars li');
        var b = true;
        var num = 1;
        var i = 0;
        for (i = 0; i < stars.length; i++) {
            if (b) {
                $(stars[i]).addClass('starred');
            }
            else {
                $(stars[i]).removeClass('starred');
            }
            if ($(this).attr('id') == $(stars[i]).attr('id')) {
                b = false;
                num = i + 1;
            }
        }
        $('#rating').attr('value', num);
    });

    // gestione menù
    $('#subnav span.closed, #subnav span.open').click(function (event) {
        event.preventDefault();
        event.stopPropagation();

        // cerco parnet li e submenu
        var p = $(this).parent().parent().parent();
        var sub = p.find('ul:first');

        // rimuovo classi
        $(this).removeClass('closed').removeClass('open');

        // controllo lo stato attuale
        if (sub.css('display') == 'block') {
            // chiudo submenu
            sub.css('display', 'none');
            $(this).addClass('closed');
        }
        else {
            // apro submenu
            sub.css('display', 'block');
            $(this).addClass('open');
        }

    });

    // voce corrente
    $('#subnav .current').addClass('last-current-voice');
    $('#subnav .current').parents('li').addClass('current');
    $('#subnav .current > a > .text > .closed').removeClass('closed').addClass('open');
    $('#subnav .current > ul').css('display', 'block');

    // prodotti accessori, fix height
    var countAccessori = $('.prodotti-accessori ul').length;
    var countAccessoriItems = $('.prodotti-accessori ul li').length;

    // controllo sia divisibile per 3 per il corretto funzionamento
    // oppure che siano solo 2 elementi
    if (countAccessori % 3 == 0 || countAccessoriItems == 2) {

        for (var k = 0; k < countAccessori; k = k + 3) {
            var countRow = 0;
            // conto i li massimi
            if ($('.prodotti-accessori ul:eq(' + (k) + ') li').length > countRow) {
                countRow = $('.prodotti-accessori ul:eq(' + (k) + ') li').length;
            }
            if ($('.prodotti-accessori ul:eq(' + (k + 1) + ') li').length > countRow) {
                countRow = $('.prodotti-accessori ul:eq(' + (k + 1) + ') li').length;
            }
            if ($('.prodotti-accessori ul:eq(' + (k + 2) + ') li').length > countRow) {
                countRow = $('.prodotti-accessori ul:eq(' + (k + 2) + ') li').length;
            }
            // adeguo le altezze
            for (var i = 0; i < countRow; i++) {
                var mheight = 0;
                var lis = $('.prodotti-accessori ul:eq(' + (k) + ') li:eq(' + i + '), .prodotti-accessori ul:eq(' + (k + 1) + ') li:eq(' + i + '), .prodotti-accessori ul:eq(' + (k + 2) + ') li:eq(' + i + ')');
                lis.each(function () {
                    var objs = $(this);
                    objs.each(function () {
                        mheight = $(this).height() > mheight ? $(this).height() : mheight;
                    });
                });
                lis.height(mheight);
            }
        }

    }

    // gestione faq
    $('.faq-text').each(function () {
        // fix height
        var obj = $(this);
        obj.css('display', 'block').height(obj.height()).hide();
    });
    $('.faq-title h3').addClass('closed');
    $('.faq-title:first h3').removeClass('closed');
    $('.faq-title:first').next().stop(true).slideDown('slow');
    $('.faq-title').click(function () {
        var obj = $(this);
        $('.faq-title h3').addClass('closed');
        obj.find('h3').removeClass('closed');
        $('.faq-text:visible').slideUp('slow');
        obj.next().stop(true).slideDown('slow');
    });

    // link master chart
    $('#footer a[href*="masterchart"]').click(function (ev) {
        ev.preventDefault();
        window.open($(this).attr('href'));
    });

    // link info
    $('.vedi-anche ul li a').each(function () {
        var obj = $(this);
        // aggiunta link trasparenza a box trasparenza ed info, info viene anche tradotto in trasparenza
        // i nuovi già sono tradotti in trasprenza e possiedono la classe, collego solo il link
        /*if (obj.text().toLowerCase() == 'info' || obj.text().toLowerCase() == 'trasparenza') {
            if (obj.attr('href').length < 5) {
                var traspLink = "http://pergine.masterchart.it/index.asp?nodeid=0";
                obj.click(function (ev) {
                    ev.preventDefault();
                    window.open(traspLink);
                });
                obj.attr('href', traspLink);
            }
            else {
                obj.click(function (ev) {
                    ev.preventDefault();
                    window.open(obj.attr('href'));
                });
            }
            obj.text('Trasparenza');
        }*/
        // traduzione box pdf in scheda prodotto e messo al primo posto
        // i nuovi già sono tradotti ed al primo posto
        //else
        if (obj.text().toLowerCase() == 'pdf') {
            var pUl = obj.parent().parent();
            var pLi = obj.parent();
            obj.text('Scheda prodotto');
            pLi.remove();
            pLi.prependTo(pUl).addClass('scheda-prodotto');
        }
    });

    // fogli informativi
    $('a[href*="web.cr-pergine.net"]').click(function (ev) {
        ev.preventDefault();
        ShowPdf(null, null, $(this).attr('href'));
    });

    // print button
    $('.printButtons a').click(function (ev) {
        ev.preventDefault();
        window.print();
    });

});

