// make it safe to use console.log always
(function (b) {
    function c() {}
    for (var d = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,timeStamp,profile,profileEnd,time,timeEnd,trace,warn".split(","), a; a = d.pop();) {
        b[a] = b[a] || c
    }
})((function () {
    try {
        console.log();
        return window.console;
    } catch (err) {
        return window.console = {};
    }
})());



function setWidth() {
    var bannerEmpty = $('#banner').find('*').length == 0;
    if (!bannerEmpty) {
        $('body').addClass('with-banner');
    }
}




$(document).ready(function () {


        //datepicker
        if ($('html').attr('lang') == 'fi') {

            jQuery(function ($) {
                $.datepicker.regional['fi'] = {
                    closeText: 'Sulje',
                    prevText: '&laquo;Edellinen',
                    nextText: 'Seuraava&raquo;',
                    currentText: 'T&auml;n&auml;&auml;n',
                    monthNames: ['Tammikuu', 'Helmikuu', 'Maaliskuu', 'Huhtikuu', 'Toukokuu', 'Kes&auml;kuu', 'Hein&auml;kuu', 'Elokuu', 'Syyskuu', 'Lokakuu', 'Marraskuu', 'Joulukuu'],
                    monthNamesShort: ['Tammi', 'Helmi', 'Maalis', 'Huhti', 'Touko', 'Kes&auml;', 'Hein&auml;', 'Elo', 'Syys', 'Loka', 'Marras', 'Joulu'],
                    dayNamesShort: ['Su', 'Ma', 'Ti', 'Ke', 'To', 'Pe', 'Su'],
                    dayNames: ['Sunnuntai', 'Maanantai', 'Tiistai', 'Keskiviikko', 'Torstai', 'Perjantai', 'Lauantai'],
                    dayNamesMin: ['Su', 'Ma', 'Ti', 'Ke', 'To', 'Pe', 'La'],
                    weekHeader: 'Vk',
                    dateFormat: 'dd.mm.yy',
                    firstDay: 1,
                    isRTL: false,
                    showMonthAfterYear: false,
                    yearSuffix: ''
                };
                $.datepicker.setDefaults($.datepicker.regional['fi']);
            });
            $.datepicker.setDefaults($.datepicker.regional['fi']);
        } else {
            $.datepicker.setDefaults($.datepicker.regional['en']);
        }


    //these functions are not to be used on blank template
    if ($('body article#blank').length == 0) {

        setWidth();



        $("#arrival").datepicker({
            altField: '#ArrDate',
            altFormat: 'dd-mm-yy',
            minDate: +0,
            onSelect: function(dateText, inst) {

                 var currentDate = $(this).datepicker('getDate');
                 var currentDepartureDate = $('#departure').datepicker('getDate');
                 var difference = 1;
                 var myDate = new Date(currentDate);
                 myDate.setDate(myDate.getDate()+difference);
                 $("#departure").datepicker("option", {
                     'minDate': myDate,
                     'defaultDate': myDate
                 }); 

            }
        });

        $("#departure").datepicker({
            altField: '#DepDate',
            altFormat: 'dd-mm-yy',
            defaultDate: +1,
            minDate: +1
     });





        //Colorbox urls with ajax

        $("a[href*=lounastulostus], a[href*=ymparisto], a[href*=test], a[href*=pysakointi], a[href*=historia], a[href*=press], a[href*=kokoushinnasto], a[href*=saunamenut], a[href*=saunahinnasto], a[href*=varauksiin-tarvittavat-tiedot],a[href*=mainokset], a[href*=tiedotteet], a[href*=kartta], a[href*=tilaa-uutiskirje], a[href*=ajo-ohjeet], a[href*=hotellipalvelut]").add("a[href*=accommodation]")
        .add("a[href*=sauna-menus],a[href*=sauna-rates], a[href*=required-info],a[href*=notifications], a[href*=driving-instructions], a[href*=map], a[href*=parking], a[href*=hotel-services], a[href*=location], a[href*=ads], a[href*=history], a[href*=press]")
        .on('click', function (e) {
            var url = $(this).attr('href');
            $.colorbox({
                href: url,
                initialWidth: 500,
                initialHeight: 800,
                onLoad: function () {
                    $('#cboxClose').hide();
                    window.location.hash = url;
                },
                onComplete : function() {
                    $('a.close').on('click', function() {
                        $.colorbox.close();
                        return false;
                    });
                    $('a.print').on('click', function() {
                        $('body').addClass('blank-print');
                        window.print();
                        return false;
                    });
                },
                onClosed : function() {
                    window.location.hash = '';
                    $('body').removeClass('blank-print');
                }
            });
            return false;
        });



        //Colorbox urls with iframe

 $("a[href*=online-varaus], a[href*=kutsu-], a[href*=varaa-], a[href*=palaute], a[href*=contact-us], a[href*=kokoustila], a[href*=yhteystiedot], a[href*=poyta], a[href*=majoitus], a[href*=tilaa-uutiskirje]").add("a[href*=accommodation]")
        .add("a[href*=newsletter], a[href*=reserve-room], a[href*=table], a[href*='startreservation.aspx']")
        .on('click', function (e) {
            var url = $(this).attr('href') + '?redirect=no';
            $.colorbox({
                href: url,
                iframe : true,
                initialWidth: 500,
                initialHeight: 800,
                 width: "1000px",
                height: "690px"
            });
            return false;
        });

        //Wider Colorbox urls with iframe

 $("a[href*=hiilijalanjalki], ")
        .on('click', function (e) {
            var url = $(this).attr('href') + '?redirect=no';
            $.colorbox({
                href: url,
                iframe : true,
                initialWidth: 500,
                initialHeight: 800,
                 width: "1100px",
                height: "690px"
            });
            return false;
        });



        //Trigger Colorbox by url hash

        var urlToLoad = window.location.hash.toString();
        if (urlToLoad != '') {
            if (urlToLoad.charAt(0) === '#') {
                urlToLoad = urlToLoad.slice(1);
            }
            var triggerElement = $("a[href='" + urlToLoad + "']:first");
            triggerElement.trigger('click')
        }

        //Full page background
        $.supersized({
            slides: [{
                image: '/files/helka/img/body-bg.jpg'
            }]
        });


        //Homepage slideshow
        // init and stop the inner slideshows
        var inners = $('.inner-slideshow').cycle().cycle('stop');

        var startAt = $('#slideshow').data('starting-slide');
        if (startAt === undefined || startAt == '') {
            var startAt = 0;
        }
        var slideshow = $('#slideshow').cycle({
            startingSlide: startAt,
            fx: 'fade',
            speed: 300,
            activePagerClass: 'active',
            timeout: 0,
            //  prev: '#prev',
            // next: '#next',
            onPagerEvent: function (zeroBasedSlideIndex, slideElement) {
                console.log('onPagerEvent');
            },
            pager: $('#carousel-menu'),
            speed: 2000,
            pagerAnchorBuilder: function (idx, slide) {
                return '#carousel-menu li:eq(' + idx + ')';
            },
            updateActivePagerLink: function (pager, currSlideIndex) {
                $(pager).find('li').removeClass('active').filter('li:eq(' + currSlideIndex + ')').addClass('active');
            },
            before: function () {
                // stop all inner slideshows
                inners.cycle('stop');

                // start the new slide's slideshow
                $(this).cycle({
                    fx: 'scrollHorz',
                    timeout: 2000,
                    autostop: true,
                    end: function () {
                        // when inner slideshow ends, advance the outer slideshow
                        //   slideshow.cycle('next');
                    }
                });
            }
        });


        $('#next').live('click', function () {
            inners.cycle('next');
        });

        $('#prev').live('click', function () {
            inners.cycle('prev');
        });



        $("#reservation-form").on('submit', function () {
            var url = $(this).attr("action") + $(this).serialize();
            $.colorbox({
                href: url,
                iframe: true,
                overlayClose: false,
                scrolling: false,
                width: "1100px",
                height: "690px",

            });
            return false;
        });

        //newsitem toggler
        $('.news-item .toggler').on('click', function (e) {
            $(this).parents('.news-item').toggleClass('open');
            e.preventDefault();
        });


        $('#tabs').on('click', 'li a', function (e) {
            var ele = $(this);
            var activeClass = 'active';
            var activateElementId = ele.attr('href');

            $(activateElementId).show().siblings('div').hide();
            ele.parents('li').addClass(activeClass).siblings('li').removeClass(activeClass);
            e.preventDefault();
        });
      

    } else {
       
 

       //table reservation form

        $("#table-reservation #q3").datepicker({
            altFormat: 'dd-mm-yy',
            minDate: +0
        });

        //room reservation

        $("#room-reservation #q15").datepicker({
            altFormat: 'dd-mm-yy',
            minDate: +0,
            onSelect: function(dateText, inst) {
                 var currentDate = $(this).datepicker('getDate');
                 var currentDepartureDate = $('#q17').datepicker('getDate');
                 var difference = 1;
                 var myDate = new Date(currentDate);
                 myDate.setDate(myDate.getDate()+difference);
                 $("#q17").datepicker("option", {
                     'minDate': myDate,
                     'defaultDate': myDate
                 }); 

            }

        });

        $("#room-reservation #q17").datepicker({
            altFormat: 'dd-mm-yy',
            defaultDate: +1,
            minDate: +1
        });

        if ($('#redirect').length > 0 && $('body').hasClass('public')) {
            var urlArray = window.location.pathname.split("/");
            urlArray.pop();
            var urlString = urlArray.join('/');
            var redirectUrl = urlString + '#' + window.location.pathname;
            window.location = redirectUrl;
        }

    }



});
