﻿var g_intQuoteLoader = 51;
var g_intCurrentQuote = 0;
var g_objQuoteLoader = null;
var g_objQuoteLoaderCanvas = null;
var g_objCanvas = null;
var g_intCompanyLogosWidth = 4 * 205;
var g_intCurrentCompanyLogosPos = 0;
var g_intGoToCompanyLogosPos = 0;
var g_intCurrentWaitingStep = 0;
var g_strCurrentOplossing = '';
var g_strLastOplossing = null;
var g_intSpeed = 2;
var g_intTimeToChangeSolution = 10;
var g_intSolution = 1;
var g_intTotalSolutions = 1;
var g_intSolutionHeight = 87;
var g_strLang = 'nl-NL';
var g_FirstLoop = true;

var intCanvasWidth = 112;
var intCanvasHeight = 112;
var intCircleDiameter = 55.5;
var strFillColor = '#FFFFFF';
var intStepsDurection = 7;
var intStepInterval = 50;
var intPause = 2;
var intCurrentStep = 0;
var intTotalSteps = Math.round(intStepsDurection * (1000 / intStepInterval));
var intCurrentWaitingStep = 0;
var intWaitSteps = Math.round((intPause * 1000) / intStepInterval);
var intStartLocation = -Math.PI * 0.5;

var g_blnIsCarrouselHovered = false;

function StartQuoteAnimation() {
    if (!!document.createElement('canvas').getContext) {
        g_objQuoteLoaderCanvas = document.createElement('canvas');
        g_objQuoteLoaderCanvas.setAttribute('class', 'divQuoteLoaderCanvas');
        $('#imgNextQuote').attr('class', 'imgNextQuoteCanvas');
        $('#imgNextQuoteHover').attr('class', 'imgNextQuoteCanvasHover');
        $('#divNextQuote').attr('class', 'divNextQuoteCanvas');
        g_objQuoteLoaderCanvas.setAttribute('height', '112');
        g_objQuoteLoaderCanvas.setAttribute('width', '112');
        g_objCanvas = g_objQuoteLoaderCanvas.getContext("2d");
        g_objQuoteLoader.appendChild(g_objQuoteLoaderCanvas);
        LoadQuoteCanvas();
    } else {
        $('#divQuoteLoader').attr('class', 'divQuoteLoaderVisible');
        LoadQuote();
    }
}

function LoadQuoteCanvas() {
    intCurrentStep = intTotalSteps - 1;
    setInterval(QuoteLoaderCanvas, intStepInterval);
}

function LoadQuote() {
    setInterval(QuoteLoader, 200);
}

function QuoteLoaderCanvas() {
    g_objCanvas.clearRect(0, 0, intCanvasWidth, intCanvasHeight);
    g_objCanvas.beginPath();
    g_objCanvas.arc(intCircleDiameter, intCircleDiameter - 0.5, intCircleDiameter - 0.5, intStartLocation, ((Math.PI * 2) / intTotalSteps) * intCurrentStep + intStartLocation, false);
    if (intCurrentStep == 0) {
        $('#divQuoteLoader').fadeIn(100);
    }
    if (intCurrentStep >= intTotalSteps) {
        if (intCurrentWaitingStep == 0) {
            $('#divQuoteLoader').fadeOut(500);
            setTimeout("$('#divQuoteToFade_" + g_intCurrentQuote + "').fadeOut(1500)", 1900);
            var IsFade = false;
            if (!document.getElementById('divQuoteToFade_' + (Math.abs(g_intCurrentQuote) + 1))) {
                //$('#divFadebackground_' + g_intCurrentQuote).fadeOut(1400);
                setTimeout("$('#divFadebackground_" + g_intCurrentQuote + "').fadeOut(1400)", 500);
            } else {
                setTimeout("$('#divFadebackground_" + g_intCurrentQuote + "').css('display', 'none')", 1900);
            }
            g_intCurrentQuote++;
            if (!document.getElementById('divQuoteToFade_' + g_intCurrentQuote)) { g_intCurrentQuote = 1; }
            setTimeout("$('#divQuoteToFade_" + g_intCurrentQuote + "').fadeIn(1400)", 1900);
            if (g_FirstLoop || g_intCurrentQuote > 1) {
                setTimeout("$('#divFadebackground_" + g_intCurrentQuote + "').fadeIn(1400)", 500);
            } else if (g_intCurrentQuote == 1) {
                setTimeout("$('#divFadebackground_" + g_intCurrentQuote + "').css('display', 'block')", 500);
            } else {
                setTimeout("$('#divFadebackground_" + g_intCurrentQuote + "').css('display', 'none')", 1900);
            }
            g_FirstLoop = false;
        }
        if (intCurrentWaitingStep >= intWaitSteps) {
            intCurrentStep = 0;
            intCurrentWaitingStep = 0;
            
        } else {
            intCurrentWaitingStep++;
        }
    } else {
        intCurrentStep++;
    }
    g_objCanvas.lineTo(intCircleDiameter, intCircleDiameter);
    g_objCanvas.fillStyle = strFillColor;
    g_objCanvas.fill();
}

