﻿var theInt = null;
var $crosslink, $navthumb;
var curclicked = 0;

theInterval = function(cur)
{
    clearInterval(theInt);
    if (typeof cur != 'undefined')
    {
        curclicked = cur;
    }
    //rimuove la classe attiva da tutte
    $crosslink.removeClass("active-num");

    $("#movers-row > div > a").eq(curclicked).addClass("active-num");
    $(".stripNav ul li a").eq(curclicked).trigger('click');

    theInt = setInterval(function()
        {
            $crosslink.removeClass("active-num");
            $("#movers-row > div > a").eq(curclicked).addClass("active-num");
            $(".stripNav ul li a").eq(curclicked).trigger('click');
            curclicked++;
            if (4 == curclicked)
            {
                curclicked = 0;
            }
        }, 10000);
    };

    $(function()
    {
        place = parseInt($('.head').css('height').substring(0, ($('.head').css('height').length - 2))) + 50;
        
        adaptPage();
        placeIt();
        initLogin();
        initHover();
        initSlideHead();

        //spostaA('Prodotti');

        $("#main-photo-slider").codaSlider();
        $navthumb = $(".active-num");
        $crosslink = $(".cross-link");

        $crosslink.click(function()
        {
            var $this = $(this);
            theInterval($this.attr('href').slice(1) - 1);
            return false;
        });

        theInterval();
    });

    function initHover()
    {
        $('.promoBlock img').bind('mouseover', function()
        {
            $(this).parent().find('.arrowImg').attr('src', 'Images/arrowButtonHover.png');
        });

        $('.promoBlock img').bind('mouseleave', function()
        {
            $(this).parent().find('.arrowImg').attr('src', 'Images/arrowButton.png');
        });

        $('.floatButton').bind('mouseover', function()
        {
            switch ($(this).attr('alt'))
            {
                case 'Portale':
                    $(this).attr('src', 'Images/portaleButtonHover.png');
                    break;
                case 'E-Commerce':
                    $(this).attr('src', 'Images/clientiButtonHover.png');
                    break;
                case 'K4U':
                    $(this).attr('src', 'Images/k4uButtonHover.png');
                    break;
            }
        });

        $('.floatButton').bind('mouseleave', function()
        {
            switch ($(this).attr('alt'))
            {
                case 'Portale':
                    $(this).attr('src', 'Images/portaleButton.png');
                    break;
                case 'E-Commerce':
                    $(this).attr('src', 'Images/clientiButton.png');
                    break;
                case 'K4U':
                    $(this).attr('src', 'Images/k4uButton.png');
                    break;
            }
        });
    }

    function initLogin()
    {
        /* Funzione di cattura della pressione del tasto enter per il portale aziendale */
        $('#portaleBox').keyup(function(e) 
        {
            if(e.keyCode == 13) 
            {
                loginPortale();
            }
        });
        
        /* Funzione di cattura della pressione del tasto enter per il portale aziendale */
        $('#clientiBox').keyup(function(e) 
        {
            if(e.keyCode == 13) 
            {
                loginClienti();
            }
        });
    
        $('#portaleImg').click(function()
        {
            var leftPortale = $('#portaleBox').position().left;
            var leftClienti = $('#clientiBox').position().left;
            var leftK4U = $('#k4uBox').position().left;

            if (leftPortale < 0)
            {
                $('#portaleBox').animate({
                    left: '+=295'
                }, 500, function()
                {
                    if (utentePortale.GetText().length > 0 && utentePortale.GetText() != 'Utente')
                    {
                        passwordPortale.Focus();
                        chkRicordaPortale.SetChecked(true);
                    }
                    else
                    {
                        utentePortale.Focus();
                        chkRicordaPortale.SetChecked(false);
                    }
                    btnPortale.SetEnabled(true);
                });
            }
            else
            {
                $('#portaleBox').animate({
                    left: '-=295'
                }, 500);

                if ($('#addTablePortale').css('display') != 'none')
                {
                    chiudiModPwd();
                }
                    
                // Pulizia Campi
                ASPxClientEdit.ClearEditorsInContainerById('PortaleContainer');
                btnPortale.SetEnabled(false);
            }

            if (leftClienti == 5)
            {
                $('#clientiImg').trigger('click');
            }

            if (leftK4U == 5)
            {
                $('#k4uImg').trigger('click');
            }
        });

        $('#clientiImg').click(function()
            {
                var leftPortale = $('#portaleBox').position().left;
                var leftClienti = $('#clientiBox').position().left;
                var leftK4U = $('#k4uBox').position().left;

                // Apertura
                if (leftClienti < 0)
                {
                    $('#clientiBox').animate({
                        left: '+=295'
                    }, 500);
                    utenteClienti.Focus();
                    btnClienti.SetEnabled(true);
                }
                else
                {
                    // Chiusura
                    $('#clientiBox').animate({
                        left: '-=295'
                    }, 500);
                    
                    // Pulizia Campi
                    ASPxClientEdit.ClearEditorsInContainerById('clientiContainer');
                    btnClienti.SetEnabled(false);
                }
                if (leftPortale == 5)
                {
                    $('#portaleImg').trigger('click');
                }
                if (leftK4U == 5)
                {
                    $('#k4uImg').trigger('click');
                }
            });

            $('#k4uImg').click(function()
            {
                var leftPortale = $('#portaleBox').position().left;
                var leftClienti = $('#clientiBox').position().left;
                var leftK4U = $('#k4uBox').position().left;

                if (leftK4U < 0)
                {
                    $('#k4uBox').animate({
                        left: '+=295'
                    }, 500);
                }
                else
                {
                    $('#k4uBox').animate({
                        left: '-=295'
                    }, 500);
                }
                if (leftClienti == 5)
                {
                    $('#clientiImg').trigger('click');
                }
                if (leftPortale == 5)
                {
                    $('#portaleImg').trigger('click');
                }
            });
        }
        
