var menuRound = {};
var myPlayer;

$(document).ready(function() {

    $('#nav .nav-link[data-toggle=slideInMenu]').on('click', function() {
        var elem = $(this);

        var scrollTime = $('#navflow').scrollTop();

        $('#navflow').stop().animate({
            scrollTop: 0
        }, scrollTime, 'swing', function() {
            $(elem).addClass('current');
            $('#navbar').addClass('sub-in');
            $($(elem).data('target')).addClass('show');
        });

    });

    $('.subnav-return').on('click', function() {

        $('#nav .nav-link.current').removeClass('current');
        setTimeout(function() {
            $('#navbar').removeClass('sub-in');
            $('.nav-level-1.show').removeClass('show');

        }, 10);


    });

    $('.nav-return').on('click', function() {
        $('.nav-bar.show').removeClass('show');
        $('#navbar_toggle').removeClass('trigger');
    });




    //$('#nav .nav-link[data-toggle=slideInMenu]').on('click',function(){	

    //menu-close-btn



    $(document).on({
        click: function() {

            if ($(this).data('ser')) {
                event.preventDefault();
                var dataStr = 'infoNo=' + $(this).data('ser');

                if ($(this).data('db')) {
                    dataStr += '&modelDb=' + $(this).data('db')
                }
                $.ajax({
                    url: "/tpl/sub/modal_info.php",
                    type: 'post',
                    data: dataStr,
                    dataType: 'html',
                    success: function(responseHtml) {

                        $('#info-modal').find('.modal-inner').html(responseHtml);
                        $('#info-modal').modal('show');
                    },
                    error: function(request) {
                        console.log(request.responseText);
                    }
                });

            }

        }
    }, '.fnmodal, .modal-link, .adv-link[data-ser]');


    $(document).on({
        click: function() {
            event.preventDefault();
            if ($(this).data('ser')) {
                var dataStr = 'tripNo=' + $(this).data('ser');

                $.ajax({
                    url: "/tpl/sub/modal_trip_rule.php",
                    type: 'post',
                    data: dataStr,
                    dataType: 'html',
                    success: function(responseHtml) {

                        $('#info-modal').find('.modal-inner').html(responseHtml);
                        $('#info-modal').modal('show');
                    },
                    error: function(request) {
                        console.log(request.responseText);
                    }
                });

            }

        }
    }, '.trip-rule');



    $(window).resize(function() {

        setTimeout(function() {
            headerAdj();
        }, 100);

        //shuffleTitleTlt();
    });

    $(window).on("scroll", function() {
        //shuffleTitleTlt();
    });



    if (!$('#pagearea').length) {
        $('.ct-br-slice-ani').each(function() {
            var sliceCtn = $(this);
            var reblildText = '<div ani-enter="fadeInRight">';
            reblildText += $(sliceCtn).html().replace(/\<br\>/g, '</div><br><div ani-enter="fadeInRight">');
            reblildText += '</div>';
            //console.log(reblildText);
            $(sliceCtn).html(reblildText);
        });


        $('.tltEff').each(function() {
            var sliceCtn = $(this);
            var reblildText = '<div htxt-enter="fadeInUp" txt-shuffle="true">';
            reblildText += $(sliceCtn).html().replace(/\<br\>/g, '</div><div htxt-enter="fadeInUp" txt-shuffle="true">');
            reblildText += '</div>';
            $(sliceCtn).html(reblildText);
            $(sliceCtn).find('[htxt-enter]').each(function() {
                if (!$(this).text().length) {
                    $(this).remove();
                }
            })

        });



        $('[htxt-enter]').each(function(index, element) {

            if ($(element).parents('.head-text-block').length) {
                var outTime = 1000;
            } else {
                var outTime = 1;
            }


            var txtShuffle = false;
            if ($(element).attr('txt-shuffle')) {
                txtShuffle = JSON.parse($(element).attr('txt-shuffle'));
            }
            //setTimeout(function(){
            $(element).textillate({
                initialDelay: 100,
                in: {
                    effect: $(element).attr('htxt-enter'),
                    delay: 100,
                    shuffle: txtShuffle
                },
                out: {
                    effect: 'fadeOut',
                    sync: true,
                    delayScale: 0,
                    delay: 0
                }
            });

            //},2000);	

        });



    }

    setTimeout(function() {
        headerAdj();
        $('.header-btmnav-wrap').addClass('enter');
    }, 100);

    $('#contactform').validator();


    $("#gotop").click(function() {
        jQuery("html,body").animate({
            scrollTop: 0
        }, 1000);
    });


    $(window).scroll(function() {

        if ($(this).scrollTop() > 300) {
            $('#gotop,#gobooking').fadeIn("fast");
        } else {
            $('#gotop,#gobooking').stop().fadeOut("fast");
        }
    });


    if ((!$('#pagearea').length) && ($("#idx_video").length)) {


        myPlayer = $("#idx_video").YTPlayer({});
        $("#idx_video").YTPAddMask('/images/raster@2x.png');

        myPlayer.on("YTPPlay", function() {
            $("#togglePlay").removeClass("play pause");
            $("#togglePlay").addClass("pause");
        });


        myPlayer.on("YTPPause", function() {
            $("#togglePlay").removeClass("play pause");
            $("#togglePlay").addClass("play");
        });





    }


    $(document).on({
        click: function() {
            $('.adv-banner').addClass('bannerHide');
            setCookie('bannerHide', 'y', 60); //set 60 minutes
        }

    }, '.advBanner-close');

    $(document).on({
        click: function() {
            $('.adv-banner').removeClass('bannerHide');
            setCookie('bannerHide', 'n', 60); //set 60 minutes
        }

    }, '.advBanner-show');

    if (!getCookie('bannerHide')) {
        setCookie('bannerHide', 'n');
    }

    if (!$('#pagearea').length) {
        if (getCookie('bannerHide') == 'y') {
            $('.adv-banner').addClass('bannerHide');
        } else {
            $('.adv-banner').removeClass('bannerHide');
        }
    }



}); //ready

