
// variabili Globali
var sStaticFolder = "";
var nWidthContent = 1280;
var nPagesTot = 6;
var nPages = 1;
var currentVideoPromo = "";
var currentVideoSpot = "";

// URL Video
//var sUrlVideoCestino = 'http://video.ferrero.com/videos/6e/b0ad9f1256e0bbd4483d31e332d02e.mp4';  // MODIFICA 2011/04/27 (commentato)
//var sUrlVideoConiglietto = 'http://video.ferrero.com/videos/f6/9edc488cc92847728d62fafefe7559.mp4';  // MODIFICA 2011/04/27 (commentato)
//var sUrlVideofacileDaPreparare = 'http://video.ferrero.com/videos/21/7084d3bafaa894d96cab91ff70335c.mp4';
//var sUrlVideoSpot_Aspetta = 'http://video.ferrero.com/videos/8a/6ad757cf3cd3714b851dbecb6365d2.mp4';
//var sUrlVideoSpot_Calcetto = 'http://video.ferrero.com/videos/9a/6b01aa22c4109584babf978265642c.mp4';
//var sUrlVideoSpot_Natale = 'http://video.ferrero.com/videos/16/3b87410919648ff350c1ede7063c02.mp4';
//var sUrlVideoSpot_Pianerottolo = 'http://video.ferrero.com/videos/e4/ddaeba9639eca22d77f044f806c599.mp4';

var sUrlVideoSpot_Telepromo_Natale = 'http://video.ferrero.com/videos/27/655abdc985b75dec42524744c16d50.mp4';
var sUrlVideoSpot_Natale = 'http://video.ferrero.com/videos/1e/f2f6f8766d98672431c640e629d677.mp4';
var sUrlVideofacileDaPreparare = 'http://video.ferrero.com/videos/6c/6a5be8d2a48eda872bdd06615ba897.mp4';

//var sUrlVideoPromoNovanta = 'http://video.ferrero.com/videos/ee/982a62868cae494c9085e95386fd66.flv';  // MODIFICA 2011/04/27 (aggiunto)
//var sUrlVideoPromoNovanta = 'http://video.ferrero.com/videos/16/3b87410919648ff350c1ede7063c02.mp4';
var sUrlVideoPromoNovanta = 'http://video.ferrero.com/videos/1e/f2f6f8766d98672431c640e629d677.mp4';


$(document).ready(
    function () {
        alignContentCenter();        
        LoadContent();
    });

$(window).resize(
    function () {
        alignContentCenter();
    });

    function LoadContent() {
        var idPage = $('.page').attr('id');
        var idCurrentPage = idPage.replace('page0', '');

        //
        var idNextPage = parseInt(idCurrentPage) + 1;

        if (idPage == "page01") {
            setLinkVideoPromo();
        }

        if (idPage == "page02") {
            setHoverProduct();
            setLinkVideoPromo();
        }

        if (idPage == "page03") {
            setFlowplayer('PlayerVideoFacileDaPreparato', sUrlVideofacileDaPreparare);
        }

        if (idPage == "page05") {
            setLinkSpot();
            setFlowplayer('PlayerVideoSpot', sUrlVideoSpot_Natale);
        }

        if (idPage == "page06") {
            setLinkMappa();
        }        

        if ((idPage != "page00") && (idPage != "page06")) {
            //
            if (!IsIOS()) {
                LoadNextContent(idNextPage, idCurrentPage);
            }
        }
    }