function initSlideHead()
{
    $("ul#scrollerHead").liScroll();
}

function adaptPage()
{
    var footH = $(".footerSmall").height();
    if ($(".footerBig").height() > 0)
    {
        footH = $(".footerBig").height();
    }
    var headH = $(".head").height();

    var object = document.getElementById('centerBody');
    object.style.minHeight = (document.documentElement.clientHeight - footH - headH) + 'px';
}

/* Altezza Orgiginale 250px */
var place = 130;
var xMove = 280;

(document.getElementById && !document.all) ? dom = true : dom = false;

function typeStart(key)
{
    
    if (key == 1)
    {
        if (dom || document.all)
        {
            document.write('<div id="clientiBox" style="height:135px; z-index:100; position:absolute; left:-290px; visibility:visible;">')
        }
    }
    else if (key == 2)
    {
        if (dom || document.all)
        {
            document.write('<div id="k4uBox" style="height:135px; z-index:100; position:absolute; left:-290px; visibility:visible;">')
        }
    }
    else if (key == 3)
    {
        if (dom || document.all)
        {
            document.write('<div id="portaleBox" style="height:135px; z-index:100; position:absolute; left:-290px; visibility:visible;">')
        }
    }
}

function typeEnd() { if (dom || document.all) { document.write('</div>') } }

function placeIt()
{
    var heightPortale = parseInt(document.getElementById('portaleBox').style.height.substring(0, document.getElementById('portaleBox').style.height.lastIndexOf('px')));
    var fillerHeight = 6;

    if (dom)
    {
        document.getElementById("clientiBox").style.top = window.pageYOffset + place + "px";
    }

    if (document.all)
    {
        document.all["clientiBox"].style.top = document.documentElement.scrollTop + place + "px"
    }
    
    if (dom)
    {
        document.getElementById("k4uBox").style.top = window.pageYOffset + place + heightPortale + fillerHeight + "px";
    }

    if (document.all)
    {
        document.all["k4uBox"].style.top = document.documentElement.scrollTop + place + heightPortale + fillerHeight + "px"
    }

    if (dom)
    {
        document.getElementById("portaleBox").style.top = window.pageYOffset + place + heightPortale + fillerHeight + fillerHeight + heightPortale + "px";
    }

    if (document.all)
    {
        document.all["portaleBox"].style.top = document.documentElement.scrollTop + place + heightPortale + fillerHeight + fillerHeight + heightPortale + "px"
    }
    window.setTimeout("placeIt()", 10);
}