function QuoteLoader() {
    g_objQuoteLoader.style.backgroundPosition = '0px -' + (g_intQuoteLoader - 1) * 112 + 'px';
    if (g_intQuoteLoader == 1) { $('#divQuoteLoader').fadeIn(100); }
    if (g_intQuoteLoader >= 51) {
        if (g_intCurrentWaitingStep == 0) {
            $('#divQuoteLoader').fadeOut(500);
            setTimeout("$('#divQuoteToFade_" + g_intCurrentQuote + "').fadeOut(1500)", 1900);
            var IsFade = false;
            if (!document.getElementById('divQuoteToFade_' + (Math.abs(g_intCurrentQuote) + 1))) {
                //$('#divFadebackground_' + g_intCurrentQuote).fadeOut(1400);
                setTimeout("$('#divFadebackground_" + g_intCurrentQuote + "').fadeOut(1400)", 500);
            } else {
                setTimeout("$('#divFadebackground_" + g_intCurrentQuote + "').css('display', 'none')", 1900);
            }
            g_intCurrentQuote++;
            if (!document.getElementById('divQuoteToFade_' + g_intCurrentQuote)) { g_intCurrentQuote = 1; }
            setTimeout("$('#divQuoteToFade_" + g_intCurrentQuote + "').fadeIn(1400)", 1900);
            if (g_FirstLoop || g_intCurrentQuote > 1) {
                setTimeout("$('#divFadebackground_" + g_intCurrentQuote + "').fadeIn(1400)", 500);
            } else if (g_intCurrentQuote == 1) {
                setTimeout("$('#divFadebackground_" + g_intCurrentQuote + "').css('display', 'block')", 500);
            } else {
                setTimeout("$('#divFadebackground_" + g_intCurrentQuote + "').css('display', 'none')", 1900);
            }
            g_FirstLoop = false;
        }
        if (g_intCurrentWaitingStep >= 10) {
            g_intQuoteLoader = 1;
            g_intCurrentWaitingStep = 0;
        } else {
            g_intCurrentWaitingStep++;
        }
    } else {
        g_intQuoteLoader++;
    }
}

//function SolutionAnime() {
//    g_intTimeToChangeSolution--;
//    if (g_intTimeToChangeSolution == 0) {
//        g_intSolution++;
//        if (g_intSolution > g_intTotalSolutions) { g_intSolution = 1; }
//        $('#aHomeOplossingItem_' + g_intSolution).click();
//    }
//    setTimeout("SolutionAnime()", 1000);
//}

//function ChangeLogoSpeed(p_intSpeed) {
//    g_intSpeed = p_intSpeed;
//}

function NextQuote() {
    g_intQuoteLoader = 50;
    intCurrentStep = intTotalSteps - 1;
}

function StartSolutionAnime() {
    setTimeout("$('#aHomeOplossingItem_1').click(); SolutionAnime()", 1000);
}

function SolutionAnime() {
    if (!g_blnIsCarrouselHovered) {
        g_intTimeToChangeSolution--;
        if (g_intTimeToChangeSolution == 0) {
            g_intSolution++;
            if (g_intSolution > g_intTotalSolutions) { g_intSolution = 1; }
            $('#aHomeOplossingItem_' + g_intSolution).click();
        }
    }
    setTimeout("SolutionAnime()", 1000);
}

