// Abilitazione effetti
var enableEffects = false

// Variabili per determinare il player
var requiredMajorVersion = 8
var requiredMinorVersion = 0
var requiredRevision = 0
var lastElement
var intervalloView

// Va' all'indice

function toIndex() {
    window.location = "/"
}

function openpresentazione() {
    dimx = 1034
    dimy = 775
    dimx += 10
    dimy += 10
    var xMax = screen.width
    var yMax = screen.height
    var xOffset = 20
    var yOffset = 20
    opzioni = "scrollbars=yes,toolbar=no,width=" + dimx + ",height=" + dimy + ",screenX=" + xOffset + ",screenY=" + yOffset + ",top=" + yOffset + ",left=" + xOffset + ",scrollbars=no,status=no,menubar=no"
    fin = window.open("/docs/presentazione-sinfon-style", "", opzioni)
}

// Apertura di uno screenshot

function openscreenshot(url, w, h) {
    w += 4
    h += 4
    var l = Math.floor((screen.width - w) / 2)
    var t = Math.floor((screen.height - h) / 2)
    window.open("/php/foto.php?url=/images/screenshots/big/" + url, "", "width=" + w + ",height=" + h + ",top=" + t + ",left=" + l)
}

function getFlashCode(name) {
    alert('a')

    return
}

function showFlash(flashMovieName) {
    $('hdiv-homepage-video').innerHTML = getFlashCode(flashMovieName)
}

// Un menù viene cliccato!

function doLink(e) {
    Event.stop(e)
    lastElement = Event.element(e)
    if (lastElement.href.indexOf('?') > 0) {
        flashMovieName = lastElement.href.replace(/.*\?/i, "")
        showFlash(flashMovieName)

        // Potrebbe essere uno strong...
        if (!lastElement.href) {
            lastElement = $(lastElement).up()
            if (!lastElement.href) {
                lastElement = $(lastElement).up()
                if (!lastElement.href) {
                    lastElement = $(lastElement).up()
                }
            }
        }

        var links = $$('#left-panel ul li a')
        for (var i = 0; i < links.length; i++) {
            if (links[i] == lastElement) {
                links[i].addClassName('current')
            } else {
                links[i].removeClassName('current')
            }
        }
        if (enableEffects) {
            var effect = new Effect.Opacity('right-panel', {
                duration: 0.2,
                from: 1,
                to: 0.5,
                afterFinish: ajaxAfter
            })
        } else {
            ajaxAfter()
        }
    }
}

function ajaxAfter() {
    Element.update('right-panel', '<em>Caricamento in corso...</em>')
    var updater = new Ajax.Updater($('right-panel'), lastElement.href + '&ajax', {
        evalScripts: true,
        onComplete: showAfterAjax
    })
}

function showAfterAjax() {
    if (enableEffects) {
        new Effect.Opacity('right-panel', {
            from: 0.5,
            to: 1,
            duration: 0.2
        })
    }
}

// Flash Plugin Detect

function detectFP() {
    var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision)
    if (!hasReqestedVersion) {
        var alternateContent = '<p><small>Questo sito richiede <strong>Adobe Flash Player</strong></small></p><a href=http://www.adobe.com/go/getflash/>' + '<img style="margin-right: 20px;" src="/images/getPlayer.gif" title="Scarica Adobe Flash Player"' + ' alt="Scarica Adobe Flash Player"/></a>'
        if ($('getFP')) Element.update('getFP', alternateContent)
    }
}

function getFlashCode(name) {
    return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="1000" height="400" id="loader" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="wmode" value="transparent"/><param name="movie" value="/flash/videohome/' + name + '.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#0d0d0f" /><embed src="/flash/videohome/' + name + '.swf" quality="high" bgcolor="#0d0d0f" width="1000" height="400" name="loader" align="middle" allowScriptAccess="sameDomain" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>'
}

function showFlash(flashMovieName) {
    $('hdiv-homepage-video').innerHTML = getFlashCode(flashMovieName)
}

function doLinkVideo(e) {
    Event.stop(e)
    var videos = $$('#pb-productslider .pb-productimage a')
    lastElement = Event.element(e)
    if (lastElement.href.indexOf('?') > 0) {
        flashMovieName = lastElement.href.replace(/.*\?/i, "")
        showFlash(flashMovieName)
    }
}

function detectSlogan() {
    if ($('header-slogan')) {
        $('header-slogan').appear({
            duration: 1.2
        })
    }
}

// Binding degli eventi.