$(document).ready(function()
{
    /* Caricamento Pagina Prodotti */
    $('dt').live('click', function()
    {
        var dd = $(this).next();

        $('.opened').next().slideToggle();

        $('.opened').removeClass('opened');

        // If the title is clicked and the dd is not currently animated,
        // start an animation with the slideToggle() method.

        if (!dd.is(':animated'))
        {
            dd.slideToggle();
            $(this).toggleClass('opened');
        }
    });
    $('.bannerTopDiv').canvasCycle();
});



function apriModPwd()
{
    $('#addTablePortale').show('fast');
    btnPortale.SetEnabled(false);
    nuovaPasswordPortale.Focus();
}

function chiudiModPwd()
{
    $('#addTablePortale').hide('fast');
    btnPortale.SetEnabled(true);
    pulisciCambioPwd();
}

function apriDettaglioProdotti(name)
{
    name = 'dt' + name;
    var obj = $("#" + name);

    var dd = obj.next();

    //chiude la sezione aperta

    if ($('.opened').next().css('display') == 'block')
    {
        $('.opened').next().css('display', 'none');
    }

    $('.opened').removeClass('opened');

    //apre la sezione richiesta
    if (!dd.is(':animated'))
    {
            obj.toggleClass('opened');
            dd.slideToggle();
    }
}

