﻿var langTimeout = 0;
var cylceStopped = false;

function initLanguageSelector() {
    var lang = $('.language');
    if (lang) {
        lang.mouseover(function() { if (langTimeout) { clearTimeout(langTimeout); langTimeout = 0; } if (!$(this).hasClass('lang-hover')) $(this).addClass('lang-hover'); });
        lang.mouseout(function() { langTimeout = setTimeout(closeLangDropDown, 2000); });
    }
    var langBtn = $('.language .language-selector');
    if (langBtn) {
        langBtn.click(function() {
            var dd = $(this).parent().find('.language-dropdown');
            if (dd.hasClass('visible'))
                dd.hide('fast', function() { $(this).removeClass('visible'); });
            else
                dd.show('fast', function() { $(this).addClass('visible'); });
        });
    }
}

function closeLangDropDown() {
    var e = $('.language');
    if (e) {
        var dd = e.find('.language-dropdown');
        if (dd.hasClass('visible'))
            dd.hide('fast', function() { $(this).removeClass('visible'); });
        if (e.hasClass('lang-hover'))
            e.removeClass('lang-hover');
    }
}

var searchHint = 'Suchen';

function removeHint() {
    var $el = $('#dnn_dnnSearch_txtSearch');
    if ($el && $el.val() == searchHint && $el.hasClass('search-hint')) {
        $el.removeClass('search-hint');
        $el.val('');
    }
}

function initSearchHint() {
    var $e = $('#dnn_dnnSearch_txtSearch');
    if ($e) {
        $e.blur(function () {
            var $el = $(this);
            if ($el.val() === '') {
                $el.val(searchHint);
                $el.addClass('search-hint');
            }
        });
        $e.focus(removeHint);
        $($e.form).submit(removeHint);
        $(window).unload(removeHint);
        $e.blur();
    }
}

function initScripts() {
    $(document).ready(function () {
        $('body').pngFix();
        $('.content-rotator').sideswap({ navigation: false });

        var sceneSpeed = 650;
        var sceneTimeout = 12000;
        var sceneStartingSlide = 0;

        //Scene cylce
        $('#scene').cycle({
            fx: 'scrollHorz',
            speed: sceneSpeed,
            timeout: sceneTimeout,
            pager: '#scene-nav',
            activePagerClass: 'scene-nav-active',
            next: '#scene-next',
            prev: '#scene-prev',
            slideExpr: '.scene-content',
            startingSlide: sceneStartingSlide,
            pagerAnchorBuilder: function (index, el) {
                var tabContent = $(el).find('.scene-nav-content').html();
                return '<li><a href="#">' + tabContent + '</a>';
            }
        });

        $('#scene-nav').delay(750).animate({ "bottom": "+=50px" }, 500);

        // stop with any click on pager
        $('#scene-nav a').click(function () {
            cylceStopped = true;
            $('#scene').cycle('pause');
        });

        //Pause cycle when hovered
        $('#scene').hover(function () {
            $('#scene').cycle('pause');
        }, function () {
            if (!cylceStopped)
                $('#scene').cycle('resume');
        });


        initLanguageSelector();

        //Init thickbox
        tb_init('a.thickbox, area.thickbox, input.thickbox'); //pass where to apply thickbox
        imgLoader = new Image(); // preload image
        imgLoader.src = tb_pathToImage;

        initSearchHint();
        initFAQs();
        initGoogle();
    });
}

function initGoogle() {
    window.___gcfg = {
        lang: currentLang,
        parsetags: 'onload' };

    var po = document.createElement('script'); 
    po.type = 'text/javascript'; 
    po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(po, s); 
}

function initFAQs() {
    var $allClickProxies = $('.faq-container .faq-question');
    if ($allClickProxies) {
        $allClickProxies
        .css({ cursor: 'pointer' })
        .click(
            function () {

                $element = $(this).parent().find('.faq-answer');

                if ($(this).hasClass('opened')) {
                    $(this).removeClass('opened');
                }
                else {
                    $(this).addClass('opened');
                }

                $element.toggle('fast');

                $allClickProxies.parent().parent().parent().parent().find('.faq-answer').filter(':visible').not($element).slideUp('fast');
                $allClickProxies.not(this).removeClass('opened');

                return false;
            }
        );
        // hide all by default
        $allClickProxies.parent().parent().parent().parent().find('.faq-answer').filter(':visible').slideUp('fast');
        $allClickProxies.removeClass('opened');
    }
}