function initSlideTrack() {
    if ($("pb-productslidertrack")) {
        if ($('productbrowser')) {
            //Overflow auto fix for IE8
            $('productbrowser').style.overflow = 'hidden'
        }
        AC.ProductBrowser.init({
            categories: [{
                id: 'pb-cat1',
                offset: 0
            }, {
                id: 'pb-cat2',
                offset: 0.304
            }, {
                id: 'pb-cat3',
                offset: 0.609
            }, {
                id: 'pb-cat4',
                offset: 1.000
            }],
            imageOverlap: 0,
            sliderCentering: 218,
            initialCategory: 0,
            arrowScrollAmount: 0.38,
            iPhoneCategories: [{
                id: 'pb-cat1',
                offset: 0.0
            }, {
                id: 'pb-cat2',
                offset: 0.304
            }, {
                id: 'pb-cat3',
                offset: 0.5
            }, {
                id: 'pb-cat4',
                offset: 1
            }],
            iPhoneScrollAmount: 0.295,
            iPhoneContainerWidth: 840
        })
    }
}

var homePageNAnimation = function () {
        if (!$('hdiv-homepage-tassello')) {
            return undefined;
        }
        var my, that;
        my = {};
        my.paddings = {};
        that = {};

        my.selectedItem = undefined;

        my.fadeOut = function (item) {
            Effect.Queues.get('q_' + item.id).invoke('cancel');
            item.fade({
                to: .2,
                duration: .4,
                queue: {
                    position: 'end',
                    scope: 'q_' + item.id
                }
            });
            item.select('.tassello_home_txt').each(function (item2) {
                item2.hide();
                item2.setStyle({
                    top: '-280px',
                    left: my.paddings[item.id] + 'px',
                    padding: '10px'
                });
            });
        };
        my.theIndex = 0;
        my.spaces = 20;
        $$('#hdiv-homepage-tassello .tassello_home').each(function (item) {
            item.select('.tassello_home_txt').each(function (item2) {
                var dim, thePad, align;
                dim = Element.getDimensions(item2);
                thePad = 200 + my.spaces * 2;
                my.theIndex++;
                align = 'left';
                if (my.theIndex > 3) {
                    thePad = -dim.width - my.spaces * 2 - 20;
                    align = 'right';
                }
                my.paddings[item.id] = thePad;
                item2.setStyle({
                    top: '-280px',
                    left: thePad + 'px',
                    padding: '10px'
                });
                item2.select('*').each(function (item3) {
                    item3.setStyle({
                        fontFamily: 'Myriad Pro',
                        fontSize: '19px',
                        letterSpacing: '1px',
                        textAlign: align
                    });
                });

                item2.select('h1').each(function (item3) {
                    item3.setStyle({
                        color: '#CB2E2E',
                        fontSize: '22px',
                        textTransform: 'uppercase'
                    });
                });
                item2.select('p').each(function (item3) {
                    item3.setStyle({
                        color: '#555',
                        fontWeight: '700'
                    });
                });
            });
            Event.observe(item, 'mouseover', function () {
                if (!my.selectedItem) {
                    $$('#hdiv-homepage-tassello .tassello_home').each(function (item2) {
                        if (item2 != item) {
                            my.fadeOut(item2);
                        }
                    });
                }
                if (my.selectedItem && my.selectedItem != item) {
                    my.fadeOut(my.selectedItem);
                }
                my.selectedItem = item;
                Effect.Queues.get('q_' + item.id).invoke('cancel');
                item.fade({
                    to: 1,
                    duration: .4,
                    queue: {
                        position: 'end',
                        scope: 'q_' + item.id
                    }
                });
                item.select('.tassello_home_txt').each(function (item2) {
                    item2.fade({
                        to: 1,
                        duration: .4,
                        queue: {
                            scope: 'q_' + item.id
                        }
                    });
                    item2.show();
                    var xx = 20;
                    if (my.paddings[item.id] > 0) {
                        var xx = xx * -1;
                    }
                    if (item2.getStyle('left') !== my.paddings[item.id] + 'px') {
                        return;
                    }
                    new Effect.Move(item2, {
                        x: xx,
                        duration: .6,
                        mode: 'relative',
                        queue: {
                            scope: 'q_' + item.id
                        }
                    });
                });
            });
        });
        return that;
    }

