Halcyon = function() {
    this.init();
}

// Prototyping the jsClass
jQuery.extend(Halcyon.prototype, {

    normalMapShowing: true,

    // Init
    init : function() {
        $('#reservationForm').submit(function(){
            return false;
        });
    },

    // Replace the fonts
    fontReplace : function() {
        Cufon.replace('.fancyFont',             { fontFamily : 'Sansumi', hover: true });
        Cufon.replace('h2',                     { fontFamily : 'Helvetika', hover: true });
        Cufon.replace('h1',                     { fontFamily : 'Helvetika', hover: true });
        Cufon.replace('h3',                     { fontFamily : 'Helvetika' });
        Cufon.replace('.bigPrice',              { fontFamily : 'Helvetika' });
        Cufon.replace('.promotionsPrice',       { fontFamily : 'Helvetika' });
        Cufon.replace('.fancyFontHel',          { fontFamily : 'Helvetika', hover: true});
    },


    // Standard Top Image Slider
    topSlider : function() {
        $('.slideIt').cycle({
            fx: 'cover',
            timeout: 7500,
            speed: 500
        });
    },


    // Add Room Gallery Sliders
    roomGallery : function(num) {
        $('#room' + num).cycle({
            fx: 'scrollHorz',
            speed: 500,
            timeout: 0,
            next: '#room' + num + 'next',
            prev: '#room' + num + 'prev'
        });
    },


    // Add Google Map Replacer to the Contact Page
    contactMapSwitch: function() {
        $('.goGoogleMaps').click(function() {
            if(Halcyon.normalMapShowing) {
                $('#topContactImage').hide();
                Halcyon.normalMapShowing = false;
            } else {
                $('#topContactImage').show();
                Halcyon.normalMapShowing = true;
            }
        });
    },


    // Check the prices for font-sizee changes
    checkPrices : function() {
        $('.priceInt').each(function() {
            pI = $(this).html();
            pIL = 0;
            if(pI != null) {
                pIL = pI.length;
            }
            if(pIL > 2) {
                $(this).parent().css('font-size', '84px');
                $(this).parent().find('.priceDecimal').css('font-size', '30px');
            }
        });
    },

    // Open a link
    openLink: function(link) {
        window.location.href = link;
    },

    // Rotate events on the home page
    cycleEvents: function() {
        $('#cycleEvents').scrollable({
            vertical: true,
            size: 3,
            speed: 500
        }).circular().autoscroll({
            autoplay: true,
            steps: 1,
            interval: 5000
        });
    },


    // Cycle the recent posts on the homepage
    cycleRecentPosts: function() {
        $('#cyclePosts').scrollable({
            vertical: true,
            size: 1,
            speed: 500
        }).circular().autoscroll({
            autoplay: true,
            steps: 1,
            interval: 5000
        });
    },


    // Submit the reservation form
    submitReservation: function() {
        formdata = $('#reservationForm').serializeArray();

        // Send to server
        $.ajax({
            type: "post",
            data: formdata,
            dataType: "json",
            async: false,
            url: '/reservations/form/reservations',
            beforeSend: function() {
                $('#reservationSubmit').val("Sending...");
                Halcyon.reservationErrors('hide', null);
            },
            success: function(response) {
                switch(response.status) {
                    case 'success':
                        Halcyon.reservationErrors('hide', null);
                        Halcyon.reservationSuccess('show', response.message);
                        $('#reservationSubmit').val("Sent Reservation");
                        $('#reservationSubmit').onclick = '';
                        break;
                    case 'failed':
                        Halcyon.reservationSuccess('hide', null);
                        Halcyon.reservationErrors('show', response.message);
                        $('#reservationSubmit').val("Submit");
                        $('#reservationSubmit').onclick = 'Halcyon.submitReservation();';

                        break;
                }
            }
        });
    },

    // Reservation form errors
    reservationErrors: function(action, errors) {
        switch(action) {
            case "show":
                $('#reservationErrors').html(errors);
                $('#reservationErrors').slideDown('fast');
                break;
            case "hide":
                $('#reservationErrors').slideUp('fast');
        }
    },

    // Reservation success
    reservationSuccess: function(action, message) {
        switch(action) {
            case "show":
                $('#reservationSuccess').html(message);
                $('#reservationSuccess').slideDown('fast');
                break;
            case "hide":
                $('#reservationSuccess').slideUp('fast');
        }
    },
    
    
    galleryClick: function()
    {
        $('.gallery a').click(function(){
            if($(this).attr('rel') != '/media/files//') {
                $('#bigImage').html('<img width="924" height="346" alt="Gallery Image" src="' + $(this).attr('rel') + '" />');
            }
        });
    },


});

/**
 * Booking link
 **/
function bookIt()
{
    // Get the form data and create the redirect link
    cDay    = $('#ci_day').val();
    cMonth  = $('#ci_month').val();
    cYear   = $('#ci_year').val();
    cNights = $('#ci_nights').val();
    
    $.ajax({
        url: '/reservations/bookit',
        async: false,
        dataType: 'text',
        data: {
            day: cDay, 
            month: cMonth, 
            year: cYear, 
            nights: cNights
        },
        type: 'post',
        success: function(response) {
            window.location.href = response;
        }     
    });   
}
