﻿/**
* static object that handles page logic
* @class 
* @constructor
* @param {jQuery} $ Reference to the jQuery object
*/
var AccoMain = function($) {

    var config = {

        /**
        * maximum number of reviews to show initially
        * @private
        */
        maxReviews: 10,

        /**
        * maximum number of tips to show initially
        * @private
        */
        maxTips: 10,

        /**
        * jquery reference of the favorites button html object
        * @private
        */
        favoriteTrigger: "#favorite"
    };

    /**
    * @namespace Private methods and variables
    */
    var priv = {

        /**
        * holds the name for the current sub-page (tab)
        * @private
        */
        currentTab: null,

        /**
        * holds the name for the current sub-sub-page (subtab)
        * @private
        */
        currentSubTab: null,

        /**
        * The Id of the current accommodation
        * @type Integer
        * @private
        */
        accoId: -1,

        /**
        * Void, fills the property currentTab with the correct value
        * @private
        */
        setCurrentTab: function() {
            priv.currentTab = $.query.get('view').toLowerCase();
            if (priv.currentTab.length < 1) {
                priv.currentTab = $('body').attr('class');
            }
            Log.Debug("AccoMain: currentTab is '" + priv.currentTab + "'");
        },

        /**
        * Void, fills the property currentTab with the correct value
        * @private
        */
        setCurrentSubTab: function() {
            priv.currentSubTab = $.query.get('sub').toLowerCase();
            if (priv.currentSubTab.length < 1) {
                priv.currentSubTab = $('body').attr('class');
            }
            Log.Debug("AccoMain: currentSubTab is '" + priv.currentSubTab + "'");
        },

        /**
        * Binds the events, sets the classes for the personal item logic
        * makes sure to update the alreadyviewed list, and adds logic to the favorites button
        * @private
        */
        personalItemsLogic: function() {
            //bind event to the page load, to update the "alreadyviewed" items
            $(window).bind("load",
                function() {
                    PersonalItems.Add("alreadyviewed", priv.accoId);
                    PersonalItems.ShowItemLinks();
                }
            );

            //test wether this is a favorite
            if (PersonalItems.Contains("favorites", priv.accoId)) {
                $(config.favoriteTrigger).addClass("is-favorite");
                $(config.favoriteTrigger + " span").html(Resource.GetText('is_favorite'));
            }

            //bind the click event for the trigger
            $(config.favoriteTrigger).bind("click",
                function(evt) {
                    Log.Debug(config.favoriteTrigger);
                    //check for add or removal of the favo
                    if (!$(this).hasClass("is-favorite")) {
                        $(this).addClass("is-favorite");
                        $(this).find("span").html(Resource.GetText('is_favorite'));
                        PersonalItems.Add("favorites", priv.accoId);

                        //update the personal item links
                        PersonalItems.ShowItemLinks();
                    }

                    //prevent event bubbling
                    return false;
                }
            );
        },

        /**
        * Load functionality for the photos tab
        */
        photosTab: function() {
            Log.Info("AccoMain: Called priv.photosTab");
            Carousel.init('#center .extended-info ul');
        },

        /**
        * Only show the first X items of a list; the rest is hidden and can be toggled by using a 'view more' link
        * @param {Integer} maxItemsCount The maximum number of items to show initially
        * @param {String} setName The class of the list
        * @param {String} (Optional) countSelector The css selector for selecting a place to show the count of remaining items
        * @private
        */
        hideItems: function(maxItemsCount, setName, countSelector) {
            Log.Info("AccoMain: Called priv." + setName + "Tab");

            //get the items html object
            var listSelector = 'ul.' + setName;
            var $items = $(listSelector + ' li');

            // hide all items that are more than the maximum
            if ($items.length > maxItemsCount) {
                for (var i = maxItemsCount; i < $items.length; i++) {
                    $($items[i]).css("display", "none");
                }

                // set the total number of items
                if (typeof (countSelector) != "undefined" && countSelector) {
                    $(countSelector).html('(' + ($items.length - maxItemsCount) + ')');
                }

                // bind the show all items event to show all items link
                $('div.show-' + setName + ' a').bind("click", function() {
                    $items.show();
                    $('div.show-' + setName).hide();
                    //TODO correct the shades?
                });

                $('div.show-' + setName).show();
            }

            //finally show the ul
            $(listSelector).show();
        },

        /**
        *  Logic for reviews tab
        */
        reviewsTab: function() {
            priv.hideItems(config.maxReviews, "reviews", "#reviews-count");
        },

        /**
        *  Logic for tips tab
        */
        tipsTab: function() {
            priv.hideItems(config.maxTips, "tips", "#tip-count");
        },

        bindWeatherPopup: function() {
            // add container for question popups
            var container = $('<div/>').attr('id', 'lightboxPopup');
            $('body').append(container);
            
            // make weather link with 'weather-promo-link' class open in lightbox
            $('a.weather-promo-link').each(function() {
                return priv.lightboxize(this, container);
            });
        },

        /**
        * Open a lightbox with contents of link URL.
        */
        lightboxize: function(element, container) {
            $(element).click(function() {
                var url = $(this).attr('href');
                var lightbox = Lightbox.CreateCached(
                    url,
                    { contentUrl: url,
                        width: '580px',
                        container: container,
                        literal: false,
                        contentUrlHasScript: true
                    }
                    );
                lightbox.Show();
                return false;
            });
            return element;
        }
    };

    /** @scope AccoMain */
    return {

        /**
        * Initializes the logic for the current page
        * to be called on $(document).ready
        */
        OnReady: function() {
            Log.Info("AccoMain: Called OnReady");

            //determine the current accoId
            priv.accoId = $("#accoId").val();

            //determine the current tab
            priv.setCurrentTab();

            //determine the current subtab, only for destinations and description main tabs
            if (priv.currentTab == "destination" || priv.currentTab == "descriptions") {
                priv.setCurrentSubTab();
            }

            //do favorite logic (bind events, set classes)
            priv.personalItemsLogic();

            // Moved criteo block from document.write() in the masterpagedetail.master file, to this place, we set it from the javascript instead.
            $("#criteoHolder").append('<div id=\"cto_se_7716320_ac\" style=\"display:none\"><div class=\"ctoWidgetServer\">http:\/\/xtravelnl.widget.criteo.com\/pvx\/<\/div><div class=\"ctoDataType\">sendEvent<\/div><div class=\"ctoParams\">wi=7716320&pt1=2&i=' + priv.accoId + '<\/div><\/div>');

            //tab specific logic
            if (priv.currentTab == "reviews") {
                priv.reviewsTab();
            }
            else if (priv.currentTab == "destination") {
                if (priv.currentSubTab == "tips") {
                    priv.tipsTab();
                }
            }
            else if (priv.currentTab == "photos") {
                priv.photosTab();
            }

            $("#center div.tools a.send").bind("click", function() {
                accoDetails.showSendAccoPopup();
                return false;
            });

            if (typeof (AccoPrices) != "undefined") {
                var accoPricesTime = new Timer();
                AccoPrices.OnReady();
                Log.Info("AccoMain: AccoPrices Javascript load time was: " + (accoPricesTime.Stop()) + " ms", -1);
            }

            accoDetails.OnReady();

            priv.bindWeatherPopup();
        },

        openWindow: function(url, w, h) {
            window.open(url, 'info', 'width=' + w + ',height=' + h + ',scrollbars=yes');
        },

        showWeatherPopup: function() {
        
            var lightbox = Lightbox.CreateCached('tab-post-weather', {
                width: "580px",
                container: document.getElementById('pWeather')
            });
            lightbox.Show();
        }
    };
} (jQuery);