function LoadNextContent(i, iCurrent) {   

    var nWidth = $('.pageList').css('width');
    nWidth = nWidth.replace('px', '');
    nWidth = parseInt(nWidth) + nWidthContent;
    $('.pageList').css({ 'width': nWidth + 'px' });

    var sHtml = "<div class=\"page\" id=\"page0" + i + "\"></div>";
    $('.pageList').append(sHtml);

    $('.page').css({ 'width': nWidthContent + 'px' });
    var objArrowNext = $('#page0' + iCurrent).find('.arrow');
    $(objArrowNext).attr('href');
    var sUrlPage = $(objArrowNext).attr('href');
    
    sUrlPage = sStaticFolder + sUrlPage + "?content=true";

    $(objArrowNext).attr({ 'href': 'javascript:;' });

    $('#page0' + i).load(sUrlPage, null, function () {
        //
        setLinkVideoPromo();

        //
        $(objArrowNext).bind('click', function () {
            var nLeft = $('.pageList').css('left');
            nLeft = nLeft.replace('px', '');
            nLeft = nLeft - nWidthContent;

            $('.pageList').animate({ 'left': nLeft + 'px' }, 500, function () {

                var objLnkVoiceMenu = $('.voice0' + iCurrent).find('a');
                $(objLnkVoiceMenu).attr({ 'class': 'no-active' });

                var objLnkVoiceMenu = $('.voice0' + i).find('a');
                $(objLnkVoiceMenu).attr({ 'class': 'active' });

                // Pagina Facile da preparare - faccio partire il video in automatico
                if (i == 3) {
                    setFlowplayer('PlayerVideoFacileDaPreparato', sUrlVideofacileDaPreparare);
                }

                // Pagina successiva a Facile da preparare - vermo il video
                if (i == 4) {
                    stopFlowplayer('PlayerVideoFacileDaPreparato');
                }

                // Pagina Spot  - faccio partire il primo video in automatico
                if (i == 5) {
                    setLinkSpot();
                    setFlowplayer('PlayerVideoSpot', sUrlVideoSpot_Natale);
                }

                // Pagina successiva a Facile da preparare - vermo il video
                if (i == 6) {
                    setLinkMappa();
                    stopFlowplayer('PlayerVideoSpot');
                }

                if (i < 6)
                    LoadNextContent(i + 1, parseInt(iCurrent) + 1);

                nPages++;
            });
        });
    });
}


function alignContentCenter() {
    var nWidth = $(window).width();
    var nHeight = $(window).height();
    var nHeightContainer = $(window).height();
    var nTopBg = 0;


    if (nWidth < 998)
        nWidth = 998;

    if (nHeight < 580)
        nHeight = 580;
    else
        if (nHeight > 775)
            nHeight = 775;
        else
            nTopBg = (nHeight - 755) / 2;

    $('.general').css({ 'width': nWidth + 'px' });
    $('.general').css({ 'height': nHeight + 'px' });

    $('.general').css({ 'background-position': 'center ' + nTopBg + 'px' });

    if (nHeightContainer < 580)
        nHeightContainer = 580;
    else
        if (nHeightContainer > 660)
            nHeightContainer = 660;

    nWidthContent = nWidth;
    $('.container').css({ 'width': nWidth + 'px' });
    $('.container').css({ 'height': nHeightContainer + 'px' });

    var nHeightPageMask = nHeightContainer - 51;
    $('.pagesMask').css({ 'width': nWidth + 'px' });
    $('.pagesMask').css({ 'height': nHeightPageMask + 'px' });

    $('.pageList').css({ 'width': (nWidth * nPagesTot) + 'px' });
    $('.pageList').css({ 'left': '-' + (nWidth * (nPages - 1)) + 'px' });
    $('.page').css({ 'width': nWidth + 'px' });
    //$('.page').css({ 'width': nWidth + 'px' });


    var nHeightBody = $(window).height();  
    $('.general').show();
}

function IsIOS() {
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))
        return true;
    else
        return false;
}

