$(document).ready(function() {
    var columnSettings = function() {$('.col:last').addClass('last');}
    
    $('body').removeClass('no-js');
    
    // Home Page Slider
    if($('#featured.coda-slider').length) {
        $('#featured').codaFader({
            autoHeight               : false,
            dynamicTabs              : false,
            autoFade                 : true,
            autoFadeInterval         : 7500,
            fadeEaseDuration         : 800,
            crossLinking             : false,
            autoSlideStopWhenClicked : true,
            withPanel                : true
        });
    }
    
    // Home Page
    if($('#content.home').length) {
        $('#checkin').datepicker({
            dateFormat: 'yy-mm-dd',
            minDate: 1,
            numberOfMonths: 2,
            altField: '#date_from',
            altFormat: 'yymmdd'
        });
        $('#checkout').datepicker({
            dateFormat: 'yy-mm-dd',
            minDate: 2,
            numberOfMonths: 2,
            altField: '#date_to',
            altFormat: 'yymmdd'
        });
        $('select:not(.nopretty)').css('display', 'none').jqTransSelect();
        $('.input-box .datepicker').click(function(e){
            $(this).prev().focus();
            e.preventDefault();
        });
    }
    
    // Page Sharing
    var pageSharingAnim = false;
    var pageSharingOpen = false;
    var pageSharingCloseTimer;
    
    function pageSharingClose() {
        if(!pageSharingAnim) {
            if(pageSharingOpen) {
                clearTimeout(pageSharingCloseTimer);
                pageSharingAnim = true;
                pageSharingOpen = false;
                $('#page-sharing .open-close').animate({rotate: '0deg'});
                $('#page-sharing').animate({width: 29}, 'normal', function(){ pageSharingAnim = false; });
            }
        }
    }
    
    if($('#page-sharing').length) {
        $('#page-sharing').mousemove(function(e){
            if(!pageSharingAnim && pageSharingOpen) {clearTimeout(pageSharingCloseTimer);}
        }).mouseleave(function(e){
            if(!pageSharingAnim && pageSharingOpen) {
                pageSharingCloseTimer = setTimeout(pageSharingClose, 1000);
            }
        });
        $('#page-sharing .open-close').click(function(e){
            if(!pageSharingAnim) {
                if(!pageSharingOpen) {
                    pageSharingAnim = true;
                    $(this).animate({rotate: '45deg'});
                    $('#page-sharing').animate({width: 208}, 'slow', function(){ pageSharingAnim = false; pageSharingOpen = true; pageSharingCloseTimer = setTimeout(pageSharingClose, 1000); });
                } else {pageSharingClose();}
            }
            e.preventDefault();
        });
    }
    
    // Auto Columns
    if($('.content-cols.cols-2').length) {
        $('.content-cols.cols-2').each(function(){
            $(this).ezColumns({columns: 2, callback: columnSettings});
        });
    }

    // FAQs Page
    if($('#page-faqs .section:first').length) {
        $('#page-faqs .section-content').css('display', 'none');
        $('#page-faqs .btn.answer').css('display', 'block').click(function(e){
            button = $(this);
            e.preventDefault();
            sectionContent = button.next();
            sectionContent.slideToggle(function(){
                if($(this).css('display') == 'block') {
                    if(siteSettings.lang == 'fr') {
                        $('span', button).text('Masquer');
                    } else {
                         $('span', button).text('Hide Answer');
                    }
                } else {
                    if(siteSettings.lang == 'fr') {
                        $('span', button).text('Voir');
                    } else {
                         $('span', button).text('Show Answer');
                    }
                }
            });
        });
    }
    
    // Article Pages (News)
    if($('#page-articles .section:first').length) {
        // reset things for js enabled browsers
        $('#page-articles .section').addClass('collapsed').find('h3 a:not(.not-dynamic)').click(function(e){
            e.preventDefault();
            section = $(this).parent().parent();
            if(section.hasClass('collapsed')) {section.removeClass('collapsed');} else {section.addClass('collapsed');}
        });
    
        $('#page-articles .btn.expand').removeClass('disabled').click(function(e){
            e.preventDefault();
            btn = $(this);
            if(!btn.hasClass('disabled')) {
                btn.addClass('disabled');
                $('#page-articles .btn.collapse').removeClass('disabled');
            }
            $('#page-articles .section').removeClass('collapsed');
        });
        
        $('#page-articles .btn.collapse').addClass('disabled').click(function(e){
            e.preventDefault();
            btn = $(this);
            if(!btn.hasClass('disabled')) {
                btn.addClass('disabled');
                $('#page-articles .btn.expand').removeClass('disabled');
            }
            $('#page-articles .section').addClass('collapsed');
        });
        
    }
    
    // Home Detail Page
    if($('#homePicture').length) {
        $('#homePicture').codaFader({
            autoHeight               : false,
            dynamicTabs              : false,
            autoFade                 : true,
            autoFadeInterval         : 8000,
            slideEaseFunction        : 'easeInOutCubic',
            slideEaseDuration        : 1200,
            crossLinking             : false,
            autoSlideStopWhenClicked : true,
            codaNavSelector          : '#coda-nav-1'
        });
    }
    
    if($('.feature-content-equal').length) {
        $('.feature-content-equal > div').equalHeights();
    }
    
    // Sidebar
    $('.sidebarSection.reservations select').css('display', 'none').jqTransSelect();
    $('.sidebarSection.reservations .input-box .datepicker').click(function(e){
        $(this).prev().focus();
        e.preventDefault();
    });
    $('.sidebarSection.reservations #checkin').datepicker({
        dateFormat: 'yy-mm-dd',
        minDate: 0,
        numberOfMonths: 2,
        altField: '#date_from',
        altFormat: 'yymmdd'
    });
    $('.sidebarSection.reservations #checkout').datepicker({
        dateFormat: 'yy-mm-dd',
        minDate: 1,
        numberOfMonths: 2,
        altField: '#date_to',
        altFormat: 'yymmdd'
    });

    // Reservation Form
    if($('.request-quote-form').length) {
        $('.person select:not(.nopretty)', '.request-quote-form').jqTransSelect();

        $('#rfCheckin').datepicker({
            dateFormat: 'yy-mm-dd',
            minDate: 1,
            numberOfMonths: 2,
            altField: '#rfDateFrom',
            altFormat: 'yymmdd'
        });
        $('#rfCheckout').datepicker({
            dateFormat: 'yy-mm-dd',
            minDate: 2,
            numberOfMonths: 2,
            altField: '#rfDateTo',
            altFormat: 'yymmdd'
        });
        $('.datepicker', '.request-quote-form .field.date').click(function(e){
            $(this).prev().focus();
            e.preventDefault();
        });
    }

    var resetFlashNotice = function () {$('.flash-notice').slideUp('slow');}

    if($('.flash-notice').length) {
        setTimeout(resetFlashNotice, 10000);
        $('.flash-notice').hide().slideDown('slow');
    }

    var highlightRequestQuoteForm = function(e) {
        e.preventDefault();

        $('html, body').animate({scrollTop: $('.request-quote-form').position().top + 30}, 1000);
        setTimeout(function(){
            $('.request-quote-form').effect('highlight', {color: '#ffe9e9'}, 2000);
        }, 1000);
    };

    var telephoneBox = function(e) {
        e.preventDefault();

        $('.request-telephone-dialog:first').dialog({
           modal: true,
           resizable: false,
           width: 350,
           height: 115
        });
    };

    var quoteBox = function(e) {
        e.preventDefault();

        $('.request-quote-dialog:first').dialog({
           modal: true,
           resizable: false,
           width: 400,
           height: 500
        });
    };

    $('.request-quote-link').click(highlightRequestQuoteForm);
    $('.request-telephone').click(telephoneBox);
    $('.request-a-quote').click(quoteBox);

    // Footer
    if($('#footer .input-box input').length) {
        $('#footer-main > .section').equalHeights();
        $('#footer .input-box input').focus(function(e){
            if($(this).val()=='Insert your email address' || $(this).val()=='Courriel') {$(this).val('');}
        });
    }
	
});

//Bill addon for the custom gift card

		function doCards(img, lang) {		
			$( "#koPage" ).empty();
			$( "#koPage" ).append($("<iframe />").attr("src", "http://fiddlerlakeresort.com/j/index.php?option=com_kocustomgiftcard&tmpl=component&view=modal&layout=modal&ko_style="+img+"&card_dir=cards&lang="+lang)).dialog({
			autoOpen: true,
			width: 830,
			height: 570
		});	
	}