function bindEvents(e) {
    var videos = $$('#pb-productslider .pb-productimage a')
    for (var i = 0; i < videos.length; i++) {
        if (videos[i].href.match(/video.*/) || videos[i].href.match(/corporate/)) {
            Event.observe(videos[i], 'click', doLinkVideo)
        }
    }
    var links = $$('#left-panel ul li a.ajax')
    for (var i = 0; i < links.length; i++) {
        Event.observe(links[i], 'click', doLink)
    }
    detectFP()
    detectSlogan()
    if ($('form-feedback')) initFeedbackForm()
    if ($('skip')) {
        mostraIntro()
        afterload()
    }
    if ($('sinfonstyle-elenco-articoli')) initSlideTrack()
    if ($('hdiv-homepage-elenco-video')) initSlideTrack()
    if ($('cubo_comunicazione_agli_studi')) evidenziaNuovaNews()
    if ($('hdiv-homepage-tassello')) evidenziaTasselliHome()
    if ($('road-show-img-notai-nuova-nomina')) {
        jQuery(function () {
            'use strict';
            initElencoRoadShow(jQuery);
        });
    }
    homePageNAnimation()
    backToHome()
}

function evidenziaNuovaNews() {
    Effect.Pulsate('cubo_comunicazione_agli_studi', {
        pulses: 5,
        duration: 4,
        from: 0.7
    });
}

function evidenziaTasselliHome() {}


var initElencoRoadShow = function ($) {
        'use strict';
        var that = {},
            my = {};

        my.initRoadShow = function (roadshow) {
            var returnValue, bg, tr;
            if (!roadshow) {
                return undefined;
            }
            returnValue = $('<table>').css({
                width: '50%',
                margin: '0 auto'
            });

            bg = 0;

            $.each(roadshow, function (index, item) {
                if (item.hidden) {
                    return;
                }
                var theColor;
                var tr;

                bg += 2;
                tr = $('<tr>');

                theColor = 'hsl(0, 0%, ' + (90 + bg) + '%)';
                tr.append($('<td>').css({
                    width: '100px'
                }).append($('<div>').width(100).height(50).css({
                    background: 'transparent url(' + item.img + ') no-repeat center center'
                })));

                tr.append($('<td>').css({
                    paddingLeft: '8px',
                    fontSize: '16px',
                    backgroundColor: '#cecece'
                }).append($('<strong>').append(item.city)).append($('<br>')).append($('<small>').append(item.date)));
                tr.appendTo(returnValue);
            });
            return returnValue;
        };


        my.loadRoadShow = function () {
            $.getJSON('/docs/homepage/news.json', function (data) {
                $('#road-show-img-notai-nuova-nomina').append(my.initRoadShow(data.roadshow));
            });
        };

        my.init = function () {
            my.loadRoadShow();
        };

        my.init();
        return that;
    };


function brochure() {
    dimx = 950
    dimy = 750
    dimx += 10
    dimy += 10
    var xMax = screen.width
    var yMax = screen.height
    var xOffset = (xMax / 2) - (dimx / 2)
    var yOffset = (yMax / 2) - (dimy / 2)
    opzioni = "scrollbars=no,toolbar=no,width=" + dimx + ",height=" + dimy + ",screenX=" + xOffset + ",screenY=" + yOffset + ",top=" + yOffset + ",left=" + xOffset + ",scrollbars=no,status=no,menubar=no"
    fin = window.open("/docs/brochure", "", opzioni)
}

function mostraIntro() {
    $('skip').observe('click', function (event) {
        Event.stop(event)
        intervalloView.stop()
        backToHome()
    })
    $('intro_img_2').setOpacity(0.5)
    $('intro_titolo').setOpacity(0)
    $('intro_testo').setOpacity(0)
    $('footer').style.background = 'none'

    new Effect.Opacity('intro_img_2', {
        from: 0.5,
        to: 1,
        duration: 1
    })
    new Effect.Opacity('intro_titolo', {
        from: 0,
        to: 1,
        duration: 1,
        delay: 0.5
    })
    new Effect.Opacity('intro_testo', {
        from: 0,
        to: 1,
        duration: 1,
        delay: 1
    })
}

function backToHome() {
    if ($('pagina_intro')) {
        $('pagina_intro').hide()
    }

    if ($('pagina_index')) {
        $('pagina_index').show()
    }

    $('footer').style.background = '#f2f2f2 url(/img/layout2010/infocert.png) no-repeat right center'
    /*StartSlideShow()*/
    /*StartSlideHotNews()*/
    initSlideTrack()
}

function afterload() {
    intervalloView = new PeriodicalExecuter(function (pe) {
        pe.stop()
        backToHome()
    }, 28);
}


// Fine del caricamento della pagina.
Event.observe(window, 'load', bindEvents)