function setCookie(key, value, minutes) {
    var expires = new Date();
    expires.setTime(expires.getTime() + (minutes * 60 * 1000));
    document.cookie = key + '=' + value + ';expires=' + expires.toUTCString() + ';path=/';
}

function getCookie(key) {
    var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
    return keyValue ? keyValue[2] : null;
}


function headerAdj() {
    if ($('.head-adj').length) {
        var ratio = 0.5625;
        if ($('.head-adj').is('.fullscreen')) {
            var ratio = $(window).height() / $(window).width();
            $('.head-adj').parents('header').eq(0).css('height', '100%');
        }
        var headerWidth = $('header').width(),
            headerHeight = $('header').height();

        if ((headerHeight / headerWidth) > ratio) {
            $('.head-adj').css({
                'width': (headerHeight / ratio) + 'px',
                'height': headerHeight + 'px',
                'margin-top': ''
            });
        } else {
            $('.head-adj').css({
                'width': headerWidth + 'px',
                'height': (headerWidth * ratio) + 'px',
                'margin-top': headerHeight - (headerWidth * ratio) + 'px'
            });
        }

        $('.head-adj').find('[data-xlink]').each(function(idx, imgElem) {
            $(imgElem).respLazyload();
        });

        /*
        if (!$('.head-adj .svgFirm').is('.fn-transed')){
        	var inkTimer, frameSteps = 24, countStep = 0;
        	var maskImg = $('.head-adj').find('.masking').eq(0);
        	
        	inkTimer= setInterval(function(){
        		countStep++;
        		$(maskImg).attr({'x': (countStep*-100)+'%'});
        		
        		if (countStep >= frameSteps){
        			clearInterval(inkTimer);
        		}
        		
        	},100);
        	
        	$('.head-adj .svgFirm').addClass('fn-transed');
        }
        */
    }
}