﻿/**
 * static object that handles page logic for the accommodation prices page
 * @class 
 * @constructor
 * @param {jQuery} $ Reference to the jQuery object
 */
var AccoPrices = function($) {

    /**
     * @namespace Private methods and variables
     */
    var priv = {

        /**
        * Image with loading animation.
        * @type jQuery
        * @private
        */
        $loader: null,

        /**
        * Container to load price table into. Has attached data('url') with url where to load it from.
        * @type jQuery
        * @private
        */
        $container: null,

        /**
        * Load price table using ajax from specified url.
        * @private
        */
        loadPrices: function(url) {
            //priv.$loader.show(); // doesn't look that good actually
            location.href = url;
            return false;
        },

        /**
        * Actions to do when price table is loaded through ajax
        * @private
        */
        onTableLoaded: function() {
            priv.bindTableQuestionPopups();
            priv.bindTransportTypeSelect();
            priv.bindAirportButtons();
            priv.bindDurationLinks();
            priv.bindMonthLinks();
            PricesTooltip.InitTooltips();
            Occupancy.OnReady();
            priv.$container.show();
            priv.$loader.hide();
        },

        /**
         * Bind click event to the airport radio buttons, but only when there
         * are multiple airports to choose from.
         * @private
         */
        bindAirportButtons: function() {
            var buttons = $('#departure-airport input[type=radio]');
            if (buttons.length > 1) {
                buttons.bind('click', function() {
                    location.href = $.query.SET('airport', $(this).val());
                });
            }
        },

        /**
        * Bind change event to the transport type select
        * @private
        */
        bindTransportTypeSelect: function() {
            $('#departure-transport select').bind('change', function(e) {
                var url = priv.url;
                url = url.replace(/&transporttype=[^&]*/, '');
                if ($(this).val()) {
                    url += url.indexOf('?') > 0 ? '' : '?';
                    url = url + "&transporttype=" + $(this).val();
                }
                location.href = url;
            });
        },

        /**
        * Bind events on the links to questions popups in the main content area each time table is reloaded.
        * @private
        */
        bindTableQuestionPopups: function() {
            // Bind methods to show popups for questions in main area
            $("#questions div.item a").bind('click', function() {
                return priv.loadInLightbox(this, "questionPopup");
            });
        },

        /**
        * @private
        */
        bindDurationLinks: function() {
            priv.bindLinks($("#prices .vacationlengths a"));
            priv.bindLinks($("#prices .to_available a"));
        },

        /**
        * @private
        */
        bindMonthLinks: function() {
            priv.bindLinks($("#months a"));
            priv.bindLinks($("#prices .soonerlater a"));
            priv.bindLinks($("#prices-bottom a"));
        },

        /**
        * @private
        */
        bindLinks: function(links) {
            links.click(function() {
                priv.url = $(this).attr('href');
                return !priv.loadPrices(priv.url);
            });
        },

        /**
         * Bind events on the links to questions popups, both in the left sidebar and in the main content area.
         * @private
         */
        bindQuestionPopups: function() {
            // add container for question popups
            $('body').append("<div id=\"questionPopup\"></div>");

            // Bind methods to show popups for questions in main area
            $("#questions div.item a").bind('click', function() {
                return priv.loadInLightbox(this, "questionPopup");
            });

            // Bind methods to show popups for questions in left sidebar
            $("#questions-left ul.questions li a").not(".more-questions").bind('click', function() {
                return priv.loadInLightbox(this, "questionPopup");
            });

            // Open faq in new window
            $("#questions-left ul.questions li a.more-questions").bind('click', function() {
                Utils.newWindow(this, 800, 640);
                return false;
            });
        },

        /**
        * Generic way to open a new lightbox: the contents of the container identified by the supplied id
        * are loaded in a lightbox. Also, the lightbox is cached and thus only has to be initialized once.
        * @param {String} id The id of the container to show in the lightbox
        * @param {Object} options (Optional) Several options can be supplied to override default behavior of just
        * loading the contents of the container. When supplied, these options will be used to initialize the lightbox.
        * @private
        */
        openLightbox: function(id, options) {
            var lightbox = Lightbox.CreateCached(id, options);
            lightbox.Show();
        },

        /**
         * Load the contents of the href of the anchor in a lightbox, wrapped in standard popup markup
         * @param {HTMLElement} anchor The anchor
         * @param {String} id ID to set on the popup container
         * @return {Boolean} false, to use in event handler and stop browser default behavior
         * @private
         */
        loadInLightbox: function(anchor, id) {
            var $anchor = $(anchor);

            var options = {
                container: document.getElementById(id),
                literal: false,
                title: $anchor.text(),
                contentUrl: $anchor.attr('href'),
                width: '643px'
            };
            var lightbox = Lightbox.CreateCached($anchor.attr('href'), options);
            lightbox.Show();
            return false;
        }
    };

    /** @scope AccoPrices */
    return {

        /**
         * Initializes the logic for the current page
         * to be called on $(document).ready
         */
        OnReady: function() {
            priv.$loader = $('#prices-loader');
            priv.$container = $('#prices-container');

            priv.url = location.href.replace(/#.*$/, '');
            priv.onTableLoaded();

            priv.bindAirportButtons();
            priv.bindQuestionPopups();
            PricesTooltip.OnReady();
            Basket.OnReady();
        },

        /**
         * Add a newly selected price to the basket. Several parameters are used.
         */
        AddToBasket: function(packageid, duration, roomtype, departuredate, transportcode, airportcode, price, accommodationid, $curobj, evt) {
            /* Don't show the animation: this is the last parameter that can have any content, since its checked if its empty or not */
            Basket.AddToBasket(packageid, duration, roomtype, departuredate, transportcode, airportcode, price, accommodationid, $curobj, evt, "no");
        },

        /*
         * Redraw the basket box. There are 2 possible situations:
         * 1. A price has been selected from the table, it will be added to the box
         * 2. The user closed the box. All parameters and previously selected prices will be cleared and the basket will be removed.
         * @param {Integer} height (Optional) The offset height of the basket. Used to determine whether to close or expand the basket.
         */
        ReDraw: function(height) {
            if (typeof (height) == "undefined") {
                height = $('#frmBasket').contents().find('#basket').length > 0 ? $('#frmBasket').contents().find('#basket').height() : 0;
            }
            Basket.ReDraw(height);
        },

        /**
         * Remove the basket; clear all parameters
        */
        RemoveBasket: function() {
            Basket.RemoveBasket();
        },

        /**
         * Get whether it is currently possible to open the tooltip
         * @return {Boolean} True if nothing prevents the tooltip from showing
         */
        CanShowTooltip: function() {
            return !Basket.IsAnimating();
        },

        /**
         * Perform certain actions when starting animation of the basket
         */
        StartingAnimation: function() {
            PricesTooltip.Hide();
        }
    };
} (jQuery);