function setLinkVideoPromo() {
    /*
    $('.lnkVideoPromo_Coniglietto').bind('click', function () {
        var idLnk = $(this).attr('id');

        if (currentVideoPromo != idLnk) {
            currentVideoPromo = idLnk;
            $('#' + idLnk).attr({ 'class': 'lnkVideoPromo_Coniglietto active' });

            var idLnkPrev = idLnk.replace('lnkConiglietto_', 'lnkCestino_');
            $('#' + idLnkPrev).attr({ 'class': 'lnkVideoPromo_Cestino' });

            var currentPlayerID = idLnk.replace('lnkConiglietto_', 'PlayerVideo_');
            setFlowplayer_Promo(currentPlayerID, sUrlVideoConiglietto);
        }
    });
        
    $('.lnkVideoPromo_Cestino').bind('click', function () {
        var idLnk = $(this).attr('id');

        if (currentVideoPromo != idLnk) {
            currentVideoPromo = idLnk;
            $('#' + idLnk).attr({ 'class': 'lnkVideoPromo_Coniglietto active' });

            var idLnkPrev = idLnk.replace('lnkCestino_', 'lnkConiglietto_');
            $('#' + idLnkPrev).attr({ 'class': 'lnkVideoPromo_Cestino' });

            var currentPlayerID = idLnk.replace('lnkCestino_', 'PlayerVideo_');
            setFlowplayer_Promo(currentPlayerID, sUrlVideoCestino);
        }
    });
    */
}

function setLinkSpot() {

    $('.lnkSpot').bind('click', function () {
        var idLnk = $(this).attr('id');

        if (currentVideoSpot != idLnk) {
            currentVideoSpot = idLnk;
            $('.lnkSpot').attr({ 'class': 'lnkSpot' });
            $('#' + idLnk).attr({ 'class': 'lnkSpot active' });

            if (idLnk == "lnkSpot_Natale") {
                setFlowplayer('PlayerVideoSpot', sUrlVideoSpot_Natale);
            } else if (idLnk == "lnkSpot_Telepromo_Natale") {
                setFlowplayer('PlayerVideoSpot', sUrlVideoSpot_Telepromo_Natale);
            } 
        }
    });
}

function OpenPromo(_id) {
    $('#' + _id).show();
    var currentPlayerID = _id.replace('promoOverlayPromo_', 'PlayerVideo_');
    //setFlowplayer_Promo(currentPlayerID, sUrlVideoConiglietto); // MODIFICA 2011/04/27 (commentato)
    setFlowplayer_Promo(currentPlayerID, sUrlVideoPromoNovanta); // MODIFICA 2011/04/27 (aggiunto)
}

function ClosePromo(_id) {
    $('#' + _id).hide();
    var currentPlayerID = _id.replace('promoOverlayPromo_', 'PlayerVideo_');
    stopFlowplayer(currentPlayerID);  
}

function setFlowplayer(_id, _url) {
    if (IsIOS()) {
        $('#' + _id).html('<video width="473" height="315" controls autoplay><source src="' + _url + '"></video>');
    } else {
        $f(_id, "Swf/flowplayer.commercial-3.2.7.swf", {
            clip: {
                url: _url,
                autoPlay: true,
                autoBuffering: true
            },
            key: sKeyFlowplayer
        });
    }
}

function setFlowplayer_Promo(_id, _url) {
    if (IsIOS()) {
        $('#' + _id).html('<video width="426" height="284" controls autoplay><source src="' + _url + '"></video>');
    } else {
        $f(_id, "Swf/flowplayer.commercial-3.2.7.swf", {
            clip: {
                url: _url,
                autoPlay: true,
                autoBuffering: true
            },
            key: sKeyFlowplayer
        });
    }
}

function stopFlowplayer(_id) {
    if (IsIOS()) {
        $('#' + _id).html('');
    } else {
        $f(_id, "Swf/flowplayer.commercial-3.2.7.swf", {
            plugins:
            {
                controls: { stop: true }
            }
        });
    }
}

function setHoverProduct() {
    $(".productMenu > ul > li").hover(function () {
        $(".productMenu > ul > li").fadeTo(1, 0.5);
        $(this).fadeTo(250, 1);
    }, function () {

    });

    $(".productMenu").hover(function () {

    }, function () {
        $(".productMenu > ul > li").fadeTo(1, 1);
    });
}