/* Campionari Online */
function getURLParam(strParamName)
{
    var strReturn = "";
    var strHref = window.location.href;
    if (strHref.indexOf("?") > -1)
    {
        var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
        var aQueryString = strQueryString.split("&");
        for (var iParam = 0; iParam < aQueryString.length; iParam++)
        {
            if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1)
            {
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return unescape(strReturn);
}

function OnGridFocusedRowChanged()
{
    grid.GetRowValues(grid.GetFocusedRowIndex(), 'ID;Tipo', OnGetRowValues);
}

function OnGetRowValues(values)
{
    if (values[0] != null)
    {
        FocusedCallback.PerformCallback('lD;' + values[0] + ';' + values[1]);
    }
}

function openNews(id)
{
    var tipo = '';
    if (id.substring(0, 1) == 'N')
    {
        tipo = 'News';
    }
    else if (id.substring(0, 1) == 'E')
    {
        tipo = 'Evento';
    }
    else if (id.substring(0, 1) == 'F')
    {
        tipo = 'Focus On';
    }

    if (tipo != '')
    {
        LoadingPanel.Show();
        FocusedCallback.PerformCallback('lD;' + id.substring(1) + ';' + tipo);
    }
}

function getUrlParam(name)
{
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function upperFirst(name)
{
    return name.slice(0, 1).toUpperCase() + name.slice(1);
}

function setHoverCertificazioni()
{
    $('.qualityItem').parent().bind('mouseover', function()
    {
        $(this).find('.qualityItem').css("opacity", 0.4);

        $(this).parent().find('.pdfHover').css('display', 'block');
    });

    $('.qualityItem').parent().bind('mouseleave', function()
    {
        $(this).parent().find('.pdfHover').css('display', 'none');
        $(this).find('.qualityItem').css("opacity", 1);
    });
}

function redirect(url)
{
    location.href = url;
}

function openEssentia()
{
    window.open('essentia3d/home.html', 'mywindow', 'fullscreen=yes, scrollbars=auto, status=no, titlebar=no, toolbar=no, location=no', false);
}

function manageContactImg(element, path, name)
{
    if($('.toolTipContact .innerToolTipContact img').attr('title') == name)
    {
        hideContactImg();
    }
    else if($('.toolTipContact').css('display') == 'block')
    {
        hardHideContactImg();
        showContactImg(element, path, name);
    }
    else
    {
        showContactImg(element, path, name);
    }
}

function showContactImg(element, path, name)
{
    $('.toolTipContact').css('left', $(element).position().left-50);
    $('.toolTipContact').css('top', $(element).position().top - 140);

    $('.toolTipContact .innerToolTipContact img').attr('src', 'Images/Contatti/' + path);
    $('.toolTipContact .innerToolTipContact img').attr('alt', name);
    $('.toolTipContact .innerToolTipContact img').attr('title', name);
    $('.toolTipContact').show('slow');

}

function hideContactImg()
{
    $('.toolTipContact').hide('slow');
    $('.toolTipContact .innerToolTipContact img').attr('src', '');
    $('.toolTipContact .innerToolTipContact img').attr('alt', '');
    $('.toolTipContact .innerToolTipContact img').attr('title', '');
}

function hardHideContactImg()
{
    $('.toolTipContact').css('display','none');
    $('.toolTipContact .innerToolTipContact img').attr('title','');
}

function openPopup(url) 
{
    window.open(url, 'Campionario', 'width=509,height=800');
}

/** Login Clienti **/

function loginClienti()
{
    if (utenteClienti.GetText() == '' || utenteClienti.GetText() == 'Utente')
    {
        utenteClienti.SetIsValid(false);
    }
    
    if (passwordClienti.GetText() == '' || passwordClienti.GetText() == 'Password')
    {
        passwordClienti.SetIsValid(false);
    }
    
    if (utenteClienti.GetIsValid() == true && passwordClienti.GetIsValid() == true)
    {
        LoadingPanel.Show();
        comunicationCallback.PerformCallback('logCl;' + utenteClienti.GetText() + ';' + passwordClienti.GetText() + ';' + chkRicordaClienti.GetChecked());
    }
    else
    {
        pulisciPwdClienti();
        popupEsitoUpload('Errore', 'In uno o pi&ugrave; campi non &egrave; stato inserito alcun valore o quello inserito risulta troppo corto.', utenteClienti);
    }
}

function pulisciPwdClienti()
{
    passwordClienti.SetText('');
    document.getElementById('ctl00_passwordClienti').setAttribute('class', 'dxeTextBox dxeTextBoxSys pwdBack');
}

function pulisciCambioPwd()
{
    nuovaPasswordPortale.SetText('');
    document.getElementById('ctl00_nuovaPasswordPortale').setAttribute('class', 'dxeTextBox dxeTextBoxSys newPwd');
    confermaPasswordPortale.SetText('');
    document.getElementById('ctl00_confermaPasswordPortale').setAttribute('class', 'dxeTextBox dxeTextBoxSys confPwd');
    nuovaPincodePortale.SetText('');
    document.getElementById('ctl00_nuovaPincodePortale').setAttribute('class', 'dxeTextBox dxeTextBoxSys newPincode');
    confermaPincodePortale.SetText('');
    document.getElementById('ctl00_confermaPincodePortale').setAttribute('class', 'dxeTextBox dxeTextBoxSys confPincode');
}

function pulisciPwd()
{
    passwordPortale.SetText('');
    document.getElementById('ctl00_passwordPortale').setAttribute('class', 'dxeTextBox dxeTextBoxSys pwdBack');
}

function lostPassword()
{
    if (utentePortale.GetText() == '' || utentePortale.GetText() == 'Utente')
    {
        utentePortale.SetIsValid(false);
    }
    
    if (utentePortale.GetIsValid() == true)
    {
        var answer = confirm("Sei sicuro di voler resettare la tua password?")
        if (answer)
        {
            comunicationCallback.PerformCallback('lst;' + utentePortale.GetText());
        }
    }
    else
    {
        popupEsitoUpload('Errore', 'Il campo dello username risulta vuoto o il valore inserito non &egrave; corretto.', utentePortale);
    }
    passwordPortale.SetIsValid(true);
}

function loginPortale()
{
    if (utentePortale.GetText() == '' || utentePortale.GetText() == 'Utente')
    {
        utentePortale.SetIsValid(false);
    }
    
    if (passwordPortale.GetText() == '' || passwordPortale.GetText() == 'Password')
    {
        passwordPortale.SetIsValid(false);
    }
    
    
    if (utentePortale.GetIsValid() == true && passwordPortale.GetIsValid() == true)
    {
        LoadingPanel.Show();
        comunicationCallback.PerformCallback('log;' + utentePortale.GetText() + ';' + passwordPortale.GetText() + ';' + chkRicordaPortale.GetChecked());
    }
    else
    {
        pulisciPwd();
        popupEsitoUpload('Errore', 'In uno o pi&ugrave; campi non &egrave; stato inserito alcun valore o quello inserito risulta troppo corto.', utentePortale);
    }
}

function chPwdPortale()
{
    passwordPortale.SetIsValid(true);
    if (utentePortale.GetIsValid() == true && nuovaPasswordPortale.GetIsValid() == true && confermaPasswordPortale.GetIsValid() == true && nuovaPincodePortale.GetIsValid() == true && confermaPincodePortale.GetIsValid() == true)
    {
        if (nuovaPasswordPortale.GetText() == confermaPasswordPortale.GetText())
        {
            if (nuovaPincodePortale.GetText() == confermaPincodePortale.GetText())
            {
                LoadingPanel.Show();
                comunicationCallback.PerformCallback('chp;' + utentePortale.GetText() + ';' + nuovaPasswordPortale.GetText() + ';' + nuovaPincodePortale.GetText() + ';' + chkRicordaPortale.GetChecked());
            }
            else
            {
                //i pincode non coincidono
                pulisciCambioPwd();
                popupEsitoUpload('Errore', 'Il pincode di conferma non &egrave; uguale al nuovo pincode inserito.', nuovaPasswordPortale);
            }
        }
        else
        {
            //le password non coincidono
            pulisciCambioPwd();
            popupEsitoUpload('Errore', 'La password di conferma non &egrave; uguale alla nuova password inserita.', nuovaPasswordPortale);
        }
    }
    else
    {
        pulisciCambioPwd();
        popupEsitoUpload('Errore', 'In uno o pi&ugrave; campi non &egrave; stato inserito alcun valore o quello inserito risulta troppo corto.', nuovaPasswordPortale);
    }
}

function OnFieldValidation(s, e) 
{
    var name = e.value;
    if(name == 'Utente' || name == null)
    {
        return;
    }
            
    if(name.length < 2)
    {
        e.isValid = false;
    }
}

function utentePortaleFunct(s, e)
{
    var name = e.value;
    
    if(name == 'Utente')
    {
        name = '';
    }
    
    if (name == null)
    {
        e.isValid = false;
        return;
    }
    if (name.length < 2)
    {
        e.isValid = false;
    }
}

function utenteClienteFunct(s, e)
{
    var name = e.value;
    
    if(name == 'Utente')
    {
        name = '';
    }
    
    if (name == null)
    {
        e.isValid = false;
        return;
    }
    if (name.length < 2)
    {
        e.isValid = false;
    }
}

function passwordPortaleFunct(s, e)
{
    var name = s.GetText();
    if (name == null)
    {
        e.isValid = false;
        return;
    }
    if (name.length < 6)
    {
        e.isValid = false;
    }
}

function chekChTextPortale(s, e)
{
    var name = s.GetText();
    if (name == null)
    {
        e.isValid = false;
        return;
    }
    if (name.length < 6)
    {
        e.isValid = false;
    }
}


/**  Richiesta Info **/

function inviaRichiestaInfo()
{
    sendMainCallBack('sR');
}

function sendMainCallBack(command)
{
    if(!cpMainPanel.InCallback())
    {
        LoadingPanel.Show();
        cpMainPanel.PerformCallback(command);
    }
}

/**  Lavora Con Noi **/

function salvaCandidatura()
{
    sendMainCallBack('sC');
}

function apriPopupDinamico(url, width, height)
{
    if(width == 0)
    {
        window.open(url,'mypoup','fullscreen=yes, scrollbars=auto, status=no, titlebar=no, toolbar=no, location=no', false);
    }
    else
    {
        window.open(url,'mypoup','fullscreen=no, width=' + width + ', height=' + height + ', scrollbars=auto, status=no, titlebar=no, toolbar=no, location=no', false);
    }
}