function ChangeLogoSpeed(p_intSpeed) {
    g_intSpeed = p_intSpeed;
}

function GoToPos() {
    if (g_intCurrentCompanyLogosPos != g_intGoToCompanyLogosPos) {
        var l_intDiff = g_intCurrentCompanyLogosPos - g_intGoToCompanyLogosPos;
        if (l_intDiff < 0) { l_intDiff = l_intDiff * -1; }
        var l_intStep = Math.ceil(l_intDiff / 5);
        if (l_intStep > 50) {l_intStep = 50; }
        if (g_intCurrentCompanyLogosPos > g_intGoToCompanyLogosPos) {
            g_intCurrentCompanyLogosPos = g_intCurrentCompanyLogosPos - l_intStep;
        } else {
            g_intCurrentCompanyLogosPos = g_intCurrentCompanyLogosPos + l_intStep;
        }
        $('#divCompanySlideLogoContainer').css('marginLeft', g_intCurrentCompanyLogosPos + 'px');
        //setTimeout('GoToPos()', 30);
    }
}

function ChangeLogosPage(p_intDirection) {
    if (g_intCurrentCompanyLogosPos == g_intGoToCompanyLogosPos) {
        g_intGoToCompanyLogosPos = g_intGoToCompanyLogosPos + p_intDirection;
        if (g_intGoToCompanyLogosPos > 0) { g_intGoToCompanyLogosPos = 0 }
        if (g_intGoToCompanyLogosPos < (820 - g_intCompanyLogosWidth)) { g_intGoToCompanyLogosPos = 820 - g_intCompanyLogosWidth; }
        GoToPos();
    } else {
        g_intGoToCompanyLogosPos = g_intGoToCompanyLogosPos + p_intDirection;
        if (g_intGoToCompanyLogosPos > 0) { g_intGoToCompanyLogosPos = 0 }
        if (g_intGoToCompanyLogosPos < (820 - g_intCompanyLogosWidth)) { g_intGoToCompanyLogosPos = 820 - g_intCompanyLogosWidth; }
    }
}

function SetBackgroundSize() {
    var l_intHeight = 0;
    l_intHeight = l_intHeight + $('#divQuoteSlide').outerHeight(true);
    l_intHeight = l_intHeight + $('#divCompanySlideContainerHolder').outerHeight(true);
    l_intHeight = l_intHeight + $('#divHomeContentBlocks').outerHeight(true);
    l_intHeight = l_intHeight + $('#divHomeOplossingen').outerHeight(true);
    $('#divHomeFadeBackgroundPattern').css('height', l_intHeight + 'px');
    $('.divHomeFadeBackground').css('height', l_intHeight + 'px');
}

function ShowOplossing(p_strId, p_this) {
    g_intTimeToChangeSolution = 5;
    g_intSolution = Math.abs($(p_strId).attr('id').replace('divHomeOplossingDetail_', ''));
    if (g_strLastOplossing) { g_strLastOplossing.className = "aHomeOplossingItem"; }
    p_this.className = 'aHomeOplossingItemSelected';
    if (g_strCurrentOplossing != '') { $(g_strCurrentOplossing).css('display','none'); }
    $(p_strId).css('display', 'block');
    $('#aScrollDown').click();
    g_strCurrentOplossing = p_strId;
    g_strLastOplossing = p_this;
}

function ScrollCompanyLogos() {
    g_intCurrentCompanyLogosPos = g_intCurrentCompanyLogosPos + g_intSpeed;
    if (g_intCurrentCompanyLogosPos > (g_intCompanyLogosWidth / 2)) {
        g_intCurrentCompanyLogosPos = g_intCurrentCompanyLogosPos - (g_intCompanyLogosWidth / 2);
    }
    if (g_intCurrentCompanyLogosPos < 0) {
        g_intCurrentCompanyLogosPos = g_intCompanyLogosWidth / 2;
    }
    $('#divCompanySlideLogoContainer').css('left', '-' + g_intCurrentCompanyLogosPos + 'px');
    //setTimeout('ScrollCompanyLogos()', 41);
}
$(document).ready(function () {
    $('.divHomeOplossingenToScroll').hover(function () {
        g_blnIsCarrouselHovered = true;
    }, function () {
        g_blnIsCarrouselHovered = false;
    });
});
